How do I grow a buffer OpenGL?

后端 未结 1 607
别跟我提以往
别跟我提以往 2021-01-22 07:03

Is it possible to grow a buffer in OpenGL?

Let\'s say I want to use instanced rendering. Everytime a spawn a new object in the world I would have to update the buffer wi

相关标签:
1条回答
  • 2021-01-22 07:23

    You definitely do not call glBufferData (...) with a new size to do this. That is going to orphan the old buffer and give you a new buffer with larger storage (whose contents are unrelated to the previous allocated memory).

    The old data will continue to exist for as long as any prior OpenGL commands that need it are still queued up in the pipeline. After those commands finish, the memory is allowed to be reclaimed by OpenGL - you do not have to worry about leaking, but in-place growth does not happen automatically.

    GL 3.1 introduced the concept of copy buffers, which will help you do this a lot more efficiently.

    Consider the following pseudo-code:

    // Bind the old buffer to `GL_COPY_READ_BUFFER`
    glBindBuffer (GL_COPY_READ_BUFFER, old_buffer);
    
    // Allocate data for a new buffer
    glGenBuffers (1, &new_buffer);
    glBindBuffer (GL_COPY_WRITE_BUFFER, new_buffer);
    glBufferData (GL_COPY_WRITE_BUFFER, ...);
    
    // Copy `old_buffer_size`-bytes of data from `GL_COPY_READ_BUFFER`
    //   to `GL_COPY_WRITE_BUFFER` beginning at 0.
    glCopyBufferSubData (GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, old_buffer_size);
    

    In real-world situations, you can copy buffer data from any binding location to any other binding location. The new GL_COPY_..._BUFFER binding points are convenient, but not necessary, because they don't interfere with any other bindings.

    0 讨论(0)
提交回复
热议问题