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

Revision 3133, 7.3 KB checked in by mattausch, 16 years ago (diff)
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        float converganceFactor;
81
82        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
83        {
84                offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
85                aoSample = tex2Dlod(ssaoTex, offs);
86               
87                sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
88
89                //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
90                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
91                normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
92                converganceFactor = max(step(8.5f, aoSample.y), 1e-3f);
93
94                //w = filterWeights[i] * normalFactor * depthFactor * converganceFactor;
95                w = filterWeights[i];// * normalFactor * converganceFactor;
96                //w = filterWeights[i] * depthFactor;
97
98                average += aoSample[index] * w;
99                total_w += w;
100        }
101
102        average *= 1.0f / max(total_w, 1e-6f);
103
104        return average;
105}
106
107
108pixel combine(fragment IN,
109                          uniform sampler2D colorsTex,
110                          uniform sampler2D ssaoTex,
111                          uniform sampler2D normalsTex,
112                          uniform sampler2D offsetTex,
113                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
114                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
115                          )
116{
117        pixel OUT;
118
119        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
120        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
121
122        //if ((ao.y < 60.0f) && (col.w < 1e10f))
123        if (col.w < 1e10f)
124        {
125                const float xoffs = 20.0f / 1024.0f;
126                const float yoffs = 20.0f / 768.0f;
127
128                /*float3 b2 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
129                float3 b3 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
130                float3 b4 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
131                float3 b5 = tex2Dlod(offsetTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
132
133                float border = step(0.001f,  b2
134
135                //float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
136*/
137               
138                /*float3 x2 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz;
139                float3 x3 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz;
140                float3 x4 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz;
141                float3 x5 = tex2Dlod(ssaoTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz;
142
143                //ao.x = (ao.x + x2 + x3 + x4 + x5) * 0.2f;
144                if (ao.y < 1.5f)
145                ao.x = (x2 + x3 + x4 + x5) * 0.25f;
146
147*/
148                const static float scaleFactor = 1.0f;
149                //const static float scaleFactor = 10.0f;
150                const static float adaptFactor = 50000.0f;
151
152                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
153                //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
154                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y), 0);
155                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor  / (adaptFactor + ao.y), 0);
156
157                if (ao.y < 10.5f) ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1, 0);
158                //ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1, 0);
159        }
160
161        //float dummy = clamp(1.0f - ao.y * 1e-2f, 0, 1);
162        //float dummy2 = ao.y < 1.5f ? 1.0f : .0f;
163        OUT.illum_col.xyz = col.xyz * ao.x;
164
165//      if (ao.y < 1.5f) OUT.illum_col.xyz = float3(10000, 10000, 0);
166//      else if (ao.y < 3.5f) OUT.illum_col.xyz = float3(10000, 0, 10000);
167//      else
168        //      if (ao.y < 10.5f) OUT.illum_col.xyz = float3(0, 10000, 0);
169        //OUT.illum_col = float4(dummy3, dummy3, dummy3, col.w);
170        OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
171
172        //OUT.illum_col.xyz = float3(1.0f - ao.x, dummy2, 0);//dummy2);
173        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
174        OUT.illum_col.w = col.w;
175
176        return OUT;
177}
178
179
180float DiscontinuityFilter2(float2 texCoord,
181                                                  float4 ao,
182                                                  uniform sampler2D ssaoTex,
183                                                  uniform sampler2D normalsTex,
184                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
185                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
186                                                  float scale,
187                                                  int index)
188{
189        float average = .0f;
190        float total_w = .0f;
191
192        const float eyeSpaceDepth = ao.w;
193        const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
194
195        float4 aoSample;
196        float3 sampleNorm;
197        float w;
198        float4 offs;
199        float depthFactor;
200        float normalFactor;
201
202        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
203        {
204                offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
205                aoSample = tex2Dlod(ssaoTex, offs);
206               
207                sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
208
209                //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
210                depthFactor = max(step(5e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
211                //normalFactor = max(step(0.6f, dot(sampleNorm, norm)), 1e-3f);
212
213                //w = filterWeights[i] * normalFactor * depthFactor;
214                //w = normalFactor * depthFactor;
215                w = depthFactor;
216
217                average += aoSample.y * w;
218                total_w += w;
219        }
220
221        average *= 1.0f / max(total_w, 1e-3f);
222
223        return average;
224}
225
226pixel smoothSsao(fragment IN,
227                                 uniform sampler2D ssaoTex,
228                                 uniform sampler2D normalsTex,
229                                 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
230                                 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
231                          )
232{
233        pixel OUT;
234
235        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
236
237        //const static float scaleFactor = 20.0f;
238        const static float scaleFactor = 10.0f;
239        //ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 0.5f, 1);
240        //ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor, 1);
241       
242        OUT.illum_col = ao;
243
244        return OUT;
245}
Note: See TracBrowser for help on using the repository browser.