views:

69

answers:

1

Hello,

I've got a lovely OpenGLES code slice that renders up images for me. When I want to, I can call a function on it:

-(UIImage *)renderToImage;

That does a lot of rendering work and returns me an image. This includes the generation of FBOs, textures, etc.

Lately, I've found myself needing to enhance this. The image generation takes four seconds, so I want to pass off the work to another thread and let the app continue. This seemed simple enough. I made a method with this code:

-(void) generateRandomNewImage:(MyViewController *)evc{
UIImage * renderedImage = [self renderToImage];
NSString * fileLoc = [self writeToTempFile:renderedImage];
NSLog(@"File location:%@",fileLoc);
[evc performSelectorOnMainThread:@selector(imageGenerationComplete:) withObject:fileLoc waitUntilDone:NO];
}

Hopefully you can see the logic going on here. This method renders the image, saves it to the filesystem, and calls a method on the main thread's viewcontroller to let it know the file is ready. This code is inside my opengl renderer. It's called here, in the main thread's viewcontroller:

thread = [[NSThread alloc] initWithTarget:renderer 
                           selector:@selector(generateRandomNewImage:) 
                           object:self];
[thread start];

To me, that seems fine too. When I run this code, I get told in my console that my framebuffer object status were error'ed, with a status of zero. I have no idea why. As a result, I get a blank image (saving to the temp files work, by the way, I've tested them).

To test, I put all of this code into the main thread, didn't create any new threads or anything. It all worked fine. As soon as I try and pass off the image generation to another thread, I hit problems.

+2  A: 

Using OpenGL in another thread is not that simple as that, only one thread can use a OpenGL context at a time, and your second thread doesn't have a OpenGL context, thus all OpenGL calls fail.

Solution: Create another OpenGL context for the second thread, and read this.

Matias Valdenegro
Read also this: http://stackoverflow.com/questions/1253145/how-to-use-opengl-es-on-a-separate-thread-on-iphone if you're considering sharing state between the contexts.
quixoto
Awesome. This is precisely what I needed/
mtc06