Compute Shader (DX11)

DX11中提供vs和ps后的又一支持直接gpu计算的Compute Shader,可以看得出物理加速不再是只属于n卡,而且gpu通用计算的道路会越发成熟,下面做下标记,下次翻译

// from DX11 SDK and gamedev
 
Compute shaders are basically the same as any other shader ... pixel shader for example. Just like the pixel shader is envoked for each pixel, the compute shader is envoked for each
"thread". A thread is a generic and independent execution entity that doesn't really require any sort of geometry.
All you have to do now is to dispatch a number of threads, and your shader will be executed for each of these threads.
In DirectX, these threads are organized into "groups". You have X * Y * Z threads in each group, and U * V * W thread groups in your application.
Threads are organized into groups for synchornization purposes.
The number of groups are specified during dispatch time, and the number of threads in each group are hardcoded in the compute shader.
The compute shaders actually don't output anything. You store your computation results in a buffer at any location. This is done via what is known as an "Unordered Access View" (UAV). There are just like any other resource views that we have in DirectX 10, except they let you to read and write at any location.
By the way, we can also create access views of this same buffer so that they can be bound to other stages of the graphics pipeline, such as the pixel shader. This is done via Shader Resource Views (SRV). These are read-only views. (Note: UAVs are available in pixel shaders as well! but only on DirectX 11 hardware).
 
pd3dImmediateContext->CSSetShader( pComputeShader, NULL, 0 );
pd3dImmediateContext->CSSetShaderResources( 0, nNumViews, pShaderResourceViews );
pd3dImmediateContext->CSSetUnorderedAccessViews( 0, 1, &pUnorderedAccessView, NULL );
pd3dImmediateContext->CSSetConstantBuffers( 0, 1, ppCB );
pd3dImmediateContext->Dispatch( X, Y, Z );
 
CS related Attribute:
numthreads(X,Y,Z):
Defines the number of threads to be executed in a single thread group. The ability to specify the size of the thread group across three dimensions allows individual threads to be accessed in a manner that logically 2D and 3D data structures.
 
CS related System Values:
SV_GroupID:
Indices for which thread group a compute shader is executing in.
SV_GroupThreadID:
Indices for which an individual thread and thread group a compute shader is executing in. SV_GroupThreadID varies across the range specified for the compute shader in the numthreads attribute.
SV_GroupIndex:
The "flattened" index of a compute shader thread within a thread group, which turns the multi-dimensional SV_GroupThreadID into a 1D value. SV_GroupIndex varies from 0 to (numthreadsX * numthreadsY * numThreadsZ) – 1.
SV_DispatchThreadID:
Indices for which combined thread and thread group a compute shader is executing in. SV_DispatchThreadID is the sum of SV_GroupID * numthreads and GroupThreadID.
 


 Compute <wbr>Shader <wbr>(DX11)



// BasicCompute11.hlsl
 
struct BufType
{
    int i;
    float f;
};
StructuredBuffer<BufType> Buffer0 : register(t0);
StructuredBuffer<BufType> Buffer1 : register(t1);
RWStructuredBuffer<BufType> BufferOut : register(u0);
 
[numthreads(1, 1, 1)]
void CSMain( uint3 DTid : SV_DispatchThreadID )
{
    BufferOut[DTid.x].i = Buffer0[DTid.x].i + Buffer1[DTid.x].i;
    BufferOut[DTid.x].f = Buffer0[DTid.x].f + Buffer1[DTid.x].f;
}

你可能感兴趣的:(thread,null,buffer,Access,each,shader)