Hello.
I was trying create an array of uniform blocks in GLSL. Actually, that went fine, but the problem is, to pass more than one block to it.
My problem in detail is that, only the last passed block is present in the shader.
The situation looks like following:
I have an array of blocks, let’s say
layout(binding=0) uniform MyBlock
{
unsigned int a;
vec3 b;
mat4 c;
}block[5];
I am certain about accessing it’s elements via block[0].a, but I’m not certain about passing the block values from the C++.
My method only works for the last passed block - like all the previous ones weren’t passed at all, ot were just erased.
I’m doing it like this:
First, prepare the general code for the block:
index = glGetUniformBlockIndex(shader, "MyBlock");
glGetActiveUniformBlockiv(shader, index, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
glGetUniformIndices(shader, namesCount, names, indices);
glGetActiveUniformsiv(shader, namesCount, indices, GL_UNIFORM_OFFSET, offsets);
glGenBuffers(1, &ubo);
Then prepare and pass the data:
memcpy(data + offsets[0], &a, sizeof(GLuint));
//repeat for all of the block members
glBindBufferBase(GL_UNIFORM_BUFFER, currentIndex, ubo);
glBufferData(GL_UNIFORM_BUFFER, blockSize, data, GL_DYNAMIC_DRAW);
The above code changes the currentIndex variable to match the array index in GLSL.
With the above code everything, including shaders, compiles fine, but all the indexed blocks (except for the last one) have null values.
For example, when I pass a value to indices 0, 1, 2, 3 and 4, the 0-3 are all nulls, but the fourth one works fine. Or if I just pass a values to 0 and 1, only the 1 is filled with the actual data.
I, for myself, am thinking the error is in
index = glGetUniformBlockIndex(shader, "MyBlock");
this line, where “MyBlock” should be “MyBlock[index]”, which I tried, but didn’t work, or in binding the buffers (the last two lines), which I tried to change to other ones, but it didn’t help as well.
So here comes my final question: how do I fill an array of uniform blocks via uniform buffer?