glReadPixels与FBO在iOS上崩溃

时间:2013-07-09 02:09:08

标签: opengl-es fbo glreadpixels

在FBO上调用glReadPixels时,我遇到了一些间歇性的崩溃。目前在我的实时应用中每天造成超过1000次崩溃。

我将在代码下方包含读取像素和生成FBO的代码。很高兴在需要时包含其他代码。

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;
    }

崩溃日志看起来像这样(有一些变化):

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    

0 个答案:

没有答案