Yashwanth CB Yashwanth CB - 1 year ago 114
Java Question

Advantage of using glMapBuffer

When I use

I get a float (byte) buffer as return type, which I can use to modify the data on the server side.

But is there any performance advantages of doing so?

I have an example

Approach 1:
I create a float buffer with vertex data and pass it to

Approach 2:
I allocate space using
and I pass no data...
I get the reference to a float buffer...
I write the float values to it to... and I unmap the buffer.

What are the pros and cons of two approaches

Am I doing the same on both?

I think that second approach avoids duplicates of buffers.

Answer Source

There are two very related aspects to this:

  1. Reducing memory usage.
  2. Avoiding unnecessary copying of data, which can hurt performance.

Calling glBufferData() with data being passed in includes the following:

  1. You allocate buffer memory to store your data.
  2. You store your data in this buffer you allocated.
  3. When you call glBufferData(), the OpenGL implementation allocates memory for the data.
  4. The OpenGL implementation copies the data from your buffer into its own allocation.

Compare this with what happens when you do the same thing with buffer mapping:

  1. When you call glBufferData(), the OpenGL implementation allocates memory for the data.
  2. When you call glMapBuffer(), the OpenGL implementation returns a pointer to its memory.
  3. You store your data in this memory.
  4. You unmap the buffer.

If you compare the two sequences, you have an extra memory allocation in the first one, which means that it requires about twice the memory in total. And the OpenGL implementation has to copy the buffer data in the first one, which is not the case in the second.

In reality, things can get a bit more complicated. Particularly on systems that have dedicated graphics memory (VRAM), there might be more copies of the data. But the principle remains, you reduce extra memory allocations and copying.

Another aspect to keep in mind is what happens beyond the initial use of the buffer, if you want to modify the content of the buffer after it was already used. Again, glMapBuffer() will generally reduce the amount of extra data copying, but it might come at the price of undesired synchronization. So it could be more efficient to pay the price for an extra copy needed for glBufferData() or glBufferSubData() to avoid synchronization points.

If you have these more complex cases where you frequently modify buffer data, you really need to start benchmarking, and you have to expect differences between vendors. You can also look into schemes where you use buffer mapping, but use a pool of buffers you cycle through instead of a single buffer, to reduce/avoid the performance penalty from synchronization.

On top of this, if you work on devices where power/thermal considerations come into play, you may want to measure power usage in addition to just execution speed. Because the fastest solution might not necessarily be the most power efficient.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download