Alpha transparency in deferred shader

Published June 11, 2010
Advertisement
When I switched from forward rendering to deferred rendering sometime ago, I really liked the elegant handling of lights. This is still the main reason
I use a deferred renderer, but I always missed the ability to render transparent surfaces like water or "fog". Well, I succeeded in adding transparent
surface support to my deferred renderer again, yeah, but only for one layer of transparency. Still, this is more than enough for the start and I'm quite
happy with the result.

Here's what I've done. The basic idea is to use stipple transparency and a simple shader to get rid of the stipple pattern in a final run. My approach is
a simplified version of the more common 3 layer model stipple transparency model.

Here's an overview of the technique:
1. Render g-buffer as always.
2. Render transparent surfaces with stipple pattern, write an alpha value greater 0 to indicate the tranparency factor.
3. Light pass (just one is needed!)
4. Final composition, combine transparent pixel with background pixels.



The stipple pattern can be generated by the following shader fragment (GLSL):
// returns 0 or 1 float stippleMask(){	vec2 stipple = fract( gl_FragCoord.xy * 0.5 );	return step(0.25,abs(stipple.x-stipple.y));}


In figure 1 you can see my test level with and without a (stipple) transparent surface. The stipple pattern is clearly visible, but this is already some kind
of transparency effect, even if it is fixed at 50%.

We want to get rid of the pattern and we want to use a fullrange transparency value of 0..1. This is done in a final composition step. In my engine I use
this step to do tonemapping and adding bloom.

First I sample four samples of the framebuffer. Remember that we have used the alpha channel to store the transparancy value, so we check if any of
the four texels have an alpha value greater 0. If this is NOT the case, we will ignore all neighbor texels. This way we will not introduce unnecessary
loss of quality.

When we got the atleast one alpha value greater 0 we have to average the texel pairs in a cross fashion. Knowing that only one texel pair contains
alpha values >0 we can add all alpha values to get the final value. Now we can simply alpha blend the background texel pair with the transparent surface
texel pair, with one exception: we don't know which pair is the background and which is the transparent pair. But this is not a problem, just
use the stipple pattern function to determine the background texel pair and do the blending.
Here's a small GLSL fragment shader demonstrating the process:
// collect color texels	vec4 color_texel_a1 = texture2D(color_depth_map, texture_coord);	vec4 color_texel_a2 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2(-1,-1));	vec4 color_texel_b1 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2( 0,-1));	vec4 color_texel_b2 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2(-1, 0));// average texel color for pair a and b	vec4 avg_a = (color_texel_a1+color_texel_a2)*0.5;	vec4 avg_b = (color_texel_b1+color_texel_b2)*0.5;// get final alpha transparency value	float alpha = max(avg_a.a,avg_b.a);	// transparent mask	float transparent_mask = step(0.01, alpha);// update alpha 	alpha = stippleMask()>0 ? alpha : 1.0-alpha;// blend background and foreground color	vec3 color = mix(avg_a.rgb,avg_b.rgb,alpha);// apply transparent mask, if none transparency is present take the center texel		color = mix(color_texel_a1.rgb,tmp_color,transparent_mask);


As you can see in the close up view, we half the resolution when we encounter a transparent surface. This can result in artifacts when we use transparent
surfaces with high frequent highlights, but on the other hand it is very simple, fast and it enables transparency in a deferred shader !
Next Entry Still alive..
2 likes 7 comments

Comments

dgreen02
Cool...that's a neat trick.
June 13, 2010 11:42 PM
Hodgman
Cool stuff =D

How well does the stippled transparency integrate with other parts of your rendering pipe, like unlit/forward-lit particles, bloom, etc?
June 14, 2010 08:26 PM
Ashaman73
Quote:Original post by Hodgman
Cool stuff =D

How well does the stippled transparency integrate with other parts of your rendering pipe, like unlit/forward-lit particles, bloom, etc?


It works quite well.
- SSAO and shadowing is done before the stipple pass.
- Particles should work as normal, but I need more testing to be sure. The stipples write to the z-buffer so that particles should integrate seemless.
- Bloom works on the stippled image and I can't see any artifacts.

June 15, 2010 10:16 AM
Litheon
Very interesting!
June 16, 2010 02:46 AM
solenoidz
That was a nice one, thanks !<BR>Let's see if I understand it right so far. <BR>This method provides order independent transparency by rendering an object via screen space checker like grid, where every even pixel has 0 aplha and every odd pixel has 1 alpha value. This is rendered using something like alphatest and filtered to bring one layer of transparency with 50 % "^alpha blend^" <BR>This is pretty nice, but is there a way to overcome the fixed 50% transparency and make it full range. 0-1.0(0-255) or maybe I missed something in the code that already provides this ?
May 20, 2011 03:46 PM
Ashaman73
[quote]I missed something in the code that already provides this ?
[/quote]
It is already provided. The basic idea is, that a 2x2 screen area contains two or four background pixels (always opaque) and two pixels can contain an transparent overlay. The transparent overlay consists of a RGB color and an alpha value, this alpha value is actually taken to blend the overlay with the background, therefore it can be 0..1.
June 12, 2012 03:43 PM
solenoidz

Thanks, I get it now. Good idea, nice trick.

August 08, 2013 09:28 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement