Search code examples
glslshadervulkanspir-vqrhi

How can I solve "syntax error, unexpected SAMPLER, expecting RIGHT_PAREN" when compiling Vulkan GLSL shader with sampler2D used as function argument?


I'm playing around with Qt's QRhi and more specifically its QtShaderTools and the qsb shader compiler tool. Basically it takes a Vulkan-GLSL shader source, and transforms it into SPIR-V and any other shader source/bytecode supported by QRhi. There's then some C++-side API to facilitate loading the generated files and run the shaders on the actual backend API (be it OpenGL, Vulkan, DX, Metal, and whatnot).

I'm trying to get a small bit of render code to work in a much larger project that uses OpenGL 3.3 Core extensively (through QOpenGL). I'm setting up the shader compilation and converting our #version 330 GLSL shader code to Vulkan GLSL #version 460.

The shader in question defines a helper function with a sampler2D as argument:

vec4 sampleFt( in sampler2D sampler, vec2 coordSource )
{ ... }

Compiling this shader with Qt's qsb tool throws an error pointing to this line:

QSpirvCompiler: Failed to parse shader
Shader baking failed: ERROR: <file>:<line>: '' :  syntax error, unexpected SAMPLER, expecting RIGHT_PAREN

I find lots of examples online of Vulkan GLSL shaders passing sampler2D as function arguments, and can't seem to find how I need to change this to make it work.

As an aside: our shaders are basically concatenated snippets of glsl, where this helper function is in a separate (and reused) source file from the actual calling code in a void main() defined in various other source files. The sampler in question is "passed to" the shader as a uniform, so removing the argument and using the uniform name could be made to work, but that seems to preclude any type of code having more than one sampler2D and calling the above helper function with both, so I think I'm missing something obvious.


Solution

  • In Vulkan sampler is a built-in type, just like sampler2D, or vec4, and you can't name a variable as a built-in type. So sampler2D sampler is an error, but sampler2D vec4, or sampler2D while would be too. It'll work if you rename the variable to smp for example.

    You can name variables sampler in OpenGL and WebGL type GLSL because sampler is only a built-in type in Vulkan type GLSL.