Is it better glBufferSubData or glMapBuffer
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.
Related videos on Youtube
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, 2022Comments
-
zeb almost 2 years
In order to update my uniform buffer objects I use
glBufferSubData
. Is it faster to useglBufferSubData
orglMapBuffer
withglUnmapBuffer
?-
BDL over 8 yearsThat depends on the use case. You will have to test it for your case.
-
-
BDL over 8 yearsEven with
glMapBuffer
one copies the data to a cpu memory array. The difference is only thatglBufferSubData
uses a user allocated array and withglMapBuffer
the driver allocates it. -
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 over 8 yearsBut 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. WithglBufferSubData
the original memory can directly be copied to gpu. -
mmostajab over 8 years@BDL Can you please provide me a reference for directly copying the data to gpu by
glBufferSubData
??? -
BDL over 8 yearsThat'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 over 8 yearsOK! I can correct myself by saying
glMapBuffer
is more general. You can do whatglBufferSubData
is doing withglMapBuffer
and even more. So, if you can use both, there should not be a difference between them but stillglMapBuffer
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 withglMapBuffer
as you just need to change the z component of the vertices. -
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
andglSubBufferData
should be identical. -
zeb over 8 yearsI'm trying to implement orphaning with unsynchronized buffers and doing some tests. Once I have some news I'll report them
-
zeb over 8 yearsI'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 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?