Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use shader constant-buffers?

tI'm struggling to understand constant buffers in DirectX. I've seen two syntaxes

float4 myVar;

declared at top level of the shader file and

cbuffer myBuffer
{
    float4 myVar;
}

as detailed on http://msdn.microsoft.com/en-gb/library/windows/desktop/bb509581(v=vs.85).aspx

I understand that constant buffers need to be assigned to slots (zero-based indexing) and can be set in code.

The two frameworks I've looked at (SlimDX vs SharpDX) seem to use different conventions for setting them - SlimDX by string name (via shader reflection?) and SharpDX by slot number - although its not clear where the slot number is obtained from?

Can anyone shed light on the difference between the two syntaxes, if they are actually different, how slot numbers are assigned to declarations in the .fx file and how the slot numbers are shared between shaders?

Any help appreciated

like image 250
AnonDev Avatar asked Apr 09 '13 09:04

AnonDev


1 Answers

Both SharpDX and SlimDX use the same convention when using raw Direct3D10/Direct3D11 API (through Direct3D10.Device.XXX.SetConstantBuffers and Direct3D11.DeviceContext.XXX.SetConstantBuffers).

As stated in the link from the MSDN documentation, section "Default constant buffers", It is explained that variable not defined inside a Constant Buffer will end-up in a default Global Constant Buffer called "$Global".

On the other hand, legacy Effect framework (which interacts with RAW Direct3D10+ API) provides access to individual variable by their name. Internally they are using ShaderReflection to query the content of Constant Buffers and automatically handle allocate/update/upload of these constant buffers. In the end this is the constant buffer that will be uploaded to the GPU, not individual variables (that are not existing outside the constant buffer). The legacy Effect framework handles dynamically which slot to bind the constant buffer to by querying the binding slot using ShaderReflection.

Slot numbers are assigned by the compiler at compile time. If you don't specify explicitly a register (see register for cbuffer in MSDN documentation), the compiler will affect the first available slot to the first cbuffer used. There is no guarantee that the compiler will affect the same slot between shaders for a same constant buffers, unless you set the register explicitly.

like image 72
xoofx Avatar answered Oct 21 '22 03:10

xoofx