OpenGL Programmable Blending – APPLE_shader_framebuffer_fetch
Large parts of the rendering pipeline are user programmable through shaders nowadays. One of the remaining fixed-functions is fragment blending: The new fragments (shaded by the fragment shader) don’t neccessarily have to overwrite the corresponding framebuffer entry, we can set different blending operations. The set of possible operations however is fixed.
There have been requests to provide a programmable blending stage for quite some time now and finally this functionality is available – somewhere we probably didn’t expect it: On iOS from Apple. The fruit-branded computer company is not exactly well known for hi-end OpenGL support but this time they provide a long awaited feature for the first time.
The extension for programmable blending is called APPLE_shader_framebuffer_fetch and is available for OpenGL ES on iOS 6. What we get with this extension is a new build-in varable called gl_LastFragData which simply holds what would be the destination of the blending operation in fixed-function (our current fragment color would be the source). Of course this is read-only, we write to the framebuffer in this shader anyway!
Beside reimplementing the known fixed-function blend modes (which now can be different on a per fragment basis!), we can add completely new modes (e.g. photoshop-like blendmodes). Also special blending modes for non-color data is possible…
Another possibility is the implementation of per-fragment post-processing effects without rendering to a texture first: just render an empty fullscreen quad, read the framebuffer color and modify it in the shader (think of color adjustments etc.).
Access to the depth is not provided, but if that’s needed, it might be able by storing the depth (probably at a lower resolution) also in one of the color channels.
If you have access to the 2012 WWDC videos from Apple (you need a developer account), check out session 513!
Now, if only we would get this also on the desktop (and as an ARB extension or even core) :-) Sadly, this might not be so simple as blending is implemented as fixed-function hardware on most GPUs. At least, that’s what Graham Sellers told me and I have no reason to doubt that:
A quick look at the Intel HD 4000 documents suggest, that programmable blending isn’t possible there as well (I took that GPU as it is up-to-date and well documented).