To effectively use DirectX 12 Command Lists in C++, follow this structured guide:
Key Concepts
- Command Queue: Submits work to the GPU (e.g., rendering, compute, copying).
- Command Allocator: Manages memory for command lists. Resetting it clears previously recorded commands.
- Command List: Records GPU commands (e.g., draw calls, resource barriers). Once closed, it can be executed.
Step-by-Step Implementation
1. Initialize the Device and Command Queue
ID3D12Device* device;
D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device));
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; // For general rendering
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
ID3D12CommandQueue* commandQueue;
device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue));
2. Create Command Allocator and Command List
ID3D12CommandAllocator* commandAllocator;
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocator));
ID3D12GraphicsCommandList* commandList;
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, commandAllocator, nullptr, IID_PPV_ARGS(&commandList));
commandList->Close(); // Initially closed after creation
3. Record Commands
Reset the allocator and command list, then record commands:
// Reset for reuse
commandAllocator->Reset();
commandList->Reset(commandAllocator, nullptr); // Rebind allocator
// Example: Clear a render target
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = renderTarget; // Your render target resource
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
commandList->ResourceBarrier(1, &barrier);
commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
// End command recording
commandList->Close();
4. Submit to Command Queue
ID3D12CommandList* ppCommandLists[] = { commandList };
commandQueue->ExecuteCommandLists(1, ppCommandLists);
Command List Types
| Type | Use Case |
|---|
D3D12_COMMAND_LIST_TYPE_DIRECT | General rendering, compute, and copy operations. |
D3D12_COMMAND_LIST_TYPE_COMPUTE | Compute shaders only. |
D3D12_COMMAND_LIST_TYPE_COPY | Data transfer (e.g., uploads, copies). |
Use Direct for most tasks.
Best Practices
- Synchronization: Ensure the GPU finishes using resources before resetting allocators. Use fences for synchronization in multi-frame scenarios.
- Command List Reuse: Reset allocators and command lists instead of recreating them every frame.
- Error Handling: Check HRESULT returns and use Debug Layers for validation.
Example Commands
- Resource Barriers: Transition resource states (e.g.,
PRESENT → RENDER_TARGET).
- Drawing:
commandList->SetPipelineState(pipelineState);
commandList->SetGraphicsRootSignature(rootSignature);
commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
commandList->DrawInstanced(3, 1, 0, 0);
- Compute:
commandList->SetPipelineState(computePipeline);
commandList->Dispatch(numGroupsX, numGroupsY, numGroupsZ);
Headers and Libraries
Include:
#include <d3d12.h>
#include <dxgi1_6.h>
#pragma comment(lib, "d3d12.lib")
#pragma comment(lib, "dxgi.lib")
Common Pitfalls
- Not closing the command list before submission.
- Modifying a closed command list (must reset allocator and list).
- Reusing allocators without synchronization (causes GPU/CPU conflicts).
By following these steps, you’ll efficiently utilize DirectX 12 command lists for rendering and compute tasks. Expand this foundation with synchronization and multi-frame rendering for production-ready code.