multithreading GL crashed SDL window

Started by
6 comments, last by Daniel Lee 18 years, 1 month ago
hi one and all, please advise as to why and how to do it properly or is it possible at all by setting up openGL and drawing routines in some threads in the linux environment. my scenario is like this:- in my main cpp--- module:-------------------------------------> SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS); pThread1 = SDL_CreateThread(threadEntryPoint, NULL); /*== the Game Loop ==*/ do{ keyBoard(); while(iRes != -1); ///----------------------------------------------------------. in the thread module, it called the following:- int tVp1setup(void){ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glViewport( 0, pScreen->h*3/16, pScreen->w*5/18, pScreen->h*13/16); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( 25.0f, /// 20.0f, GLfloat(pScreen->w*5/18) / GLfloat(pScreen->h*13/16), 1.0f, 5000.0f); gluLookAt(0.0, 0.0, 120.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); ///--> SDL_GL_SwapBuffers(); }///end of tVp1Setup(). But only when the above is called, it crashed the SDL window. please advise or provide sample of multithreading openGL in SDL in Linux. Thank you. mrdaniel@singnet.com.sg
Advertisement
Does your main thread also touch the opengl state? If it does, try and synchronize GL Calls.
OpenGL contexts are managed per thread. That means, in practice, that you should make all GL calls in the thread that created the context.
(Well, OK, that's not entirely true, you could use glXMakeCurrent to transfer ownership of the context, at cost of performance(?) )
Quote:Original post by udaykv
Does your main thread also touch the opengl state? If it does, try and synchronize GL Calls.


And this synchronization would be achieved by waiting for glFinish() to return before switching the thread that renders.

However, I strongly urge you to reconsider your design. Usually things like this end up with far worse performance than a single rendering thread -design. I don't know what your design is like... are you, for example, trying to parallelize something like scene graph/spatial hierarchy traversal? I'd do that with a thread-safe render queue implementation. This render queue would be sent to the API for rendering from a single thread, with appropriate state-sorting. The state-sorting could be implemented with a few threads but it's not likely to be a bottleneck.
Olli Salli
Why are you creating a thread in the first place?
You also need this code to setup an OpenGL context in the SDL window.

        pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);	glViewport(0, 0, Width, Height);	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);		glClearDepth(1.0);								glMatrixMode(GL_PROJECTION);	glLoadIdentity();    	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();


You are calling glClear before you created a context, likely why it is crashing.
---John Josef, Technical DirectorGlass Hat Software Inc
my current design has the screen divided into 4 viewports. Hence there is 4 threads. Each thread will take care of one viewport. But (if you refer back to my posting) i only use 1 thread to test that idea and even one single use of glClear(,,), crashed the SDL window.
I mentioned SDL window as the main module of the project is created using :-
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) != 0){
pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
}

/// creates threads etc....
/// game loop

i hope that updates your request for my experiment.

please advise further.

thanks.


Quote:Original post by Daniel Lee
hi one and all,

please advise as to why and how to do it properly or is it possible at all by setting up
openGL and drawing routines in some threads in the linux environment.

my scenario is like this:-

in my main cpp--- module:------------------------------------->

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
pThread1 = SDL_CreateThread(threadEntryPoint, NULL);
/*== the Game Loop ==*/
do{
keyBoard();
while(iRes != -1);

///----------------------------------------------------------.

in the thread module, it called the following:-


int tVp1setup(void){

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);

glViewport( 0,
pScreen->h*3/16,
pScreen->w*5/18,
pScreen->h*13/16);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 25.0f, /// 20.0f,
GLfloat(pScreen->w*5/18) / GLfloat(pScreen->h*13/16),
1.0f,
5000.0f);
gluLookAt(0.0, 0.0, 120.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );

///--> SDL_GL_SwapBuffers();
}///end of tVp1Setup().

But only when the above is called, it crashed the SDL window.

please advise or provide sample of multithreading openGL in SDL in Linux.

Thank you.
mrdaniel@singnet.com.sg


Try this:

int tVp1setup(void){	glViewport(	0, 			pScreen->h*3/16, 			pScreen->w*5/18, 			pScreen->h*13/16);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		glEnable(GL_DEPTH_TEST);						glMatrixMode(GL_PROJECTION);			glLoadIdentity();			gluPerspective(	25.0f, /// 20.0f,                         GLfloat(pScreen->w*5/18) / GLfloat(pScreen->h*13/16),                         1.0f,                         5000.0f);	gluLookAt(0.0, 0.0, 120.0,                   0.0, 0.0, 0.0,                   0.0, 1.0, 0.0 );					///-->  SDL_GL_SwapBuffers();}///end of tVp1Setup().


---John Josef, Technical DirectorGlass Hat Software Inc
wrt my earlier post, your lot of code has no chance to try, as even with a single line of code like glClearColor(0, 0, 0, 0), that crashed the SDL window.

thanks.

This topic is closed to new replies.

Advertisement