D3D12 Hello World Sample
Category
Direct3D 12
Platform
Windows
Tags
Direct3D 12 Graphics Sample API WindowsThis 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
- Device and Swap Chain Initialization: Setting up the core Direct3D 12 device and managing the presentation of rendered frames.
- Command Queue and Command List: Understanding how to record and submit rendering commands.
- Resource Creation: Creating essential graphics resources like render target views.
- Pipeline State Object (PSO): Configuring the graphics pipeline for rendering.
- Drawing Primitives: Rendering basic geometric shapes.
Prerequisites
To build and run this sample, you will need:
- Windows 10 or later
- A DirectX 12 compatible graphics card
- Visual Studio (latest version recommended) with the "Desktop development with C++" workload
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)