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
2.05k stars 561 forks source link

spir-v to glsl generate uniform binidng for non opaque types uniforms #2016

Closed xubury closed 2 years ago

xubury commented 2 years ago

I use shaderc to compile glsl to spir-v binary, then convert it back to glsl code using SPIRV-Cross. But the SPIRV-Cross compiler generate explicit binding point for non opaque type uniform which is not acceptable by OpenGL. OpenGL generate no errors using the spir-v binary. So I believe it's not caused by shaderc? The original glsl:

 #version 460

 uniform float f;

 void main()
 {
     gl_Position = vec4(f, f, f, 1.0);
 }

After converting:

 #version 460

 layout(location = 0, binding = 0) uniform float f;

 void main()
 {
     gl_Position = vec4(f, f, f, 1.0);
 }
xubury commented 2 years ago

After looking into the assembly, shaderc generate binding for non opaque types uniforms. Hmm, don't know why this is ok in spir-v binary form.

; Version: 1.0
; Generator: Google Shaderc over Glslang; 10
; Bound: 28
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Vertex %main "main" %_ %gl_VertexID %gl_InstanceID
               OpSource GLSL 460
               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
               OpSourceExtension "GL_GOOGLE_include_directive"
               OpName %main "main"
               OpName %gl_PerVertex "gl_PerVertex"
               OpMemberName %gl_PerVertex 0 "gl_Position"
               OpMemberName %gl_PerVertex 1 "gl_PointSize"
               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
               OpName %_ ""
               OpName %f "f"
               OpName %gl_VertexID "gl_VertexID"
               OpName %gl_InstanceID "gl_InstanceID"
               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
               OpDecorate %gl_PerVertex Block
               OpDecorate %f Location 0
               OpDecorate %f DescriptorSet 0
               OpDecorate %f Binding 0
               OpDecorate %gl_VertexID BuiltIn VertexId
               OpDecorate %gl_InstanceID BuiltIn InstanceId
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
       %uint = OpTypeInt 32 0
     %uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
%_ptr_UniformConstant_float = OpTypePointer UniformConstant %float
          %f = OpVariable %_ptr_UniformConstant_float UniformConstant
    %float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_ptr_Input_int = OpTypePointer Input %int
%gl_VertexID = OpVariable %_ptr_Input_int Input
%gl_InstanceID = OpVariable %_ptr_Input_int Input
       %main = OpFunction %void None %3
          %5 = OpLabel
         %18 = OpLoad %float %f
         %19 = OpLoad %float %f
         %20 = OpLoad %float %f
         %22 = OpCompositeConstruct %v4float %18 %19 %20 %float_1
         %24 = OpAccessChain %_ptr_Output_v4float %_ %int_0
               OpStore %24 %22
               OpReturn
               OpFunctionEnd
HansKristian-Work commented 2 years ago

Non-opaque uniforms like this is allowed in the GL environment, not Vulkan. This isn't really supported by SPIRV-Cross. SPIRV-Cross focues on the Vulkan environment as a baseline when it comes to SPIR-V features.

In this particular case, the SPIR-V is questionable, non-opaque uniforms don't have bindings or descriptor sets:

               OpDecorate %f DescriptorSet 0
               OpDecorate %f Binding 0

If I remove those from the assembly, the codegen is:

#version 460

layout(location = 0) uniform float f;

void main()
{
    gl_Position = vec4(f, f, f, 1.0);
}

which should be fine for GL.

xubury commented 2 years ago

@HansKristian-Work I use shaderc to generate the SPIR-V. Since shaderc is based on glslang, I have post this issue to glslang: https://github.com/KhronosGroup/glslang/issues/3024