source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg @ 3168

Revision 3168, 5.8 KB checked in by mattausch, 16 years ago (diff)

removed bug (second aa output), normsl normalization now during mrt output

Line 
1#include "../shaderenv.h"
2#include "common.h"
3
4
5/*************************************************/
6/*     Filter for combining ssao with image      */
7/*************************************************/
8
9
10struct fragment
11{
12        float2 texCoord: TEXCOORD0;
13        float3 view: TEXCOORD1;
14};
15
16
17struct pixel
18{
19        float4 illum_col: COLOR0;
20};
21
22
23/** Filter taking into account depth and normal differences,
24    and convergence of a sample 
25*/
26float DiscontinuityFilter(float2 texCoord,
27                                                  float4 ao,
28                                                  float4 color,
29                                                  uniform sampler2D ssaoTex,
30                                                  uniform sampler2D normalsTex,
31                                                  uniform sampler2D colorsTex,
32                                                  uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
33                                                  uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
34                                                  float scale
35                                                  )
36{
37        float average = .0f;
38        float total_w = .0f;
39
40        const float eyeSpaceDepth = color.w;
41
42        const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
43
44        float4 aoSample;
45        float3 sampleNorm;
46        float3 samplePos;
47        float w;
48        float4 sampleTexCoord;
49        float depthFactor;
50        float normalFactor;
51        float convergenceFactor;
52        float sampleDepth;
53
54        for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
55        {
56                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
57
58                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
59                sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
60
61                // check depth discontinuity
62                sampleDepth = tex2Dlod(colorsTex, sampleTexCoord).w;
63                //sampleDepth = aoSample.w;
64               
65                //depthFactor = 1.0f / max(abs(eyeSpaceDepth - sampleDepth), 1e-2f);
66                depthFactor = 1.0f - step(1e-2f, abs(1.0f - eyeSpaceDepth / sampleDepth));
67                //normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-3f);
68                normalFactor = max(dot(sampleNorm, centerNormal), 1e-3f);
69                //convergenceFactor = min(100.0f, aoSample.y);
70                convergenceFactor = aoSample.y;
71
72                // combine the weights
73                w = filterWeights[i] * convergenceFactor * depthFactor * normalFactor;
74                //w = normalFactor * convergenceFactor;
75
76                average += aoSample.x * w;
77                total_w += w;
78        }
79
80        average /= max(total_w, 1e-6f);
81
82        return saturate(average);
83}
84
85
86
87/** Filter taking into account depth and normal differences,
88   and convergence of a sample 
89*/
90float DiscontinuityFilter2(float2 texCoord,
91                                                   float4 ao,
92                                                   float4 color,
93                                                   uniform sampler2D ssaoTex,
94                                                   uniform sampler2D normalsTex,
95                                                   uniform sampler2D colorsTex,
96                                                   uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
97                                                   float scale,
98                                                   float3 bl,
99                                                   float3 br,
100                                                   float3 tl,
101                                                   float3 tr)
102{
103        float average = .0f;
104        float total_w = .0f;
105
106        const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
107        const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
108
109        float4 aoSample;
110        float3 sampleNorm;
111        float3 samplePos;
112        float w;
113        float4 sampleTexCoord;
114        float spatialFactor;
115        float normalFactor;
116        float convergenceFactor;
117        float len;
118
119        for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
120        {
121                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
122
123                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
124                sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
125
126                // check spatial discontinuity
127                samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
128                len = min(SqrLen(centerPos - samplePos), 1e2f);
129
130                spatialFactor = 1.0f / max(len, 1e-3f);
131
132                //normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-3f);
133                normalFactor = max(dot(sampleNorm, samplePos), 1e-3f);
134                //convergenceFactor = min(100.0f, aoSample.y);
135                convergenceFactor = aoSample.y;
136
137                // combine the weights
138                w = convergenceFactor * spatialFactor * normalFactor;
139
140                average += aoSample.x * w;
141                total_w += w;
142        }
143
144        average /= max(total_w, 1e-6f);
145
146        return saturate(average);
147}
148
149
150/** Function combining image and indirect illumination buffer using a
151        depth and normal aware discontinuity filter.
152*/
153pixel combine(fragment IN,
154                          uniform sampler2D colorsTex,
155                          uniform sampler2D ssaoTex,
156                          uniform sampler2D normalsTex,
157                          uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
158                          uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
159                          uniform float4x4 modelViewProj,
160                          uniform float3 bl,
161                          uniform float3 br,
162                          uniform float3 tl,
163                          uniform float3 tr
164                          )
165{
166        pixel OUT;
167
168        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
169        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
170
171        // reconstruct position from the eye space depth
172        /*const float4 worldPos = float4(-IN.view * eyeSpaceDepth, 1.0f);
173        // compute w factor from projection in order to control filter size
174        const float4 projPos = mul(modelViewProj, worldPos);
175        const float distanceScale = 1.0f / projPos.w;
176*/
177
178        // filter up to a certain convergance value and leave out background (sky) by checking depth
179        if ((ao.y < 100.0f) && (col.w < 1e10f))
180        //if (col.w < 1e10f)
181        {
182                //const float eyeSpaceDepth = col.w; const float distanceScaleWeight = 2.0f;
183                //const float distanceScale = distanceScaleWeight / (eyeSpaceDepth + distanceScaleWeight);
184                const float distanceScale = 1.0f;
185
186                const float convergence = ao.y;
187                const float adaptWeight = 25.0f;
188                const float convergenceScale = SSAO_CONVERGENCE_WEIGHT / (convergence + SSAO_CONVERGENCE_WEIGHT);
189
190                const float scale = NUM_SSAO_FILTER_WIDTH * convergenceScale * distanceScale;
191
192                // the filtered ssao value
193                ao.x = DiscontinuityFilter2(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
194        }
195
196        OUT.illum_col.xyz = col.xyz * ao.x;
197        //OUT.illum_col.xyz = float3(ao.x, ao.x, ao.x);
198        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
199        OUT.illum_col.w = col.w;
200
201        return OUT;
202}
Note: See TracBrowser for help on using the repository browser.