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

Started by
2 comments, last by Volgogradetzzz 8 years, 7 months ago

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?

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.

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.

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?

This topic is closed to new replies.

Advertisement