source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg @ 2911

Revision 2911, 5.7 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2884]1#include "../shaderenv.h"
2
[2881]3////////////////////
4// Screen Spaced Ambient Occlusion shader
5// based on shader of Alexander Kusternig
6
7
8struct fragment
9{
10         // normalized screen position
11        float4 pos: WPOS;
[2889]12        float2 texCoord: TEXCOORD0;
13        float3 view: TEXCOORD1;
[2881]14};
15
16
17struct pixel
18{
19        float4 illum_col: COLOR0;
20};
21
22
[2891]23float2 myreflect(float2 pt, float2 n)
[2881]24{
25        // distance to plane
26        float d = dot(n, pt);
27        // reflect around plane
28        float2 rpt = pt - d * 2.0f * n;
[2886]29
[2881]30        return rpt;
31}
32
33
34
35/** The ssao shader returning the an intensity value between 0 and 1
36*/
[2904]37float2 ssao(fragment IN,
[2881]38                   uniform sampler2D positions,
39                   uniform sampler2D noiseTexture,
40                   uniform float2 samples[NUM_SAMPLES],
41                   uniform float3 currentNormal,
42                   uniform float4 centerPosition
[2904]43                   //,uniform float3 currentViewDir
[2881]44                   )
45{
46        // the w coordinate from the persp. projection
47        float w = centerPosition.w;
48
49        // Check in a circular area around the current position.
50        // Shoot vectors to the positions there, and check the angle to these positions.
51        // Summing up these angles gives an estimation of the occlusion at the current position.
52
53        float total_ao = 0.0;
[2904]54        float numSamples = 0;
[2881]55
56        for (int i = 0; i < NUM_SAMPLES; ++ i)
57        {
[2892]58                const float2 offset = samples[i];
[2881]59
[2903]60#if 1
[2881]61                ////////////////////
[2903]62                // add random noise: reflect around random normal vector (warning: slow!)
63                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
[2892]64                const float2 offsetTransformed = myreflect(offset, mynoise);
[2903]65#else
66                const float2 offsetTransformed = offset;
67#endif
[2881]68                // weight with projected coordinate to reach similar kernel size for near and far
69                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
70
[2904]71                if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
72                        ++ numSamples;
73
[2881]74                // sample downsampled texture in order to speed up texture accesses
75                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
76                //float3 sample_position = tex2D(positions, texcoord).xyz;
77
78                float3 vector_to_sample = sample_position - centerPosition.xyz;
79                const float length_to_sample = length(vector_to_sample);
80
81                float3 direction_to_sample = vector_to_sample / length_to_sample;
82
[2885]83                // angle between current normal and direction to sample controls AO intensity.
[2881]84                const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
85
86                // distance between current position and sample position controls AO intensity.
87                const float distance_intensity =
88                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
89
[2911]90#if 0
[2885]91                // if surface normal perpenticular to view dir, approx. half of the samples will not count
92                // => compensate for this (on the other hand, projected sampling area could be larger!)
[2911]93                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
94                total_ao += cos_angle * distance_intensity * view_correction;
95#endif
[2887]96                total_ao += cos_angle * distance_intensity;
[2881]97        }
98
[2904]99        return float2(max(0.0f, 1.0f - total_ao), numSamples);
[2890]100        //return saturate(dot(currentViewDir, currentNormal));
[2881]101}
102
103
104/** The mrt shader for screen space ambient occlusion
105*/
106pixel main(fragment IN,
107                   uniform sampler2D colors,
108                   uniform sampler2D positions,
109                   uniform sampler2D normals,
110                   uniform sampler2D noiseTexture,
111                   uniform float2 samples[NUM_SAMPLES],
112                   uniform sampler2D oldTex,
113                   const uniform float4x4 oldModelViewProj,
114                   uniform float maxDepth,
[2897]115                   uniform float temporalCoherence
[2881]116                   )
117{
118        pixel OUT;
119
120        float4 norm = tex2D(normals, IN.texCoord.xy);
121       
122        // the ambient term
123        const float amb = norm.w;
124
125        // expand normal
126        float3 normal = normalize(norm.xyz);
[2891]127       
[2881]128        /// the current view direction
[2904]129        //float3 viewDir = normalize(IN.view);
[2881]130
131        // the current world position
132        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
133       
134        // the current color
135        const float4 currentCol = tex2D(colors, IN.texCoord.xy);
136        const float currentDepth = currentCol.w;
137
[2904]138        const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition);//, viewDir);
[2881]139               
140
141        /////////////////
142        //-- compute temporally smoothing
143
144        float4 realPos = centerPosition * maxDepth;
145        realPos.w = 1.0f;
146
147        // reproject
148        float4 oldPos = mul(oldModelViewProj, realPos);
149
150        const float newDepth = oldPos.z / oldPos.w;
151
152        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
153        float4 oldCol = tex2D(oldTex, tex);
154
155        const float oldDepth = oldCol.w;
156        const float depthDif = 1.0f - newDepth / oldDepth;
157
[2897]158        float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
[2904]159        float oldNumSamples = oldCol.y;
160
[2897]161        float newWeight;
[2881]162
[2904]163        if (//(temporalCoherence > 0) &&
[2897]164                (tex.x >= 0.0f) && (tex.x < 1.0f) &&
[2881]165                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
[2904]166                (abs(depthDif) < 1e-3f)
167                && (oldNumSamples > ao.y - 1.5f) // check if something happened in the surrounding area
168                )
[2881]169        {
[2904]170                // increase the weight for convergence
171                newWeight = oldWeight + 1.0f;
[2897]172
173                //OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
[2904]174                OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight;
175
176                //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
[2881]177        }
178        else
179        {
[2904]180                OUT.illum_col.xy = ao.xy;
[2897]181                newWeight = 0;
[2881]182        }
183
[2897]184
[2904]185        //OUT.illum_col.y = ao.y;
[2897]186        OUT.illum_col.z = newWeight;
[2880]187        OUT.illum_col.w = currentDepth;
188
[2881]189        return OUT;
190}
[2880]191
[2881]192
[2880]193pixel combine(fragment IN,
194                          uniform sampler2D colors,
195                          uniform sampler2D ssaoTex)
196{
197        pixel OUT;
198
[2884]199        float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
[2880]200        float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
201
[2891]202        OUT.illum_col = col * ao.x;
[2898]203        //OUT.illum_col = (float4)ao.x;
[2887]204
[2884]205        OUT.illum_col.w = ao.w;
[2880]206
207        return OUT;
[2873]208}
Note: See TracBrowser for help on using the repository browser.