ping pong between same framebuffer two color attachments 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-&gt;bindFramebuffer(); //fb which has two color attachments
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
effect1-&gt;setConstPerFrameUniforms();
//get 0 attachment from fb scene prepass to apply effect and draw on fbDeferredLighting current color attach
effect1-&gt;setTextureUniforms(fbScene, 0); 
quad-&gt;draw();


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

fbDeferredLighting-&gt;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]-&gt;getTextureID(), 0);
glClear(GL_DEPTH_BUFFER_BIT); //only clear depth
effectX-&gt;setConstPerFrameUniforms();
//get other color attach and use it to apply effect on current
effectX-&gt;setTextureUniforms(fbDeferredLighting , colorAttachment0Other); 
quad-&gt;draw();


Is this correct?

EDIT:
I wonder, maybe rewrite it to compute shaders, that would simplify things.
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.