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: Texture2D used with SampleCmp is not converted to depth2D in all functions #2333

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. It seems that spirv-cross can change the parameter of the function that contains the call to SampleCmpto depth2D but can´t traverse the hierarchy to change all the needed places

Texture2D depthTex;
SamplerComparisonState depthSampler;

float DepthSampleCmp(Texture2D tex, SamplerComparisonState cmpSampler) 
{
  return tex.SampleCmp(cmpSampler, float2(1, 0), 0);
}

float ps_main() : SV_TARGET 
{
    return DepthSampleCmp( depthTex, depthSampler);
}

Replicated in Shader Playground https://shader-playground.timjones.io/36b79836bf8f6953e9f9d17122b58004

HansKristian-Work commented 3 weeks ago

Again, this is invalid SPIR-V:

%param_var_tex = OpVariable %_ptr_Function_type_2d_image Function
%param_var_cmpSampler = OpVariable %_ptr_Function_type_sampler Function
         %26 = OpLoad %type_2d_image %depthTex
               OpStore %param_var_tex %26
         %27 = OpLoad %type_sampler %depthSampler
               OpStore %param_var_cmpSampler %27
         %28 = OpFunctionCall %float %DepthSampleCmp %param_var_tex %param_var_cmpSampler

You cannot load and store opaque texture objects like this. -fcgl seems to be "disable legalization", which obviously will not generate correct code.