KhronosGroup / SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Apache License 2.0
1.96k stars 549 forks source link

MSL: non-const reference cannot bind to vector element when passing a vector component as out parameter #2332

Closed webez closed 3 weeks ago

webez commented 1 month ago

Hi, I have found that this hlsl code, compiled with Dxc -fcgl , generates non valid msl with error: non-const reference cannot bind to vector element. I am using Dxc -fcgl and almost non spirv-opt passes to have msl as faithful to the original hlsl code as possible.

void Func(out float a) {
  a = 1;
}

float3 ps_main(): SV_TARGET {
    float3 vect;
    Func(vect.x);
    return vect;
}

Replicated in Shader Playground https://shader-playground.timjones.io/74fa5859a78e876ae148496f74810e1e

HansKristian-Work commented 3 weeks ago

This is invalid SPIR-V.

error: line 52: Pointer operand '19[%19]' must be a memory object declaration
  %20 = OpFunctionCall %void %Func %19

You cannot take an access chain and pass it as a parameter like this. DXC is generating illegal code here.

I don't know what -fcgl is, but it's not documented.

webez commented 3 weeks ago

-fcgl enables CodeGenHighLevel in Dxc (which disables legalization) and is needed to generate msl that still looks like the original hlsl source.

HansKristian-Work commented 3 weeks ago

Yes, but if it generates invalid SPIR-V, it generates invalid SPIR-V. At least maybe run partial legalization that does not require inlining.