I am trying to subclass CAEAGLLayer with a gl context. That is, instead of creating a subclass of UIView, which returns a CAEAGLLayer and binds the gl context to this layer from a subclass of UIView, I directly subclass this layer and try to adjust the context in the init layer, for example:
- (id)init { self = [super init]; if (self) { self.opaque = YES; _glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; NSAssert([EAGLContext setCurrentContext:_glContext], @""); glGenRenderbuffers(1, &_colorRenderBuffer); glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer); [_glContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:self]; glGenFramebuffers(1, &_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
up to this point everything seems beautiful. However, I then try to create a shader program using the pass-thru vertex / fragment shader pair and , while the link to the program returns no errors, the check fails, saying: "The current framebuff frame is invalid."
The code that links and checks the shader program (after attaching the shaders) looks like this: just in case:
- (BOOL)linkAndValidateProgram { GLint status; glLinkProgram(_shaderProgram); #ifdef DEBUG GLint infoLogLength; GLchar *infoLog = NULL; glGetProgramiv(_shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength); if (infoLogLength > 0) { infoLog = (GLchar *)malloc(infoLogLength); glGetProgramInfoLog(_shaderProgram, infoLogLength, &infoLogLength, infoLog); NSLog(@"Program link log:\n%s", infoLog); free(infoLog); } #endif glGetProgramiv(_shaderProgram, GL_LINK_STATUS, &status); if (!status) { return NO; } glValidateProgram(_shaderProgram); #ifdef DEBUG glGetProgramiv(_shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength); if (infoLogLength > 0) { infoLog = (GLchar *)malloc(infoLogLength); glGetProgramInfoLog(_shaderProgram, infoLogLength, &infoLogLength, infoLog); NSLog(@"Program validation log:\n%s", infoLog); free(infoLog); } #endif glGetProgramiv(_shaderProgram, GL_VALIDATE_STATUS, &status); if (!status) { return NO; } glUseProgram(_shaderProgram); return YES; }
I am wondering if there might be some additional tweaking at some point in the CAEAGLLayer life cycle that I might not be aware of and might skip trying to set GL to init?