Shaders and Constant Buffer behavior?

Started by
2 comments, last by NightCreature83 12 years ago
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?
Advertisement
It'll keep the currently set cbuffer until you change it yourself.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement