View Full Version : glMapBuffer/glUnMapBuffer multiple buffers clarification

08-05-2014, 01:37 PM
Hi, I was doing some debugging with openGL mapped buffers (specifically in lwjgl), and I ran into something that suggests I may have the wrong idea about buffer mappings. The documentation doesn't say much about it, and I looked for an ARB spec txt (since those always seem to do a better job of fleshing out the details), but haven't found one, so I'm hoping someone can clarify this for me.
How I thought they worked:
- calling glMapBuffer for a target that has another buffer already mapped to it would effectively unmap the first one and map the new one
- calling glUnMapBuffer just unmaps whatever buffer is currently mapped to that target (generates error if there are none)

What appears to be the case now (from my debugging):
- calling glMapBuffer for a target that has another buffer already mapped to it will result in both buffers being mapped to that target
- calling glUnMapBuffer for a target that has a buffer bound to it will unmap that buffer if it is currently mapped (error otherwise), and leave any other mappings to the same target unchanged
- calling glUnMapBuffer for a target that has no buffer bound to it will unmap all buffers that are bound to that target (not so sure about this one)

The new situation means it's possible to map multiple buffers to the same target, which I would find particularly useful because I often need to have multiple different buffers mapped simultaneously, and I'm kindof running out of targets to use for the mappings... but I need to be sure that this is actually what is supposed to happen before I can safely use it. I haven't found any documentation that makes it clear.

Thanks for any help.

08-05-2014, 02:15 PM
The original GL_ARB_vertex_buffer_object extension is what you were looking for: https://www.opengl.org/registry/specs/ARB/vertex_buffer_object.txt

The extract relevant to your question is:

Is separate protocol needed for MapBuffer/UnmapBuffer?

RESOLVED: Yes. Buffers can only be mapped once. Buffers need not be bound for the duration of their mapping...

So therefore it's possible to do:
glBindBuffer (GL_ARRAY_BUFFER, b0);
void *data0 = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY);
glBindBuffer (GL_ARRAY_BUFFER, b1);
void *data1 = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY);
glBindBuffer (GL_ARRAY_BUFFER, 0);

// do interesting and/or exciting stuff with data0 and data1

glBindBuffer (GL_ARRAY_BUFFER, b0);
glUnmapBuffer (GL_ARRAY_BUFFER);
glBindBuffer (GL_ARRAY_BUFFER, b1);
glUnmapBuffer (GL_ARRAY_BUFFER);
glBindBuffer (GL_ARRAY_BUFFER, 0);
I have of course omitted error-checking for clarity.

The documentation at https://www.opengl.org/sdk/docs/man2/xhtml/glMapBuffer.xml specifies the conditions when a buffer object becomes automatically unmapped:

A buffer object's mapped data store is automatically unmapped when the buffer object is deleted or its data store is recreated with glBufferData.

It seems reasonable to assume that this also occurs when the GL context is destroyed.