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

Revision 3148, 3.2 KB checked in by mattausch, 16 years ago (diff)

done a little cleanup

Line 
1#include "../shaderenv.h"
2
3
4/******************************************/
5/* Filter for combining ssao with image   */
6/******************************************/
7
8
9struct fragment
10{
11        float2 texCoord: TEXCOORD0;
12        float3 view: TEXCOORD1;
13};
14
15
16struct pixel
17{
18        float4 illum_col: COLOR0;
19};
20
21
22/** Filter taking into account depth and normal differences,
23   and convergence of a sample 
24*/
25float DiscontinuityFilter(float2 texCoord,
26                                                  float4 ao,
27                                                  float4 color,
28                                                  uniform sampler2D ssaoTex,
29                                                  uniform sampler2D normalsTex,
30                                                  uniform sampler2D colorsTex,
31                                                  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
32                                                  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
33                                                  float scale)
34{
35        float average = .0f;
36        float total_w = .0f;
37
38        const float eyeSpaceDepth = color.w;
39
40        const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
41        //const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
42
43        float4 aoSample;
44        float3 sampleNorm;
45        float3 samplePos;
46        float w;
47        float4 sampleTexCoord;
48        float depthFactor;
49        float normalFactor;
50        float convergenceFactor;
51        float sampleDepth;
52
53        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
54        {
55                sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
56
57                aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
58                sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
59                //sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
60
61                // check depth discontinuity
62                sampleDepth = tex2Dlod(colorsTex, sampleTexCoord).w;
63                //sampleDepth = aoSample.w;
64               
65                //depthFactor = max(step(1e-1f, 1.0f - abs(1.0f - eyeSpaceDepth / sampleDepth)), 1e-3f);
66                depthFactor = 1.0f / max(abs(eyeSpaceDepth - sampleDepth), 1e-3f);
67                //normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-6f);
68                normalFactor = max(dot(sampleNorm, centerNormal), 1e-6f);
69                convergenceFactor = min(200.0f, aoSample.y) * 0.01f;
70
71                // combine the weights
72                w = depthFactor * normalFactor * convergenceFactor;
73
74                average += aoSample.x * w;
75                total_w += w;
76        }
77
78        average /= max(total_w, 1e-1f);
79
80        return saturate(average );
81}
82
83/** Function combining image and indirect illumination buffer using a
84        depth and normal aware discontinuity filter.
85*/
86pixel combine(fragment IN,
87                          uniform sampler2D colorsTex,
88                          uniform sampler2D ssaoTex,
89                          uniform sampler2D normalsTex,
90                          uniform sampler2D offsetTex,
91                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
92                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
93                          )
94{
95        pixel OUT;
96
97        float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
98        float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
99
100        if ((ao.y < 70.0f) && (col.w < 1e10f))
101        //if (col.w < 1e10f)
102        {
103                const float scaleFactor = 1.0f;
104                const float adaptFactor = 10.0f;
105               
106                const float scale =     adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y);
107                       
108                ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, filterWeights, scale);
109        }
110
111        OUT.illum_col.xyz = col.xyz * ao.x;
112
113        //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
114        OUT.illum_col.w = col.w;
115
116        return OUT;
117}
Note: See TracBrowser for help on using the repository browser.