glFramebufferTexture2D使用EAGLContext返回无效状态的其他原因是什么?

时间:2017-05-11 22:05:56

标签: ios swift opengl-es opengl-es-2.0

Khronos的一般OpenGL-ES 2.0文档似乎不完整,至少在iOS实现方面https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glFramebufferTexture2D.xml

我已经确认没有符合GL_INVALID_OPERATION条件,但这是我正在返回的状态。

此操作有时会成功完成,但由于某种原因通常会出错。

在我将帧缓冲区连接到纹理

的函数中失败了
  func fullColorFrameBuffer(_ fbo:GLuint, rbo:GLuint, texture:GLuint, size:CGSize) -> GLuint
{
    print("full color framebuffer fbo:\(fbo) rbo:\(rbo) texture:\(texture) size:\(size)")
    glBindFramebuffer(GLenum(GL_FRAMEBUFFER), fbo)
    glBindRenderbuffer(GLenum(GL_RENDERBUFFER), rbo)

    glRenderbufferStorage(GLenum(GL_RENDERBUFFER), GLenum(GL_RGBA8_OES), GLsizei(size.width), GLsizei(size.height))
    glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), rbo)

    self.assertNoGLError()

    glActiveTexture(GLenum(GL_TEXTURE0))
    glBindTexture(GLenum(GL_TEXTURE_2D), fbo)

    self.assertNoGLError()

    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GL_CLAMP_TO_EDGE);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GL_CLAMP_TO_EDGE);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_LINEAR);

    self.assertNoGLError()

    glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GLint(GL_RGBA), GLsizei(size.width), GLsizei(size.height), 0, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), nil)

    self.assertNoGLError()

    glFramebufferTexture2D(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_TEXTURE_2D), texture, 0)

    self.assertNoGLError()

    let status = glCheckFramebufferStatus(GLenum(GL_FRAMEBUFFER))

    if status != GLenum(GL_FRAMEBUFFER_COMPLETE)
    {
        NSLog("failed to make complete framebuffer object %x", status)
    }

    self.assertNoGLError()

    return status
}

我在这里创建句柄:

 fileprivate func createBuffers()
{
    glEnableVertexAttribArray(GLenum(LCAttributes.vertex.rawValue))
    glVertexAttribPointer(GLenum(LCAttributes.vertex.rawValue), 2, GLenum(GL_FLOAT), 0, GLsizei(2 * MemoryLayout<GLfloat>.size), nil)

    glEnableVertexAttribArray(GLenum(LCAttributes.texCoord.rawValue))
    glVertexAttribPointer(GLenum(LCAttributes.texCoord.rawValue), 2, GLenum(GL_FLOAT), 0, GLsizei(2 * MemoryLayout<GLfloat>.size), nil)

    glGenFramebuffers(1, &framebufferHandle)
    glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebufferHandle)

    glGenRenderbuffers(1, &renderbufferHandle)
    glBindRenderbuffer(GLenum(GL_RENDERBUFFER), renderbufferHandle)

    glGenTextures(1, &targetTexture)


    print("createBuffers fbo:\(framebufferHandle) rbo:\(renderbufferHandle) texture:\(targetTexture)")
}

简单地说;在iOS上从此函数返回GL_INVALID_OPERATION的其他任何已知原因是什么?或者,这个错误的调查点是什么?我已经验证我正在使用正确的线程,并且使用了相同的EAGLContext。我已经验证视图已正确设置。

1 个答案:

答案 0 :(得分:0)

已经有一段时间了,但我在iOS上遇到了类似的麻烦。 IIRC,它与RenderBuffer存储格式有关。试试这个:

    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GLsizei(size.width), GLsizei(size.height));
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);

如果没有这样做,请告诉我,我将发布我的整个RTT创建功能。