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

Revision 3134, 8.0 KB checked in by mattausch, 16 years ago (diff)

filtering working more nicely now

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
18
19
20inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
21{
22        float3 x1 = lerp(bl, tl, w.y);
23        float3 x2 = lerp(br, tr, w.y);
24        float3 v = lerp(x1, x2, w.x);
25
26        return v;
27}
28
29
30// reconstruct world space position
31inline float3 ReconstructSamplePos(uniform sampler2D tex,
32                                                                   float2 texcoord,
33                                                                   float3 bl, float3 br, float3 tl, float3 tr)
34{
35        const float eyeSpaceDepth = tex2Dlod(tex, float4(texcoord, 0, 0)).w;
36       
37        float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
38        float3 samplePos = -viewVec * eyeSpaceDepth;
39
40        return samplePos;
41}
42
43
44float Filter(float2 texCoord,
45                         uniform sampler2D ssaoTex,
46                         uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
47                         uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
48                         float scale)
49{
50        float average = .0f;
51        float w = .0f;
52
53        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
54        {       
55                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale, 0, 0)).x;
56                w += filterWeights[i];
57        }
58
59        average *= 1.0f / (float)w;
60
61        return average;
62}
63
64#define USE_POSITION
65
66float DiscontinuityFilter(float2 texCoord,
67                                                  float4 ao,
68                                                  uniform sampler2D ssaoTex,
69                                                  uniform sampler2D normalsTex,
70                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
71                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
72                                                  float scale,
73                                                  float3 bl,
74                                                  float3 br,
75                                                  float3 tl,
76                                                  float3 tr)
77{
78        float average = .0f;
79        float total_w = .0f;
80
81#ifdef USE_POSITION
82        const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
83#else
84        const float eyeSpaceDepth = ao.w;
85#endif
86
87        const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
88
89        float4 aoSample;
90        float3 sampleNorm;
91        float3 samplePos;
92        float w;
93        float4 sampleTexCoord;
94        float depthFactor;
95        float normalFactor;
96        float convergenceFactor;
97
98        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
99        {
100                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
101                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
102                sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
103
104#ifdef USE_POSITION
105                samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
106                depthFactor = max(step(1.0f - 5e-1f, 1.0f - length(samplePos - centerPos)), 1e-3f);
107#else // use depth
108                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
109#endif
110                normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
111                convergenceFactor = min(60.0f, aoSample.y) * 0.01f;//max(step(18.5f, aoSample.y), 1e-3f);
112
113                w = filterWeights[i] * normalFactor * depthFactor * convergenceFactor;
114                //w = filterWeights[i] * normalFactor * converganceFactor;
115                //w = filterWeights[i] * normalFactor * depthFactor;
116
117                average += aoSample.x * w;
118                total_w += w;
119        }
120
121        average *= 1.0f / max(total_w, 1e-6f);
122
123        return average;
124}
125
126
127pixel combine(fragment IN,
128                          uniform sampler2D colorsTex,
129                          uniform sampler2D ssaoTex,
130                          uniform sampler2D normalsTex,
131                          uniform sampler2D offsetTex,
132                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
133                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
134                          uniform float3 bl,
135                          uniform float3 br,
136                          uniform  float3 tl,
137                          uniform float3 tr)
138{
139        pixel OUT;
140
141        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
142        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
143
144        //if ((ao.y < 60.0f) && (col.w < 1e10f))
145        if (col.w < 1e10f)
146        {
147                const float xoffs = 20.0f / 1024.0f;
148                const float yoffs = 20.0f / 768.0f;
149
150                /*float3 b2 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
151                float3 b3 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
152                float3 b4 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
153                float3 b5 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
154
155                float border = step(0.001f,  b2
156                //float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
157*/
158               
159                /*float3 x2 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
160                float3 x3 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
161                float3 x4 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
162                float3 x5 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
163
164                //ao.x = (ao.x + x2 + x3 + x4 + x5) * 0.2f;
165                if (ao.y < 1.5f)
166                ao.x = (x2 + x3 + x4 + x5) * 0.25f;
167
168*/
169                const static float scaleFactor = 1.0f;
170                //const static float scaleFactor = 10.0f;
171                const static float adaptFactor = 50000.0f;
172
173                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
174                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
175                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y), 0);
176                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor  / (adaptFactor + ao.y), 0);
177
178                if (ao.y < 60.5f) ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor, bl, br, tl, tr);
179                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1, 0);
180        }
181
182        //float dummy = clamp(1.0f - ao.y * 1e-2f, 0, 1);
183        //float dummy2 = ao.y < 1.5f ? 1.0f : .0f;
184        OUT.illum_col.xyz = col.xyz * ao.x;
185
186        //      if (ao.y < 1.5f) OUT.illum_col.xyz = float3(10000, 10000, 0);
187        //      else if (ao.y < 3.5f) OUT.illum_col.xyz = float3(10000, 0, 10000);
188        //      else
189        //      if (ao.y < 10.5f) OUT.illum_col.xyz = float3(0, 10000, 0);
190        //OUT.illum_col = float4(dummy3, dummy3, dummy3, col.w);
191        OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
192
193        //OUT.illum_col.xyz = float3(1.0f - ao.x, dummy2, 0);//dummy2);
194        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
195        OUT.illum_col.w = col.w;
196
197        return OUT;
198}
199
200
201float DiscontinuityFilter2(float2 texCoord,
202                                                  float4 ao,
203                                                  uniform sampler2D ssaoTex,
204                                                  uniform sampler2D normalsTex,
205                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
206                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
207                                                  float scale,
208                                                  int index)
209{
210        float average = .0f;
211        float total_w = .0f;
212
213        const float eyeSpaceDepth = ao.w;
214        const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
215
216        float4 aoSample;
217        float3 sampleNorm;
218        float w;
219        float4 offs;
220        float depthFactor;
221        float normalFactor;
222
223        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
224        {
225                offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
226                aoSample = tex2Dlod(ssaoTex, offs);
227               
228                sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
229
230                //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
231                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
232                //normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
233
234                //w = filterWeights[i] * normalFactor * depthFactor;
235                //w = normalFactor * depthFactor;
236                w = depthFactor;
237
238                average += aoSample.y * w;
239                total_w += w;
240        }
241
242        average *= 1.0f / max(total_w, 1e-3f);
243
244        return average;
245}
246
247pixel smoothSsao(fragment IN,
248                                 uniform sampler2D ssaoTex,
249                                 uniform sampler2D normalsTex,
250                                 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
251                                 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
252                          )
253{
254        pixel OUT;
255
256        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
257
258        //const static float scaleFactor = 20.0f;
259        const static float scaleFactor = 10.0f;
260        //ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 0.5f, 1);
261        //ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor, 1);
262       
263        OUT.illum_col = ao;
264
265        return OUT;
266}
Note: See TracBrowser for help on using the repository browser.