3
votes

I am using OpenGL to render chunks of blocks in a Voxel game. Each chunk has its own vertex buffer, and the existing render function (simplified) is as follows:

foreach (Chunk c in chunks)
{
    glBindBuffer(BufferID.Array, c.handle);
    if (c.dirty)
    {
        glBufferData(BufferID.Array, ... , BufferUsage.StaticDraw);
        c.dirty = false;
    }

    // For position, normal, colour and UV
    glEnableVertexAttribArray(...);
    glVertexAttribPointer(...);

    glUseProgram(shader.Handle);
    glDrawArrays(DrawMode.TriangleStrip, 0, c.vertexBuffer.Length);
}

The code above works, however there is high CPU usage on glEnableVertexAttribArray, glVertexAttribPointer and glUseProgram.

Can I call these functions before the loop starts, or must it be called after every glBindBuffer?

1
Do the operations behind those functions need to be different for each "chunk"? If not, then why are they in the loop in the first place? And if so, then the question is moot. These are not random things that you assemble in an arbitrary order; each of these functions do something. So if you know what these functions do, you have the answer to your question.Nicol Bolas
The map is constantly changing and is split into chunks so that I don't have to recalculate the entire map every time a block is added/removed/changes colour. The operations behind these functions (glEnableVertexAttribArray, glVertexAttribPointer and glUseProgram) are the same for every chunk.user5932360

1 Answers

2
votes

Before there were Vertex Array Objects (VAOs) you had indeed to redo the bindings whenever you wanted to change the memory layout and/or pointer. Then Vertex Array Objects got introduced, and for a time people where sad because with the drivers back then performance still left a lot to be desired (as of writing this, this was about 10 years ago – time flies). Ever since then drivers improved a lot.

Essentially a VAOs stores which buffers are bound to what attribute, and the memory layout of those bindings. https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object