Is there a way in Opengl es 2.0 fragment shader, to get a previous fragment color

Viewed 310

My code is drawing a background image and drawing some other images (particles) on top of that image. I want to make the particles have some blending effects, like darken, lighten, burn ... the same as Canvas globalcompositeoperation does . So in the fragment shader, i need to get the previous fragment color and blend it with the new color. But i could not find a way to do it.

2 Answers

No, there is no possibility within the standard. However, with the extensions EXT_shader_framebuffer_fetch (non-Mali devices) and ARM_shader_framebuffer_fetch (Mali devices) a value from the framebuffer can be read (since OpenGL 2.0 and for OpenGL ES 2.0 / 3.0):

This extension provides a mechanism whereby a fragment shader may read existing framebuffer data as input. This can be used to implement compositing operations that would have been inconvenient or impossible with fixed-function blending. It can also be used to apply a function to the framebuffer color, by writing a shader which uses the existing framebuffer color as its only input.

Note that there is no guarantee that hardware will support an extension. You need to test whether the extension is supported or not at runtime.

If you want to read fragments from the previous rendering, the usual way is to implement multiple rendering passes and render to a texture. See also LearnOpenGL - Deferred Shading.

In many cases, there is no need to read fragments in the fragment shader. A lot of rendering effects can be implemented using the standard Blending functionality. The blend function can be changed with glBlendFunc and the blend equation can be changed with glBlendEquation.

Yes it is possible. First, render result to the texturebuffer. Then send the texturebuffer to the second shader to apply the effects.

to find out how to do this, look for the keywords GLES20.glBindFramebuffer (GL_FRAMEBUFFER, mFramebuff);

Related