如何使用 glDrawElementsInstanced + Texture Buffer 对象?

How to use glDrawElementsInstanced + Texture Buffer Objects?

我的用例是一堆我想绘制的带纹理的四边形。我正在尝试使用四边形的相同索引数组多次绘制它,并使用 GLSL 中的 gl_InstanceIDgl_VertexID 从纹理缓冲区检索纹理和位置信息。

我理解纹理缓冲区的方式是创建它和我的实际缓冲区,link 它们,然后我放入实际缓冲区的任何内容都会神奇地出现在我的纹理缓冲区中?

所以我有我的顶点数据和索引数据:

struct Vertex
{
    GLfloat position[4];
    GLfloat uv[2];
};

Vertex m_vertices[4] =
{
    {{-1,1,0,1},{0,1}},
    {{1,1,0,1},{1,1}},
    {{-1,-1,0,1},{0,0}},
    {{1,-1,0,1},{1,0}}
};

GLuint m_indices[6] = {0,2,1,1,2,3};

然后我为四边形创建我的 VAO、VBO 和 IBO:

glGenBuffers(1,&m_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER,m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(Vertex)*4,&m_vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);

glGenVertexArrays(1,&m_vao);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER,m_vertexBuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,4,GL_FLOAT, GL_FALSE, sizeof(struct Vertex),(const GLvoid*)offsetof(struct Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE, sizeof(struct Vertex),(const GLvoid*)offsetof(struct Vertex, uv));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);

glBindVertexArray(m_vao);
glGenBuffers(1, &m_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*6,&m_indices,GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
glBindVertexArray(0);

我很确定我已经正确完成了上述操作。我的四边形有 4 个顶点,有六个索引来绘制三角形。

接下来我为纹理缓冲区创建缓冲区和纹理:

glGenBuffers(1,&m_xywhuvBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, m_xywhuvBuffer);
glBufferData(GL_TEXTURE_BUFFER, sizeof(GLfloat)*8*100, nullptr, GL_DYNAMIC_DRAW);   // 8 floats

glGenTextures(1,&m_xywhuvTexture);
glBindTexture(GL_TEXTURE_BUFFER, m_xywhuvTexture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, m_xywhuvBuffer); // they're in pairs of 2, in r,g of each texel.

glBindBuffer(GL_TEXTURE_BUFFER,0);

所以,这个想法是每四个纹素属于一个四边形,或者 gl_InstanceID。

当我绘制四边形时,它们执行以下操作:

glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_TEXTURE_BUFFER, m_xywhuvBuffer);

std::vector<GLfloat> xywhuz =
{
    -1.0f + position.x / screenDimensions.x * 2.0f,
    1.0f - position.y / screenDimensions.y * 2.0f,
    dimensions.x / screenDimensions.x,
    dimensions.y / screenDimensions.y,
    m_region.x,
    m_region.y,
    m_region.w,
    m_region.h
};

glBufferSubData(GL_TEXTURE_BUFFER, sizeof(GLfloat)*8*m_rectsDrawnThisFrame, sizeof(GLfloat)*8, xywhuz.data());

m_rectsDrawnThisFrame++;

所以我为每个四边形增加 m_rectsDrawThisFrame。你会注意到我传递的数据是 8 个 GLfloats,所以属于每个 gl_InstanceID 的 4 个纹素中的每一个都是 x,y 位置,宽度和高度,然后是真实的相同细节我要为四边形添加纹理的纹理。

最后,一旦我所有的 rects 都更新了 GL_TEXTURE_BUFFER 的部分,我运行这个:

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,texture); // this is my actual texture that the quads take a section from to texture themselves.
glUniform1i(m_program->GetUniformLocation("tex"),1);
glUniform4f(m_program->GetUniformLocation("color"),1,0,1,1);

glBindVertexArray(m_vao);

glDrawElementsInstanced(GL_TRIANGLES,4,GL_UNSIGNED_INT,0,m_rectsDrawnThisFrame);

m_rectsDrawnThisFrame = 0;

我重置了抽奖次数。我还注意到我必须激活第二个插槽中的纹理。 Texture Buffer Object用完了吗?

最后是我的 Vert 着色器

#version 410

layout (location = 0) in vec4 in_Position;
layout (location = 1) in vec2 in_UV;

out vec2 ex_texcoord;

uniform samplerBuffer buf;

void main(void)
{
    vec2 position = texelFetch(buf,gl_InstanceID*4).xy;
    vec2 dimensions = texelFetch(buf,gl_InstanceID*4+1).xy;
    vec2 uvXY = texelFetch(buf,gl_InstanceID*4+2).xy;
    vec2 uvWH = texelFetch(buf,gl_InstanceID*4+3).xy;

    if(gl_VertexID == 0)
    {
        gl_Position = vec4(position.xy,0,1);
        ex_texcoord = uvXY;
    }
    else if(gl_VertexID == 1)
    {
        gl_Position = vec4(position.x + dimensions.x, position.y,0,1);
        ex_texcoord = vec2(uvXY.x + uvWH.x, uvXY.y);
    }
    else if(gl_VertexID == 2)
    {
        gl_Position = vec4(position.x, position.y + dimensions.y, 0,1);
        ex_texcoord = vec2(uvXY.x, uvXY.y + uvWH.y);
    }
    else if(gl_VertexID == 3)
    {
        gl_Position = vec4(position.x + dimensions.x, position.y + dimensions.y, 0,1);
        ex_texcoord = vec2(uvXY.x + uvWH.x, uvXY.y + uvWH.y );
    }
}

还有我的 Frag 着色器

#version 410

in vec2 ex_texcoord;

uniform sampler2D tex;
uniform vec4 color = vec4(1,1,1,1);

layout (location = 0) out vec4 FragColor;

void main()
{
    FragColor = texture(tex,ex_texcoord) * color;
}

现在的问题是,在我没有在 GLIntercept 中报告任何错误之后,我在屏幕上没有绘制任何东西。

有什么帮助吗?

您的代码中存在一个微妙的问题,肯定会阻止它工作。在 VAO/VBO 设置代码的末尾,您有:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
glBindVertexArray(0);

GL_ELEMENT_ARRAY_BUFFER 绑定是 VAO 状态的一部分。如果在绑定 VAO 时取消绑定,则此 VAO 将没有元素数组缓冲区绑定。这意味着您稍后绘制时没有索引。

您应该简单地删除此调用:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

此外,由于您有 6 个索引,绘图调用的第二个参数应为 6:

glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, m_rectsDrawnThisFrame);

除此之外,我觉得一切都很合理。但是代码比较多,所以我不能保证我会发现所有问题。

I also noticed that I had to activate the texture in the second slot. Does the Texture Buffer Object use up one?

是的。 buffer texture需要绑定,sampler变量的值设置为对应的texture unit。由于您在设置期间绑定了缓冲区纹理,永远不要解除绑定,并且采样器变量的默认值为 0,您可能在那里没问题。但我认为更明确地设置它会更清晰。你准备画画的地方:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_BUFFER, m_xywhuvTexture);
glUniform1i(m_program->GetUniformLocation("buf"), 0);