Still learning to work with UBOs, I’ve successfully used it to set V/P matrices, however I’m trying to set another, separate UB block in my fragment shader, but my data isn’t coming as expected. I’ve queried UNIFORM_BUFFER_OFFSET_ALIGNMENT and it requires multiples of 256, I’ve hardcoded in my sizes/offsets for simplicity of the example.
//foreach shader
glUniformBlockBinding(shader->id(), glGetUniformBlockIndex(shader->id(), "Transforms"), 0);
glUniformBlockBinding(shader->id(), glGetUniformBlockIndex(shader->id(), "LightCounter"), 1);
//endfor
//...
glBufferData(GL_UNIFORM_BUFFER, 512, NULL, GL_STATIC_DRAW);
//...
glBindBufferRange(GL_UNIFORM_BUFFER, 0, programId_, 0, 256);
glBindBufferRange(GL_UNIFORM_BUFFER, 1, programId_, 256, 256);
//...
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(projection));
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(view));
uint32_t tmp = 5;
glBufferSubData(GL_UNIFORM_BUFFER, 256, sizeof(uint32_t), &tmp);
uint32_t tmp2 = 8;
glBufferSubData(GL_UNIFORM_BUFFER, 256 + sizeof(uint32_t), sizeof(uint32_t), &tmp2);
//vertex shader
layout(std140) uniform Transforms
{
mat4 projection;
mat4 view;
};
//fragment shader
layout(std140) uniform LightCounter
{
uint lightCount[64];
};
As previously stated, my mat4’s in my vertex shader populate correctly, but in my fragment shader, lightCount[16]
is set to 5, and none of the rest of the valuse are set to 8. I’m sure there is just something I’m not understanding about UBs or the std140 layout. If anyone can help, that would be greatly appreciated.
Thanks.