Graphics Reference
In-Depth Information
a matching alignment. Some compilers also support pragmas for specifying the packing
alignment. The packing for an HLSL constant buffer can also be manually specified though
the packoffset keyword. Listing 6.13 demonstrates usage of packoffset to declare a
tightly packed constant buffer with four-byte alignment.
cbuffer VSConstants
{
float4x4 WorldMatrix : packoffset(c0);
float4x4 ViewProjMatrix : packoffset (c4);
fioat3 Color : packoffset(c8);
uint EnableFog : packoffset(c8.w);
float2 ViewportXY : packoffset(c9);
float2 ViewportWH : packoffset(c9.z);
}
Listing 6.1 3. Specifying constant buffer packing.
When a constant buffer is declared in HLSL, the compiler automatically maps it
to one of 15 constant buffer registers for the corresponding stage of the pipeline. These
registers are named cb0 through cb14, and the index of the register directly corresponds
to the slot passed to *SSetConstantBuffers when binding a constant buffer to a shader
stage. The register index can be queried for a shader program using the reflection APIs, so
that the host application knows which slot to specify. Alternatively, the register index can
be manually specified in the HLSL declaration, using the register keyword. Listing 6.14
demonstrates the usage of this keyword.
cbuffer VSConstants : register(cb0)
{
float4x4 WorldMatrix : packoffset (c0);
float4x4 ViewProjMatrix : packoffset(c4);
float 3 Color : packoffset(c8);
uint EnableFog : packoffset(c8.w);
float2 ViewportXY : packoffset (c9);
float2 ViewportWH : packoffset(c9.z);
}
Listing 6.14. Specifying the constant buffer register index.
6.4.1 Default Constant Buffers
Any global variables declared without the static const modifiers will be treated by
the compiler as constants inside a default constant buffer named $Globals. Similarly,
Search WWH ::




Custom Search