Updating multiple uniform block variables

I’m running into an issue with updating multiple uniform blocks and their variables via binding points. See code below.

The shaders are simple and allow coloring of vertices for basic primitives (e.g. triangles, lines, quads).


#version 430 core

uniform MatrixBlock
{
	mat4 World;
	mat4 View;
};

uniform ProjectionBlock
{
	mat4 Projection;
};

in vec3 Position;
in vec4 Color;

out FS_INPUT
{
	vec4 Color;
} fsOutput;

void main()
{
	vec4 position = vec4(Position, 1.0f);
	position = position * World;
	position = position * View;
	position = position * Projection;
	gl_Position = position;
	fsOutput.Color = Color;
}


#version 430 core

in FS_INPUT
{
	vec4 Color;
} fsInput;

out vec4 FragColor;

void main()
{
	FragColor = fsInput.Color;
}

The code below runs in a loop and updates the variables in the uniform block for the appropriate buffer. variableBuffer is an instance of a wrapper class around a uniform buffer. m_uniformBuffer is the actual OpenGL variable. I’m simply getting the block index of the uniform buffer (variableBuffer) and associating the block index and binding slot to the buffer. The binding slot of the MatrixBlock and ProjectionBlock are 0 and 1. Then I’m updating the variables (matrices in this case) with the appropriate values. For each uniform block that is found in the shader code, a new instance of the VariableBuffer class is created. So for below, the code will execute for the MatrixBlock and for the ProjectionBlock. The for loop will run twice for the World and View variables of MatrixBlock and once for the Projection matrix of ProjectionBlock. With the code as is, nothing renders on screen. If I update the vertex shader above and only have one uniform block (ProjectionMatrix) with a binding slot of 0, everything on screen renders correctly. From what I’ve read online with binding slots with uniform blocks, you can assign them to different slots, or the same if grouping, and render content, but for some reason, nothing renders when more than one uniform exists in the shaders.


GLuint program = shaderProgram->GetProgramHandle();
GLuint blockIndex = glGetUniformBlockIndex(program, variableBuffer->GetName().c_str());
GLuint bindingSlot = variableBuffer->GetBindingSlot();
glUniformBlockBinding(program, blockIndex, bindingSlot);
glBindBuffer(GL_UNIFORM_BUFFER, m_uniformBuffer);

int numVariables = variableBuffer->GetNumVariables();
ShaderVariableManager* shaderVarMgr = renderer->GetShaderVariableManager();
int shaderVarDataOffset = 0;

for (int index = 0; index < numVariables; ++index)
{
  ShaderVariable* shaderVar = variableBuffer->GetVariable(index);
  int variableSize = shaderVar->GetSize();

  if (shaderVar->GetVariableType() == ShaderVariable::VariableType::Matrix)
  {
    Matrix4 matrix = shaderVarMgr->GetMatrixVariableData(shaderVar);
    Matrix4 transposedMatrix;
    Matrix4::Transpose(matrix, transposedMatrix);
    glBufferData(GL_UNIFORM_BUFFER, variableSize, nullptr, GL_DYNAMIC_DRAW);
    glBindBufferBase(GL_UNIFORM_BUFFER, bindingSlot, m_uniformBuffer);
    glBufferSubData(GL_UNIFORM_BUFFER, shaderVarDataOffset, variableSize, &transposedMatrix);
  }

  shaderVarDataOffset += variableSize;
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.