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

Revision 3162, 3.8 KB checked in by mattausch, 16 years ago (diff)

playing around with filter: returning to world space depth difference for filter kernel??

Line 
1#include "../shaderenv.h"
2
3
4/******************************************/
5/* Filter for combining ssao with image   */
6/******************************************/
7
8
9struct fragment
10{
11        float2 texCoord: TEXCOORD0;
12        float3 view: TEXCOORD1;
13};
14
15
16struct pixel
17{
18        float4 illum_col: COLOR0;
19};
20
21
22/** Filter taking into account depth and normal differences,
23   and convergence of a sample 
24*/
25float DiscontinuityFilter(float2 texCoord,
26                                                  float4 ao,
27                                                  float4 color,
28                                                  uniform sampler2D ssaoTex,
29                                                  uniform sampler2D normalsTex,
30                                                  uniform sampler2D colorsTex,
31                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
32                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
33                                                  float scale)
34{
35        float average = .0f;
36        float total_w = .0f;
37
38        const float eyeSpaceDepth = color.w;
39
40        const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
41        //const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
42
43        float4 aoSample;
44        float3 sampleNorm;
45        float3 samplePos;
46        float w;
47        float4 sampleTexCoord;
48        float depthFactor;
49        float normalFactor;
50        float convergenceFactor;
51        float sampleDepth;
52
53        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
54        {
55                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
56
57                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
58                sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
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/** Function combining image and indirect illumination buffer using a
87        depth and normal aware discontinuity filter.
88*/
89pixel combine(fragment IN,
90                          uniform sampler2D colorsTex,
91                          uniform sampler2D ssaoTex,
92                          uniform sampler2D normalsTex,
93                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
94                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
95                          uniform float4x4 modelViewProj
96                          )
97{
98        pixel OUT;
99
100        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
101        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
102
103        // reconstruct position from the eye space depth
104        /*const float4 worldPos = float4(-IN.view * eyeSpaceDepth, 1.0f);
105        // compute w factor from projection in order to control filter size
106        const float4 projPos = mul(modelViewProj, worldPos);
107        const float distanceScale = 1.0f / projPos.w;
108*/
109
110        const float eyeSpaceDepth = col.w;
111
112        const float scaleFactor = 2.0f;
113        const float distanceScale = scaleFactor / (eyeSpaceDepth + scaleFactor);
114
115        const float convergence = ao.y;
116
117        const float adaptFactor = 5.0f;
118        const float convergenceScale = adaptFactor / (convergence + adaptFactor);
119
120
121        if ((ao.y < 100.0f) && (col.w < 1e10f))
122        //if (col.w < 1e10f)
123        {
124                ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, filterWeights, convergenceScale * distanceScale);
125        }
126
127        //OUT.illum_col.xyz = col.xyz * ao.x;
128        OUT.illum_col.xyz = float3(ao.x, ao.x, ao.x);
129        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
130        OUT.illum_col.w = col.w;
131
132        return OUT;
133}
Note: See TracBrowser for help on using the repository browser.