2

According to khronos.org, GL_MAX_UNIFORM_BLOCK_SIZE refers to the maximum size in basic machine units of a uniform block. The value must be at least 16384.

I have a fragment shader, where I declared a uniform interface block and attached a uniform buffer object to it.

#version 460 core

layout(std140, binding=2) uniform primitives{
    vec3 foo[3430];
};
...

If I query the size of GL_MAX_UNIFORM_BLOCK_SIZEwith:

 GLuint info;
    glGetUniformiv(shaderProgram.getShaderProgram_id(), GL_MAX_UNIFORM_BLOCK_SIZE, reinterpret_cast<GLint *>(&info));
    cout << "GL_MAX_UNIFORM_BLOCK_SIZE: " << info << endl;

I get: GL_MAX_UNIFORM_BLOCK_SIZE: 22098. It is ok, but for example: when I changes the size of the array to 3000 (instead of 3430), I get GL_MAX_UNIFORM_BLOCK_SIZE: 21956

As far as I know, GL_MAX_UNIFORM_BLOCK_SIZE should be a constant depending on my GPU. Then why does it change, when I modify the size of the array?

Fox1942
  • 276
  • 2
  • 18
  • 2
    [Don't use `vec3` in UBOs.](https://stackoverflow.com/q/38172696/734069) Also, initialize your variables. – Nicol Bolas Apr 10 '20 at 13:32
  • Yeah, it was also a problem. I sent glm::vec3 from C++ to vec3 in GLSL. Because the glsl vec3 has 16 byte base aligment, it took the next vec3's first element as the fourth element of the previous vec3, which is a vec4 in real. I switched to vec4 on C++ side and now it's giving me the right vertices. However, it is not clear to me, that when I switch to vec4 in glsl, the values in the shader storage are wrong. As far as I know, glsl vec3 and vec4 have 16 bytes as base aligments. – Fox1942 Apr 11 '20 at 21:17

1 Answers1

3

GL_MAX_UNIFORM_BLOCK_SIZE is properly queried with glGetIntegerv. It is a constant defined by the implementation which tells you the implementation-defined maximum. glGetUniform returns the value of a uniform in the given program. You probably got an OpenGL error of some kind, since GL_MAX_UNIFORM_BLOCK_SIZE is not a valid uniform location, and therefore your integer was never written to. So you're just reading uninitialized data.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982