Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm starting with DX11 and I'm not sure if I'm doing the things right. I want to have both pixel and vertex shader program in one file. Both use some shared and some different constant buffers. So it looks like this:


cbuffer ForVS : register(b0) 
    float4x4 wvp;

cbuffer ForVSandPS : register(b1) 
    float4 stuff;
    float4 stuff2;

cbuffer ForVS2 : register(b2) 
    float4 stuff;
    float4 stuff2;

cbuffer ForPS : register(b3) 
    float4 stuff;
    float4 stuff2;


And in code I use

mContext->VSSetConstantBuffers( 0, 1, bufferVS);
mContext->VSSetConstantBuffers( 1, 1, bufferVS_PS);
mContext->VSSetConstantBuffers( 2, 1, bufferVS2);

mContext->PSSetConstantBuffers( 1, 1, bufferVS_PS);
mContext->PSSetConstantBuffers( 3, 1, bufferPS);

The numbering of buffers in PS is what bugs me, is it alright to bind random slots to shaders (in this example 1 and 3)? Does that mean it still uses just two buffers or does it initialize 0 and 2 buffer pointers to empty?

Thank you.

share|improve this question
up vote 1 down vote accepted

Yes, I'm pretty sure it's fine to leave any unused slots unbound. Internally what happens is likely that the constant buffer pointers get put in a little array that the shader can load from, so all that will happen is some entries of that array will be NULL, but since the pixel shader doesn't read them it doesn't matter.

share|improve this answer
Also, once a buffer has been bound to a slot, it keeps using that buffer until another one is bound to that same slot or it is set to NULL. So you don't need the second mContext->PSSetConstantBuffers( 1, 1, bufferVS_PS); call if you have not set a different buffer to slot 1 on bufferVS_PS. – dotminic Mar 23 '12 at 9:59
so what is the difference between VSSetConstantBuffers and PSSetConstantBuffers if I can set pixel shader constants with VSSetConstantBuffers, also am I supposed to set buffers to NULL after each switch to new material are is that reset automatically by switching shader – Marek Mar 23 '12 at 12:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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