3
$\begingroup$

In order to optimize I'm trying to set up a setup which ping pong between same framebuffer two color attachments to apply some postprocess effects. It seems to work (effects are applied) but concurently I'm getting a lot of strange artifacts like:

  • blinking squares
  • borders on depth discontinuities
  • color spilling on screen (window resize removes it)

Which change or disappear partly when I for example remove one step (one post process effect) from "ping ponging".

If I do this ordinary way using different framebuffers seems to remove them but I'm not 100% sure if it is not a mess in the shaders or something but they seem fine.

In each frame I'm doing ping pong like that:

First step - render to current color attach using other fb texture

fbDeferredLighting->bindFramebuffer(); //fb which has two color attachments
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
effect1->setConstPerFrameUniforms();
//get 0 attachment from fb scene prepass to apply effect and draw on fbDeferredLighting current color attach
effect1->setTextureUniforms(fbScene, 0); 
quad->draw();

And then repeating some number of times: swap step and drawing, using same fb swapped attachment:

fbDeferredLighting->bindFramebuffer();
//after swap - now current=1, other=0 or current=0, other=1
std::swap(colorAttachment0Current, colorAttachment0Other);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texturesDiffuse[colorAttachment0Current]->getTextureID(), 0);
glClear(GL_DEPTH_BUFFER_BIT); //only clear depth
effectX->setConstPerFrameUniforms();
//get other color attach and use it to apply effect on current
effectX->setTextureUniforms(fbDeferredLighting , colorAttachment0Other); 
quad->draw();

Is this correct?

EDIT: I wonder, maybe rewrite it to compute shaders, that would simplify things.

$\endgroup$
1
$\begingroup$

Actually, GL_COLOR_BUFFER_BIT also need to be cleared

After creation of the framebuffer, draw buffer need to be set to:

glDrawBuffer(GL_COLOR_ATTACHMENT0);

and then after each swap:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

instead of only

glClear(GL_DEPTH_BUFFER_BIT);

To clear current attachment 0. I don't really understand these clearing bits extensively but this seems to fix blinking "chess board" artifact.

Borders seems to be connected with motion blur shader.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.