制服将它们改成均匀的块后不起作用

时间:2013-06-13 19:58:06

标签: opengl glsl uniform

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

我的统一块定义如下:

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

0 个答案:

没有答案