Why are there VSSetConstantBuffers(), GSSetConstantBuffers(), PSSetConstantBuffers() (and so on) instead of just one SetConstantBuffers() for all shaders?

Should I have completely separate constant buffers for every shader type or should I use different functions (e.g. VSSet.../PSSet...) to set the same constant buffer when it holds data used in multiple shader types (such as light position or transform matrices)?


Because the alternative is worse.

There's a set-constant-buffer function for each major shader type because it is often desirable to have a completely different set of constants for each (and also, because one does not necessarily utilize every type of shader in all scenarios).

It's usually the case that each stage of the shader pipeline does a drastically different operation. The vertex shader transforms geometry from model space to clip space, the geometry shader manipulates or produces new vertex or adjacency information, the fragment shader makes color determinations, and so on. Because of the wildly different intents of each shader type, one will usually have very different constant inputs.

It would not make sense (and would be drastically limiting) to homogenize the interface such that you could have only a single constant buffer for every stage of the shader pipeline.

In your case, if you happen to be able to usefully use the same constant buffer for all shader stages, then its fine to set that buffer to each stage using the appropriate method. As your usages of each shader stage become more advanced, the likelihood that you'll want exactly the same data across every stage will decrease.

| improve this answer | |
  • \$\begingroup\$ How does the StartSlot argument work? Do I have to use a different one for every constant buffer? Are there separate slots for different shader types (vs/ps/etc)? \$\endgroup\$ – NPS May 12 '13 at 21:57
  • \$\begingroup\$ @NPS each shader has it's own slots. When setting multiple constantbuffers, you are able to select a startslot, it will increment the slotnumber for each constantbuffer passed. \$\endgroup\$ – Jeroen van Langen Nov 25 '16 at 9:16

In systems that manage distinct shader types, as opposed to those with uber-shaders, it's extremely helpful to be able to segregate the constant buffers for the different shader types - consider cases where the vertex shader, and it's associated constant buffer remain fixed - however the pixel shader definition alternates between a pure material based shader, a pure texture based shader, and a texture and material based shader - if this were conflated with the vertex shader constant buffer, then you could end up doubling the number (ok 2n - 1 for the purists :-) of constant buffer definitions for no good reason, not to mention requiring that some constant buffer information would be updated when it didn't actually need to be

| improve this answer | |
  • \$\begingroup\$ But I imagine that if I could "attach" a CB to a whole shader (not just vertex or pixel one) then I could do what you say, too (I can have more than one CB for a shader anyway, right?). \$\endgroup\$ – NPS May 18 '13 at 9:28
  • \$\begingroup\$ Yes you can - the issue is that a material pixel shader needs information about material color, whereas a texture pixel shader needs information about the texture, and the combination of texture and material needs information on both - I'm from the school of thought that says up front investment in differentiating vertex and pixel shaders leads to more management, but the concomitant removal of branches in the shaders leads to more throughput \$\endgroup\$ – Mark Mullin May 18 '13 at 13:25
  • \$\begingroup\$ I did not understand that sentence. Like at all. :P \$\endgroup\$ – NPS May 18 '13 at 22:23
  • \$\begingroup\$ one expensive design decision is worth millions of condition checks at runtime \$\endgroup\$ – Mark Mullin May 19 '13 at 9:53
  • \$\begingroup\$ But what does it have to do with what I said? Attaching constant buffers to one "whole" shader doesn't limit me in any way compared to attaching CB's to different kinds of shaders. I think. \$\endgroup\$ – NPS May 19 '13 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.