Sign in to follow this  
Inderpreet Singh

Questions about compute shader

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)?

Edited by Inder

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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;

Edited by Inder

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this