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

Revision 3212, 7.3 KB checked in by mattausch, 16 years ago (diff)

made ssao parameters more flexible
started to implement lense flare

RevLine 
[3109]1#include "../shaderenv.h"
[3163]2#include "common.h"
[3109]3
4
[3163]5/*************************************************/
6/*     Filter for combining ssao with image      */
7/*************************************************/
[3148]8
9
[3109]10struct fragment
11{
12        float2 texCoord: TEXCOORD0;
13        float3 view: TEXCOORD1;
14};
15
16
17struct pixel
18{
19        float4 illum_col: COLOR0;
20};
21
22
[3148]23/** Filter taking into account depth and normal differences,
[3163]24    and convergence of a sample 
[3148]25*/
[3123]26float DiscontinuityFilter(float2 texCoord,
27                                                  float4 ao,
[3137]28                                                  float4 color,
[3123]29                                                  uniform sampler2D ssaoTex,
30                                                  uniform sampler2D normalsTex,
[3137]31                                                  uniform sampler2D colorsTex,
[3167]32                                                  uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
33                                                  uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
[3163]34                                                  float scale
35                                                  )
[3109]36{
37        float average = .0f;
38        float total_w = .0f;
39
[3142]40        const float eyeSpaceDepth = color.w;
[3134]41
[3167]42        const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
[3109]43
44        float4 aoSample;
45        float3 sampleNorm;
[3134]46        float3 samplePos;
[3109]47        float w;
[3134]48        float4 sampleTexCoord;
[3120]49        float depthFactor;
[3123]50        float normalFactor;
[3134]51        float convergenceFactor;
[3137]52        float sampleDepth;
[3109]53
[3167]54        for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
[3109]55        {
[3134]56                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
[3137]57
[3134]58                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
[3167]59                sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
[3109]60
[3148]61                // check depth discontinuity
[3142]62                sampleDepth = tex2Dlod(colorsTex, sampleTexCoord).w;
63                //sampleDepth = aoSample.w;
[3148]64               
[3162]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);
[3195]70                convergenceFactor = aoSample.y + 1.0f;
[3109]71
[3148]72                // combine the weights
[3162]73                w = filterWeights[i] * convergenceFactor * depthFactor * normalFactor;
74                //w = normalFactor * convergenceFactor;
[3123]75
[3134]76                average += aoSample.x * w;
[3109]77                total_w += w;
78        }
79
[3162]80        average /= max(total_w, 1e-6f);
[3109]81
[3155]82        return saturate(average);
[3109]83}
84
[3155]85
[3163]86
87/** Filter taking into account depth and normal differences,
88   and convergence of a sample 
89*/
90float DiscontinuityFilter2(float2 texCoord,
91                                                   float4 ao,
92                                                   float4 color,
93                                                   uniform sampler2D ssaoTex,
94                                                   uniform sampler2D normalsTex,
95                                                   uniform sampler2D colorsTex,
[3167]96                                                   uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
[3163]97                                                   float scale,
98                                                   float3 bl,
99                                                   float3 br,
100                                                   float3 tl,
101                                                   float3 tr)
102{
103        float average = .0f;
104        float total_w = .0f;
105
[3193]106        //const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
107        const float3 centerPos = ReconstructSamplePos(colorsTex, texCoord, bl, br, tl, tr);
[3167]108        const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
[3163]109
110        float4 aoSample;
111        float3 sampleNorm;
112        float3 samplePos;
113        float w;
114        float4 sampleTexCoord;
115        float spatialFactor;
116        float normalFactor;
117        float convergenceFactor;
[3167]118        float len;
[3163]119
[3192]120        const float convergenceThresh = 200.0f;
121
[3167]122        for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
[3163]123        {
[3198]124                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, .0f, .0f);
[3163]125
126                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
[3212]127               
[3163]128                // check spatial discontinuity
[3198]129                // note: using the depth from the color texture is not 100% correct as depth was
[3193]130                // not scaled with the interpolated view vector depth yet ...
131                samplePos = ReconstructSamplePos(colorsTex, sampleTexCoord.xy, bl, br, tl, tr);
132                //samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
133
[3195]134                len = min(SqrLen(centerPos - samplePos), 1e2f);
[3163]135                spatialFactor = 1.0f / max(len, 1e-3f);
136
[3195]137                convergenceFactor = aoSample.y + 1.0f;
[3212]138
139                //sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
140                //normalFactor = max(step(.2f, dot(sampleNorm, centerNormal)), 1e-2f);
141
[3163]142                // combine the weights
[3195]143                w = convergenceFactor * convergenceFactor * spatialFactor;// * normalFactor;
[3198]144               
[3163]145                average += aoSample.x * w;
146                total_w += w;
147        }
148
149        average /= max(total_w, 1e-6f);
150
151        return saturate(average);
152}
153
154
[3148]155/** Function combining image and indirect illumination buffer using a
156        depth and normal aware discontinuity filter.
157*/
[3109]158pixel combine(fragment IN,
159                          uniform sampler2D colorsTex,
160                          uniform sampler2D ssaoTex,
161                          uniform sampler2D normalsTex,
[3167]162                          uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
163                          uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
[3163]164                          uniform float4x4 modelViewProj,
165                          uniform float3 bl,
166                          uniform float3 br,
167                          uniform float3 tl,
[3205]168                          uniform float3 tr,
[3206]169                          uniform float w,
170                          uniform float h
[3148]171                          )
[3109]172{
173        pixel OUT;
174
175        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
[3204]176        float4 ao =  tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
[3109]177
[3198]178        // get the minimum convergence by exactly sampling the 4 surrounding
179        // texels in the old texture, otherwise flickering because convergence
180        // will be interpolated when upsampling and filter size does not match!
181        float4 texelCenterConv;
182        const float xoffs = .5f / w; const float yoffs = .5f / h;
[3195]183
[3198]184        // get position exactly between old texel centers
185        float2 center;
[3204]186        center.x = (floor(IN.texCoord.x * w - .5f) + 1.0f) / w;
187        center.y = (floor(IN.texCoord.y * h - .5f) + 1.0f) / h;
[3205]188
[3204]189        texelCenterConv.x = tex2Dlod(ssaoTex, float4(center + float2( xoffs,  yoffs), 0, 0)).y;
190        texelCenterConv.y = tex2Dlod(ssaoTex, float4(center + float2( xoffs, -yoffs), 0, 0)).y;
191        texelCenterConv.z = tex2Dlod(ssaoTex, float4(center + float2(-xoffs, -yoffs), 0, 0)).y;
192        texelCenterConv.w = tex2Dlod(ssaoTex, float4(center + float2(-xoffs,  yoffs), 0, 0)).y;
[3196]193
[3198]194        const float m1 = min(texelCenterConv.x, texelCenterConv.y);
195        const float m2 = min(texelCenterConv.z, texelCenterConv.w);
196
[3203]197        const float minConvergence = min(m1, m2);
198
[3204]199        const float convergence = minConvergence;
200        //const float convergence = 0;
[3195]201        //const float convergence = ao.y;
202
[3206]203        // filter reaches size 1 pixel when sample size reaches threshold
[3204]204        // afterwards we do not use the filter anymore
[3195]205
[3167]206        // filter up to a certain convergance value and leave out background (sky) by checking depth
[3206]207        if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (col.w < 1e10f))
[3164]208        {
209                const float distanceScale = 1.0f;
[3161]210
[3203]211                // descend to zero filter size after reaching thres pixels
[3206]212                const float convergenceWeight = SSAO_CONVERGENCE_THRESHOLD / (SSAO_FILTER_WIDTH - 1.0f);
[3195]213                const float convergenceScale = convergenceWeight / (convergence + convergenceWeight);
214                const float scale = SSAO_FILTER_WIDTH * convergenceScale * distanceScale;
[3160]215
[3167]216                // the filtered ssao value
[3198]217                ao.x = DiscontinuityFilter2(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
[3120]218        }
219
[3192]220        if (col.w < 1e10f)
[3209]221                OUT.illum_col.xyz = col.xyz * max(1e-1f, 1.0f - ao.x);
[3192]222        else
223                OUT.illum_col.xyz = col.xyz;
224
[3198]225        //OUT.illum_col.xyz = float3(ao.x, ao.x, step(thres, convergence));
[3205]226        //OUT.illum_col.xyz = float3(0, convergence, 0);
[3204]227        //OUT.illum_col.xyz = float3(abs(center.x - IN.texCoord.x) * 16.0f, abs(center.y - IN.texCoord.y) * 12.0f, 0);
228       
[3197]229        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-3f, 0, 1), 1);
[3195]230        //OUT.illum_col.xyz = float3(0, 1.0f - step(0.5f + NUM_SAMPLES, convergence), 1);
[3109]231        OUT.illum_col.w = col.w;
232
233        return OUT;
[3198]234}
Note: See TracBrowser for help on using the repository browser.