3
\$\begingroup\$

If you define a constant buffer in a shader, for example

cbuffer test : register(c2)
{
    float4x4 data;
}

But never actually use the data in the shader, does that incur any runtime cost at all? It's convenient for me to do this but not at the cost of slowing runtime performance at all. When I time it I don't see any difference...

\$\endgroup\$
2
\$\begingroup\$

It should not incur any runtime cost on the GPU. A constant buffer is just a chunk of data in memory somewhere; if it's never accessed, there shouldn't be any cost to having it there. It's possible that it adds slightly to the CPU cost due to the graphics driver needing to validate things when the constant buffer is updated or bound to the pipeline. But if you're measuring no change in timings then I wouldn't worry about it.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

For a completely unused constant buffer the shader compiler will see what's happening and D3D won't even allocate a slot for it to be bound in. You can easily verify this by using the reflection API on the compiled shader.

This means you can declare every constant buffer in every shader, and still not go over the limit as long as you don't actually use too many of them.

This means you can create a single header file for them, and use preprocessor macros to make it compile both as C/C++ structure definitions and as shader constant buffers. That way the two sides can't get out of sync with each other.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Great, thank you. The idea of creating a common C++/HLSL header using preprocessor macros is a great one too, at the moment I'm maintaining two almost identical sets of definitions. Although padding might be an issue... \$\endgroup\$ – JohnB Aug 22 '13 at 7:19
  • \$\begingroup\$ One way to handle the padding is to avoid shader constant data types that aren't a multiple of 16 bytes. \$\endgroup\$ – Adam Aug 22 '13 at 8:23
  • \$\begingroup\$ Indeed. Although I have a few constants for some of the shaders that are single floats such as a time do do animations. I'll find a way though :) \$\endgroup\$ – JohnB Aug 22 '13 at 8:36

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.