openglglslshader-storage-buffer

How to acquire the sampler2D type from a Shader Storage Buffer Object inside a shader?


I am trying to get a variable of type sampler2D into my shaders without using an uniform variable.

Instead, I want to hand it over using a Shader Storage Buffer Object (SSBO). What type of variable must be declared in the structure to hand over? How can I then cast this type to sampler2D in the shaders?


Solution

  • You cannot do that, samplers are just opaque types and not real variables whoe value is accessible in the shader. The OpenGL implementation will use the value you set to the sampler via glUniform1i to actually set up the texture sampling for how that has to be done on thee particular hardware - in some implementations, changing the value of the sampler actually triggers a recompilation or patching of the shader code.

    As a result, sampler types can only be declared in the default uniform block of a GLSL shader, the values for the samplers cannot come from an SSBO, UBO, TBO or whatever, and you cannot use sampler types als l-values in GLSL.

    The only way around those limitations are bindless textures which allow exactly what you want to do. But be warned that those are not a core feature of any OpenGL version to date (GL 4.6 as time of writing), so only exist as GL extensions currently. Also, HW and driver support for that feature is far from universal at this point in time.