D3D12 Hello World Sample

Category

Direct3D 12

Platform

Windows

Tags

Direct3D 12 Graphics Sample API Windows

This sample demonstrates the fundamental steps required to initialize Direct3D 12 and render a simple triangle to the screen. It serves as a foundational example for developers looking to start with Direct3D 12 graphics programming.

Key Concepts Illustrated

Prerequisites

To build and run this sample, you will need:

Code Snippet (Conceptual)

Here's a glimpse of the core initialization logic:

// Include necessary headers #include <d3d12.h> #include <dxgi1_6.h> #include <wrl.h> // For ComPtr // ... // Initialize D3D12 Device Microsoft::WRL::ComPtr<ID3D12Device> pDevice; // ... D3D12CreateDevice ... // Create Command Queue Microsoft::WRL::ComPtr<ID3D12CommandQueue> pCommandQueue; // ... pDevice->CreateCommandQueue ... // Create Swap Chain Microsoft::WRL::ComPtr<IDXGISwapChain3> pSwapChain; // ... CreateDXGIFactory ... && ... factory->CreateSwapChain ... // Set up Render Target View (RTV) Microsoft::WRL::ComPtr<ID3D12Resource> pRenderTarget; D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle; // ... pDevice->CreateRenderTargetView ... // Create Root Signature and Pipeline State Object (PSO) Microsoft::WRL::ComPtr<ID3D12RootSignature> pRootSignature; Microsoft::WRL::ComPtr<ID3D12PipelineState> pPipelineState; // ... Define and create PSO ... // Record Commands into Command List Microsoft::WRL::ComPtr<ID3D12CommandAllocator> pCommandAllocator; Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> pCommandList; // ... pDevice->CreateCommandAllocator ... && pDevice->CreateCommandList ... // Clear the render target and draw the triangle // ... pCommandList->ClearRenderTargetView ... // ... pCommandList->IASetPrimitiveTopology ... // ... pCommandList->SetGraphicsRootSignature ... // ... pCommandList->SetPipelineState ... // ... pCommandList->DrawInstanced ... // Execute commands // ... pCommandQueue->ExecuteCommandLists ... // Present the frame // ... pSwapChain->Present ...

This sample is designed to be a straightforward starting point. Explore the code to understand each step involved in setting up a Direct3D 12 rendering pipeline.

Download Sample (ZIP)