Expand description
Lowering of Immediate globals.
WGSL lays out immediates like storage buffers, but HLSL can only read root constants
through a cbuffer, which packs values differently: every nested struct, array and matrix
starts on a new 16-byte register. Padding cannot fix this, since it only moves members later.
Instead, we declare a flat array of uint4 registers, and assemble the typed value in a
static variable at the start of each entry point that uses it. All byte offsets are
compile-time constants, much like storage loads from ByteAddressBuffers:
β
struct Inner { a: u32, b: u32 }
struct Data { x: u32, inner: Inner, y: u32 }
var<immediate> data: Data;β
cbuffer data_block : register(b0) { uint4 data_raw[1]; };
static Data data;
void main() {
data = ConstructData(
asuint(data_raw[0].x),
ConstructInner(asuint(data_raw[0].y), asuint(data_raw[0].z)),
asuint(data_raw[0].w)
);
// ...
}ConstantsΒ§
- COMPONENTS π
- Component names of a register, indexed by the wordβs position within it.
- WORDS_
PER_ πREGISTER - The number of 4-byte words in one register of the raw immediates
cbuffer.
FunctionsΒ§
- immediate_
word π - Returns an expression for the 4-byte word with index
wordin the raw immediatesraw.