4
\$\begingroup\$

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:

Shader.fx

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – dotminic
    Mar 23, 2012 at 9:59
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Marek
    Mar 23, 2012 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .