Hello Guest

Using Stack vs Direct Allocation for Uniforms

  • 0 Replies
  • 3621 Views
Using Stack vs Direct Allocation for Uniforms
« on: September 30, 2020, 18:24:51 »
I would like to know what is accepted practice for allocating uniform buffers. These uniforms are specifically going to be updated frequently.

Method #1: "Direct Allocation"
Code: [Select]
FloatBuffer directBuffer = BufferUtils.createFloatBuffer(16);

// Render Loop
while (condition) {
    myMatrix.putInto(directBuffer);
    glUniformMatrix4fv(uniformLocation, false, directBuffer);
}

Method #2: "Stack Allocation"
Code: [Select]
// Render Loop
while (condition) {
    try (MemoryStack stack = pushStack()) {
        FloatBuffer stackBuffer = stack.mallocFloat(16);
        myMatrix.putInto(stackBuffer);
        glUniformMatrix4fv(uniformLocation, false, stackBuffer);
    }
}

The potentially naïve side of me would like to believe that Method #1 would be most ideal. Because the buffer can be recycled each render call and utilized throughout the program's lifetime.