Jump to content
  • Advertisement
Sign in to follow this  
Inderpreet Singh

Questions about compute shader

This topic is 1198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!