source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg @ 3304

Revision 3304, 4.7 KB checked in by mattausch, 15 years ago (diff)

tried out scaling vs reduction of samples

Line 
1#include "../shaderenv.h"
2#include "common.h"
3
4
5/*************************************************/
6/*     Filter for combining ssao with image      */
7/*************************************************/
8
9
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
23float2 FilterSample(float4 sampleTexCoord,
24                                        uniform sampler2D ssaoTex,
25                                        uniform sampler2D colorsTex,
26                                        float3 centerPos,
27                                        float3 bl,
28                                        float3 br,
29                                        float3 tl,
30                                        float3 tr)
31{
32        float2 aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
33               
34        // check spatial discontinuity
35        // note: using the depth from the color texture is not 100% correct as depth was
36        // not scaled with the interpolated view vector depth yet ...
37        float3 samplePos = ReconstructSamplePos(colorsTex, sampleTexCoord.xy, bl, br, tl, tr);
38        //samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
39
40        float len = min(SqrLen(centerPos - samplePos), 1e2f);
41        float spatialFactor = 1.0f / max(len, 1e-3f);
42
43        float convergenceFactor = aoSample.y + 1.0f;
44
45        // combine the weights
46        float w = convergenceFactor * convergenceFactor * spatialFactor;// * normalFactor;
47
48        float average = aoSample.x * w;
49
50        return float2(average, w);
51}
52
53
54/** Filter taking into account depth, normal discontinuities
55   and ssao convergence of a sample (the higher the more reliably
56   has the sample a correct ssao value)
57*/
58float FilterXY(float2 texCoord,
59                           uniform sampler2D ssaoTex,
60                           uniform sampler2D colorsTex,
61                           float3 bl,
62                           float3 br,
63                           float3 tl,
64                           float3 tr,
65                           float2 xyStep,
66                           float convergence)
67{
68        float2 result = float2(0.0f, 0.0f);
69       
70        const float3 centerPos = ReconstructSamplePos(colorsTex, texCoord, bl, br, tl, tr);
71
72        const float scale = saturate((SSAO_CONVERGENCE_THRESHOLD - convergence) / SSAO_CONVERGENCE_THRESHOLD);
73        //const int radius = SSAO_FILTER_RADIUS * saturate((SSAO_CONVERGENCE_THRESHOLD - convergence) / SSAO_CONVERGENCE_THRESHOLD);
74       
75        //for (int i = -radius; i <= radius; ++ i)
76        for (int i = -SSAO_FILTER_RADIUS; i <= SSAO_FILTER_RADIUS; ++ i)
77        {
78                float4 sampleTexCoord = float4(texCoord + i * xyStep * scale, .0f, .0f);
79                result += FilterSample(sampleTexCoord, ssaoTex, colorsTex, centerPos, bl, br, tl, tr);
80        }
81
82        result.x /= max(result.y, 1e-6f);
83
84        return saturate(result.x);
85}
86
87/** In between step that only filters in one direction
88*/
89pixel FilterSsaoFullRes(fragment IN,
90                                                uniform sampler2D colorsTex,
91                                                uniform sampler2D ssaoTex,
92                                                uniform float3 bl,
93                                                uniform float3 br,
94                                                uniform float3 tl,
95                                                uniform float3 tr,
96                                                uniform float2 xyStep
97                                                 )
98{
99        pixel OUT;
100
101        const float depth = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0)).w;
102
103        OUT.illum_col = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
104        // just take unfiltered convergence in current pixel
105        const float convergence = OUT.illum_col.y;
106
107        // filter reaches size 1 pixel when sample size reaches threshold
108        // afterwards we do not use the filter anymore
109
110        // filter up to a certain convergance value and leave out background (sky) by checking depth
111        if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
112        {
113                // the filtered ssao value
114                OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep, convergence);
115        }
116
117        return OUT;
118}
119
120
121/** Function combining image and indirect illumination buffer using a
122    depth and convergence aware discontinuity filter.
123*/
124pixel CombineSsaoFullRes(fragment IN,
125                                                 uniform sampler2D colorsTex,
126                                                 uniform sampler2D ssaoTex,
127                                                 uniform float3 bl,
128                                                 uniform float3 br,
129                                                 uniform float3 tl,
130                                                 uniform float3 tr,
131                                                 uniform float2 xyStep
132                                                 )
133{
134        pixel OUT;
135
136        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
137        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
138
139        const float depth = col.w;
140        // just take unfiltered convergence in current pixel
141        const float convergence = ao.y;
142
143        // filter reaches size 1 pixel when sample size reaches threshold
144        // afterwards we do not use the filter anymore
145
146        // filter up to a certain convergance value and leave out background (sky) by checking depth
147        if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < DEPTH_THRESHOLD))
148        {
149                // the filtered ssao value
150                ao.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep, convergence);
151        }
152
153        // just apply ssao if we are not in the sky
154        if (depth < DEPTH_THRESHOLD)
155        {
156                OUT.illum_col.xyz = col.xyz * max(2e-2f, 1.0f - ao.x);
157                //OUT.illum_col.xyz = col.xyz * ao.x;
158        }
159        else
160        {
161                OUT.illum_col.xyz = col.xyz;
162        }
163
164        OUT.illum_col.w = col.w;
165
166        return OUT;
167}
Note: See TracBrowser for help on using the repository browser.