2

我刚刚将我的矩阵制服切换到一个块,但即使我没有改变我的矩阵的生成方式,它们也不再工作,只是它们进入着色器的方式

我的统一块定义如下:

layout(row_major, std140) uniform MatrixBlock {
    mat4 model;
    mat4 view;
    mat4 proj;
};

我这样设置统一块:

void UniformBlock::generateBuffer() {
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_UNIFORM_BUFFER, buffer);
    glBufferData(GL_UNIFORM_BUFFER, size(), NULL, GL_STREAM_DRAW);
    glBindBufferRange(GL_UNIFORM_BUFFER, bindingIndex, buffer, 0, size());
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

size()192在这种情况下只返回。

然后我将程序绑定到绑定索引,如下所示:

void UniformBlock::bind(GLuint program) const {
    GLuint index = glGetUniformBlockIndex(program, getBlockName().c_str());

    if(index != GL_INVALID_INDEX) {
        glUniformBlockBinding(program, index, getBindingIndex());
    }
}

getBlockName()"MatrixBlock"在这种情况下返回。

然后我使用以下方法设置矩阵:

void MatrixBlock::bindMatrix(size_t offset, const Matrix4& matrix) {
    MatrixBlock& self = instance();

    glBindBuffer(GL_UNIFORM_BUFFER, self.getBuffer());
    glBufferSubData(GL_UNIFORM_BUFFER,
                    offset * Matrix4::size(),
                    Matrix4::size(),
                    matrix.asArray()); //Matrix4 holds the actual matrix data in a std::array<float, 16>, this method gets the std::array.begin()
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

其中offset之一在哪里:

enum Variables {
    MODEL = 0,
    VIEW = 1,
    PROJECTION = 2,
};

MatrixBlock只是一个子类UniformBlock(它是一个单例,因此是第一行)

Matrix4::size()返回sizeof(float) * 16

现在,当我像这样查询制服变量的大小时:

GLuint blockIndex;
GLuint indices[3];
GLint offsets[3];
GLsizei sizes[3];

glBindBuffer(GL_UNIFORM_BUFFER, MatrixBlock::instance().getBuffer());
blockIndex = glGetUniformBlockIndex(getProgram(), "MatrixBlock");
glGetActiveUniformBlockiv(getProgram(), blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, (GLint*)indices);
glGetActiveUniformsiv(getProgram(), 3, indices, GL_UNIFORM_OFFSET, offsets);
glGetActiveUniformsiv(getProgram(), 3, indices, GL_UNIFORM_SIZE, sizes);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

我明白了

Name               Index Offset Size
MatrixBlock.model  0     0      1
MatrixBlock.view   1     64     1
MatrixBlock.proj   2     128    1

为什么尺寸报告为一?不应该是64吗?这是我问题的根源吗?


您可以在此处查看 github 上的整个源代码:lerp@github

4

0 回答 0