PDA

View Full Version : Using buffer mapped with glMapBuffer in a different thread?



Desperado
02-18-2018, 05:32 AM
Greetings,

can I safely write to buffers mapped with glMapBuffer in a thread different from the one that holds the gl context? I have a program that currently does a lot of large glBufferData calls in the main thread and I want to move the burden caused by the internal memcpy somewhere else.

Regards!

Dark Photon
02-18-2018, 12:47 PM
can I safely write to buffers mapped with glMapBuffer in a thread different from the one that holds the gl context?

Yes, if you either: 1) Make sure the content is only active on one thread at a time, or 2) Use multiple contexts which share objects and are careful about synchronizing buffer updates and use of the resulting data. However, I wouldn't expect a performance miracle from doing either. For more details, see:

See:


Parallel OpenGL FAQ (https://eyescale.github.io/equalizergraphics.com/documentation/parallelOpenGLFAQ.html)
OpenGL and Multithreading (https://www.khronos.org/opengl/wiki/OpenGL_and_multithreading)

Another solution that is sometimes used is to Map/Unmap in the "GL" thread (the only thread that talks to GL), but move the memcpys into the mapped buffer region into a background thread. That offloads the memcpy to another thread, and can avoid a lot of context swapping inefficiency.

Yet another option to consider: using PERSISTENT COHERENT buffer mappings with memcpys on a background thread (see Buffer Object Streaming#Persistent mapped streaming (https://www.khronos.org/opengl/wiki/Buffer_Object_Streaming#Persistent_mapped_streamin g) in the OpenGL wiki) and synchronization to coordinate updates. That has similar advantages to the last approach, but also gets rid of all the Maps/Unmaps too. You can do the updates whenever and wherever you want. Just don't forget to synchronize!

Desperado
02-18-2018, 02:27 PM
Another solution that is sometimes used is to Map/Unmap in the "GL" thread (the only thread that talks to GL), but move the memcpys into the mapped buffer region into a background thread. That offloads the memcpy to another thread, and can avoid a lot of context swapping inefficiency.


Isn't this exactly what I was proposing (mapping in the main thread, writing (=memcpy) in the worker thread? )

And do the maps/unmaps have more performance penalties so I should move them too?

Regards anyways

Dark Photon
02-18-2018, 06:02 PM
Sorry, I read your question differently than you intended.


can I safely (write to buffers mapped with glMapBuffer in a thread) (different from the one that holds the gl context)?

vs.

can I safely (write to buffers in a thread) (different from the one that holds the gl context and calls glMapBuffer)?


The former had me thinking the glMapBuffers and the buffer writes were off in some background thread together.

Desperado
02-19-2018, 03:09 AM
Then I'm happy. Thank you!