当我改变struct成员的顺序时,我注意到了统一缓冲区对象的奇怪行为(着色器中的struct与cpp中的struct相同)。这是我的源代码(工作版):
CPP:
struct Matrices {
// vec3, mat4 - glm library
mat4 projectionMatrix;
mat4 viewMatrix;
vec3 eyePosition;
};
GLSL:
layout (std140) uniform matrices {
mat4 projectionMatrix;
mat4 viewMatrix;
vec3 eyePosition;
};
CPP ubo代:
GLuint tmpUBO[2];
glGenBuffers(2, tmpUBO);
uniformBlocks["matrices"].bindingPoint = MATRICES_BINDING_POINT;
uniformBlocks["matrices"].ubo = tmpUBO[0];
glBindBuffer(GL_UNIFORM_BUFFER, tmpUBO[0]);
glBufferData(GL_UNIFORM_BUFFER, sizeof(Matrices), &matrices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
CPP更新ubo数据:
glBindBuffer(GL_UNIFORM_BUFFER, uniformBlocks["matrices"].ubo);
GLvoid *p =
glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(Matrices),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT);
memcpy(p, &matrices, sizeof(Matrices));
glUnmapBuffer(GL_UNIFORM_BUFFER);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
CPP将ubo绑定到链接和活动着色器:
GLuint bi = glGetUniformBlockIndex(handle, name);
glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, ubo);
glUniformBlockBinding(handle, bi, bindingPoint);
这个版本工作得很好,但是当我改变struct成员的顺序时(在glsl和cpp中),例如:
struct Matrices {
vec3 eyePosition;
mat4 projectionMatrix;
mat4 viewMatrix;
}
移动相机或更改视角时,不会更新其中一个或两个矩阵。
有人知道造成这种情况的原因是什么吗?