How to reallocate renderbuffers?

I’ve got my program to the point where I can allocate and write to a framebuffer with color and depth/stencil attachments. Now I’m trying to figure out how to destroy and reallocate the renderbuffers (say as a response to a rezise event).

At the start of every paintGL() call, I make a call to setupFramebuffer() to make sure my framebuffer is setup correctly. I’ve recently added the flag _rebuildFramebuffers which when set indicates that the current framebuffers should be disposed of and new ones created to match the viewport size. However, after I do this I can no longer draw to my framebuffer (even though glCheckFramebufferStatus is still returning GL_FRAMEBUFFER_COMPLETE).

How would I fix this?

void DocumentPanel::setupFramebuffer()
{
    QOpenGLFunctions_3_3_Core *f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

    int w = width();
    int h = height();

    if (_rebuildFramebuffers)
    {
        if (_renderBufferColorID != 0)
        {
            f->glDeleteRenderbuffers(1, &_renderBufferColorID);
            _renderBufferColorID = 0;
        }

        if (_renderBufferDepthStencilID != 0)
        {
            f->glDeleteRenderbuffers(1, &_renderBufferDepthStencilID);
            _renderBufferDepthStencilID = 0;
        }
        _rebuildFramebuffers = false;
    }

    if (_frameBufferID == 0)
    {
        f->glGenFramebuffers(1, &_frameBufferID);
        f->glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
    }

    if (_renderBufferColorID == 0)
    {
        f->glGenRenderbuffers(1, &_renderBufferColorID);
        f->glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferColorID);

        f->glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, w, h);
        f->glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBufferColorID);
    }

    if (_renderBufferDepthStencilID == 0)
    {
        f->glGenRenderbuffers(1, &_renderBufferDepthStencilID);
        f->glBindRenderbuffer(GL_RENDERBUFFER, _renderBufferDepthStencilID);
        f->glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
        f->glBindRenderbuffer(GL_RENDERBUFFER, 0);

        f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBufferDepthStencilID);
    }

    GLenum status = f->glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
    {
        //error
        QString err;

        switch (status)
        {
        case GL_FRAMEBUFFER_UNDEFINED:
            err = "GL_FRAMEBUFFER_UNDEFINED";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
            err = "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
            err = "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
            err = "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
            err = "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER ";
        break;
        case GL_FRAMEBUFFER_UNSUPPORTED :
            err = "GL_FRAMEBUFFER_UNSUPPORTED ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE :
            err = "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE ";
        break;
        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  :
            err = "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS  ";
        break;
        }

        f->glBindFramebuffer(GL_FRAMEBUFFER, 0);

        qDebug() << "Error building frambuffer: " << err;
        return;
    }

    //Rendering to window again
    f->glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

This statement is contradictory. Either you have a variable that says it’s a renderbuffer but it actually holds an FBO, or you are telling OpenGL to delete a renderbuffer by calling the function that deletes framebuffers.

Either way, your code is wrong.

Looks like that was one of my problems. I also needed to recreate the entire FBO. I couldn’t just reuse the old one with new attachments.

        if (_frameBufferID != 0)
        {
            f->glDeleteFramebuffers(1, &_frameBufferID);
            _frameBufferID = 0;
        }

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.