D3D11 Compute Shader - Part 2

To understand the concept of Compute Shader, let's start from basic.

Compute Shader (CS) Threads
A thread is basic CS processing element.

1. CPU kicks off CS thread groups.
// Total number of thread groups = nX * nY * nZ
pDevice->Dispatch( nX, nY, nZ );

2. Each CS declares the number of threads on the "thread group".
// Total number of threads per thread group = X * Y * Z
[numthreads(X,Y,Z)]
void cs_main(...)
{
   ...
}

Example
// CPU
pDevice->Dispatch( 3, 2, 1 );

// CS
[numthreads(4, 4, 1)]
void cs_main(...)
{
   ...
}

// # of thread groups = 3*2*1 = 6
// # of threads per group = 4*4*1 = 16
// # of total threads = 6 * 16 = 96

N.B: Picture taken from GDC09 Slide "Shader Model 5.0 and Compute Shader"

CS Parameter Input
void cs_main(uint3 groupID          : SV_GroupID,
             uint3 groupThreadID    : SV_GroupThreadID,
             uint3 dispatchThreadID : SV_DispatchThreadID,
             uint  groupIndex       : SV_GroupIndex)
{
   ...
}

// groupID          : index [0..nX), [0..nY), [0..nZ)
// groupThreadID    : index [0..X), [0..Y), [0..Z)
// dispatchThreadID : global thread offset = groupID.xyz * (X,Y,Z) + groupThreadID.xyz
// groupIndex       : flattened version of groupThreadID

That's it for now, we will continue with more stuff in Part 3!

Comments

Popular posts from this blog

World, View and Projection Matrix Internals

BASH: Reading Text File

GDC 2015 Links