DirectX (Direct3D)

Microsoft's graphics and compute API for Windows and Xbox. DirectCompute runs GPU compute through HLSL compute shaders under Direct3D 11 and 12, and DirectML builds machine learning on top of it. It is the default path for Windows games and the Windows ML stack, on any vendor's GPU.

MicrosoftC++ · HLSLWindowsDirectComputeHLSL

Official docs ↗ ← All libraries

Install

# Install Visual Studio with the "Desktop development with C++" workload.
# It includes the Windows SDK, the Direct3D 11/12 headers, and the HLSL
# compilers dxc.exe (DXIL) and fxc.exe. Then from a Developer prompt:
dxc -T cs_6_0 -E main double.hlsl -Fo double.cso

Hello, GPU

double.hlsl — a DirectCompute (HLSL) compute shader

// double.hlsl — HLSL compute shader
RWStructuredBuffer<float> data : register(u0);

[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
    data[id.x] = data[id.x] * 2.0;
}

/* Host (C++ with Direct3D 11, abbreviated):
   D3D11CreateDevice(...) -> ID3D11Device / ID3D11DeviceContext
   compile double.hlsl (dxc/fxc or D3DCompile) -> CreateComputeShader
   CreateBuffer (STRUCTURED, UAV) -> upload the array
   CreateUnorderedAccessView(buffer) -> CSSetUnorderedAccessViews(0, 1, &uav)
   CSSetShader(cs); Dispatch(n / 64, 1, 1);
   copy to a STAGING buffer -> Map() -> the array is now doubled            */

Run it:

# compile with dxc (above), then create the device and Dispatch from the host program

Learn more