openglglslshadershader-storage-buffer

Is it legal to reuse Bindings for several Shader Storage Blocks


Suppose that I have one shader storage buffer and want to have several views into it, e.g. like this:

layout(std430,binding=0) buffer FloatView { float floats[]; };
layout(std430,binding=0) buffer IntView { int ints[]; };

Is this legal GLSL? opengl.org says no:

Two blocks cannot use the same index.

However, I could not find such a statement in the GL 4.5 Core Spec or GLSL 4.50 Spec (or the ARB_shader_storage_buffer_object extension description) and my NVIDIA Driver seems to compile such code without errors or warnings.


Solution

  • Does the OpenGL specification expressly forbid this? Apparently not. Or at least, if it does, I can't see where.

    But that doesn't mean that it will work cross-platform. When dealing with OpenGL, it's always best to take the conservative path.

    If you need to "cast" memory from one representation to another, you should just use separate binding points. It's safer.


    There is some official word on this now. I filed a bug on this issue, and they've read it and decided some things. Specifically, the conclusion was:

    • There are separate binding namespaces for: atomic counters, images, textures, uniform buffers, and SSBOs.
    • We don't want to allow aliasing on any of them except atomic counters, where aliasing with different offsets (e.g. sharing a binding) is allowed.

    In short, don't do this. Hopefully, the GLSL specification will be clarified in this regard.


    This was "fixed" in the revision 7 of GLSL 4.5:

    It is a compile-time or link-time error to use the same binding number for more than one uniform block or for more than one buffer block.

    I say "fixed" because you can still perform aliasing manually via glUniform/ShaderStorageBlockBinding. And the specification doesn't say how this will work exactly.