Is it better glBufferSubData or glMapBuffer

11,806

The good thing about glMapBuffer is that you dont need to copy the data first in an array and then use glBufferSubData to fill the opengl buffer. With glMapBuffer, you can copy the data directly to part of memory which opengl will fetch to GPU when it is necessary. From my point of view, there glMapBuffer should be faster when you want to fill a big buffer which is going to be updated frequently. Also, how you are copying the data into the buffer between glMapBuffer and glUnmapBuffer is also important.

If show us the code which you are using the glMapBuffer and how big is the data, then we can judge easier. Anyway, in the end measurements can show you which one is better.

UPDATE: OpenGL Insight Asynchronous Buffer Transfer Chapter. In this chapter, the implicit synchronization of glMapBuffer and glSubBufferData functions may be interesting for you.

Share:
11,806

Related videos on Youtube

zeb
Author by

zeb

I write code for work, hobby and fun mostly. I work in a software house developing our software which is used in the furniture inustry with out configurator and CAD. For my own fun I'm developing a graphic engine that I want to expand (at some point) in order to also use it in order to make some game. I have fairly well knowledge of C/C++ (since I work every day with those), C#, WPF, JavaScript, PHP, SQL and some Cocoa/Objective-C.

Updated on June 18, 2022

Comments

  • zeb
    zeb almost 2 years

    In order to update my uniform buffer objects I use glBufferSubData. Is it faster to use glBufferSubData or glMapBuffer with glUnmapBuffer?

    • BDL
      BDL over 8 years
      That depends on the use case. You will have to test it for your case.
  • BDL
    BDL over 8 years
    Even with glMapBuffer one copies the data to a cpu memory array. The difference is only that glBufferSubData uses a user allocated array and with glMapBuffer the driver allocates it.
  • mmostajab
    mmostajab over 8 years
    @BDL That's what I meant exactly. "you can copy the data directly to part of memory which opengl will fetch to GPU when it is necessary". by memory I meant system memory and not GPU memory. OpenGL always creates the buffers on system memory and fetch data on GPU when it is necessary.
  • BDL
    BDL over 8 years
    But the conclusion is wrong: When copying a large memory segment to gpu glMapBuffer requires you to copy the data from it's original location to the mapped cpu memory (which can already be slow). Then the mapped memory is copied to gpu. With glBufferSubData the original memory can directly be copied to gpu.
  • mmostajab
    mmostajab over 8 years
    @BDL Can you please provide me a reference for directly copying the data to gpu by glBufferSubData???
  • BDL
    BDL over 8 years
    That's free to choose by the driver. In the end, it's not the point to discuss internal mechanisms, but to point out, that from my point of view there is no fact that would support that "glMapBuffer should be faster when you want to fill a big buffer which is going to be updated frequently"
  • mmostajab
    mmostajab over 8 years
    OK! I can correct myself by saying glMapBuffer is more general. You can do what glBufferSubData is doing with glMapBuffer and even more. So, if you can use both, there should not be a difference between them but still glMapBuffer can do much more. So, if you just need to change the z values of your vertex coordinates, you should be able to do that faster with glMapBuffer as you just need to change the z component of the vertices.
  • mmostajab
    mmostajab over 8 years
    @BDL Also, when I said it is faster, I meant it is faster because you do not need to create a buffer in user side and fill it with the data which you want to copy in the buffer. You can directly copy the data into the part of memory which opengl will transfer to gpu, directly. If this chunk of data which is going to be copied is already in an array, and we dont need to create any additional array in user side, then glMapData and glSubBufferData should be identical.
  • zeb
    zeb over 8 years
    I'm trying to implement orphaning with unsynchronized buffers and doing some tests. Once I have some news I'll report them
  • zeb
    zeb over 8 years
    I've implemented orphaning with unsynchronized buffers and the time I spend updating my buffers now is really low. The problem now is that even with VSync deactivated my SwapBuffers function is slowing down everything. I mean, to draw my entire scene I spend 1.8ms and SwapBuffers takes 11672.8ms. Can't figure out why.
  • Zebrafish
    Zebrafish about 6 years
    @mmostajab Like you say in your comment, "if you just need to change the z values of your vertex coordinates, you should be able to do that faster with glMapBuffer as you just need to change the z component of the vertices." As the mapped memory is system memory, not GPU memory, that the GPU will copy after, how does the GPU know which parts of the memory block to copy and which not. For example you map the buffer and get a pointer, and you go along x...y... z(change)...x...y...z(change), this memory is a block on the system RAM, it needs to be copied to the GPU, so again, how does it know?