source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg @ 3028

Revision 3028, 7.5 KB checked in by mattausch, 16 years ago (diff)

debug version: what to do with shader programs??

Line 
1#include "../shaderenv.h"
2
3////////////////////
4// Screen Spaced Ambient Occlusion shader
5// based on shader of Alexander Kusternig
6
7
8#define USE_EYE_SPACE_DEPTH 1
9
10
11struct fragment
12{
13        float2 texCoord: TEXCOORD0;
14        float3 view: TEXCOORD1;
15};
16
17
18struct pixel
19{
20        float4 illum_col: COLOR0;
21};
22
23
24inline float2 myreflect(float2 pt, float2 n)
25{
26        // distance to plane
27        float d = dot(n, pt);
28        // reflect around plane
29        float2 rpt = pt - d * 2.0f * n;
30
31        return rpt;
32}
33
34
35inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
36{
37        float3 x1 = lerp(bl, tl, w.y);
38        float3 x2 = lerp(br, tr, w.y);
39        float3 v = lerp(x1, x2, w.x);
40
41        return v;
42}
43
44
45// reconstruct world space position
46inline float3 ReconstructSamplePos(uniform sampler2D colors,
47                                                                   float2 texcoord,
48                                                                   float3 bl, float3 br, float3 tl, float3 tr)
49{
50        //const float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w;
51        const float eyeSpaceDepth = tex2D(colors, texcoord).w;
52        float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
53        float3 samplePos = -viewVec * eyeSpaceDepth;
54
55        return samplePos;
56}
57
58
59/** The ssao shader returning the an intensity value between 0 and 1
60*/
61float2 ssao(fragment IN,
62                   uniform sampler2D colors,
63                   uniform sampler2D noiseTexture,
64                   uniform float2 samples[NUM_SAMPLES],
65                   uniform float3 currentNormal,
66                   uniform float3 centerPosition,
67                   uniform float scaleFactor,
68                   uniform float3 bl,
69                   uniform float3 br,
70                   uniform float3 tl,
71                   uniform float3 tr,
72                   uniform float3 viewDir
73                   )
74{
75        // Check in a circular area around the current position.
76        // Shoot vectors to the positions there, and check the angle to these positions.
77        // Summing up these angles gives an estimation of the occlusion at the current position.
78
79        float total_ao = 0.0;
80        float numSamples = 0;
81
82
83        for (int i = 0; i < NUM_SAMPLES; ++ i)
84        {
85                const float2 offset = samples[i];
86
87#if 1
88                ////////////////////
89                // add random noise: reflect around random normal vector (warning: slow!)
90
91                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
92                const float2 offsetTransformed = myreflect(offset, mynoise);
93#else
94                const float2 offsetTransformed = offset;
95#endif
96                // weight with projected coordinate to reach similar kernel size for near and far
97                const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor;
98
99                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
100
101                const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr);
102
103
104                ////////////////
105                //-- compute contribution of sample using the direction and angle
106
107                float3 dirSample = samplePos - centerPosition;
108                const float magSample = length(dirSample);
109                // normalize
110                dirSample /= magSample;
111
112                // angle between current normal and direction to sample controls AO intensity.
113                const float cosAngle = max(dot(dirSample, currentNormal), 0.0f);
114
115                // the distance_scale offset is used to avoid singularity that occurs at global illumination when
116                // the distance to a sample approaches zero
117                const float intensity = SAMPLE_INTENSITY / (DISTANCE_SCALE + magSample * magSample);
118
119#if 1
120                // if surface normal perpenticular to view dir, approx. half of the samples will not count
121                // => compensate for this (on the other hand, projected sampling area could be larger!)
122                const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal);
123                total_ao += cosAngle * intensity * viewCorrection;
124#else
125                total_ao += cosAngle * intensity;
126#endif
127        }
128
129        return float2(max(0.0f, 1.0f - total_ao), numSamples);
130}
131
132#pragma position_invariant main
133
134/** The mrt shader for screen space ambient occlusion
135*/
136pixel main(fragment IN,
137                   uniform sampler2D colors,
138                   uniform sampler2D normals,
139                   uniform sampler2D noise,
140                   uniform float2 samples[NUM_SAMPLES],
141                   uniform sampler2D oldTex,
142                   const uniform float4x4 oldModelViewProj,
143                   const uniform float4x4 modelViewProj,
144                   uniform float temporalCoherence,
145                   uniform float3 eyePos,
146                   uniform float3 bl,
147                   uniform float3 br,
148                   uniform float3 tl,
149                   uniform float3 tr
150                   )
151{
152        pixel OUT;
153
154        float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0));
155        const float3 normal = normalize(norm.xyz);
156
157        /// reconstruct position from the eye space depth
158        float3 viewDir = IN.view;
159        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
160        const float3 eyeSpacePos = -viewDir * eyeDepth;
161        const float3 centerPosition = eyePos + eyeSpacePos;
162
163        const float4 realPos = float4(centerPosition, 1.0f);
164
165
166        ////////////////
167        //-- calculcate the current projected depth for next frame
168       
169        float4 currentPos = mul(modelViewProj, realPos);
170       
171        const float w = SAMPLE_RADIUS / currentPos.w;
172        currentPos /= currentPos.w;
173       
174        const float precisionScale = 1e-3f;
175        const float currentDepth = currentPos.z * precisionScale;
176
177        const float2 ao = ssao(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
178
179
180        /////////////////
181        //-- compute temporally smoothing
182
183
184        // reproject new frame into old one
185       
186        // calculate projected depth
187        float4 projPos = mul(oldModelViewProj, realPos);
188        projPos /= projPos.w;
189
190        // the current depth projected into the old frame
191        const float projDepth = projPos.z * precisionScale;
192
193        // fit from unit cube into 0 .. 1
194        const float2 tex = projPos.xy * 0.5f + 0.5f;
195
196        // retrieve the sample from the last frame
197        float4 oldCol = tex2D(oldTex, tex);
198
199        const float oldDepth = oldCol.z;
200        //const float depthDif = 1.0f - projDepth / oldDepth;
201        const float depthDif = projDepth - oldDepth;
202
203
204        //const float oldNumSamples = oldCol.y;
205        const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
206
207        float newWeight;
208
209        // the number of valid samples in this frame
210        //const float newNumSamples = ao.y;
211
212        if ((temporalCoherence > 0) &&
213                (tex.x >= 0.0f) && (tex.x < 1.0f) &&
214                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
215                (abs(depthDif) < MIN_DEPTH_DIFF)
216                // if visibility changed in the surrounding area we have to recompute
217                //&& (oldNumSamples > 0.8f * newNumSamples)
218                )
219        {
220                // increase the weight for convergence
221                newWeight = oldWeight + 1.0f;
222                OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
223                //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
224        }
225        else
226        {       
227                OUT.illum_col.x = ao.x;
228                newWeight = 0;
229        }
230
231        OUT.illum_col.y = newWeight;
232        OUT.illum_col.z = currentDepth;
233
234        return OUT;
235}
236
237
238float Filter(float2 texCoord,
239                         uniform sampler2D ssaoTex,
240                         uniform float2 filterOffs[NUM_DOWNSAMPLES],
241                         uniform float filterWeights[NUM_DOWNSAMPLES]
242)
243{
244        float average = .0f;
245        float w = .0f;
246
247        for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
248        {
249                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x;
250                w += filterWeights[i];
251        }
252
253        average *= 1.0f / (float)w;
254
255        return average;
256}
257
258
259pixel combine(fragment IN,
260                          uniform sampler2D colors,
261                          uniform sampler2D ssaoTex,
262                          uniform float2 filterOffs[NUM_DOWNSAMPLES],
263                          uniform float filterWeights[NUM_DOWNSAMPLES]
264                          )
265{
266        pixel OUT;
267
268        float4 col = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
269        float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
270
271        //if (ao.y < 2000.0f)
272        //      ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
273
274        OUT.illum_col = col * ao.x;
275        //OUT.illum_col.xyz = float3(ao.x,1-ao.y*1e-2f, 0);
276        OUT.illum_col.w = col.w;
277
278        return OUT;
279}
Note: See TracBrowser for help on using the repository browser.