source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg @ 2976

Revision 2976, 6.3 KB checked in by mattausch, 16 years ago (diff)

cannot read mipmap, but stayed on gpu

Line 
1////////////////////
2// SSAO + color bleeding shader
3// based on shader of Alexander Kusternig
4
5#include "../shaderenv.h"
6
7
8struct fragment
9{
10         // normalized screen position
11        float4 pos: WPOS;
12        float4 texCoord: TEXCOORD0;
13        float3 view: COLOR0;
14};
15
16
17struct pixel2
18{
19        float4 ssao_col: COLOR0;
20        float4 illum_col: COLOR1;
21};
22
23
24struct pixel
25{
26        float4 illum_col: COLOR0;
27};
28
29
30float2 myreflect(float2 pt, float2 n)
31{
32        // distance to plane
33        float d = dot(n, pt);
34        // reflect around plane
35        float2 rpt = pt - d * 2.0f * n;
36        return rpt;
37}
38
39struct GiStruct
40{
41        float3 illum;
42        float2 ao;
43};
44
45
46/** Computes  diffuse reflections + ambient occlusion
47*/
48GiStruct globIllum(fragment IN,
49                                 uniform sampler2D colors,
50                                 uniform sampler2D positions,
51                                 uniform sampler2D noiseTexture,
52                                 uniform float2 samples[NUM_SAMPLES],
53                                 uniform float3 currentNormal,
54                                 uniform float4 centerPosition,
55                                 float w
56                                 //, uniform float3 currentViewDir
57                                 )
58{
59        GiStruct gi;
60
61        // Check in a circular area around the current position.
62        // Shoot vectors to the positions there, and check the angle to these positions.
63        // Summing up these angles gives an estimation of the occlusion at the current position.
64
65        // ao is in stored in the w component
66        float3 total_color = float3(0, 0, 0);
67        float total_ao = 0.0f;
68        float numSamples = 0.0f;
69
70        ////////////
71        //-- the main sampling loop
72
73        for (int i = 0; i < NUM_SAMPLES; i ++)
74        {
75                float2 offset = samples[i];
76
77#if 1
78                ////////////////////
79                // add random noise: reflect around random normal vector (warning: slow!)
80                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
81                float2 offsetTransformed = myreflect(offset, mynoise);
82#else
83                float2 offsetTransformed = offset;
84#endif
85                // weight with projected coordinate to reach similar kernel size for near and far
86                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
87
88                if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
89                        ++ numSamples;
90
91                // use lower lod level to improve cache coherence
92                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz;
93                float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, GI_MIPMAP_LEVEL)).xyz;
94
95                float3 vector_to_sample = sample_position - centerPosition.xyz;
96                const float length_to_sample = length(vector_to_sample);
97
98                float3 direction_to_sample = vector_to_sample / length_to_sample;
99
100                // Angle between current normal and direction to sample controls AO intensity.
101                float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
102
103                // distance between current position and sample position controls AO intensity.
104                const float distance_intensity =
105                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
106
107                // if normal perpenticular to view dir, only half of the samples count
108                /*
109                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
110                total_color.w -= cos_angle * distance_intensity * view_correction;
111                total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
112                */
113                total_ao += cos_angle * distance_intensity;
114                total_color += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
115        }
116
117        gi.illum = total_color;
118        gi.ao = float2(max(0.0f, 1.0f - total_ao), numSamples);
119
120        //return saturate(total_color);
121        return gi;
122}
123
124
125/** The mrt shader for screen space ambient occlusion + indirect illumination
126*/
127pixel2 main(fragment IN,
128                   uniform sampler2D colors,
129                   uniform sampler2D positions,
130                   uniform sampler2D normals,
131                   uniform sampler2D noiseTexture,
132                   uniform float2 samples[NUM_SAMPLES],
133                   uniform sampler2D oldSsaoTex,
134                   uniform sampler2D oldIllumTex,
135                   const uniform float4x4 oldModelViewProj,
136                   uniform float maxDepth,
137                   uniform float temporalCoherence
138                   )
139{
140        pixel2 OUT;
141
142        float4 norm = tex2D(normals, IN.texCoord.xy);
143        float3 normal = normalize(norm.xyz);   
144        // something like a constant ambient term
145        const float amb = norm.w;
146        /// the current view direction
147        //float3 viewDir = normalize(IN.view);
148
149        // the w coordinate from the persp. projection
150        float w = norm.w;
151        // the current world position
152        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
153        // the current color
154        const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
155        // the current depth is stored in the w component
156        const float currentDepth = centerPosition.w;
157
158        GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, centerPosition, w); //, viewDir);
159       
160
161        /////////////////
162        //-- compute temporally smoothing
163
164        float4 realPos = centerPosition * maxDepth;
165        realPos.w = 1.0f;
166
167        float4 oldPos = mul(oldModelViewProj, realPos);
168
169        const float newDepth = oldPos.z / oldPos.w;
170
171        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
172
173        float4 oldSsao = tex2D(oldSsaoTex, tex);
174        float4 oldIllum = tex2D(oldIllumTex, tex);
175
176        const float oldDepth = oldSsao.w;
177        const float depthDif = 1.0f - newDepth / oldDepth;
178
179        float oldWeight = clamp(oldSsao.z, 0, temporalCoherence);
180        float newWeight;
181
182        const float oldNumSamples = oldSsao.y;
183        const float oldAvgDepth = oldSsao.z;
184
185        if ((temporalCoherence > 0.0f) &&
186                (tex.x >= 0.0f) && (tex.x < 1.0f) &&
187                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
188                (abs(depthDif) < 1e-3f)
189                // check if something changed in the surrounding area
190                && (oldNumSamples > 0.2 * gi.ao.y)
191                //&& (oldAvgDepth / newAvgDepth > 0.99)
192                )
193        {
194                newWeight = oldWeight + 1;
195
196                OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight;
197                OUT.illum_col.xyz  = (gi.illum + oldIllum.xyz * oldWeight) / newWeight;
198        }
199        else
200        {
201                newWeight = 0;
202
203                OUT.ssao_col.xy = gi.ao.xy;
204                OUT.illum_col.xyz = gi.illum;
205        }
206
207        OUT.ssao_col.z = newWeight;
208        OUT.ssao_col.w = currentDepth;
209
210        return OUT;
211}
212
213
214pixel combine(fragment IN,
215                          uniform sampler2D colors,
216                          uniform sampler2D ssaoTex,
217                          uniform sampler2D illumTex
218                          )
219{
220        pixel OUT;
221
222        float4 col = tex2D(colors, IN.texCoord.xy);
223        float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
224        float4 illum = tex2D(illumTex, IN.texCoord.xy);
225       
226        OUT.illum_col = (col + illum) * ao;
227        OUT.illum_col.w = col.w;
228
229        return OUT;
230}
Note: See TracBrowser for help on using the repository browser.