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

Revision 3366, 23.7 KB checked in by mattausch, 15 years ago (diff)
Line 
1#include "../shaderenv.h"
2#include "common.h"
3
4////////////////////
5// Screen Spaced Ambient Occlusion shader
6// based on shader of Alexander Kusternig
7
8
9#define USE_EYESPACE_DEPTH 1
10
11
12struct fragment
13{
14        float2 texCoord: TEXCOORD0;
15        float3 view: TEXCOORD1;
16};
17
18
19struct pixel2
20{
21        float4 illum_col: COLOR0;
22        float4 col: COLOR1;
23};
24
25
26// this function is inspired from the paper of shamulgaan in order
27// to get a physical expression for the occlusion culling
28inline float occlusionPower(float radius, float dist)
29{
30        return 6.283185307179586476925286766559f * (1.0f - cos(asin(radius / dist)));
31}
32
33
34
35// reconstruct world space position
36inline float3 ReconstructSamplePos(float eyeSpaceDepth,
37                                                                   float2 texcoord,
38                                                                   float3 bl, float3 br, float3 tl, float3 tr)
39{
40        float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
41        float3 samplePos = -viewVec * eyeSpaceDepth;
42
43        return samplePos;
44}
45
46
47float ComputeConvergence(uniform sampler2D tex, float2 texCoord, float2 res)
48{
49        // get the minimum convergence by exactly sampling the 4 surrounding
50        // texels in the old texture, otherwise flickering because convergence
51        // will be interpolated when upsampling and filter size does not match!
52
53        const float2 invRes = float2(1.0f / res.x, 1.0f / res.y);
54
55        // get position exactly between texel centers
56        float2 center = (floor(texCoord * res) + float2(.5f)) * texCoord;
57        //center.x = (floor(texCoord.x * res.x - .5f) + 1.0f) / res.x;
58        //center.y = (floor(texCoord.y * res.y - .5f) + 1.0f) / res.y;
59        //center.y = (floor(texCoord.y * res.y) + .5f) * yOffs;
60
61        /*texelCenterConv.x = tex2Dlod(tex, float4(center + float2( xoffs,  yoffs), 0, 0)).y;
62        texelCenterConv.y = tex2Dlod(tex, float4(center + float2( xoffs, -yoffs), 0, 0)).y;
63        texelCenterConv.z = tex2Dlod(tex, float4(center + float2(-xoffs, -yoffs), 0, 0)).y;
64        texelCenterConv.w = tex2Dlod(tex, float4(center + float2(-xoffs,  yoffs), 0, 0)).y;
65
66        const float m1 = min(texelCenterConv.x, texelCenterConv.y);
67        const float m2 = min(texelCenterConv.z, texelCenterConv.w);
68
69        const float convergence = min(m1, m2);*/
70
71        //const float convergence = tex2Dlod(tex, float4(center, 0, 0)).y;
72        const float convergence = tex2Dlod(tex, float4(texCoord, 0, 0)).y;
73
74        return convergence;
75}
76
77/** This shader computes the reprojection and stores
78        the ssao value of the old pixel as well as the
79        weight of the pixel in the new frame.
80*/
81inline float3 Reproject(float4 worldPos,
82                                                float eyeSpaceDepth,
83                                                float2 texcoord0,
84                                                float3 oldEyePos,
85                                                sampler2D oldTex,
86                                                float4x4 oldModelViewProj,
87                                                sampler2D colors,
88                                                float3 projPos,
89                                                float invW,
90                                                float3 oldbl,
91                                                float3 oldbr,
92                                                float3 oldtl,
93                                                float3 oldtr,
94                                                float3 diffVec
95                                                )
96{
97        // compute position from old frame for dynamic objects + translational portion
98        const float3 translatedPos = diffVec - oldEyePos + worldPos.xyz;
99
100
101        /////////////////
102        //-- reproject into old frame and calculate texture position of sample in old frame
103
104        // note: the old model view matrix only holds the view orientation part
105        float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f));
106        backProjPos /= backProjPos.w;
107       
108        // fit from unit cube into 0 .. 1
109        const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
110        // retrieve the sample from the last frame
111        const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
112
113        // the ssao value in the old frame
114        const float ssao = oldPixel.x;
115
116        // calculate eye space position of sample in old frame
117        const float oldEyeSpaceDepth = oldPixel.w;
118
119        // vector from eye pos to old sample
120        const float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
121        const float invLen = 1.0f / length(viewVec);
122        const float projectedEyeSpaceDepth = invLen * length(translatedPos);
123        //const float projectedEyeSpaceDepth = length(translatedPos);
124       
125        const float depthDif = abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth);
126
127        // the weight of the accumulated samples from the previous frames
128        float w;
129        float idx;
130
131
132        //////////////
133        //-- reuse old value only if it was still valid in the old frame
134
135        if (1
136                && (oldTexCoords.x > 0) && (oldTexCoords.x < 1.0f)
137                && (oldTexCoords.y > 0) && (oldTexCoords.y < 1.0f)
138                && (depthDif <= MIN_DEPTH_DIFF)
139                )
140        {
141                // pixel valid => retrieve the convergence weight
142                /*float w1 = tex2Dlod(oldTex, float4(oldTexCoords + float2(0.5f / 1024.0f, 0), .0f, .0f)).y;
143                float w2 = tex2Dlod(oldTex, float4(oldTexCoords - float2(0.5f / 1024.0f, 0), .0f, .0f)).y;
144                float w3 = tex2Dlod(oldTex, float4(oldTexCoords + float2(0, 0.5f / 768.0f), .0f, .0f)).y;
145                float w4 = tex2Dlod(oldTex, float4(oldTexCoords - float2(0, 0.5f / 768.0f), .0f, .0f)).y;
146
147                w = min(min(w1, w2), min(w3, w4));*/
148               
149                //w = ComputeConvergence(oldTex, oldTexCoords, float2(1024.0f, 768.0f));
150                w   = oldPixel.y;
151                idx = floor(oldPixel.z);
152                //idx = oldPixel.z;
153        }
154        else
155        {       
156                w   = .0f;
157                idx = .0f;
158        }
159
160        return float3(ssao, w, idx);
161}
162
163
164/** The ssao shader returning the an intensity value between 0 and 1.
165        This version of the ssao shader uses the dotproduct between
166        pixel-to-sample direction and sample normal as weight.
167
168    The algorithm works like the following:
169        1) Check in a circular area around the current position.
170        2) Shoot vectors to the positions there, and check the angle to these positions.
171        3) Summing up these angles gives an estimation of the occlusion at the current position.
172*/
173float3 ssao2(fragment IN,
174                         sampler2D colors,
175                         sampler2D noiseTex,
176                         sampler2D samples,
177                         float3 normal,
178                         float3 centerPosition,
179                         float radius,
180                         float3 bl,
181                         float3 br,
182                         float3 tl,
183                         float3 tr,
184                         float3 viewDir,
185                         float convergence,
186                         float sampleIntensity,
187                         bool isMovingObject,
188                         sampler2D normalTex,
189                         float idx
190                         )
191{
192        float total_ao = .0f;
193        float validSamples = .0f;
194        float numSamples = .0f;
195
196        for (int i = 0; i < NUM_SAMPLES; ++ i)
197        {
198                float2 offset;
199
200                const float2 ssaoOffset =
201                        tex2Dlod(samples, float4((0.5f + i + idx) / NUM_PRECOMPUTED_SAMPLES, 0.5f, .0f, .0f)).xy;
202
203                ////////////////////
204                //-- add random noise: reflect around random normal vector
205                //-- (affects performance for some reason!)
206
207                if (!USE_OPTIMIZATION ||
208                        (convergence < SSAO_CONVERGENCE_THRESHOLD))
209                {
210                        float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
211                        //offset = myreflect(samples[i], mynoise);
212                        //offset = myrotate(samples[i], mynoise.x);
213                        offset = myrotate(ssaoOffset, mynoise.x);
214                }
215                else
216                {
217                        offset = ssaoOffset;
218                }
219               
220                // weight with projected coordinate to reach similar kernel size for near and far
221                const float2 texcoord = IN.texCoord.xy + offset * radius;
222
223                const float4 sampleColor = tex2Dlod(colors, float4(texcoord, .0f, .0f));
224                const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
225               
226
227                ////////////////
228                //-- compute contribution of sample using the direction and angle
229
230                float3 dirSample = samplePos - centerPosition;
231
232                const float minDist = 1e-6f;
233                const float delta = 1e-3f;
234
235                const float lengthToSample = length(dirSample);
236                const float sampleWeight = 1.0f / (lengthToSample + delta);
237
238                dirSample /= max(lengthToSample, minDist); // normalize
239
240
241                // angle between current normal and direction to sample controls AO intensity.
242                const float cosAngle = dot(dirSample, normal);
243
244                // the normal of the current sample
245                const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz);
246               
247                // angle between current normal and direction to sample controls AO intensity.
248                //const float cosAngle2 = dot(-dirSample, sampleNormal);
249                const float cosAngle2 = .5f + dot(sampleNormal, -normal) * .5f;
250
251                dirSample *= minDist;
252                const float aoContrib = sampleIntensity * sampleWeight;
253
254                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
255                //total_ao += max(cosAngle, .0f) * max(cosAngle2, .0f) * aoContrib;
256                total_ao += max(cosAngle, .0f) * cosAngle2 * aoContrib;
257
258                ++ numSamples;
259
260                // check if the samples have been valid in the last frame
261                // only mark sample as invalid if in the last / current frame
262                // they possibly have any influence on the ao
263
264                const float changeFactor = sampleColor.y;
265                const float pixelValid = sampleColor.x;
266
267                // hack:
268                // we check if the sample could have been near enough
269                // to the current pixel or if the angle is small enough
270                // to have any influence in the current or last frame
271#if 1
272                const float tooFarAway = step(0.5f, lengthToSample - changeFactor);
273                const float partlyResetThres = 1.0f;
274
275                if (pixelValid <= partlyResetThres)
276                        validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle));
277                else
278                        validSamples = max(validSamples, pixelValid);
279#endif
280
281#ifdef USE_GTX
282                // we can bail out early and use a minimal #samples)
283                // if some conditions are met as long as the hardware supports it
284                if (numSamples >= MIN_SAMPLES)
285                {
286                        //break;
287                        // if the pixel belongs to a static object and all the samples stay valid in the current frame
288                        if (!isMovingObject && (validSamples < 1.0f) && (convergence > NUM_SAMPLES)) break;
289                        // if the pixel belongs to a dynamic object but the #accumulated samples for this pixel is sufficiently high
290                        // (=> there was no discontinuity recently)
291                        //else if (isMovingObject && (convergence > SSAO_CONVERGENCE_THRESHOLD)) break;
292                        else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break;
293                }
294#endif
295        }
296
297        // "normalize" ao contribution
298        total_ao /= numSamples;
299
300#if 1
301        // if surface normal perpenticular to view dir, approx. half of the samples will not count
302        // => compensate for this (on the other hand, projected sampling area could be larger!)
303        const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
304        total_ao *= viewCorrection;
305#endif
306
307        //return float3(total_ao, validSamples, numSamples);
308        return float3(min(1.0f, total_ao), validSamples, numSamples);
309}
310
311//#define TRYOUT
312
313#ifdef TRYOUT
314
315/** The ssao shader returning the an intensity value between 0 and 1.
316        This version of the ssao shader uses the dotproduct between
317        pixel-to-sample direction and sample normal as weight.
318
319    The algorithm works like the following:
320        1) Check in a circular area around the current position.
321        2) Shoot vectors to the positions there, and check the angle to these positions.
322        3) Summing up these angles gives an estimation of the occlusion at the current position.
323*/
324float3 ssao(fragment IN,
325                        sampler2D colors,
326                        sampler2D noiseTex,
327                        sampler2D samples,
328                        float3 normal,
329                        float3 centerPosition,
330                        float radius,
331                        float3 bl,
332                        float3 br,
333                        float3 tl,
334                        float3 tr,
335                        float3 viewDir,
336                        float convergence,
337                        float sampleIntensity,
338                        bool isMovingObject,
339                        float oldIdx
340                        )
341{
342        float total_ao = .0f;
343        float validSamples = .0f;
344        float numSamples = .0f;
345
346        for (int i = 0; i < NUM_SAMPLES; ++ i)
347        {
348                float2 offset;
349
350                const float2 ssaoOffset =
351                        tex2Dlod(samples, float4((0.5f + i + oldIdx) / NUM_PRECOMPUTED_SAMPLES, 0.5f, .0f, .0f)).xy;
352
353
354                ////////////////////
355                //-- add random noise: reflect around random normal vector
356                //-- (affects performance for some reason!)
357
358                if (!USE_OPTIMIZATION ||
359                        (convergence < SSAO_CONVERGENCE_THRESHOLD))
360                {
361                        float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
362                        //offset = myreflect(samples[i], mynoise);
363                        //offset = myrotate(samples[i], mynoise.x);
364                        offset = myrotate(ssaoOffset, mynoise.x);
365                }
366                else
367                {
368                        offset = ssaoOffset;
369                }
370
371
372                // weight with projected coordinate to reach similar kernel size for near and far
373                const float2 texcoord = IN.texCoord.xy + offset * radius;
374
375                const float4 sampleColor = tex2Dlod(colors, float4(texcoord, .0f, .0f));
376                const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
377               
378
379                ////////////////
380                //-- compute contribution of sample using the direction and angle
381
382                float3 dirSample = samplePos - centerPosition;
383
384                const float minDist = 1e-6f;
385                const float eps = 1e-3f;
386
387                const float lengthToSample = length(dirSample);
388                const float sampleWeight = 1.0f / max(lengthToSample, eps);
389
390                dirSample /= max(length(dirSample), minDist); // normalize
391
392                // angle between current normal and direction to sample controls AO intensity.
393                const float cosAngle = dot(dirSample, normal);
394
395                //const float aoContrib = sampleIntensity / sqrLen;
396                const float aoContrib = sampleIntensity * sampleWeight;
397                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
398
399                total_ao += max(cosAngle, .0f) * aoContrib;
400
401                ++ numSamples;
402
403#ifdef PERFORMANCE_TEST
404                // check if the samples have been valid in the last frame
405                // only mark sample as invalid if in the last / current frame
406                // they possibly have any influence on the AO
407
408                const float changeFactor = sampleColor.y;
409                const float pixelValid = sampleColor.x;
410
411                // hack:
412                // we check if the sample could have been near enough to the current pixel
413                // or if the angle is small enough
414                // to have any influence in the current or last frame
415
416#if 1
417                const float partlyResetThres = 1.0f;
418
419                const float tooFarAway = step(.5f, lengthToSample - changeFactor);
420                if (0)//pixelValid <= partlyResetThres)
421                        validSamples = max(validSamples, pixelValid * (1.0f - tooFarAway) * step(-0.1f, cosAngle));
422                else
423                        validSamples = max(validSamples, pixelValid);
424#endif
425
426#ifdef USE_GTX
427                // we can bail out early and use a minimal #samples)
428                // if some conditions are met as long as the hardware supports it
429                if (numSamples >= MIN_SAMPLES)
430                {
431                        //break;
432                        // if the pixel belongs to a static object and all the samples stay valid in the current frame
433                        if (!isMovingObject && (validSamples < 1.0f) && (convergence > NUM_SAMPLES)) break;
434                        // if the pixel belongs to a dynamic object but the #accumulated samples for this pixel is sufficiently high
435                        // (=> there was no discontinuity recently)
436                        //else if (isMovingObject && (convergence > SSAO_CONVERGENCE_THRESHOLD)) break;
437                        else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break;
438                }
439#endif
440
441#endif // PERFORMANCE_TEST
442        }
443
444        // "normalize" ao contribution
445        total_ao /= numSamples;
446
447#if 1
448        // if surface normal perpenticular to view dir, approx. half of the samples will not count
449        // => compensate for this (on the other hand, projected sampling area could be larger!)
450        const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
451        total_ao *= viewCorrection;
452#endif
453
454        //return float3(total_ao, validSamples, numSamples);
455        return float3(min(1.0f, total_ao), validSamples, numSamples);
456}
457
458#else
459
460float3 ssao(fragment IN,
461                        sampler2D colors,
462                        sampler2D noiseTex,
463                        sampler2D samples,
464                        float3 normal,
465                        float3 centerPosition,
466                        float radius,
467                        float3 bl,
468                        float3 br,
469                        float3 tl,
470                        float3 tr,
471                        float3 viewDir,
472                        float convergence,
473                        float sampleIntensity,
474                        bool isMovingObject,
475                        float oldIdx,
476                        sampler2D attribsTex,
477                        float3 oldPos
478                        )
479{
480        float total_ao = .0f;
481        float validSamples = .0f;
482        float numSamples = .0f;
483
484        //float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
485
486        for (int i = 0; i < NUM_SAMPLES; ++ i)
487        {
488                float2 offset;
489
490                const float2 ssaoOffset =
491                        tex2Dlod(samples, float4((0.5f + i + oldIdx) / NUM_PRECOMPUTED_SAMPLES, .5f, .0f, .0f)).xy;
492
493
494                ////////////////////
495                //-- add random noise: reflect around random normal vector
496                //-- (affects performance for some reason!)
497
498                if (!USE_OPTIMIZATION ||
499                        (convergence < SSAO_CONVERGENCE_THRESHOLD))
500                {
501                        float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
502                        //offset = myreflect(samples[i], mynoise);
503                        //offset = myrotate(samples[i], mynoise.x);
504                        offset = myrotate(ssaoOffset, mynoise.x);
505                }
506                else
507                {
508                        offset = ssaoOffset;
509                }
510
511
512                // weight with projected coordinate to reach similar kernel size for near and far
513                const float2 texcoord = IN.texCoord.xy + offset * radius;
514
515                const float4 sampleColor = tex2Dlod(colors, float4(texcoord, .0f, .0f));
516                const float3 oldSamplePos = sampleColor.xyz;
517                const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
518               
519               
520
521                ////////////////
522                //-- compute contribution of sample using the direction and angle
523
524                float3 dirSample = samplePos - centerPosition;
525
526                const float minDist = 1e-6f;
527                const float eps = 1e-3f;
528
529                const float lengthToSample = length(dirSample);
530                const float sampleWeight = 1.0f / max(lengthToSample, eps);
531
532                dirSample /= max(length(dirSample), minDist); // normalize
533
534                // angle between current normal and direction to sample controls AO intensity.
535                const float cosAngle = dot(dirSample, normal);
536
537                //const float aoContrib = sampleIntensity / sqrLen;
538                const float aoContrib = sampleIntensity * sampleWeight;
539                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
540
541                total_ao += max(cosAngle, .0f) * aoContrib;
542                ++ numSamples;
543
544
545//#ifdef PERFORMANCE_TEST
546#if 1
547                // check if the samples have been valid in the last frame
548                // only mark sample as invalid if in the last / current frame
549                // they possibly have any influence on the ao
550
551                if (cosAngle >= 0)
552                {
553                        const float oldDistance = length(oldSamplePos - oldPos);
554                        const float distanceDiff = abs(oldDistance - lengthToSample);
555
556                        float pixelValid = (distanceDiff > 1e-3f) ? 100.0f : 0;
557
558                        validSamples = max(validSamples, pixelValid);
559                }
560#ifdef USE_GTX
561                // we can bail out early and use a minimal #samples)
562                // if some conditions are met as long as the hardware supports it
563                if (numSamples >= MIN_SAMPLES)
564                {
565                        //break;
566                        // if the pixel belongs to a static object and all the samples stay valid in the current frame
567                        if (!isMovingObject && (validSamples < 1.0f) && (convergence > NUM_SAMPLES)) break;
568                        // if the pixel belongs to a dynamic object but the #accumulated samples for this pixel is sufficiently high
569                        // (=> there was no discontinuity recently)
570                        //else if (isMovingObject && (convergence > SSAO_CONVERGENCE_THRESHOLD)) break;
571                        else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break;
572                }
573#endif
574
575#endif // PERFORMANCE_TEST
576        }
577
578        // "normalize" ao contribution
579        total_ao /= numSamples;
580
581#if 1
582        // if surface normal perpenticular to view dir, approx. half of the samples will not count
583        // => compensate for this (on the other hand, projected sampling area could be larger!)
584        const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
585        total_ao *= viewCorrection;
586#endif
587
588        //return float3(total_ao, validSamples, numSamples);
589        return float3(min(1.0f, total_ao), validSamples, numSamples);
590}
591#endif
592
593
594/** The mrt shader for screen space ambient occlusion
595*/
596pixel2 main(fragment IN,
597                        uniform sampler2D colors,
598                        uniform sampler2D normals,
599                        uniform sampler2D noiseTex,
600                        uniform sampler2D samples,
601                        uniform sampler2D oldTex,
602                        uniform float4x4 modelViewProj,
603                        uniform float4x4 oldModelViewProj,
604                        uniform float temporalCoherence,
605                        uniform float3 bl,
606                        uniform float3 br,
607                        uniform float3 tl,
608                        uniform float3 tr,
609                        uniform float3 oldEyePos,
610                        uniform float3 oldbl,
611                        uniform float3 oldbr,
612                        uniform float3 oldtl,
613                        uniform float3 oldtr,
614                        uniform sampler2D attribsTex,
615                        uniform float kernelRadius,
616                        uniform float sampleIntensity
617                        )
618{
619        pixel2 OUT;
620
621        //const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
622        const float3 normal = tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz;
623
624        // reconstruct position from the eye space depth
625        const float3 viewDir = IN.view;
626        const float4 mycolor = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
627        const float eyeSpaceDepth = mycolor.w;
628        const float4 eyeSpacePos = float4(-viewDir * eyeSpaceDepth, 1.0f);
629
630
631        ////////////////
632        //-- calculcate the current projected posiion (also used for next frame)
633       
634        float4 projPos = mul(modelViewProj, eyeSpacePos);
635        const float invw = 1.0f / projPos.w;
636        projPos *= invw;
637       
638        //const float radiusMult = kernelRadius;
639        //const float radiusMult = 3e-2;
640        const float radiusMult = kernelRadius * invw;
641       
642#ifdef PERFORMANCE_TEST
643
644        float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
645
646        const float sqrMoveSpeed = SqrLen(diffVec);
647        const bool isMovingObject = (sqrMoveSpeed > DYNAMIC_OBJECTS_THRESHOLD);
648
649       
650        /////////////////
651        //-- compute temporal reprojection
652
653        float3 temporalVals = Reproject(eyeSpacePos, eyeSpaceDepth, IN.texCoord, oldEyePos,
654                                        oldTex, oldModelViewProj,
655                                                                        colors,
656                                                                        projPos.xyz,
657                                                                        invw,
658                                                                        oldbl, oldbr, oldtl, oldtr,
659                                                                        diffVec
660                                                                        );
661       
662        const float oldSsao = temporalVals.x;
663       
664        float oldWeight = temporalVals.y;
665        float oldIdx = temporalCoherence > 1 ? temporalVals.z : 0;
666        //float oldIdx = temporalVals.z;
667       
668#else
669
670        const float3 diffVec      = float3(.0f);
671        const bool isMovingObject = false;
672        const float oldSsao       = .0f;
673       
674        float oldWeight = .0f;
675        float oldIdx    = .0f;
676       
677#endif
678
679        float3 ao;
680
681        // cull background note: this should be done with the stencil buffer
682        if (eyeSpaceDepth < DEPTH_THRESHOLD)
683        {
684                if (1)
685                {
686#ifdef TRYOUT
687                        ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz,
688                                      radiusMult, bl, br, tl, tr, normalize(viewDir),
689                                          oldWeight, sampleIntensity, isMovingObject, oldIdx);
690#else
691
692                          ao = ssao(IN, colors, noiseTex, samples,
693                                      normal, eyeSpacePos.xyz, radiusMult, bl,
694                                          br, tl, tr, normalize(viewDir),
695                                          oldWeight, sampleIntensity, isMovingObject, oldIdx,
696                                          attribsTex, mycolor.xyz);
697#endif                           
698                }
699                else
700                {
701                        /*ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, radiusMult,
702                                   bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity,
703                                           isMovingObject, normals, oldIdx);
704                                           */
705                }
706        }
707        else
708        {
709                 ao = float3(1.0f);
710        }
711
712
713#ifdef PERFORMANCE_TEST
714
715        ///////////
716        //-- check if we have to reset pixel because one of the sample points was invalid
717        //-- only do this if the current pixel does not belong to a moving object
718
719        // the weight equals the number of sampled shot in this pass
720        const float newWeight = ao.z;
721        // completely reset the ao in this pixel
722        const float completelyResetThres = 20.0f;
723        // don't fully reset the ao in this pixel, but give low weight to old solution
724        const float partlyResetThres = 1.0f;
725       
726        // don't check for moving objects, otherwise almost no coherence
727        if (1)//!isMovingObject)
728        {
729                if (ao.y > completelyResetThres)
730                {
731                        oldWeight = .0f;
732                        oldIdx    = .0f;
733                }
734                else if (ao.y > partlyResetThres)
735                {
736                        const float factor = 4.0f;
737                        //if (oldIdx >= factor * newWeight) oldIdx = 0;
738                        //oldWeight = min(oldWeight, factor * newWeight);
739                        oldWeight = oldIdx = .0f;
740                }
741        }
742
743
744        //////////
745        //-- blend ao between old and new samples (and avoid division by zero)
746
747        OUT.illum_col.x = ao.x * newWeight + oldSsao * oldWeight;
748        OUT.illum_col.x /= (newWeight + oldWeight);
749        //OUT.illum_col.x = clamp(OUT.illum_col.x, 0, 1);
750        //OUT.illum_col.x = ao.y;
751
752        // the new weight for the next frame
753        const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence);
754
755        OUT.illum_col.y = combinedWeight;
756        OUT.illum_col.z = oldIdx + newWeight; // the new index
757        OUT.illum_col.w = eyeSpaceDepth;
758
759        // this value can be used to check if this pixel belongs to a moving object
760        OUT.col.x = SqrLen(diffVec);
761        //OUT.illum_col.z = SqrLen(diffVec);
762
763#else
764
765        OUT.illum_col.x = ao.x;
766        OUT.illum_col.w = eyeSpaceDepth;
767       
768#endif
769
770        return OUT;
771}
Note: See TracBrowser for help on using the repository browser.