【发布时间】:2015-12-16 01:14:35
【问题描述】:
最初使用glDrawElementsInstancedBaseVertex 来绘制场景网格。所有网格顶点属性都在单个缓冲区对象中交错。总共只有 30 个独特的网格。所以我一直用实例计数等调用draw 30 次,但现在我想使用glMultiDrawElementsIndirect 将draw 调用批处理为一个。由于我没有使用此命令功能的经验,因此我一直在到处阅读文章以了解实现,但收效甚微。 (出于测试目的,所有网格都只实例化一次)。
OpenGL 参考页中的命令结构。
struct DrawElementsIndirectCommand
{
GLuint vertexCount;
GLuint instanceCount;
GLuint firstVertex;
GLuint baseVertex;
GLuint baseInstance;
};
DrawElementsIndirectCommand commands[30];
// Populate commands.
for (size_t index { 0 }; index < 30; ++index)
{
const Mesh* mesh{ m_meshes[index] };
commands[index].vertexCount = mesh->elementCount;
commands[index].instanceCount = 1; // Just testing with 1 instance, ATM.
commands[index].firstVertex = mesh->elementOffset();
commands[index].baseVertex = mesh->verticeIndex();
commands[index].baseInstance = 0; // Shouldn't impact testing?
}
// Create and populate the GL_DRAW_INDIRECT_BUFFER buffer... bla bla
然后下线,设置完成后我会画一些图。
// Some prep before drawing like bind VAO, update buffers, etc.
// Draw?
if (RenderMode == MULTIDRAW)
{
// Bind, Draw, Unbind
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_indirectBuffer);
glMultiDrawElementsIndirect (GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 30, 0);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
}
else
{
for (size_t index { 0 }; index < 30; ++index)
{
const Mesh* mesh { m_meshes[index] };
glDrawElementsInstancedBaseVertex(
GL_TRIANGLES,
mesh->elementCount,
GL_UNSIGNED_INT,
reinterpret_cast<GLvoid*>(mesh->elementOffset()),
1,
mesh->verticeIndex());
}
}
现在glDrawElements... 在切换时仍然可以像以前一样正常工作。但是尝试glMultiDraw... 会给出无法区分的网格,但是当我将所有命令的firstVertex 设置为0 时,网格看起来几乎是正确的(至少可以区分),但在某些地方仍然很大程度上是错误的?我觉得我遗漏了有关间接多重绘图的重要内容?
【问题讨论】: