Jump to content
  • Advertisement
Sign in to follow this  

Shaders and Constant Buffer behavior?

This topic is 2356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When setting constant buffers to a shader, does DirectX assignn it to the specific shader that is currently loaded, and remember it is for that shader? Or does it keep the same buffer applied through shader changes, and the buffer must be changed as well?

As an example, say I have two different vertex shaders, and each has it's own constant buffer, like the simplified snippet below.

ID3D11Device* Device;
ID3D11DeviceContext* DevCon;
ID3D11VertexShader* Shader1;
ID3D11VertexShader* Shader2;
ID3D11Buffer* CBuff1;
ID3D11Buffer* CBuff2;
//initialization code

//Render Routine
DevCon->VSSetShader(Shader1, NULL, 0);
DevCon->VSSetConstantBuffers(0, 1, CBuff1);
//Draw stuff with shader1
DevCon->VSSetShader(Shader2, NULL, 0);
DevCon->VSSetConstantBuffers(0, 1, CBuff2);
//Draw stuff with shader2

Now, if I then swap back to Shader1...

DevCon->VSSetShader(Shader1, NULL, 0);

Does DirectX remember to use CBuff1, or do I need to reset it as well?

Share this post

Link to post
Share on other sites
To expand the answer above, I believe it works this way: (I'm 4 months into DirectX, so take this for what it is worth).

There are a number of constant buffer registers, to which you can bind constant buffers. These registers need to be bound for each shader stage explicitly.

What this means is that you bind the constant buffers to the shader stages, not the particular shader. You can bind one shader to the pixel stage, bind the constant buffer to a constant buffer register, draw, and then bind another shader to the pixel shader. If you don't explicitly change what is bound to a constant buffer register, it will remain bound.

I hope that is clear.

Share this post

Link to post
Share on other sites
Any renderstates stay set untill you explicitly set a new one. The fastest instruction is always the one you don't have to execute, and thats exactly how the DX runtime operates

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!