shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

SPIR-V: Initialization of variables with its own value during declaration #4459

Closed chaoticbob closed 5 days ago

chaoticbob commented 5 days ago

Slang does not support this behavior AFAIK, just filing this for documentation purposes.

The shader below contains variables that use their own components for initialization during their declarations:

float4 main(float4 a : A) : SV_Target
{
    float2 v = float2(a.x, v.x);
    float2 v2 = float2(v2.y, v.x);

    float2x2 m = float2x2(v.x, m._m00, a.x, a.z);
    float2x2 m2 = float2x2(m._m01, v2.x, a.x, a.z);

    return float4(m._m00, m._m01, m2._m10, m2._m11) + float4(v + v2, 0, 0);
}

DXC/DXIL errors out when compiling this shader but DXC/SPIR-V has no problem compiling it. Thankfully, Slang also errors out when compiling it:

shader.hlsl(8): error 30015: undefined identifier 'v'.
    float2 v = float2(a.x, v.x);
                           ^
shader.hlsl(9): error 30015: undefined identifier 'v2'.
    float2 v2 = float2(v2.y, v.x);
                       ^~
shader.hlsl(11): error 30015: undefined identifier 'm'.
    float2x2 m = float2x2(v.x, m._m00, a.x, a.z);

I can't think of a practical case where this would be useful. Besides looking scary, it also promotes bad practice.

Reference: https://github.com/microsoft/DirectXShaderCompiler/issues/6719