Results 1 to 3 of 3

Thread: Sharing opengl context with worker thread

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sharing opengl context with worker thread

    Hello,
    I am using QGLWidget to display an opengl scene in the main thread.
    I would like to load the texture from a worker QThread.
    My problem is that I cannot find a way to share the opengl context between the threads.

    I tried to:
    - create a QGLWidget in the worker thread with shareWidget= main thread QGLWidget. Crash
    - create a QGLContext with create(glWidget->context()); . No crash but opengl return errors and textures are not updated.

    Any advice?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sharing opengl context with worker thread

    You should at all times avoid sharing objects between threads, unless you absolutely must share one.
    It's a nightmare to maintain code that does this, let alone to get it right in the first place.

    Instead, create the texture in the thread and notify the main thread that the texture is finished. Then, from within your main thread, get the texture from the worker thread and use the context from within the main thread.

  3. #3
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing opengl context with worker thread

    Quote Originally Posted by tbscope View Post
    You should at all times avoid sharing objects between threads, unless you absolutely must share one.
    It's a nightmare to maintain code that does this, let alone to get it right in the first place.
    Thanks for your answer. It does indeed look like something difficult to achieve properly.

    Quote Originally Posted by tbscope View Post
    Instead, create the texture in the thread and notify the main thread that the texture is finished. Then, from within your main thread, get the texture from the worker thread and use the context from within the main thread.
    This is the method I'm using at the moment but I'm not satisfied with the speed:
    - The worker thread reads one texture from file and uses a QSemaphore release to notify the main thread.
    - In the main thread QGLWiget paintGL routine (maybe it is not the best place) I check for the semaphore and get the texture in the opengl context.

    The problem is that the number of textures read is limited by the frames per second and the sync between thread seems to even make things worse since I get only around 10 textures/s (whereas the worker thread is able to produce around 150 textures/s).

    One way would be to read textures and accumulate them in a buffer.
    But this will make the code more complex.

    I was hoping to be able to share opengl context to just load the textures directly from the worker thread.

Similar Threads

  1. Worker thread
    By doggrant in forum Newbie
    Replies: 4
    Last Post: 3rd November 2009, 15:49
  2. Worker thread problem
    By hkvm in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2009, 20:12
  3. Showing progress bar while worker thread works
    By olidem in forum Qt Programming
    Replies: 6
    Last Post: 27th April 2009, 20:43
  4. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.