View Full Version : Multithreaded OpenGL
teenytim
02-08-2001, 07:13 PM
Hi all!
I'm programming an app that uses a separate thread when loading maps, models, and textures, so I can display a 'Loading' graphic with a progress bar. The problem is, after everything is loaded, none of the textures show up. I tried coding it wihtout multi-threading, and it worked perfectly, but I really want my progress bar! Does anybody know why OpenGL texture loading doesn't work in a separate thread? I've tried everything, including checking for created rc, pixel formats, and all sorts of other crap. This is really starting to get annoying...
Tim
zangel
02-08-2001, 10:39 PM
You must call all gl-Functions in the same rendering context.Try loading textures in separate thread and uploading in the rendering thread.
teenytim
02-09-2001, 10:04 AM
The rendering context is the same! I'm just calling the gl-functions from a different thread. Does it really matter?
Tim
mcraighead
02-09-2001, 10:15 AM
Yes, it does matter. You can only use a context with one thread at a time. If you want to use two OpenGL threads, you need to create two contexts and bind them -- in different threads -- to the same window, and then watch out for synchronization hazards.
- Matt
teenytim
02-09-2001, 01:19 PM
Thanks Matt, I'll try it right away. Is this done so the video card can sync everything?
Tim
mcraighead
02-09-2001, 02:13 PM
No, it's because OpenGL drivers have no built-in synchronization of GL commands. Taking a critical section every time you called a GL entry point would be extremely expensive.
It is also rather uninteresting to have two threads talk to one GC, seeing as OpenGL is a state-based API.
- Matt
teenytim
02-09-2001, 02:44 PM
Ah, I understand.
Well, I tried it, and it didn't work. Right now I'm doing this:
WinMain()
{
MainRC=wglCreateContext(...);
LoadingRC=wglCreateContext(...);
wglMakeCurrent(MainRC);
ThreadHandle=CreateThread(0,0,&LoadThread,0,CREATE _SUSPENDED,&tid);
//code taken out for brevity
ResumeThread(LoadThread);
}
DWORD WINAPI LoadThread(LPVOID arg1)
{
HDC thedc;
thedc=GetDC(WindowHWND);
if(LoadingRC==NULL)
return 0;
wglMakeCurrent(thedc,LoadingRC);
TestLand.LoadMap("Data\\maps\\TestMap.txt");
AllDataLoaded=TRUE;
LoadThreadHandle=NULL;
return 0;
}
I delete both contexts in WM_DESTROY. Is this right?
Tim
[This message has been edited by teenytim (edited 02-09-2001).]
[This message has been edited by teenytim (edited 02-09-2001).]
mcraighead
02-09-2001, 03:37 PM
I'd suggest using the exact same DC and making sure that your app has a CS_OWNDC window.
Beyond that, I wouldn't know. I write drivers, not apps. http://www.opengl.org/discussion_boards/ubb/smile.gif
- Matt
teenytim
02-09-2001, 04:20 PM
I got it working! I also found a texture loading bug that cause multiple texture uploads to the same texture.
Anyways, thanks Matt and zangel for answering.
Tim
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.