iOS上的OpenGL。可以在单独的线程上调用glTexImage2D吗?

时间:2011-03-01 23:54:13

标签: ios nsoperation nsoperationqueue glteximage2d

在iOS上,我总是假设可以通过NSOperation子类在单独的线程上创建OpenGL纹理 - glTexImage2D。有人可以确认/否认。

由于纹理创建可能会挂起GUI - 糟糕! - 有没有人想出一个他们满意的解决方法?

谢谢,
道格

1 个答案:

答案 0 :(得分:2)

是的,请查看cocos2d for iPhone中的CCTextureCache.m。

cocos2d-iphone / cocos2d / CCTextureCache.m

NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];

// textures will be created on the main OpenGL context
// it seems that in SDK 2.2.x there can't be 2 threads creating textures at the same time
// the lock is used for this purpose: issue #472
[contextLock_ lock];
if( auxGLcontext == nil ) {
    auxGLcontext = [[EAGLContext alloc]
                           initWithAPI:kEAGLRenderingAPIOpenGLES1
                           sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]];

    if( ! auxGLcontext )
        CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");
}

if( [EAGLContext setCurrentContext:auxGLcontext] ) {

    // load / create the texture
    CCTexture2D *tex = [self addImage:async.data];

    /* This method calls glTexImage2D. */


    // The callback will be executed on the main thread
    [async.target performSelectorOnMainThread:async.selector withObject:tex waitUntilDone:NO];        

    [EAGLContext setCurrentContext:nil];
} else {
    CCLOG(@"cocos2d: TetureCache: EAGLContext error");
}
[contextLock_ unlock];

[autoreleasepool release];