Volumetric Light Scattering (help)

Started by
7 comments, last by spek 12 years ago
I'm trying to implement the following screen-space based technique into my renderer but am having trouble getting it to work.

http://http.develope...gems3_ch13.html

Unfortunately I don't have access to the book's demo code but afaik there's nothing more "special" going on than what is written there.

The shader is rather simple and short though it seems I'm still doing something wrong here:

// 2D Vector
extern float2 GBufferTextureSize;
// 3D Vector
const float3 LightPos = float3(0.0f, -1.0f, 1.0f);

// Matrices
float4x4 WVP;
// Sample our Composite Scene
sampler2D SceneSampler : register(s0);

// Constants
const int NUM_SAMPLES = 16;
float Density = 0.8f;
float Weight = 0.9f;
float Decay = 0.5f;
float Exposure = 0.5f;

struct VSI_VolLightScattering
{
float4 Position : POSITION0;
float2 UV : TEXCOORD0;
};

struct PSI_VolLightScattering
{
float4 Position : SV_POSITION;
float2 UV : TEXCOORD0;
float4 ScreenLightPos : TEXCOORD1;
};





PSI_VolLightScattering VolumetricLightScattering_VS(VSI_VolLightScattering input)
{
PSI_VolLightScattering output = (PSI_VolLightScattering)0;
output.Position = float4(input.Position.xy, 1, 1);
output.UV = input.UV + GBufferTextureSize;
output.ScreenLightPos = mul(LightPos, WVP);
return output;
}





float4 VolumetricLightScattering_PS(PSI_VolLightScattering input) : COLOR0
{
half2 ssPos = input.ScreenLightPos.xy / input.ScreenLightPos.w * float2(0.5, -0.5) + 0.5;
half2 deltaTexCoord = (input.UV - ssPos);
deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;
half3 color = tex2D(SceneSampler, input.UV);
half illuminationDecay = 1.0f;
for(int i = 0; i < NUM_SAMPLES; i++)
{
input.UV -= deltaTexCoord;
half3 Sample = tex2D(SceneSampler, input.UV);
Sample *= illuminationDecay * Weight;
color += Sample;
illuminationDecay *= Decay;
}

return float4(color.rgb * Exposure, 1);
}




In engine it looks like this:
http://cl.ly/3J0r241G2e0J200P1i3b

It's just blurring the whole screen.


Did anyone implement this technique or can help me in any way ?

Also, I'm using a pow approach on my skydome to fake a sun, how would that translate to the Light's Position in my shader here (directional light)?
Advertisement
Well, asides the shader it actually does one more important step, rendering a darkened scene. Everything not black will result in blur streaks, into a specific direction (usually towards or away from the sun or another dominent lightsource). So if you apply the shader on a normal colored scene, everything will blur. Just render the scene black, a darkish color for the skybox eventually, and a bright (flare)sprite for the sun. Also reduce your "density" factor to shorten the blurstreaks.Now it seems as if the loop takes quite big steps when adding "deltaTexCoord" to the sample UV. If it keeps going wrong, triple check the lightPos and vectors. I remember that went wrong in my case :D
I just use the scene texture (being the final rendered scene) and read that in my scattering shader and output it to a fullscreen quad. How would I draw this "black" scene first ?
Do I just make the input black ? I'm a little confused on how to do this technique using a deferred renderer.

Rendering to a fullscreen quad I also would not have access to the light's world matrix.
There are several ways. One straight forward way is to render the scene again in a background pass, on a texture target. But very simple only:
- Clear the screen with the color that represents the skybox light (black, or a dark color as well)
- Render the scene geometry, but just output black pixels (no textures, no lighting, no nothing)
- Render the lightsource sprite/flare(s) <- btw, you may want to render the sprite bigger as usual to make sure you got some pixels to blur

Another way is to store a bit extra data in the alpha channel while doing your main render. IF you have that channel available that is. In the RGB you store colors as always, then in the alpha you store the blur intensity. Black means nothing, white means maximum blur. So typically the skybox outputs a low or 0 value to the alpha, the geometry 0, and the flare(s) a relative high value. Then your post-blur shader, simply multiply the input texture color like this:


