OpenGL glMultiDrawElementsIndirect with interleaved buffers

Originally using a glDrawElementsInstancedBaseVertexscene grid to draw. All attributes of the grid vertices alternate in one buffer object. Only 30 unique grids. So I called the triplex 30 times with the number of instances, etc., but now I want to trigger the drawing calls to one using glMultiDrawElementsIndirect. Since I have no experience with this function, I read articles here and there to understand the implementation with little success. (For testing purposes, all cells are set only once).

Command structure from the OpenGL man page.

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

Then later along the line, after setting, I draw a picture.

// 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 , ( ), ? , - ?

+4
1
//Indirect data
commands[index].firstVertex     = mesh->elementOffset();

//Direct draw call
reinterpret_cast<GLvoid*>(mesh->elementOffset()),

, . firstVertex ; . , firstVertex:

commands[index].firstVertex     = mesh->elementOffset() / sizeof(GLuint);

. , , , , . ;)

+4

Source: https://habr.com/ru/post/1620231/


All Articles