PDA

View Full Version : 0n multiple glut/opengl windows in multiple threads



kaas
02-25-2002, 01:18 AM
I am trying to run multiple opengl windows in a multi threaded program. Some of the windows use glut, some don't. Glut runs in one thread, the rest run in their own thread. With just glut windows running there is no problem. But as soon as a (glx) window is opened, both windows turn to black.
Any ideas as to why this is and how to avoid it?

lobstah
02-25-2002, 02:12 AM
Hi,

I'm also having a simliar problem; black screen when multi-threading. I create the DC and RC in the thread upon first entry into the rendering function. After the DC and RC have been created and for subsequent calls to this function, I get the DC and RC from wglGet functions, make a call to wglMakeCurrent to ensure I get the correct DC and RC for the thread, invalidate the rect, render the scene, swap buffers, and validate the rect.. Result: I get the black screen. I've checked the return values from wgl functions and no error. I've also checked for OpenGL errors have not encountered any. But I'm know I'm missing something.

[This message has been edited by lobstah (edited 02-25-2002).]

kaas
02-25-2002, 02:45 AM
Same exact scenario here......

lobstah
02-25-2002, 03:12 AM
Yeah, bummer.

I've read the help file for wgl and from what I read, as long the RC is made current for the current thread and this is the only thread rendering to the RC, then rendering (OpenGL calls) should be okay.

But, of course there is something else that should be done.

The handle to the window for the DC (and RC) are created in another thread, but that shouldn't make a difference.

Well, let's see if an OpenGL guru can shed some light.

[This message has been edited by lobstah (edited 02-25-2002).]

lobstah
02-25-2002, 03:42 AM
Found this (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/ntopnglo_9zjn.asp)
S T A R T

Multithread OpenGL Drawing Strategies

The GDI does not support multiple threads. You must use a distinct device context and a distinct rendering context for each thread. This tends to limit the performance advantages of using multiple threads with single-processor systems running OpenGL applications. However, there are ways to use threads with a single processor system to greatly increase performance. For example, you can use a separate thread to pass OpenGL rendering calls to dedicated 3-D hardware.

Symmetric multiprocessing (SMP) systems can greatly benefit from using multiple threads. An obvious strategy is to use a separate thread for each processor to handle OpenGL rendering in separate windows. For example, in a flight-simulation application you could use separate processors and threads to render the front, back, and side views.

A thread can have only one current, active rendering context. When you use multiple threads and multiple rendering contexts, you must be careful to synchronize their use. For example, use one thread only to call SwapBuffers after all threads finish drawing.

E N D

The last sentence could be a clue as to a solution for the black screen of nothing.

lobstah
02-25-2002, 09:25 AM
Can anyone shed any light on the "black screen of nothing"?

kaas
02-25-2002, 12:39 PM
I had come across the same type of statements. No multithreading in GL. Except that glut DOES support it. But I need off screen rendering as well. Something glut does not support. ****.
Something for opengl 2 definitely. While there at it, someone please allow any depth/resolution for off-screen rendering in opengl 2 as well. It's absurd that when drawing to arbitrary memory I cannot draw to anything but the standard hardware accelerated resolutions.

Well, off to the glut software to see how they did it...... unless someone has a better idea, or even just an explanation as to why the black screen?

knackered
02-25-2002, 12:46 PM
I've never had this problem being described. I use multiple contexts in multiple windows in a multi-threaded app all the time, and it's always worked for me. Using Nvidia chipsets and Wildcats.

lobstah
02-25-2002, 01:10 PM
Knackered,

What are the order of OGL instructions?
Here's the order of what I'm doing:

T1 is for Thread 1 (Window thread)
T2 is for Thread 2 (Rendering thread)

T1: GetDC for window
T1: SetPixelFormat
T1: CreateEvent (unsignaled state)
T1: _beginThread
T1: InvalidateRect
T1: SetEvent

T2: If first time in, wglCreateContext (DC from T1) and make current
T2: WaitForSingleObject (in rendering func.)
T2: wglGetCurrentDC
T2: wglGetCurrentContext
T2: wglMakeCurrent
T2: OpenGL....
T2: glflush
T2: SwapBuffers
T2: ValidateRect

I've checked all the return values from the function and all was reported well.

I'm a bit puzzled.

wimmer
02-25-2002, 08:50 PM
Modifying GLUT to use pbuffers is not too hard, I did this for Win32...

Michael

kaas
02-25-2002, 11:50 PM
Originally posted by knackered:
I've never had this problem being described. I use multiple contexts in multiple windows in a multi-threaded app all the time, and it's always worked for me. Using Nvidia chipsets and Wildcats.

Do you use glut or just gl?

husakm
05-30-2002, 12:55 AM
I can only tell what works O.K. :
You must avoid cross/thread OpenGl calls or
you must do following:
Have a array of correct RC and HDC values for each window.
After finishing rendering to the window from one thread call
glMakeCurrent(NULL,NULL) to make the initialized rendering
contex available to other threads.
Befor rendering ractivate the rendering contect
by wglMakeCurrent(hdc, hglrc) with hdc/hgrlc related
to the window you want to render ... Mutlitrhead rednering to
multiple window shuld work in this way ....