Skip to main content

Module immediates

Module immediates 

Source
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 word in the raw immediates raw.