2

My GLSL fragment shader skips the "if" statement. The shader itself is very short.

I send some data via a uniform buffer object and use it further in the shader. However, the thing skips the assignment attached to the "if" statement for whatever reason.

I checked the values of the buffer object using glGetBufferSubData (tested with specific non zero values). Everything is where it needs to be. So I'm really kinda lost here. Must be some GLSL weirdness I'm not aware of.

Currently the

#version 420

layout(std140, binding = 2) uniform textureVarBuffer
{
    vec3    colorArray;             // 16 bytes
    int     textureEnable;          // 20 bytes
    int     normalMapEnable;        // 24 bytes
    int     reflectionMapEnable;    // 28 bytes

};

out vec4 frag_colour;

void main() {
    frag_colour = vec4(1.0, 1.0, 1.0, 0.5);

    if (textureEnable == 0) {
        frag_colour = vec4(colorArray, 0.5);    
    }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96

1 Answers1

2

You are confusing the base alignment rules with the offsets. The spec states:

The base offset of the first member of a structure is taken from the aligned offset of the structure itself. The base offset of all other structure members is derived by taking the offset of the last basic machine unit consumed by the previous member and adding one. Each structure member is stored in memory at its aligned offset. The members of a toplevel uniform block are laid out in buffer storage by treating the uniform block as a structure with a base offset of zero.

It is true that a vec3 requires a base alignment of 16 bytes, but it only consumes 12 bytes. As a result, the next element after the vec3 will begin 12 bytes after the aligned offset of the vec3 itself. Since the alignment rules for int are just 4 bytes, there will be no padding at all.

derhass
  • 43,833
  • 2
  • 57
  • 78