5

Is there any way to automatically compile OpenGL shaders for Vulkan? The problem is with the uniforms.

'non-opaque uniforms outside a block' : not allowed when using GLSL for Vulkan

I have tried compiling for OpenGL, then decompiling with spirv-cross with --vulkan-semantics, but it still has non-opaque uniforms.

spirv-cross seems to only have tools for compiling Vulkan shaders for OpenGL.

[--glsl-emit-push-constant-as-ubo]
[--glsl-emit-ubo-as-plain-uniforms]
me'
  • 494
  • 3
  • 14

1 Answers1

6

A shader meant for OpenGL consumption will not work on Vulkan. Even ignoring the difference in how they consider uniforms, they have very different resource models. Vulkan uses descriptor sets and binding points, with all resources using the same binding indices (set+binding). By contrast, OpenGL gives each kind of resource its own unique set of indices. So a GLSL shader meant for OpenGL consumption might assign a texture uniform and a uniform block to the same binding index. But you can't do that in a GLSL shader meant for Vulkan, not unless the two resources are in different descriptor sets.

If you want to share shaders, you're going to need to employ pre-processor trickery to make sure that the shader assigns resources (including how it apportions uniforms) for the specific target that the shader is being compiled for.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    Doesn't glslang have `--auto-map-bindings` and `--auto-map-locations`? – me' Nov 23 '19 at 22:56
  • Also, couldn't you just use a separate descriptor set for each resource type? It's not optimal, but you wouldn't need to rewrite the shader. – me' Nov 23 '19 at 23:22
  • 1
    @me': "*Doesn't glslang have --auto-map-bindings and --auto-map-locations?*" What good is that? In order for Vulkan to consume such shaders, you would need to introspect the generated SPIR-V. Additionally, there's no way for two shaders that would otherwise have compatible descriptor layouts to actually have compatible descriptor layouts. So every pipeline change has to have a descriptor set change. And you're going to have difficulty with descriptor pool allocation. And so forth. Resource binding really ought to be strictly controlled by your program. – Nicol Bolas Nov 23 '19 at 23:29
  • So, it's going to cause far more problems then rewriting the shader? – me' Nov 23 '19 at 23:43