• Advertisement
Sign in to follow this  

DirectCompute - set numthreads from application code (or read from shader).

This topic is 944 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

Hello.

 

In my application (C++ side) I need to know the total number of threads that will be used in compute shader. Right now I have this numbers in two places - on C++ side and inside shader. When I need to change a value, I need to do it twice and we all know this is bad. Is there a way to pass this data from C++ to shader or vice versa?

Share this post


Link to post
Share on other sites
Advertisement
Defines/Macros.

shader code stub
[numthreads(NUMX,NUMY,NUMZ)]
void CS(uint3 threadId: SV_DispatchThreadID)
{
    // do your thing
}
And then provide those defines through the shader compilation process. For the command line compiler it's -DNUMX=8 etc. For D3DCompile it's the pDefines parameter.

I occasionally grant defaults for such things, so I can check-compile without providing those values (again shader code)
#ifndef NUMX
#define NUMX 8
#endif

Edit: Don't know if you can get numthreads from a compiled shader easily through shader reflection, never tried. There's dcl_thread_group declaration in the assembly, but from a quick glance at the shader reflection interface it doesn't look like it's conveniently retrievable.

Edit2: Blind in the morning tongue.png . See below, Thanks Tiago.

Share this post


Link to post
Share on other sites

You can use ID3D11ShaderReflection::GetThreadGroupSize to get the thread group size of a compute shader in C++.

 

I do it in my engine right after creating the compute shader and store the thread group size together with the compute shader pointer in a struct, so I always have access to the info when I want the use the shader. 

Edited by TiagoCosta

Share this post


Link to post
Share on other sites

Thank you, guys.

@TiagoCosta, I got this to work.

@unbird, I'm using VS2013 build chain that builds changed shader automatically. I can supply this definitions to shader compiler, but how can I get them both for application and shader?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement