Sign in to follow this  

Constant buffers in HLSL

Recommended Posts

Posted (edited)

I have some small questions about the usage of constant buffers in HLSL.

Currently, I distribute my data over constant buffers depending on the per-frame or per-draw character of that data. I wonder, if it is advisable to distribute the data additionally on the per-shader character of that data? For example: I could bind 2 constant buffers to both the VS and PS, or I could bind 4 constant buffers exclusively to the VS or PS. Note that the latter would require an additional map/unmap per draw and per frame. Due to this performance penalty, I assume this would not result in any benefits. But maybe someone has different thoughts on this?

Could it be beneficiary to use float3x3 instead of matrix (float4x4) in a constant buffer to reduce the size and thus bandwidth requirements? All codes samples, I have seen so far, use float4x4 for transforming normals and texture coordinates, whereas in both cases float3x3 would suffice. Due to the packing, float3x3 needs 3x float4s, whereas float4x4 needs 4x float4s. So in case I have 1 separate float4 available, I could add this float4 between the float3x3s.


Edited by matt77hias

Share this post

Link to post
Share on other sites

Constant buffer member alignement pad everything to 16B, a float3 cannot straddle over the boundary.

If your matrix is orthonormal (scale, rot, translation ), you can store 3 float4 and do 3 dots in the shader (because row 4 is always (0,0,0,1).

Trying to save a few bytes in a constant buffer unless you have hundreds of thousands of them and many more reads won't matters.


What matters much is the underlying cost of binding them, if you have a separate VS and PS. It means more work, more reflexivity, more map/unmap, more driver renaming, etc.


If you use win8 and +, you can also merge constant buffer in a single buffer and use an offset aware set call

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