glResolveMultisampleFramebufferAPPLE() 在 iOS 上生成 GL_INVALID_OPERATION (OpenGL ES 2.0)

glResolveMultisampleFramebufferAPPLE() generate GL_INVALID_OPERATION on iOS (OpenGL ES 2.0)

我想使用函数 glReadPixels() 来截取我的场景。如果我不使用多重采样,它会很好用。但是如果我这样做,我会在 glResolveMultisampleFramebufferAPPLE() 中得到 GL_INVALID_OPERATION。有办法解决这个问题吗?

我的保存功能:

    var wid = GLint()
    var hei = GLint()
    glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_WIDTH), &wid)
    glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_HEIGHT), &hei)
    let byteLength = Int(hei * wid) * 4
    let bytes = UnsafeMutablePointer<GLubyte>.alloc(byteLength)

    // init non-multisampled frame buffer
    var framebuffer: GLuint = 0
    var colorRenderbuffer: GLuint = 0

    glGenFramebuffersOES(1, &framebuffer)
    glBindFramebufferOES(GLenum(GL_FRAMEBUFFER_OES), framebuffer)

    glGenRenderbuffersOES(1, &colorRenderbuffer)
    glBindRenderbufferOES(GLenum(GL_RENDERBUFFER_OES), colorRenderbuffer)
    glRenderbufferStorageOES(GLenum(GL_RENDERBUFFER_OES), GLenum(GL_RGBA8_OES), wid, hei)
    glFramebufferRenderbufferOES(GLenum(GL_FRAMEBUFFER_OES), GLenum(GL_COLOR_ATTACHMENT0_OES), GLenum(GL_RENDERBUFFER_OES), colorRenderbuffer)

    glBindFramebufferOES(GLenum(GL_DRAW_FRAMEBUFFER_APPLE), framebuffer)

    var default: GLint = 0
    glGetIntegerv(GLenum(GL_FRAMEBUFFER_BINDING_OES), &default)
    glBindFramebufferOES(GLenum(GL_READ_FRAMEBUFFER_APPLE), GLuint(default));

    myglGetError() // OK
    glResolveMultisampleFramebufferAPPLE()
    myglGetError() // GL_INVALID_OPERATION

    glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)

    glReadPixels(0, 0, GLsizei(wid), GLsizei(hei), GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), bytes)

    glBindFramebuffer(GLenum(GL_FRAMEBUFFER), GLuint(default));
    glDeleteFramebuffers(1, &framebuffer)

我使用由 GLKitglkView.drawableMultisample = GLKViewDrawableMultisample.Multisample4X

初始化的默认帧缓冲区

我试过你的样本,似乎经过一些修改后它可以工作。 修改代码:

        var wid = GLint()
    var hei = GLint()
    glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_WIDTH), &wid)
    glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_HEIGHT), &hei)
    var def: GLint = 0
    glGetIntegerv(GLenum(GL_FRAMEBUFFER_BINDING_OES), &def)

    // init non-multisampled frame buffer
    var framebuffer: GLuint = 0
    var colorRenderbuffer: GLuint = 0

    glGenFramebuffersOES(1, &framebuffer)
    glBindFramebufferOES(GLenum(GL_FRAMEBUFFER_OES), framebuffer)

    glGenRenderbuffersOES(1, &colorRenderbuffer)
    glBindRenderbufferOES(GLenum(GL_RENDERBUFFER_OES), colorRenderbuffer)
    glRenderbufferStorageOES(GLenum(GL_RENDERBUFFER_OES), GLenum(GL_RGBA8_OES), wid, hei)
    glFramebufferRenderbufferOES(GLenum(GL_FRAMEBUFFER_OES), GLenum(GL_COLOR_ATTACHMENT0_OES), GLenum(GL_RENDERBUFFER_OES), colorRenderbuffer)

    glBindFramebufferOES(GLenum(GL_DRAW_FRAMEBUFFER_APPLE), framebuffer)

    //commented
    //here GL_FRAMEBUFFER_BINDING_OES will be overrided by previous call of
    // 'glBindRenderbufferOES(GLenum(GL_RENDERBUFFER_OES), colorRenderbuffer)'
    //var def: GLint = 0
    //glGetIntegerv(GLenum(GL_FRAMEBUFFER_BINDING_OES), &def
    glBindFramebufferOES(GLenum(GL_READ_FRAMEBUFFER_APPLE), GLuint(def));

    var err = glGetError()
    print(String(format: "Error %X",  err))
    glResolveMultisampleFramebufferAPPLE()
    err = glGetError()
    print(String(format: "Error %X",  err)) // GL_INVALID_OPERATION

    glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)

据我所知,这里还引用了 APPLE_framebuffer_multisample.txt 扩展说明,解释了为什么修改后的代码有效。

Calling BindFramebuffer with set to FRAMEBUFFER binds the framebuffer to both DRAW_FRAMEBUFFER_APPLE and READ_FRAMEBUFFER_APPLE.

APPLE_framebuffer_multisample