[color=#000000]half4 [color=#660066]Sample[color=#000000] [color=#666600]=[color=#000000] tex2D[color=#666600]([color=#660066]SceneSampler[color=#666600],[color=#000000] input[color=#666600].[color=#000000]UV[color=#666600]).rgba;
sample.rgb *= sample.a;
[color=#000000] [color=#660066]Sample[color=#000000] [color=#666600]*=[color=#000000] illuminationDecay [color=#666600]*[color=#000000] [color=#660066]Weight[color=#666600];


13.5.1 The Occlusion Pre-Pass Method

[color=#000000][font=Verdana, Geneva, Arial, Helvetica,]If we render the occluding objects black and untextured into the source frame buffer, image processing to generate light rays is performed on this image. Then the occluding scene objects are rendered with regular shading, and the post-processing result is additively blended into the scene. This approach goes hand in hand with the common technique of rendering an unshaded depth pre-pass to limit the depth complexity of fully shaded pixels. [/font]Figure 13-3[color=#000000][font=Verdana, Geneva, Arial, Helvetica,] shows the steps involved.[/font]
[/quote]

You can also use your deferred render's depth texture instead of re-rendering everything.

I don't think you need to render the light source either. You know your light source's screen space position, so you can take that into account along with the depth buffer occlusion.
So do I understand that right, I draw my whole scene just black + something bright which I then input to the shader above. And this shader blurs the picture (which would be just the light source), stretches that blurred image somehow to the light position and adds that to my "normal" scene?

And that black geometry is drawn to a seperate RT (let's call it BlackTarget), which then gets the shader applied and outputs the finished image ? ("normal" scene + Light shafts?)

Btw using the deferred lighting (Light pre pass) way I already have to render my geometry a second time. So I'm currently using a second renderTarget output in there to just output everything black in there. That should work right ?

update: video deleted* (forgive the music ^^)
>> So do I understand that right, I draw my whole scene just black + something bright which I then input to the shader above. And this shader blurs the picture (which would be just the light source), stretches that blurred image somehow to the light position and adds that to my "normal" scene
That's pretty much it indeed. The GPU Gems example also slightly lits the environment with the same lightsource, but just try it with a black scene first for ease. The blur streaks towards (or from) the lightsource screen position is an important element. And it seems that goes wrong in your case. Wrong coordinates probably. I remember my first attempt calculated the screen positions all wrong.

As a first test, I would test the inputUV and density setting. If deltaTexCoord keeps 0, you should get an unblurred copy of the original input, If not, the input.uv value is wrong. It seems the blur goes into exactly the wrong direction, with too big steps. What happens if you inverse the deltaTexCoord and make it 10 times smaller? If it keeps weird, double check ssPos.


>> And that black geometry is drawn to a seperate RT (let's call it BlackTarget), which then gets the shader applied and outputs the finished image ? ("normal" scene + Light shafts?
That's an option. The finished imagine can be added to the other results with an additive blend. Though render it to a separate target requires to render the entire environment twice. You said you are using a deferred pipeline. I would try to reserve a value into of those textures indeed and use that texture as input for the lightscatter shader.
You mean setting deltaTexCoord to 0 ? If I do that I get an unblurred standard scene image.
Also negating and scaling it doesn't make it look much better.

I'm not quite sure how to get the position of my sun, which is just done in a seperate shader like this:
// Calculate Sun
float3 Normal = normalize(input.PositionOS);
float3 Sun = normalize(vSunPos);
float sunDot = dot(Sun, Normal);
float4 sunVector = 1000.0f * pow(max(0, sunDot), 350.0f);


This is being applied to my skydome geometry.
Setting the delta to 0 means an unblurred result indeed, but did you test that? To make sure original UV coordinates are ok. Try to elimate all possible errors. Most likely one or more coordinates aren't properly converted to screen space.

The way how the sun position and vector are calculated seem odd to me, but I'm not a matrix-math hero. But what you really want to know are the 2D screen coordinates of the sun, and the 2D direction vectors between the rendered pixels and that sun. First make sure you have that sun position correct before going to blur. In case you render your screen quad that does the post effect with corner coordinates in the range of -1..+1, the sunPosition should be somewhere in that range too. Here's an article (also doing blur as post effect) that explains some code that also converts world-positions to screen points:
http://http.developer.nvidia.com/GPUGems3/gpugems3_ch27.html

This topic is closed to new replies.

Advertisement