Summit
Summit

Reputation: 2258

How to draw from a Multisampled buffer to default frame buffer

I am trying to render from a multisampled framebuffer to a default frame buffer.

first i enable multisampling with these commands.

glfwWindowHint(GLFW_SAMPLES, 4);
glEnable(GL_MULTISAMPLE);

Than i create a multisampled buffer

unsigned int fboMsaaId, rboDepthId;
    unsigned int MsaaTexture;
    // Generate texture
    glGenTextures(1, &MsaaTexture);
    glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, 800, 600, false);   
    // create a multisample buffer
    glGenFramebuffers(1, &fboMsaaId);
    glGenRenderbuffers(1, &rboDepthId);
    glBindFramebuffer(GL_FRAMEBUFFER, fboMsaaId);
    glBindRenderbuffer(GL_RENDERBUFFER, rboDepthId);
    glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, 800, 600);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, MsaaTexture, 0);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,rboDepthId); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

In the render loop.

while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);
        // render
        // ------
        glBindFramebuffer(GL_FRAMEBUFFER, fboMsaaId);
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        // draw our  triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        
        
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
        glBindFramebuffer(GL_READ_FRAMEBUFFER, fboMsaaId);
        glDrawBuffer(GL_BACK);
        glBlitFramebuffer(0, 0, 800, 600, 0, 0, 800, 600, GL_COLOR_BUFFER_BIT, GL_LINEAR);
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

If i render into the default framebuffer i am able to see the drawn object but when i blit from multisampled frame buffer to default frame buffer nothing is drawn on the screen.

Upvotes: 1

Views: 334

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474436

What you want to do is make it so that the default framebuffer is not multisampled. That is, you want to blit from a multisampled framebuffer to a non-multisampled framebuffer.

Yes, it is legal to blit between two multisampled framebuffers, but this is only legal if they have the same sample count. And while you did request a 4-sampled default framebuffer, the default framebuffer is not entirely under your control. Implementations get to play fast-and-loose as to how many samples a default framebuffer gets.

So it's best to avoid this possibility altogether and use a non-multisampled default framebuffer.

Upvotes: 3

Related Questions