I receive INVALID OPERATION when I use glBlitFramebuffer to implement MSAA

821 views Asked by At

Perhaps I have some stupid problems. I'll be appreciate if someone could reply them. All the problems are based on Android environment and OpenGL ES.

  1. How to verified whether I has opened the MSAA or not ? If I draw some GL_POINTS with points size 50, there are some small squares. If I enabled 4x MSAA, can the small squares become round points ?

  2. I tried my best to enable MSAA with FBO and BlitFBO. But it draw nothing and there is an error INVALID_OPERATION after glBlitFramebuffer() calling.

Here is the complete projects I mentioed above:https://github.com/Enoch-Liu/GL

And the following is the key codes:

void Renderer::MultisampleAntiAliasing() {
    glGenRenderbuffers(1, &m_MSColor);
    glBindRenderbuffer(GL_RENDERBUFFER, m_MSColor);
    glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, m_width, m_height);
    checkGLError("GenMSColorBuffer");

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


    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_MSColor);
    checkGLError("FboRbo,COLORATTACHMENT");

    glGenRenderbuffers(1, &m_MSDepth);
    glBindRenderbuffer(GL_RENDERBUFFER, m_MSDepth);
    glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, m_width, m_height);
    checkGLError("GenDepthBuffer");

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_MSDepth);
    checkGLError("DepthBuffer,Renderbuffer");
    GLenum drawBufs[] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1, drawBufs);
    checkGLError("DrawBuffer");

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        LOG_ERROR("failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
    }
}


void Renderer::drawFrame() {
    //LOG_INFO("drawFrame %d x %d", width, height);
    static float r=0.9f;
    static float g=0.2f;
    static float b=0.2f;

    LOG_INFO("xxx %d, %d", m_width,m_height);
    if (OPENMSAA)
    {
        glBindFramebuffer(GL_FRAMEBUFFER, m_MSFBO);
        glBindRenderbuffer(GL_RENDERBUFFER, m_MSColor);
        checkGLError("BindTwoBuffers");
    }

    glViewport(0,0,m_width,m_height);
    glScissor(0,0,m_width,m_height);

    glClearColor(r, g, b, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);

    const GLfloat landscapeOrientationMatrix[16] = {
            1.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 1.0f };
    const GLfloat color[4] = {
            1.0f, 0.0f, 0.0f, 1.0f
    };
    glUseProgram( m_program );
    glUniformMatrix4fv(m_uMvp, 1, GL_FALSE, landscapeOrientationMatrix);
    glUniform4fv(m_uColor, 1, color);
    m_p = glGetAttribLocation(m_program, "vPosition");
    m_p1 = glGetAttribLocation(m_program, "vPosition1");

    glEnableVertexAttribArray( m_p );
    glVertexAttribPointer( m_p , 3, GL_FLOAT, false, 3 * sizeof( float ), squareCoords);

    glDrawArrays(GL_POINTS, 0, 4);
    glDisableVertexAttribArray( m_p );
    glFlush();
    checkGLError("Before Blit");
    if (OPENMSAA)
    {
        glBindFramebuffer(GL_READ_FRAMEBUFFER, m_MSFBO);
        checkGLError("BindReadBuffer");
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
        checkGLError("BindFramebuffer");
        glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
        checkGLError("BlitFramebufferColor");
        glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
        checkGLError("BlitFramebufferDepth");
        glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    }
}

The framebuffer is complete.

1

There are 1 answers

1
Andreas On BEST ANSWER

The internal format of the depth buffers have to match: https://www.opengl.org/discussion_boards/showthread.php/173275-Alternative-to-glBlitFramebuffer%28%29

Looking at your github project you are not configuring a depth buffer at all. From your project:

const EGLint attribs[] = {

//        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,

        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,

        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,

        EGL_BLUE_SIZE, 8,

        EGL_GREEN_SIZE, 8,

        EGL_RED_SIZE, 8,

        EGL_ALPHA_SIZE, 8,

        EGL_SAMPLE_BUFFERS, 1,

        EGL_SAMPLES, 4,

        EGL_NONE

};