Compute Queues

Compute queues are the foundation of task dispatch in DirectCompute. They enable you to submit compute workloads to the GPU, control execution order, and synchronize resources across multiple queues.

Basics

A compute queue is represented by the ID3D12CommandQueue interface. Each queue is associated with a specific D3D12_COMMAND_LIST_TYPE (typically D3D12_COMMAND_LIST_TYPE_COMPUTE for compute work).

// Creating a compute command queue
D3D12_COMMAND_QUEUE_DESC desc = {};
desc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
desc.NodeMask = 0;

ID3D12CommandQueue* pComputeQueue = nullptr;
device->CreateCommandQueue(&desc, IID_PPV_ARGS(&pComputeQueue));

Submitting Work

Work is submitted via command lists. Record your compute commands into an ID3D12GraphicsCommandList and then close and execute them on the queue.

// Record a compute dispatch
ID3D12GraphicsCommandList* cmdList = nullptr;
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, pAllocator, nullptr, IID_PPV_ARGS(&cmdList));

cmdList->SetPipelineState(pComputePSO);
cmdList->SetComputeRootSignature(pRootSig);
cmdList->Dispatch(threadGroupX, threadGroupY, threadGroupZ);
cmdList->Close();

ID3D12CommandList* ppLists[] = { cmdList };
pComputeQueue->ExecuteCommandLists(_countof(ppLists), ppLists);

Synchronization

Use fences to synchronize CPU and GPU or to coordinate multiple queues.

// Create a fence
ID3D12Fence* pFence = nullptr;
device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&pFence));
UINT64 fenceValue = 1;
HANDLE fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);

// Signal and wait
pComputeQueue->Signal(pFence, fenceValue);
if(pFence->GetCompletedValue() < fenceValue){
    pFence->SetEventOnCompletion(fenceValue, fenceEvent);
    WaitForSingleObject(fenceEvent, INFINITE);
}
fenceValue++;

Advanced Topics

FeatureDescription
Multi‑GPUAssign queues to specific adapters using NodeMask.
PriorityAdjust queue priority for latency‑critical workloads.
Cooperative SchedulingUse D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT for long‑running tasks.