OpenGL ES 2.0 renderbufferStorage 崩溃

OpenGL ES 2.0 renderbufferStorage crash

最近我的应用程序似乎崩溃了,因为 renderbufferStorage 方法 returns false。我不知道会发生什么,所以我之前添加了测试,但它们从不引发异常。我在主线程上,当前上下文似乎很好,eaglLayer 也是,但无论如何它在 renderbufferStorage 崩溃..

EAGLContext* defaultEAGLContext = getDefaultContext();
EAGLContext* currentContext = [EAGLContext currentContext];

if(![NSThread isMainThread]) {
    NSLog(@"ERROR : renderbufferStorage not called on main thread");
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"renderbufferStorage not called on main thread"
                                 userInfo:nil];
}

if(!currentContext) {
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"Current context is null"
                                 userInfo:nil];
}

if(!defaultEAGLContext) {
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"Default context is null"
                                 userInfo:nil];
}

if(defaultEAGLContext != currentContext) {
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"Default context is different than current context"
                                 userInfo:nil];
}

if(!_eaglLayer) {
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"EAGL layer error"
                                 userInfo:nil];
}

if (![defaultEAGLContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer]) {
    NSLog(@"ERROR : Failed to obtain renderbuffer storage from layer");
    @throw [NSException exceptionWithName:@"ERROR"
                                   reason:@"Failed to obtain renderbuffer storage from layer"
                                 userInfo:nil];
}

你知道为什么这个方法会 return 错误,即使是随机的吗?提前致谢

您应该写下崩溃的内容,但最常见的错误是忘记覆盖目标视图的 layerClass 方法。

您在渲染缓冲区存储中使用其层的视图必须具有以下覆盖:

+ (Class)layerClass {
    return [CAEAGLLayer class];
}

实际上我的init方法被调用了两次,因为一些线程没有被同步。因此 "renderbufferStorage" 被调用了 2 次,第二次返回 false,因为存储已经从层中获取。