KhronosGroup / glslang

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Other
2.98k stars 823 forks source link

HLSL: shader with fmod where y is integer not producing OpFMod, changing y to float works #1960

Closed danginsburg closed 4 years ago

danginsburg commented 4 years ago

Compiling the following shader with glslang using glslangValidator -e MainPs -D -V -S frag test.hlsl -o test.spv:

    struct PS_INPUT
    {
        float4 vColor : COLOR0;
        float4 vPositionSs : SV_Position;
    };

    bool RejectStipplePixel( PS_INPUT i )
    {
        float xCoord = ( i.vPositionSs.x - 0.5 );
        float yCoord = ( i.vPositionSs.y - 0.5 );

        int yOffset = int( fmod( yCoord, 2 ) );
        float a = fmod( xCoord + yOffset, 2 );
        return ( a < 1.0 );
    }

    float4 MainPs( PS_INPUT i ) : SV_TARGET0
    {
        if ( !RejectStipplePixel( i ) )
        {
            clip( -1.0 );
        }

        return i.vColor;
    }

Produces spir-v that never executes fmod:

; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 7
; Bound: 69
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %MainPs "MainPs" %i_vColor %i_vPositionSs %_entryPointOutput
               OpExecutionMode %MainPs OriginUpperLeft
               OpSource HLSL 500
               OpName %MainPs "MainPs"
               OpName %i_vColor "i.vColor"
               OpName %i_vPositionSs "i.vPositionSs"
               OpName %_entryPointOutput "@entryPointOutput"
               OpDecorate %i_vColor Location 0
               OpDecorate %i_vPositionSs BuiltIn FragCoord
               OpDecorate %_entryPointOutput Location 0
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
   %i_vColor = OpVariable %_ptr_Input_v4float Input
%i_vPositionSs = OpVariable %_ptr_Input_v4float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput = OpVariable %_ptr_Output_v4float Output
     %MainPs = OpFunction %void None %3
          %5 = OpLabel
         %62 = OpLoad %v4float %i_vColor
               OpStore %_entryPointOutput %62
               OpReturn
               OpFunctionEnd

This appears to be because the y value in fmod is an integer. If I change the shader so that it is a floating point value:

        int yOffset = int( fmod( yCoord, 2.0 ) );
        float a = fmod( xCoord + yOffset, 2.0 );

Then it produces the correct spir-v. Fxc accepts it either way:

; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 7
; Bound: 113
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %MainPs "MainPs" %i_vColor %i_vPositionSs %_entryPointOutput
               OpExecutionMode %MainPs OriginUpperLeft
               OpSource HLSL 500
               OpName %MainPs "MainPs"
               OpName %i_vColor "i.vColor"
               OpName %i_vPositionSs "i.vPositionSs"
               OpName %_entryPointOutput "@entryPointOutput"
               OpDecorate %i_vColor Location 0
               OpDecorate %i_vPositionSs BuiltIn FragCoord
               OpDecorate %_entryPointOutput Location 0
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
       %bool = OpTypeBool
        %int = OpTypeInt 32 1
  %float_0_5 = OpConstant %float 0.5
    %float_2 = OpConstant %float 2
    %float_1 = OpConstant %float 1
%_ptr_Input_v4float = OpTypePointer Input %v4float
   %i_vColor = OpVariable %_ptr_Input_v4float Input
%i_vPositionSs = OpVariable %_ptr_Input_v4float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput = OpVariable %_ptr_Output_v4float Output
     %MainPs = OpFunction %void None %3
          %5 = OpLabel
         %70 = OpLoad %v4float %i_vColor
         %73 = OpLoad %v4float %i_vPositionSs
         %98 = OpCompositeExtract %float %73 0
         %99 = OpFSub %float %98 %float_0_5
        %101 = OpCompositeExtract %float %73 1
        %102 = OpFSub %float %101 %float_0_5
        %104 = OpFMod %float %102 %float_2
        %105 = OpConvertFToS %int %104
        %108 = OpConvertSToF %float %105
        %109 = OpFAdd %float %99 %108
        %110 = OpFMod %float %109 %float_2
        %112 = OpFOrdLessThan %bool %110 %float_1
         %84 = OpLogicalNot %bool %112
               OpSelectionMerge %85 None
               OpBranchConditional %84 %86 %85
         %86 = OpLabel
               OpKill
         %85 = OpLabel
               OpStore %_entryPointOutput %70
               OpReturn
               OpFunctionEnd
johnkslang commented 4 years ago

Strange; it just replaces the whole fmod() with 0. I will look into it.