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: invalid code generated when atan2 and saturate are used in mixed precision environment #2309

Closed BlurryLight closed 2 months ago

BlurryLight commented 2 months ago

the following hlsl generate invalid msl: https://godbolt.org/z/7aM4j9Tnb dxc args: -T ps_6_6 -E PSMain -spirv -enable-16bit-types

float4 PSMain(PSInput input) : SV_Target0
{
  return saturate(atan2(1.0h, 2.0h));
}

which generate invalid msl: https://shader-playground.timjones.io/ebece9aee93af143eb7801593f8cf51a

fragment PSMain_out PSMain()
{
    PSMain_out out = {};
    out.out_var_SV_Target0 = float4(float(clamp(precise::atan2(half(1.0), half(2.0)), half(0.0), half(1.0))));
    return out;
}

the error msg is "error: call to 'clamp' is ambiguous", because it tries to call a function clamp(float,half,half).

Potential cause

it seems spirv-cross always emits precise::atan2 instead of the relaxed precision version. https://github.com/KhronosGroup/SPIRV-Cross/blob/06407561ece7d7e78544112189f3eee13adf9959/spirv_msl.cpp#L10415-L10416

HansKristian-Work commented 2 months ago

Problem is that there is no half overload in MSL, even for fast:: variant. Trivial to workaround I suppose.