Questions about compute shader

Started by
8 comments, last by Syntac_ 9 years ago

I have couple of questions for the following compute shader program


Buffer<float> InputBuf : register( t0 );
RWBuffer<fIoat> OutputBuf : register( u0 );
// Group size
#define size_x 20
#define size_y 1
// Declare one thread fo r each texel of the input texture.
[numthreads(size_x, size_y, 1)]
void CSMAIN( uint3 DispatchThreadID : SV_DispatchThreadID )
{
float Value = InputBuf.Load( DispatchThreadID.x );
OutputBuf[DispatchThreadID.x] = 2.0f * Value;
} 

1. Is this program executed by single thread?

2. What DispatchThread.x is doing?

Advertisement

1. No. It's specifying that each thread group should have 20x1 threads in it, so it will always execute with at least 20 threads. The actual number of threads executed depends on the number of thread groups that are dispatched, which is a parameter of the Dispatch() function called by the CPU.

2. SV_DispatchThreadID gives you GroupID * ThreadGroupSize + GroupThreadID. In this case it's being used to assign every thread to a particular index of the input and output buffers. So for instance thread 7 in group #5 would read and write using index (5 * 20 + 7) = 107.

It says that this program is doubling the contents of buffer resource. How it is doubling?

What is the value of DispatchThread.x?

No. It's specifying that each thread group should have 20x1 threads in it, so it will always execute with at least 20 threads. The actual number of threads executed depends on the number of thread groups that are dispatched, which is a parameter of the Dispatch() function called by the CPU.

What I meant was the function void CSMAIN is executed by single thread(Which is a thread number generated by SV_DispatchThread)?

It says that this program is doubling the contents of buffer resource. How it is doubling?

To paraphrase the soruce code, it's:
for( i=0; i!=20; ++i )
  output[i] = 2.0f * input[i];

What is the value of DispatchThread.x?

MJP said above, it's the ID of the current thread.
[numthreads(20, 1, 1)] means that in each group, there's 20*1*1 threads (20 threads in the "x" axis).
Assuming that in the C++ code, only one group is dispatched, then DispatchThread.x will be a number from 0 to 19.

If 5 groups are dispatched from the C++ code, then DispatchThread.x will be a number from 0 to 99.

What I meant was the function void CSMAIN is executed by single thread(Which is a thread number generated by SV_DispatchThread)?

It's executed by at least 20 single threads.

Thanks for the reply.

One last question - What will be the value of "Value" variable let's say for thread 17(only group is dispatched) ?

Whatever is read from the buffer at index 17.

Thanks!


Texture2D<float> InputTex : register( t0 );
RWTexture2D<float> OutputTex : register( u0 );
// Group size
#define size_x 20
#define size_y 20
// Declare one thread fo r each texel of the input texture.
[numthreads(size_x, size_y, 1)]
void CSMAIN( uint3 DispatchThreadID : SV_DispatchThreadID )
{
int3 texturelocation = int3( 0,0, 0 );
texturelocation.x = DispatchThreadID.x;
texturelocation.y = DispatchThreadID.y;
float Value = InputTex.Load( texturelocation );
OutputTex[DispatchThreadID.xy] = 2.0f * Value;
} 

And in this code DispatchThreadID.x and DispatchThreadID.y have same value like thread 17 of x and thread 17 of y?

And Can I write like this OutputTex[ texturelocation ] = 2.0f * Value;

I'm really confused! :(

There will be threads in the group where DispatchThreadID.x == DispatchThreadID.y.

20 * 20 (400) threads will be dispatched as defined by size_x and size_y, so the DispatchThreadID has the range (0..19, 0..19).

This topic is closed to new replies.

Advertisement