关于帧缓冲附件的特定组合的OpenGL:GL_FRAMEBUFFER_UNSUPPORTED

时间:2015-06-20 10:28:42

标签: c++ opengl textures nvidia framebuffer

我试图将多个目标附加到帧缓冲对象。我有以下问题:

使用浮动纹理附件和深度附件时没有错误。 使用浮动纹理附件和整数纹理附件时也没有错误。虽然这些组合起作用,但我不能同时使用float,integer和depth附件。这会导致GL_FRAMEBUFFER_UNSUPPORTED状态。

这是我的代码:

//working:
Framebuffer fb = Framebuffer(
    1280,720,
    {
        //AttachmentInfo(GL_DEPTH_COMPONENT16,GL_DEPTH_ATTACHMENT),
        AttachmentInfo(GL_RG32F,GL_COLOR_ATTACHMENT0),
        AttachmentInfo(GL_RGB16UI,GL_COLOR_ATTACHMENT1),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT2),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT3),
        AttachmentInfo(GL_RGBA16F,GL_COLOR_ATTACHMENT4),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT5),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT6),
    }
);

//working:
Framebuffer fb = Framebuffer(
    1280,720,
    {
        AttachmentInfo(GL_DEPTH_COMPONENT16,GL_DEPTH_ATTACHMENT),
        AttachmentInfo(GL_RG32F,GL_COLOR_ATTACHMENT0),
        //AttachmentInfo(GL_RGB16UI,GL_COLOR_ATTACHMENT1),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT2),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT3),
        AttachmentInfo(GL_RGBA16F,GL_COLOR_ATTACHMENT4),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT5),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT6),
    }
);

//NOT working:
Framebuffer fb = Framebuffer(
    1280,720,
    {
        AttachmentInfo(GL_DEPTH_COMPONENT16,GL_DEPTH_ATTACHMENT),
        AttachmentInfo(GL_RG32F,GL_COLOR_ATTACHMENT0),
        AttachmentInfo(GL_RGB16UI,GL_COLOR_ATTACHMENT1),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT2),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT3),
        AttachmentInfo(GL_RGBA16F,GL_COLOR_ATTACHMENT4),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT5),
        AttachmentInfo(GL_RGB32F,GL_COLOR_ATTACHMENT6),
    }
);

注意:我在使用NVidia GTX 970显卡时才会出现此错误。在较旧的ATI卡上没有发生错误。

我在每次glbinding函数调用后使用glGetError来调用gl*,所以我知道没有发生其他错误。

这是Framebuffer对象的构造函数代码:

Framebuffer::Framebuffer(uint width,uint height,initializer_list<AttachmentInfo> attachments)
:width(width),height(height)
{
    glGenFramebuffers(1,&fbID);
    glBindFramebuffer(GL_FRAMEBUFFER,fbID);

    if(std::none_of(attachments.begin(),attachments.end(),[](const AttachmentInfo& a){
        return a.attachment == GL_DEPTH_ATTACHMENT;
    })){
        //depth test needs either a depth renderbuffer or depth attachment.
        unsigned int rboID=0;
        glGenRenderbuffers(1, &rboID);
        glBindRenderbuffer(GL_RENDERBUFFER, rboID);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,rboID);
    }

    vector<GLenum> buffers;
    glActiveTexture(GL_TEXTURE0);

    for(AttachmentInfo attachment:attachments){
        GLuint tID;
        glGenTextures(1, &tID);
        glBindTexture(GL_TEXTURE_2D,tID);

        GLint param;
        glGetInternalformativ(GL_TEXTURE_2D, attachment.internalFormat, GL_FRAMEBUFFER_RENDERABLE, 1, &param);

        if(param != GL_FULL_SUPPORT){
            _log(WARNING,"Internal format " << attachment.internalFormat << " support is " << GLenum(param) << ".");
        }

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

        if(attachment.attachment == GL_DEPTH_ATTACHMENT){
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GLint(GL_COMPARE_REF_TO_TEXTURE));
              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GLint(GL_LESS));
        }
        glTexStorage2D(GL_TEXTURE_2D,1,attachment.internalFormat,width,height);
        glFramebufferTexture(GL_FRAMEBUFFER, attachment.attachment, tID, 0);

        if(Tools::startsWith(glbinding::Meta::getString(attachment.attachment),"GL_COLOR_ATTACHMENT")){
            buffers.push_back(attachment.attachment);
        }
        texMap[attachment.attachment]=new Texture(tID,GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,0);
    }
    if(buffers.empty()){
        buffers.push_back(GL_NONE);
    }
    glDrawBuffers(buffers.size(),buffers.data());

    GLenum error=glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if(error != GL_FRAMEBUFFER_COMPLETE){
        _log(ERROR,"Framebuffer error: " << error);
    }
    unbind();
}

我真的没有想法,我能做些什么才能让它发挥作用......

2 个答案:

答案 0 :(得分:4)

  

我真的没有想法,我能做些什么才能让它发挥作用......

您的OpenGL实现告诉您,您选择的配置不受支持。你必须接受这一点。 OpenGL规范不要求所有实现都支持特定的格式组合,因此如果您想要编程为可移植且健壮,如果您收到GL_FRAMEBUFFER_UNSUPPORTED错误,则必须逐步降低格式要求,直到找到了一个可行的组合。

对支持的内容的一个非常好的提示是枚举支持创建OpenGL上下文的所有FBConfigs;通常这是允许的FBO配置的子集。

答案 1 :(得分:2)

以防其他人像我一样遇到这个问题。支持的格式的确切组合因 OpenGL 实现而异,但任何实现都必须支持一组格式。如果你坚持这些,你就不必担心便携性。 OpenGL wiki Framebuffer CompletenessRequired Image Formats 中描述了这些要求。