Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Using buffer mapped with glMapBuffer in a different thread?

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    8

    Using buffer mapped with glMapBuffer in a different thread?

    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!

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,394
    Quote Originally Posted by Desperado View Post
    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:


    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 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!

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    8
    Quote Originally Posted by Dark Photon View Post
    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

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,394
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    8
    Then I'm happy. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •