Sign in to follow this  
Hornsj3

Performance in Compute: Dispatch vs. Wasted Threads

Recommended Posts

Bear with me, this may be a long one.

I'm still perfecting a compute shader to generate custom mip-map values. My current problem is whether or not to have thousands of wasted threads, or multiple dispatch calls.

Here is a sample of how I'm setting up the number of threads in a thread group. This has no wasted threads, so long as my mip-level is at least 32x32.

[code]
#define sizeMip32_x 32
#define sizeMip32_y 32

[numthreads(sizeMip32_x, sizeMip32_y, 1)]
void CS32( uint3 DispatchThreadID : SV_DispatchThreadID )
{
...
}[/code]


The problem arises after start generating 16x16 and below.

On the one hand I could specify separate shaders for each level below 16x16, such as this:

[code]

#define sizeMip16_x 16
#define sizeMip16_y 16

[numthreads(sizeMip16_x, sizeMip16_y, 1)]
void CS16( uint3 DispatchThreadID : SV_DispatchThreadID )
{

...
}

... continue to 1x1
[/code]

All of these shaders would be compiled at the same time. I would then use logic in C++ to determine which to call, based on the dimensions of the mip-level. That is approach one.

The second approach is to use logic in the shader, coupled with memory barriers, to generate the mip hierarchy from high resolution down to 1x1.
this would only require the 32x32 shader and 1 dispatch call but will invoke (rough estimate for base level of 512x512) about 200,000 threads which add no value. (256x256 threads mip-level 1 ... to 1x1).

This is because, in the first iteration, each thread will be utilized in calculating the next level. It will sample each pixel in the 4 pixels underlying the mip-level pixel and write the maximum value it finds. The second mip-level calculated will still run all 256x256 threads but will duplicate the work of the 4 adjacent threads (at each level) due to the nature of the algorithm.

So, given all of this, would it be better to have separate shaders and multiple dispatch calls or waste hundreds of thousands of threads in a single dispatch?

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