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

Revision 3130, 5.5 KB checked in by mattausch, 16 years ago (diff)

worked on reducing flickering
try: use box filter instead of poisson kernel for blurring + for blurring temporal coherence values
try: use full resolution offset texture or try linear interpol, otherwise strange effects

Line 
1#include "../shaderenv.h"
2
3
4struct fragment
5{
6        float2 texCoord: TEXCOORD0;
7        float3 view: TEXCOORD1;
8};
9
10
11struct pixel
12{
13        float4 illum_col: COLOR0;
14};
15
16
17
18float Filter(float2 texCoord,
19                         uniform sampler2D ssaoTex,
20                         uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
21                         uniform float filterWeights[NUM_SSAO_FILTERSAMPLES])
22{
23        float average = .0f;
24        float w = .0f;
25
26        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
27        {
28                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x;
29                w += filterWeights[i];
30        }
31
32        average *= 1.0f / (float)w;
33
34        return average;
35}
36
37
38float Filter(float2 texCoord,
39                         uniform sampler2D ssaoTex,
40                         uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
41                         uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
42                         float scale)
43{
44        float average = .0f;
45        float w = .0f;
46
47        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
48        {       
49                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale, 0, 0)).x;
50                w += filterWeights[i];
51        }
52
53        average *= 1.0f / (float)w;
54
55        return average;
56}
57
58
59float DiscontinuityFilter(float2 texCoord,
60                                                  float4 ao,
61                                                  uniform sampler2D ssaoTex,
62                                                  uniform sampler2D normalsTex,
63                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
64                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
65                                                  float scale,
66                                                  int index)
67{
68        float average = .0f;
69        float total_w = .0f;
70
71        const float eyeSpaceDepth = ao.w;
72        const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
73
74        float4 aoSample;
75        float3 sampleNorm;
76        float w;
77        float4 offs;
78        float depthFactor;
79        float normalFactor;
80
81        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
82        {
83                offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
84                aoSample = tex2Dlod(ssaoTex, offs);
85               
86                sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
87
88                //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
89                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
90                normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
91
92                w = filterWeights[i] * normalFactor * depthFactor;
93                //w = filterWeights[i] * depthFactor;
94
95                average += aoSample[index] * w;
96                total_w += w;
97        }
98
99        average *= 1.0f / max(total_w, 1e-6f);
100
101        return average;
102}
103
104
105pixel combine(fragment IN,
106                          uniform sampler2D colorsTex,
107                          uniform sampler2D ssaoTex,
108                          uniform sampler2D normalsTex,
109                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
110                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
111                          )
112{
113        pixel OUT;
114
115        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
116        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
117
118        //if ((ao.y < 60.0f) && (col.w < 1e10f))
119        if (col.w < 1e10f)
120        {
121                const static float scaleFactor = 1.0f;
122                //const static float scaleFactor = 10.0f;
123                const static float adaptFactor = 50.0f;
124
125                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
126                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
127                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y), 0);
128                ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor  / (adaptFactor + ao.y), 0);
129        }
130
131        //OUT.illum_col.xyz = col.xyz * ao.x;
132        //OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
133        OUT.illum_col.xyz = float3(1.0f - ao.x, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
134        OUT.illum_col.w = col.w;
135
136        return OUT;
137}
138
139float DiscontinuityFilter2(float2 texCoord,
140                                                  float4 ao,
141                                                  uniform sampler2D ssaoTex,
142                                                  uniform sampler2D normalsTex,
143                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
144                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
145                                                  float scale,
146                                                  int index)
147{
148        float average = .0f;
149        float total_w = .0f;
150
151        const float eyeSpaceDepth = ao.w;
152        const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
153
154        float4 aoSample;
155        float3 sampleNorm;
156        float w;
157        float4 offs;
158        float depthFactor;
159        float normalFactor;
160
161        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
162        {
163                offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
164                aoSample = tex2Dlod(ssaoTex, offs);
165               
166                sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
167
168                //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
169                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
170                normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
171
172                //w = filterWeights[i] * normalFactor * depthFactor;
173                w = normalFactor * depthFactor;
174
175                average += aoSample.y * w;
176                total_w += w;
177        }
178
179        average *= 1.0f / max(total_w, 1e-3f);
180
181        return average;
182}
183
184pixel smoothSsao(fragment IN,
185                                 uniform sampler2D ssaoTex,
186                                 uniform sampler2D normalsTex,
187                                 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
188                                 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
189                          )
190{
191        pixel OUT;
192
193        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
194
195        //const static float scaleFactor = 20.0f;
196        const static float scaleFactor = 10.0f;
197        ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor * ao.z, 1);
198        //ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor, 1);
199       
200        OUT.illum_col = ao;
201
202        return OUT;
203}
Note: See TracBrowser for help on using the repository browser.