 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Fri Sep 14, 2007 7:34 am Post subject: OpenGl + SDL not working inside thread? (solved) |
|
|
Hello.
I'm trying to use SDL + OpenGL and do the drawing stuff inside a thread. But the app crashes whenever I call some OpenGL routine inside a thread. However it works fine if it's done in the main func. Like this:
| Code: |
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
int width = 480;
int height = 272;
int main( int argc, char *argv[] )
{
SDL_Init (SDL_INIT_VIDEO);
if (!SDL_SetVideoMode( width, height, 32, SDL_OPENGL )) {
fprintf (stdout, "Failed to create window\n");
SDL_Quit( );
return 0;
}
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 10);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
static int rotation = 0;
float bheight;
bheight = 3.0f;
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Clear buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
// Spin round a bit
glRotatef(rotation++, 0, 0, 1);
// Draw bar
glBegin(GL_POLYGON);
glVertex3f(-0.1, -1, 0);
glVertex3f(0.1, -1, 0);
glVertex3f(0.1, bheight, 0);
glVertex3f(-0.1, bheight, 0);
glEnd();
glFinish();
SDL_GL_SwapBuffers();
SDL_Delay( 5000 );
SDL_Quit( );
return 0;
}
|
But for some reason the app crashes when I try to do the rendering inside a thread like this:
| Code: |
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <GL/gl.h>
#include <GL/glu.h>
SDL_Thread *thread = NULL;
int width = 480;
int height = 272;
int func( void *data )
{
while (1) {
static int rotation = 0;
float bheight;
bheight = 3.0f;
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Clear buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
// Spin round a bit
glRotatef(rotation++, 0, 0, 1);
// Draw bar
glBegin(GL_POLYGON);
glVertex3f(-0.1, -1, 0);
glVertex3f(0.1, -1, 0);
glVertex3f(0.1, bheight, 0);
glVertex3f(-0.1, bheight, 0);
glEnd();
glFinish();
SDL_GL_SwapBuffers();
SDL_Delay(1000 / 70);
}
}
int main( int argc, char *argv[] )
{
SDL_Init (SDL_INIT_VIDEO);
if (!SDL_SetVideoMode( width, height, 32, SDL_OPENGL )) {
fprintf (stdout, "Failed to create window\n");
SDL_Quit( );
return 0;
}
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 10);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
thread = SDL_CreateThread( func, NULL );
SDL_Delay( 5000 );
SDL_Quit( );
return 0;
}
|
I need this inside a thread because I'm working on something like a visualizer interface. Is there anyway to solve this? Hope anyone can help.
Sayounara
Last edited by theHobbit on Sat Sep 15, 2007 2:23 am; edited 1 time in total |
|
| Back to top |
|
 |
saulotmalo2
Joined: 10 Sep 2007 Posts: 43
|
Posted: Fri Sep 14, 2007 7:43 am Post subject: |
|
|
i'm not sure about this but try using this while(1) on the main thread... is weird but it is for ensure that the thread creates well (also i have noticed that you're using SDL_Delay but it is only for try.
| Code: |
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <GL/gl.h>
#include <GL/glu.h>
SDL_Thread *thread = NULL;
int width = 480;
int height = 272;
int func( void *data )
{
while (1) {
static int rotation = 0;
float bheight;
bheight = 3.0f;
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Clear buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
// Spin round a bit
glRotatef(rotation++, 0, 0, 1);
// Draw bar
glBegin(GL_POLYGON);
glVertex3f(-0.1, -1, 0);
glVertex3f(0.1, -1, 0);
glVertex3f(0.1, bheight, 0);
glVertex3f(-0.1, bheight, 0);
glEnd();
glFinish();
SDL_GL_SwapBuffers();
SDL_Delay(1000 / 70);
}
}
int main( int argc, char *argv[] )
{
SDL_Init (SDL_INIT_VIDEO);
if (!SDL_SetVideoMode( width, height, 32, SDL_OPENGL )) {
fprintf (stdout, "Failed to create window\n");
SDL_Quit( );
return 0;
}
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 10);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
thread = SDL_CreateThread( func, NULL );
while(1){ }
SDL_Delay( 5000 );
SDL_Quit( );
return 0;
} |
|
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Fri Sep 14, 2007 8:13 am Post subject: |
|
|
This bit
in your main loop is going to suck up all the CPU time. You need to put a sleep in there.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
saulotmalo2
Joined: 10 Sep 2007 Posts: 43
|
Posted: Fri Sep 14, 2007 9:16 am Post subject: |
|
|
| yes, it slows down a lot the CPU but i tell him to try in order of testing if the problem was that the thread creation cost is to high not for ever. also as i've said before he is already using a SDL_Delay but i'm not sure of this working good. |
|
| Back to top |
|
 |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Fri Sep 14, 2007 9:50 am Post subject: |
|
|
| Thanks tryied it but with the same results. Maybe OpenGL isn't thread safe? |
|
| Back to top |
|
 |
saulotmalo2
Joined: 10 Sep 2007 Posts: 43
|
Posted: Fri Sep 14, 2007 10:00 am Post subject: |
|
|
| another question may be... are you sure the code inside is executing? i mean have you tried to put a cout or printf in order of knowing if it is running well? It sounds weird but when something doesn't work most times is a very simple thing other times can be because big problems ;) |
|
| Back to top |
|
 |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Fri Sep 14, 2007 10:12 am Post subject: |
|
|
Thanks for the fast reply. Yep I put | Code: | | fprintf( stdout, "Inside Thread" ); | just before the | Code: | glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); | routines and yes the stdout.txt file says a lot of 'Inside Thread' strings :) |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Sep 14, 2007 4:13 pm Post subject: |
|
|
| A quick check of the SDL code shows that SDL_CreateThread() doesn't create a thread with the PSP_THREAD_ATTR_VFPU attribute set, so any use of the VFPU will crash the PSP. I do believe PSPGL is rife with VFPU usage. |
|
| Back to top |
|
 |
theHobbit
Joined: 30 Sep 2006 Posts: 65
|
Posted: Sat Sep 15, 2007 2:30 am Post subject: |
|
|
Thanks a lot J.F. I have changed this line in the SDL_systhread.c file:
| Code: | thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
priority, 0x8000, 0, NULL); |
with this:
| Code: | thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
priority, 0x8000, PSP_THREAD_ATTR_VFPU, NULL); |
Rebuilt SDL, and is working now : ). Well hope that doesn't break something else...
Thanks Everyone!! |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Tue Sep 18, 2007 3:03 am Post subject: |
|
|
| Committed in rev. 2315 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|