Rendering from a different thread

Anyone have any insights with using SDL & OpenGL with the rendering code and
SDL_GL_SwapBuffers looping in a non-main thread? Is this safe on all
platforms? Is this a good idea for a real-time rendering application like a
game? What are the pros/cons compared to drawing only in the main thread?

Thanks,
chaz

chaz

afaik you can only render in the main thread - at least, I’ve tried to no avail.
SDL docs seem to support this.

Ed> ----- Original Message -----

From: onefriedrice@brokenzipper.com (Charles McGarvey)
To: sdl at lists.libsdl.org
Sent: Thursday, 3 January, 2008 6:58:44 AM
Subject: [SDL] rendering from a different thread.

Anyone have any insights with using SDL & OpenGL with the rendering
code and
SDL_GL_SwapBuffers looping in a non-main thread? Is this safe on all
platforms? Is this a good idea for a real-time rendering application
like a
game? What are the pros/cons compared to drawing only in the main
thread?

Thanks,
chaz


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

  ___________________________________________________________

Support the World Aids Awareness campaign this month with Yahoo! For Good http://uk.promotions.yahoo.com/forgood/

Hi Chaz,
I believe this is pretty much a pure OpenGL question; you might get
more complete answers on the opengl.org forums. This is a pretty good
FAQ on the issue:
http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html

The first question in part 2 pretty well sums up how I like to write
OpenGL apps with multithreading: “The preferred way is to dedicate
one thread to dispatch the OpenGL commands, and offload the
CPU-intensive operations to worker threads.”–
Terry Welsh - mogumbo ‘at’ gmail.com
www.reallyslick.com | www.mogumbo.com

Message: 4
Date: Wed, 2 Jan 2008 23:58:44 -0700
From: “Charles McGarvey”
Subject: [SDL] rendering from a different thread.
To:
Message-ID: <7C1CEE3159FC4201908479CB1F9121E5 at windoze>
Content-Type: text/plain; format=flowed; charset=“iso-8859-1”;
reply-type=original

Anyone have any insights with using SDL & OpenGL with the rendering code and
SDL_GL_SwapBuffers looping in a non-main thread? Is this safe on all
platforms? Is this a good idea for a real-time rendering application like a
game? What are the pros/cons compared to drawing only in the main thread?

Thanks,
chaz

That should work on most platforms, but it’s safest to have all the
graphics stuff in the main thread and the other things in their own
threads. You also need to make very sure that you’re only doing
graphics stuff in one thread, because some OpenGL implementations go
absolutely drugged-squirrel psycho if you don’t.
-:sigma.SBOn Jan 2, 2008 11:58 PM, Charles McGarvey wrote:

Anyone have any insights with using SDL & OpenGL with the rendering code and
SDL_GL_SwapBuffers looping in a non-main thread? Is this safe on all
platforms? Is this a good idea for a real-time rendering application like a
game? What are the pros/cons compared to drawing only in the main thread?

afaik you can only render in the main thread - at least, I’ve tried
to no avail.
SDL docs seem to support this.

Ed

I’ve succesfuly rendered using a seperate thread and it worked
just as suspected. The way I did it was to set up a render thread
which rendered the graphics constantly, while a timer signaled the
main thread to process the game logic at a constant frame rate.

Cheers,
Peter

And then you just add a mutex so that the game scene structures aren’t
modified while the scene is being drawn? This seems like a nice way to go.,
decoupling the drawing code and just letting it take off with vsync on.
What platforms have you tried this on?> ----- Original Message -----

From: peterketting@sysmatrix.net (Peter Ketting)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Thursday, January 03, 2008 10:33 PM
Subject: Re: [SDL] rendering from a different thread.

afaik you can only render in the main thread - at least, I’ve tried
to no avail.
SDL docs seem to support this.

Ed

I’ve succesfuly rendered using a seperate thread and it worked
just as suspected. The way I did it was to set up a render thread
which rendered the graphics constantly, while a timer signaled the
main thread to process the game logic at a constant frame rate.

Cheers,
Peter


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

afaik you can only render in the main thread - at least, I’ve tried
to no avail.
SDL docs seem to support this.

Ed

I’ve succesfuly rendered using a seperate thread and it worked
just as suspected. The way I did it was to set up a render thread
which rendered the graphics constantly, while a timer signaled the
main thread to process the game logic at a constant frame rate.

Sorry for my ignorance, but is this at all significantly different
from using the MAIN thread to setup a separate processing thread that
has callbacks to logic every so often, and on the main thread you
constantly render in opengl? I see this question of OpenGL rendering
from separate threads come up a lot and the answer is always "In some
cases (maybe most) you can and it’s fine, but in a few you can’t,"
thus the running suggestion is, "You probably aught not do it."
Taking that, it seems simple that if you want to have a thread for
rendering that is separate from your thread for processing (which
seems perfectly reasonable to me), why not make it the MAIN thread for
rendering, just to be safe, and a second thread for processing?

Is there something terribly special about the MAIN thread that you’d
rather use it for processing than rendering? I mean I’ve written
programs with threads, but I’ve not ever studied the ins and outs of
threaded systems and how they handle thread priorities and what
significance there may or may not be for threads spawned from the main
thread (or OTHER threads, even) versus the main thread itself. So…
is there a big difference?

– Scott

PS: Sorry if it’s an uneducated question.On Jan 4, 2008, at 2:33 PM, Peter Ketting wrote:

is there a big difference?

No. In general it’s better to do your rendering from the main thread,
and logic in other threads. It’s possible to set up rendering in other
threads, but takes more hoops to jump through.

See ya!
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment