MultiThreading with OpenGL

Hi, i’d like to have an OpenGL app (Win32) with 2 thread:

  1. The main window app (the one with main OGL context).
  2. The OGL loading textures thread.

How can I realize it?
Is there any samples?

Thnx in adivce, Emanem! :smiley:

hi,

I am in search of a solution for the same issue reported an year ago. :slight_smile:

I am creating a screensaver for windows with ken burn’s effect like in Mac default savers. This requires displaying images with zoom/pan/blend effects. I use 25 fps.

The JPEG images i need to display are over 1MB of size and take abot ~2 seconds to get converted to texture from file. But during this time I need keep the screen getting upated without blocking.
So I created a separate thread which will pre-load next image to be displayed while current image is getting rendered.

But this setup is not working. The main thread which is drawing the frames, renders blank screen. :frowning: But the same works if i load the image on main thread. But the UI stops updating for 2 sec. and then continues. This is not expected behavior. I can’t even preload all the images before in hand since it affects the start-up time for a screensaver.

Please let me know if there is any way of solving this issue.

Thanks
Giri

OpenGL context is not shared between threads so you have to load all GL specific data in the main thread or use multiple context and share data between them ( I have never used the second alternative so I cant help you with it )

But imho when you are loading an image then the most time consuming part is loading from file and/or decompression. Both of these things can be done in other thread. You just have to call glTexImage2D and other functions like this in the main thread )

I tested the time that it takes to load Jpeg from file and texture generation. following is the findings

08-Jun-2005 12:20:17,781 - Load Jpeg start
08-Jun-2005 12:20:17,984 - Load Jpeg end
08-Jun-2005 12:20:17,984 - Build 2d Tex start
08-Jun-2005 12:20:19,140 - Build 2d Tex end

I use ‘gluBuild2DMipmaps’ to load OpenGL texture. It takes about ~1.5 seconds. This is still a concern to me.

Is there any way to speed-up texture conversion?

Thanks
Girish

I tested the time that it takes to load Jpeg from file and texture generation. following is the findings

08-Jun-2005 12:20:17,781 - Load Jpeg start
08-Jun-2005 12:20:17,984 - Load Jpeg end
08-Jun-2005 12:20:17,984 - Build 2d Tex start
08-Jun-2005 12:20:19,140 - Build 2d Tex end

I use ‘gluBuild2DMipmaps’ to load OpenGL texture. It takes about ~1.5 seconds. This is still a concern to me.

Is there any way to speed-up texture conversion?

Thanks
Girish

Originally posted by Girish:

But this setup is not working. The main thread which is drawing the frames, renders blank screen. :frowning: But the same works if i load the image on main thread. But the UI stops updating for 2 sec. and then continues. This is not expected behavior.

Make sure your main message loop is not waiting for messages, but always looping? And perhaps you need to check whether a texture is fully loaded or not before rendering?

Originally posted by Girish:
Is there any way to speed-up texture conversion?
You may have a look at hardware mipmap generation, using the following extenstion : [GL_SGIS_generate_mipmap](http://oss.sgi.com/projects/ogl-sample/ registry/SGIS/generate_mipmap.txt), which has been integrated in core OpenGL since then (1.4?).

I am in search of a solution for the same issue reported an year ago. :slight_smile:

That should be pretty simple.

Create your window,
select and set a pixelformat (once!),
create the main rendering thread,
create your main rendering OpenGL context,
wglMakeCurrent in the main thread,
create your texture loading OpenGL context,
NOW: wglShareList(mainContext, textureLoadingContext)!
Create the texture loading thread,
wglMakeCurrent of the texture loading OpenGL context to the texture loading thread.

Now both OpenGL contexts run on the same window, bound to different threads, BUT texture objects are shared between the contexts.

A texture downloaded inside the texture thread is available to the main thread for rendering.

Make sure you synchronize the two threads correctly, e.g. never use a texture in the main thread which is currently downloaded in the other thread.

Check these two links for more discussions:
http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?ubb=get_topic;f=9;t=000497
http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?ubb=get_topic;f=3;t=013336

Relic, Thanks a lot for your guidence.

I implemented the logic as per your suggestion. The program now runs very well without any issue!

My problem is solved!.

Thank you
Giri

You’re welcome.
I wonder why I didn’t answer this a year ago. :wink:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.