-
Notifications
You must be signed in to change notification settings - Fork 0
Vertex Shader Constant Packing
Zack edited this page Dec 3, 2024
·
2 revisions
The vertex shader compiler will automatically pack constants together when possible, to be more efficient with memory.
Take this example with 2 float2s
float2 const1 = float2(1.0f, 0.5f);
float2 const2 = float2(0.25f, 0.1f);In the assembly, everything is a float4, so without packing it looks like this:
float4 const1 = float4(1.0f, 0.5f, 0.0f, 0.0f);
float4 const2 = float4(0.25f, 0.1f, 0.0f, 0.0f);2 components are essentially getting wasted with each constant.
So the compiler will automatically combine them into a single constant behind the scenes
float4 packedConst = float4(1.0f, 0.5f, 0.25f, 0.1f);
#define const1 packedConst.xy
#define const2 packedConst.zwA by-product of this is that any time you create a new constant either explicitly or implicitly, it will look for an existing one, so it will only create a new constant if it has to.
float4 packedConst = float4(1.0f, 0.5f, 0.25f, 0.1f);
float myConst = 0.25f; // myConst will point to packedConst.z
// And also any of those constants can be used in the code without the compiler wasting memory
float myVar = 0.5f * myConst + 0.1f;