glReadPixels使用多重采样返回null

时间:2018-07-11 07:41:38

标签: ios opengl-es screenshot

我对this one有同样的问题,但是根据这些提示,我无法从glReadPixels获取数据。

我粘贴了源代码,我的代码与上一个代码几乎相同。我在快照之前设置了GL_READ_FRAMEBUFFER_APPLE,但数据返回了空值。

创建我的帧缓冲区

- (void)createFrameBuffer {
glGenRenderbuffers(1, &colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];

GLint backingWidth;
GLint backingHeight;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

glGenFramebuffers(1, &defaultFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_RENDERBUFFER, colorRenderBuffer);

glGenFramebuffers(1, &sampleFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, sampleFramebuffer);

glGenRenderbuffers(1, &sampleColorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, sampleColorRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, backingWidth, backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, sampleColorRenderbuffer);

glGenRenderbuffers(1, &sampleDepthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, sampleDepthRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, sampleDepthRenderbuffer);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
    NSAssert(true, @"buffer is not complete");
    NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}

渲染功能

- (void)render {
FC_PERFORMANCE_START();
if ([EAGLContext currentContext] !=_context) {
    [EAGLContext setCurrentContext:_context];
}
[self destoryFrameBuffer];
[self createFrameBuffer];
[self.director setup:self.frame.size contentScale:self.contentScaleFactor backgroudColor:self.backgroundColor];
[self.director mainloop];
glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, sampleFramebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, defaultFrameBuffer);
glResolveMultisampleFramebufferAPPLE();
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
const GLenum discards[]  = {GL_COLOR_ATTACHMENT0,GL_DEPTH_ATTACHMENT};
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE,2,discards);
glBindFramebuffer(GL_FRAMEBUFFER, sampleFramebuffer);
[_context presentRenderbuffer:GL_RENDERBUFFER];
FC_PERFORMANCE_END("FCViewOpenGL Render");

}

屏幕截图功能

- (nullable UIImage*)snapShot{
__block UIImage *ret = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
    glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, defaultFrameBuffer);

    GLint backingWidth;
    GLint backingHeight;
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

    NSInteger x = 0, y = 0, width2 = backingWidth, height2 = backingHeight;
    NSInteger dataLength = width2 * height2 * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    CHECK_GL_ERROR_DEBUG();
    glReadPixels((GLint)x, (GLint)y, (GLsizei)width2, (GLsizei)height2, GL_RGBA, GL_UNSIGNED_BYTE, data);
    GLenum attachments[] = {GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT   };
    glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
    CHECK_GL_ERROR_DEBUG();

    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(width2, height2, 8, 32, width2 * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                    ref, NULL, true, kCGRenderingIntentDefault);

    NSInteger widthInPoints, heightInPoints;
    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        CGFloat scale = self.contentScaleFactor;
        widthInPoints = width2 / scale;
        heightInPoints = height2 / scale;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
    }
    else {
        widthInPoints = width2;
        heightInPoints = height2;
        UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));
    }

    CGContextRef cgcontext = UIGraphicsGetCurrentContext();

    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    free(data);
    CFRelease(ref);
    CFRelease(colorspace);
    CGImageRelease(iref);
    ret = image;
});
return ret;
}

任何人都可以给我一些帮助吗?

1 个答案:

答案 0 :(得分:1)

请参见OpenGL ES 3.2 Specification; 16.1.2 ReadPixels; page 406

  

如果INVALID_OPERATION(请参见第9节)的值非零,读取的帧缓冲区是完成的帧缓冲区,并且读帧缓冲区的READ_FRAMEBUFFER_BINDING的有效值为1

     

....

     

如果读取的帧缓冲区是多采样的(SAMPLE_BUFFERS的有效值为1) ...


另请参见OpenGL-Refpages; OpenGL ES 3.0; glReadPixels

  如果SAMPLE_BUFFERS不为零,则读取帧缓冲区已完成,并且读取帧缓冲区的GL_INVALID_OPERATION的值大于零,则生成

GL_READ_FRAMEBUFFER_BINDING


这意味着您不能使用GL_SAMPLE_BUFFERS多采样帧缓冲区。您将获得glReadPixels操作错误。在GL_INVALID_OPERATION之后使用glGetError获取错误信息。


要解决此问题,您必须克服传统的帧缓冲区。请执行以下步骤:

  • 创建第二个帧缓冲区对象,该对象是常规对象(不是多采样)。
  • 绑定多样本帧缓冲区以进行读取(glReadPixels)。
  • 绑定用于绘制(GL_READ_FRAMEBUFFER_APPLE)的常规帧缓冲区。
  • 使用glBlitFramebuffer复制将像素从多采样(读取)帧缓冲区复制到常规(绘制)帧缓冲区。
  • 复制后,绑定常规帧缓冲区以进行读取(GL_DRAW_FRAMEBUFFER_APPLE)。
  • 最后使用GL_READ_FRAMEBUFFER_APPLE来读取传统的帧缓冲区。


当然,您也可以直接从默认的帧缓冲区读取像素。