DirectCompute
{{Short description|Application programming interface from Microsoft}}
Microsoft DirectCompute is an application programming interface (API) that supports running compute kernels on general-purpose computing on graphics processing units on Microsoft's Windows Vista, Windows 7 and later versions. DirectCompute is part of the Microsoft DirectX collection of APIs, and was initially released with the DirectX 11 API but runs on graphics processing units that use either DirectX 10 or DirectX 11.{{cite web |url=https://developer.nvidia.com/directcompute |title=DirectCompute |author= |date= 28 September 2010|website=developer.nvidia.com |publisher=NVIDIA |access-date=22 March 2015}} The DirectCompute architecture shares a range of computational interfaces with its competitors: OpenCL from Khronos Group, compute shaders in OpenGL, and CUDA from NVIDIA.
The DirectCompute API brings enhanced multi-threading capabilities to leverage the emerging advanced compute resources.{{Cite journal |last1=James |first1=Dave |last2=Chiapetta |first2=Marco |title=The Directx Evolution |url=https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=108366162&site=eds-live&scope=site |format=PDF |journal=Maximum PC |language=en |volume=19 |issue=8 |pages=52–59 |issn=1522-4279 |access-date=2024-08-07 |via=MasterFILE Complete}} The API is designed for non-graphical applications to access and use GPU resources.{{Cite journal |last=Mohr |first=Neil |title=Beyond Graphics with Gpgpus: Maximum PC |url=https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=131014389&site=eds-live&scope=site |journal=Maximum PC |pages=46–51 |issn=1522-4279 |via=MasterFILE Complete}}
Compute Pipeline
The Compute pipeline is a type of graphics pipeline used for dispatching and executing compute shaders. Compute pipelines are run through compute command lists, which are restricted to recording only copy and compute commands. Compute shaders are used for general-purpose algorithms and computations, and are run through parallel processors on the GPU. This parallel execution model done by the compute pipeline is organized into a dispatch, thread groups, and threads. The dispatch is a 3-dimensional container of thread groups, and a thread group is a 3-dimensional container of threads.{{Cite web |last=Young |first=Eric |date=2010-09-20 |title=DirectCompute - Optimizations and Best Practices |url=https://www.nvidia.com/content/gtc-2010/pdfs/2260_gtc2010.pdf |access-date=2024-02-14}} Thread groups are ran on the GPU in waves.{{Cite web |last1=Lively |first1=David |last2=Gruen |first2=Holger |date=2017-03-03 |title=Wave Programming in D3D12 and Vulkan |url=https://gpuopen.com/wp-content/uploads/2017/07/GDC2017-Wave-Programming-D3D12-Vulkan.pdf |access-date=2024-02-15 |website=gpuopen}}
This pipeline allows for workloads to be easily sent to the GPU without the need for restructuring all of a program's code.{{Cite journal |last=Graham-Smith |first=Darien |date=September 2021 |title=The Return of GPU Computing |url=https://search.ebscohost.com/login.aspx?direct=true&db=f6h&AN=151222838&site=eds-live&scope=site |journal=PCPro |language=English |issue=323 |pages=44–47 |issn=1355-4603 |access-date=2024-06-10 |via=MasterFILE Complete}}
A typical compute pipeline contains a read-only shader resource view as an input, constant buffer views for additional resource constants, and an unordered access view for an output. It is important to set the resource states of these resources in order to improve performance.{{Cite web |last=Kramer |first=Lou |date=2022-07-22 |title=Compute Shaders |url=https://gpuopen.com/presentations/2021/ComputeShaders-final.pdf |access-date=2024-03-11 |website=gpuopen}}
Example code
The initialization of the compute pipeline involves creating the root signature, reading the compute shader, and creating the pipeline state object.
// Set the root signature
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC root_signature_desc{
1, root_parameters,
0, nullptr
};
// Serialize the root signature
Microsoft::WRL::ComPtr
Microsoft::WRL::ComPtr
D3DX12SerializeVersionedRootSignature(
&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1_1,
root_signature_blob.GetAddressOf(), error_blob.GetAddressOf()
);
// Create the root signature
Microsoft::WRL::ComPtr
device->CreateRootSignature(
0, root_signature_blob->GetBufferPointer(),
root_signature_blob->GetBufferSize(),
IID_PPV_ARGS(root_signature.GetAddressOf())
);
// Read the compute shader
Windows::WRL::ComPtr
D3DReadFileToBlob(L"C:/path/to/compute/shader", compute_shader.GetAddressOf());
// Create the compute pipeline state object (PSO)
struct PipelineStateStream {
CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE root_signature;
CD3DX12_PIPELINE_STATE_STREAM_CS compute_shader;
} pipeline_state_stream;
// Setting the root signature and the compute shader to the PSO
pipeline_state_stream.root_signature = root_signature.Get();
pipeline_state_stream.compute_shader = CD3DX12_SHADER_BYTECODE{compute_shader.Get()};
D3D12_PIPELINE_STATE_STREAM_DESC pipeline_state_stream_desc{
sizeof(PipelineStateStream), &pipeline_state_stream
};
// Create pipeline state
device->CreatePipelineState(
&pipeline_state_stream_desc,
IID_PPV_ARGS(pipeline_state_stream.GetAddressOf())
);
After the initialization of the compute pipeline, every frame, the pipeline state must be set, the compute root signature must be set, and the dispatch is run.
command_list->SetPipelineState(pipeline_state);
command_list->SetComputeRootSignature(root_signature);
command_list->Dispatch(groups_x, groups_y, groups_z);
See also
Further reading
- [https://logins.github.io/graphics/2020/10/31/D3D12ComputeShaders.html Compute Shaders in D3D12]
References
{{Reflist}}
External links
- [http://msdn.microsoft.com/en-us/library/ff476331%28v=VS.85%29.aspx Compute Shader Overview]
- [https://channel9.msdn.com/Tags/directcompute-lecture-series DirectCompute Lecture Series]
- [http://www.gdcvault.com/play/1013698/Advanced-DirectX-11-DirectCompute-by Advanced DirectX 11: DirectCompute by Example]
- [http://on-demand-gtc.gputechconf.com/gtcnew/on-demand-gtc.php?searchByKeyword=DirectCompute&searchItems=&sessionTopic=&sessionEvent=&sessionYear=&sessionFormat=&submit=&select=+ GTC On-Demand]
{{Microsoft Windows components}}
{{Microsoft development tools}}
{{Microsoft APIs}}
{{DEFAULTSORT:Directcompute}}
{{microsoft-stub}}