KhronosGroup / SPIRV-Tools

Apache License 2.0
1.04k stars 545 forks source link

spirv-link: Produces duplicate pointers which confuse drivers #4224

Open DataBeaver opened 3 years ago

DataBeaver commented 3 years ago

When compiling and linking the attached shaders using these commands:

glslangValidator -G phong.vert.glsl -o phong.vert.spv
glslangValidator -G phong.frag.glsl -o phong.frag.spv
spirv-link phong.vert.spv phong.frag.spv -o phong.spv

The resulting binary has two pointer declarations for the Lighting block, with identical bindings:

               OpDecorate %__2 DescriptorSet 0
               OpDecorate %__2 Binding 1
               OpDecorate %__8 DescriptorSet 0
               OpDecorate %__8 Binding 1
   %Lighting = OpTypeStruct %_arr_LightSourceParameters_uint_1 %v4float %v4float %v3float %float %v4float %float
%_ptr_Uniform_Lighting = OpTypePointer Uniform %Lighting
        %__2 = OpVariable %_ptr_Uniform_Lighting Uniform
        %__8 = OpVariable %_ptr_Uniform_Lighting Uniform

One pointer is used in the vertex stage and the other in the fragment stage. Apparently this arrangement confuses at least Nvidia Linux drivers (460.56) when using OpenGL, resulting in the vertex shader not being able to read values from the uniform block. If I disassemble the module, remove the declaration and decorations of %8, replace all uses with %2, and finally reassemble the module, it works fine and my object renders correctly.

I'm uncertain of whether this is an issue with spirv-link generating a duplicate pointer or Nvidia drivers not processing it correctly. My best guess is that it has something to do with aliasing, though I haven't tried if adding an AliasedPointer decoration would help. If it's an Nvidia issue, maybe you can at least give me some extra information to present to them.

Test shaders: phong.zip

pierremoreau commented 3 years ago

That’s an issue with spirv-link; it de-duplicates types and exported/imported variables/functions, but does nothing regarding uniform/buffer/input/ouput blocks as it was first started for OpenCL.

Thank you for providing the shaders and details. I will try to have a look at it soon, but no idea when.

devshgraphicsprogramming commented 2 years ago

Is there any sort of comprehensive documentation on the limitations of SPIR-V linker?

pierremoreau commented 2 years ago

Is there any sort of comprehensive documentation on the limitations of SPIR-V linker?

There is not. I would say, this is the main ones that I am aware of for graphics, and #3128 for OpenGL or Vulkan compute. For OpenCL, it is mostly lacking some features like supporting the SPV_KHR_linkonce_odr extension, or specifying math flags at link time.