Jump to content
  • Advertisement

DX11 Does buffer number matter in ID3D11DeviceContext::PSSetConstantBuffers()?

Recommended Posts

Does buffer number matter in ID3D11DeviceContext::PSSetConstantBuffers()? I added 5 or six constant buffers to my framework, and later realized I had set the buffer number parameter to either 0 or 1 in all of them - but they still all worked! Curious why that is, and should they be set up to correspond to the number of constant buffers?

Similarly, inside the buffer structs used to pass info into the hlsl shader, I added padding inside the c++ struct to make a struct containing a float3 be 16 bytes, but in the declaration of the same struct inside the hlsl shader file, it was missing the padding value - and it still worked! Do they need to be consistent or not? Thanks.

    struct CameraBufferType
        XMFLOAT3 cameraPosition;
        float padding;

Share this post

Link to post
Share on other sites

Well, I don't know why it worked for you with so little knowledge about your specific app. For me, I like to be very explicit about bind slot IDs for buffers and resources. I declare them in a shared header between C++ app code and HLSL shaders. Then the application uses SetConstantBuffers() with that ID and the shaders are specifying bind locations with that ID as well. HLSL is tricky because you have to concatenate the number with a string, like this:

cbuffer MyCBuffer : register(b4)

For that I am using a macro like this:

#define CBUFFER(name, slot) cbuffer name : register(b ## slot)

Regarding the padding, you are right, HLSL does automatically pad your structs when needed, but I like to keep that explicit as well, so I insert dummy data members where padding would occur. This makes it easier to share structs with the C++ application which you should also do in a shared header in my opinion.

Share this post

Link to post
Share on other sites

If you don't explicitly link your resources to slots via the register keyword in HLSL, the compiler will remove all unused resources and then automatically assign them to slot 0, 1, 2, etc 

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!