# Unused constant buffers in shaders

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...

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.

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.

• 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... Aug 22 '13 at 7:19
• One way to handle the padding is to avoid shader constant data types that aren't a multiple of 16 bytes.