Table of Contents

Class GlobalInitializerReader

Namespace
ShadowDusk.HLSL
Assembly
ShadowDusk.HLSL.dll

Reads the initializers of global (and cbuffer) numeric declarations out of an HLSL effect and evaluates them to the default-value blobs an .mgfx / .knifx carries.

Why this exists. DXC drops the initializer of a global that lands in $Globals (it only warns), and vkd3d's DXBC does not carry it either, so nothing downstream of the compilers knows float R = 1; had a value. fxc (and so mgfxc on DirectX) does keep it. The initializer has to come from the source: this reads the preprocessed text the compilers are given (macros expanded, conditionals evaluated, the compile's own defines honoured), finds declaration-scope numeric declarations, and evaluates the initializer itself.

What it evaluates. Numeric literals (decimal, hex, exponents, f/h/u/l suffixes), true/false, the arithmetic, shift, bitwise, comparison, logical and ternary operators, (type) casts, scalar / vector / matrix constructors (float4(1,0,0,1), float2x2(...), float4(v.xy, 0, 1)), swizzles, constant indexing, brace lists, arrays of any of those, earlier const / static const scalars, and a small set of math intrinsics. Float arithmetic rounds to 32 bits at every step. Anything else (a function call it does not model, a struct, a texture) is reported in Failures, never silently turned into zero.

Blob layout. Exactly what MonoGame's EffectParameter.SetValue would leave in Data: scalars and vectors are their components; a matrix is written column by column (Data[c * Rows + r] = M[r][c], the "HLSL expects matrices to be transposed" layout); int is its 32-bit value, and bool is 0xFFFFFFFF for true, the value D3D reflection hands mgfxc.

public static class GlobalInitializerReader
Inheritance
GlobalInitializerReader
Inherited Members

Methods

Read(string, string)

Reads the global initializers of flattenedSource: the effect with its #includes inlined and the compile's macros prepended as #define lines (the output of Preprocessor.Flatten, optionally after the FX pre-parse).

public static Result<GlobalInitializers, ShaderError> Read(string flattenedSource, string sourceFile)

Parameters

flattenedSource string

The flattened source.

sourceFile string

Display name used in diagnostics.

Returns

Result<GlobalInitializers, ShaderError>

The defaults and failures, or an SD0009 error when the preprocessed view cannot be built.