OpenGL ES 2.0 renderbufferStorage崩溃

时间:2016-02-16 09:34:56

标签: ios opengl-es-2.0

最近我的应用程序崩溃了,因为renderbufferStorage方法返回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];
}

你知道为什么这个方法会返回false,甚至是随机的吗?提前致谢

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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