I'm getting some intermittent crashing when calling glReadPixels on an FBO. Currently causing more than 1000 crashes per day in my live app.
I'll include below the code for reading the pixels and generating the FBO. Happy to include other code used if needed.
typedef struct
{
GLuint frameBufferID;
GLuint textureID;
int textureNum;
} FBO;
+ (void) readPixelsFromFBO: (FBO) fbo withWidth: (int) width andHeight: (int) height intoData: (NSMutableData*) data
{
glActiveTexture( GL_TEXTURE0 + fbo.textureNum );
glBindTexture( GL_TEXTURE_2D, fbo.textureID );
glBindFramebuffer( GL_FRAMEBUFFER, fbo.frameBufferID );
glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data.mutableBytes );
GLenum error = glGetError();
if ( error != 0 )
NSLog( @"readPixelsFromFBO ERROR: %d", error );
}
+ (FBO) createFBOWithWidth: (int) width andHeight: (int) height andTextureNum: (int) textureNum
{
FBO fbo;
fbo.textureNum = textureNum;
// Generate the IDs
glGenFramebuffers( 1, &fbo.frameBufferID );
glGenTextures( 1, &fbo.textureID );
// Create the texture
glActiveTexture( GL_TEXTURE0 + textureNum );
glBindTexture( GL_TEXTURE_2D, fbo.textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
GLubyte* bytes = (GLubyte*) calloc( width * height * 4, sizeof( GLubyte ) );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes );
free( bytes );
// Create the frame buffer
glBindFramebuffer( GL_FRAMEBUFFER, fbo.frameBufferID );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.frameBufferID, 0 );
// FBO status check
GLenum status = glCheckFramebufferStatus( GL_FRAMEBUFFER );
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE:
NSLog(@"FBO created");
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
NSLog(@"FBO unsupported");
break;
default:
/* programming error; will fail on all hardware */
NSLog( @"Framebuffer Error: make sure that the context was created!" );
break;
}
return fbo;
}
The crash logs look like this (with some variation):
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x1
Crashed Thread: 0
Thread 0 Crashed:
0 libGPUSupportMercury.dylib 0x36d0ae22 <redacted> + 10
1 IMGSGX543GLDriver 0x324bff31 <redacted> + 245
2 IMGSGX543GLDriver 0x324c0011 <redacted> + 37
3 IMGSGX543GLDriver 0x324c3a81 <redacted> + 405
4 GLEngine 0x348712f9 <redacted> + 1257
5 OpenGLES 0x34915da3 _glReadPixels + 55