I want to use multiple framebuffers sequentially. (For example, render scene into first buffer → apply HDR shader → blit first and second buffers → apply FXAA shader → send to renderbuffer).
Here’s what I do in code:
unsigned int hdrFBO;
unsigned int fxaaFBO;
unsigned int RBO;
InitializeBuffers();
loop
{
//Bind first buffer
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
//Clear screen
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Render start
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 624, 480);
DrawScene();
glDisable(GL_DEPTH_TEST);
//Render finish
//Apply post-process shader
hdrShader.use(); // <- 1
//Unbind first buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// blit buffers
glBindFramebuffer(GL_READ_FRAMEBUFFER, hdrFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fxaaFBO);
glBlitFramebuffer(0, 0, 624, 480, 0, 0, 624, 480, GL_COLOR_BUFFER_BIT, GL_NEAREST);
//bind second buffer
glBindFramebuffer(GL_FRAMEBUFFER, fxaaFBO);
//Apply anti-aliasing shader
fxaaShader.use(); // <- 2
//unbind second buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// blit to renderbuffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, fxaaFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, RBO);
glBlitFramebuffer(0, 0, 624, 480, 0, 0, 624, 480, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//draw post-processed screen quad
glBindVertexArray(quadVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, RBO); // use the now resolved color attachment as the quad's texture
glDrawArrays(GL_TRIANGLES, 0, 6);
}
With two framebuffers - only the last post-process shader is applied (2).
When I use only one of these buffers with renderbuffer everything works correctly.
As I understand it, the blit function should send information from the first buffer to the second and after that, all shaders after the function “glBindFramebuffer(GL_READ_FRAMEBUFFER, fxaaFBO);” will be applied to the already post-processed hdr frame from first framebuffer.
The problem is that applying the fxaa shader seems to overwrite the frame while ignoring the hdr frame, resulting frame with anti-aliasing, but without hdr.
If I remove the “fxaaShader.use();” line, the renderbuffer gets the frame with the applied hdr from the first buffer.
I don’t understand where the problem is, can somebody help me?