2
\$\begingroup\$

I'm finding Compute shaders very confusing. I was wondering, if I were to provide an example, and maybe one more later, if someone could demonstrate how to convert it into a Compute shader for D3D11?

Here is the example, it's just a function:

void tonemap( out float4 low, out float4 high, float3 rgb, float scale)
{
    rgb = rgb*scale;
    low = rgb.xyzz;
    high = rgb.xyzz-saturate(rgb.xyzz);
}

Where would I start with this?

\$\endgroup\$
5
\$\begingroup\$

There's little point in converting your example to DirectCompute. It has no need to share data between pixels, so you are actually much better off doing a global tone-map operator in a Pixel Shader.

A more interesting case would be in trying to do a local tone-map operator where pixels need to share data between them (something you can't do in a pixel shader in a single pass). That's what the classic DirectX SDK sample HDRToneMappingCS11 is doing.

Other interesting DirectCompute shaders can be found in FluidCS11, NBodyGravityCS11, and BC6HBC7EncoderCS. There are also a number of compute shaders in the MiniEngine DirectX demo.

You can look at AdaptiveTessellationCS40 for ideas as well, but it's probably not a good choice for actually implementing tessellation on modern hardware--it was intended as a way to emulate tessellation hardware for Feature Level 10.x cards.

Of course, you can also use DirectCompute for completely non-graphics related tasks. For example, the ComputeShaderSort11 sample which implements a bitonic sort.

Take a look at the BasicCompute11 for a very simple example of using DirectCompute that does a simple sum of two arrays. This sample is really more about how to do all the setup and creating the buffers, so the shader there isn't really all that useful beyond showing how to deal with double-precision data buffers.

\$\endgroup\$
  • \$\begingroup\$ Thanks. Seems I need to do more reading and examine that SDK more. \$\endgroup\$ – user90214 Aug 22 '16 at 22:11
  • \$\begingroup\$ It' a bit of a challenge because the DirectX SDK itself is deprecated (see MSDN). There are, however, a lot of useful things in there which is why I published a ton of it to GitHub using the Windows SDK instead of the legacy DirectX SDK. \$\endgroup\$ – Chuck Walbourn Aug 23 '16 at 3:48
  • \$\begingroup\$ Thanks again :) Didn't realize you worked for Microsoft, so I really got a pro answer :) I have one more question if you don't mind: are there any types of shaders that should not be used as compute shaders? For example, are ssao-methods, bloom, lighting effects, maybe shadows all ok to use in Compute shaders, or no? \$\endgroup\$ – user90214 Aug 23 '16 at 15:26
  • \$\begingroup\$ It depends entirely on the specifics of the scenario and which hardware you are using, and of course what Direct3D Feature Level you require. If you require Feature Level 11+, then using DirectCompute for post-processing generally is probably a good option. That said, if your algorithm fits within the limits of a Pixel Shader and all other things being equal then that is likely faster than using compute since the hardware is well optimized for that kind of workload. \$\endgroup\$ – Chuck Walbourn Aug 23 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.