This section details the functions, structures, and interfaces available for DirectCompute programming on Windows.
HRESULT CreateComputeShader(
[in] const void* pShaderBytecode,
[in] SIZE_T BytecodeLength,
[in] ID3D11ClassLinkage* pClassLinkage,
[out, optional] ID3D11ComputeShader** ppComputeShader
);
Creates a compute shader from compiled shader code.
Returns one of the Direct3D 11 Return Codes.
Use this method to create a compute shader object. Compute shaders are essential for general-purpose computation on the GPU.
void Dispatch(
[in] UINT ThreadGroupCountX,
[in] UINT ThreadGroupCountY,
[in] UINT ThreadGroupCountZ
);
Dispatches one or more compute-க்கப்படுகிறது thread groups.
This function dispatches threads in groups. The total number of threads launched is (ThreadGroupCountX
* ThreadGroupCountY
* ThreadGroupCountZ
) multiplied by the group size defined in the compute shader.
HRESULT CreateBuffer(
[in] const D3D11_BUFFER_DESC* pDesc,
[in, optional] const D3D11_SUBRESOURCE_DATA* pInitialData,
[out, optional] ID3D11Buffer** ppBuffer
);
Creates a buffer resource.
Returns one of the Direct3D 11 Return Codes.
Buffers are fundamental data containers. For DirectCompute, they are used for vertex data, index data, constant data, and unordered access data.
void CSSetShaderResources(
[in] UINT StartSlot,
[in] UINT NumViews,
[in, optional] ID3D11ShaderResourceView* const* ppShaderResourceViews
);
Sets an array of shader resource views to the compute shader pipeline stage.
Use this function to bind data (buffers, textures) to the compute shader for reading.
void CSSetUnorderedAccessViews(
[in] UINT StartUAV,
[in] UINT NumUAVs,
[in, optional] ID3D11UnorderedAccessView* const* ppUnorderedAccessViews,
[in, optional] const UINT* pUAVInitialCounts
);
Sets an array of unordered-access views (UAVs) to the compute shader pipeline stage.
UAVs allow compute shaders to read and write data, enabling parallel algorithms like reductions, sorting, and simulation.
typedef struct D3D11_BUFFER_DESC {
UINT ByteWidth;
D3D11_USAGE Usage;
UINT BindFlags;
UINT CPUAccessFlags;
UINT MiscFlags;
UINT StructureByteStride;
} D3D11_BUFFER_DESC;
Describes a buffer resource.
typedef struct D3D11_UNORDERED_ACCESS_VIEW_DESC {
DXGI_FORMAT Format;
D3D11_UAV_DIMENSION ViewDimension;
union {
D3D11_BUFFER_UAV Buffer;
D3D11_TEXTURE1D_UAV Texture1D;
D3D11_TEXTURE2D_UAV Texture2D;
D3D11_TEXTURE3D_UAV Texture3D;
};
} D3D11_UNORDERED_ACCESS_VIEW_DESC;
Describe an unordered-access view (UAV).
This structure describes the view of a buffer or texture resource that can be accessed via unordered-access views.
DirectCompute leverages Shader Model 5.0, which introduces new features specifically for GPGPU programming:
InterlockedAdd
, WaveGetWaveId
for advanced parallel algorithms.Compute shaders are written in HLSL (High-Level Shading Language) and compiled into bytecode.
// Define thread group shared memory
groupshared float shared_data[64];
// Define buffer resources
RWStructuredBuffer<float> outputBuffer : register(u0);
StructuredBuffer<float> inputBuffer : register(t0);
// Thread group size
[numthreads(64, 1, 1)]
void main(uint3 dispatchThreadID : SV_DispatchThreadID, uint3 groupThreadID : SV_GroupThreadID, uint groupID : SV_GroupID)
{
// Load data into shared memory
shared_data[groupThreadID.x] = inputBuffer[dispatchThreadID.x];
// Synchronize threads to ensure all data is loaded
GroupMemoryBarrierWithGroupSync();
// Perform computation using shared data
float result = shared_data[groupThreadID.x] * 2.0f;
// Write result to the output buffer
outputBuffer[dispatchThreadID.x] = result;
}