Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Updating dynamic vertex buffer (frequently)

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    12

    Question Updating dynamic vertex buffer (frequently)

    Hello there.
    I'm trying to replicate this strategy in OpenGL, but have no clear idea how.

    Code :
      //transfer transformed vertices to GPU (before drawing)
        VertexPositionColorTexture *pData = NULL; //destination to copy vertices into
     
        if (SUCCEEDED(m_VB->Lock( 0, 0, ( void** )&pData, m_vertexBufferPosition == 0 ? D3DLOCK_DISCARD : D3DLOCK_NOOVERWRITE )))
        {
          //move pointer to unused section of vertex buffer
          pData += m_vertexBufferPosition * VERTICES_PER_SPRITE;
     
          for (int i = 0; i < batchSize; i++)
          {
            sprites[i]->FillVertices(pData, i * VERTICES_PER_SPRITE);
          }
     
          m_VB->Unlock();
        }

    So far I've found glMapBuffer and glMapBufferRange (similar to lock operation in directx), but I'm not sure if I understand them right.
    When I lock the buffer in DX with flag D3DLOCK_DISCARD I'm sure that if the data in a vertex buffer is currently being used - I will get a new chunk of memory (while GPU keeps using old memory for drawing behind the scenes), will the same happen with glMapBuffer? or should I do it myself? and if so how I should do it? Talking about D3DLOCK_NOOVERWRITE I see it as just mapping the buffer in the range it is not being used, so here glMapBufferRange might work perfectly for the situation.

    What do you think?
    How would you go about it?

    Thanks for any help.

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    There's some info on this in the wiki.
    glBufferData with NULL as the last argument before mapping will work on all hardware that supports buffer objects, but the other ways are probably neater if you are targeting more recent hardware only.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    12
    Yeah thanks, thats what I was looking for.
    Here's another document by Nvidia (in case someone will have the same question): http://coitweb.uncc.edu/~krs/courses...Using-VBOS.pdf

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2002
    Location
    Bratislava, Slovakia
    Posts
    19
    If you are updating the buffer frequently the most important parameter is the usage, in your case you should use GL_STREAM_DRAW. Alternative to D3DLOCK_DISCARD is GL_MAP_INVALIDATE_BUFFER_BIT but you can use it only in glMapBufferRange. Better way is to use multiple buffers with the flag GL_MAP_UNSYNCHRONIZED_BIT, usually four are enough because the driver GPU queue is usually 2-3 frames behind (use fences for syncing). This solution doesn't involve driver's memory management and will save some CPU time. You can use one big buffer too, with your own address management but don't forget the unsynchronized flag.

    You can find more in free chapter from OpenGL Insights
    http://www.seas.upenn.edu/~pcozzi/Op...rTransfers.pdf
    Last edited by l_hrabcak; 11-22-2012 at 04:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •