I
I
IliaNeverov2021-07-28 19:49:24
OpenGL
IliaNeverov, 2021-07-28 19:49:24

How to properly use uniform buffers?

Hello everyone, I'm having a problem using uniform buffers.
I have 3 objects that are drawn using three shader programs

It seems that only from two shader programs I get uniform blocks (although in the shader programs themselves these uniform blocks are indicated by a different name)

A piece of code responsible for the uniform buffer
unsigned int FirstProgram = glGetUniformBlockIndex(shaders.ShaderPrograms[0]->mProgram, "Matrices");//достаю номер юниформ блока в первой шейдерной программе
  unsigned int SecondProgram = glGetUniformBlockIndex(shaders.ShaderPrograms[1]->mProgram, "Matrices");//достаю номер юниформ во второй шейдерной программе 
  glUniformBlockBinding(shaders.ShaderPrograms[0]->mProgram, FirstProgram, 0);
  glUniformBlockBinding(shaders.ShaderPrograms[1]->mProgram, SecondProgram, 0);
  unsigned int uboMatrices;
  glGenBuffers(1, &uboMatrices);
  glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
  glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(glm::mat4), NULL, GL_STATIC_DRAW);
  glBindBuffer(GL_UNIFORM_BUFFER, 0);
  glBindBufferRange(GL_UNIFORM_BUFFER, 0, uboMatrices, 0, 2 * sizeof(glm::mat4));
  glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
  glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(proj));
  glBindBuffer(GL_UNIFORM_BUFFER, 0);


here is the code for the vertex shaders of these programs (fragment shaders are exactly the same)

#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;


layout (std140) uniform Matrices1
{
     mat4 projectionMatrix;
     mat4 ViewMatrix;
};

out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;

void main()
{
    gl_Position = projectionMatrix * ViewMatrix *  ModelMatrix * vec4(position, 1.0f);
    FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
    Normal = mat3(transpose(inverse(ModelMatrix))) * normals;  
    TexCoord = uv;
}

#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;


layout (std140) uniform Matrices2
{
     mat4 projectionMatrix;
     mat4 ViewMatrix;
};

out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;

void main()
{
    gl_Position = projectionMatrix * ViewMatrix *  ModelMatrix * vec4(position, 1.0f);
    FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
    Normal = mat3(transpose(inverse(ModelMatrix))) * normals;  
    TexCoord = uv;



But from somewhere the third shader program also takes the contents of the block uniforms
its code (only the vertex shader (fragment ones are the same everywhere) )

#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;


layout (std140) uniform Matrices3
{
     mat4 projectionMatrix;
     mat4 ViewMatrix;
};

out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;

void main()
{
    gl_Position = projectionMatrix * ViewMatrix *  ModelMatrix * vec4(position, 1.0f);
    FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
    Normal = mat3(transpose(inverse(ModelMatrix))) * normals;  
    TexCoord = uv;
}

And all three objects, despite the fact that, in theory, this data should not come to the third one, they are normally rendered
61018a382cafa116229961.png
. Please explain why everything happens like this, uniform blocks from shaders are found despite the name, the third program takes 2 matrices from somewhere (the contents of the shader buffer)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IliaNeverov, 2021-07-31
@IliaNeverov

I already figured it out, it turns out this is due to the fact that all uniform blocks in shaders are by default associated with the anchor point 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question