Threaded Rendering Loop on iOS?

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
Post Reply
visor24
Gnoblar
Posts: 16
Joined: Mon Mar 12, 2012 2:21 pm

Threaded Rendering Loop on iOS?

Post by visor24 »

I'm working on multi-threaded version of our game on iOS.
The idea is to dedicate one thread to renderer, and the other to everything else.

Since it seems multitouch event only goes to main thread, I decided to create a user thread for renderer, and let main thread handle input/game/etc.
And the thing is, I can't display a thing in manually created thread.
Only when I call renderOneFrame() using NSTimer or CADisplayLink (which is of course in main thread), then everything is rendered fine.

If I use traditional rendering loop like this in separate thread:

while ( !exit )
{
renderer->renderOneFrame();
}

Then I just see black blank screen with no obvious error message.

I have noticed that kind render loop doesn't work on iOS when I first tried to port our game, but hoped it might be a little different in separate thread, which turns out to be not true.
This is a little bit weird because when I manually create a EAGL context and rendering code (not using ogre), then it seems just fine wherever I put render call in.

So here is my question.
Is there any way to put ogre::renderOneFrame() in the traditional rendering loop, not using NSTimer or CADisplayLink, so that I can place that loop in separate thread?
And if not, is there way to work around this problem otherwise?

Thanks.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Threaded Rendering Loop on iOS?

Post by masterfalcon »

Unfortunately, no. Rendering has to be done from the main thread. Other operations can be done on a second thread, loading assets for example, but that hasn't been thoroughly tested at this point.
visor24
Gnoblar
Posts: 16
Joined: Mon Mar 12, 2012 2:21 pm

Re: Threaded Rendering Loop on iOS?

Post by visor24 »

Yeh.. I just also found out I messed up the experimental code when I tried to create EAGLContext manually and draw something on it.
Rendering call was in user created thread but context creation code was in main thread.
When I move that code into created thread then I see same silent blank screen again.
I could create shared context in user thread and draw something on it, but apparently functions like glGenFrameBufferOES() cannot be called there.

Apparently iOS never allows to create valid context in non-main thread then I wonder why I couldn't find out this earlier.
At least my latest google search couldn't find any reliable sources states such limitation.

Anyway, since I still want to implement multi-threading on iOS (and Android in near future), I have some new questions.
1. Is there a way to receive Multitouch event in user created thread? I'm asking this because if that's possible, then I can move game loop into that thread and let main thread handle rendering without much hassle.
2. Does Android also have such limitation - i.e. can create context only in main thread?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Threaded Rendering Loop on iOS?

Post by masterfalcon »

1. If I recall UI events need to be handled on the main thread.
2. Good question, I don't know.
Post Reply