1 | #include "../shaderenv.h"
|
---|
2 | #include "common.h"
|
---|
3 |
|
---|
4 | ////////////////////
|
---|
5 | // Screen Spaced Ambient Occlusion shader
|
---|
6 | // based on shader of Alexander Kusternig
|
---|
7 |
|
---|
8 |
|
---|
9 | #define USE_EYESPACE_DEPTH 1
|
---|
10 |
|
---|
11 |
|
---|
12 | struct fragment
|
---|
13 | {
|
---|
14 | float2 texCoord: TEXCOORD0;
|
---|
15 | float3 view: TEXCOORD1;
|
---|
16 | };
|
---|
17 |
|
---|
18 |
|
---|
19 | struct pixel
|
---|
20 | {
|
---|
21 | float4 illum_col: COLOR0;
|
---|
22 | };
|
---|
23 |
|
---|
24 | // this function is inspired from the paper of shamulgaan in order
|
---|
25 | // to get a physical expression for the occlusion culling
|
---|
26 | inline float occlusionPower(float radius, float dist)
|
---|
27 | {
|
---|
28 | return 6.283185307179586476925286766559f * (1.0f - cos(asin(radius / dist)));
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | // reconstruct world space position
|
---|
34 | inline float3 ReconstructSamplePos(float eyeSpaceDepth,
|
---|
35 | float2 texcoord,
|
---|
36 | float3 bl, float3 br, float3 tl, float3 tr)
|
---|
37 | {
|
---|
38 | float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
|
---|
39 | float3 samplePos = -viewVec * eyeSpaceDepth;
|
---|
40 |
|
---|
41 | return samplePos;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /** This shader computes the reprojection and stores
|
---|
47 | the ssao value of the old pixel as well as the
|
---|
48 | weight of the pixel in the new frame.
|
---|
49 | */
|
---|
50 | inline float2 temporalSmoothing(float4 worldPos,
|
---|
51 | float eyeSpaceDepth,
|
---|
52 | float2 texcoord0,
|
---|
53 | float3 oldEyePos,
|
---|
54 | sampler2D oldTex,
|
---|
55 | float4x4 oldModelViewProj,
|
---|
56 | sampler2D colors,
|
---|
57 | float3 projPos,
|
---|
58 | float invW,
|
---|
59 | float3 oldbl,
|
---|
60 | float3 oldbr,
|
---|
61 | float3 oldtl,
|
---|
62 | float3 oldtr,
|
---|
63 | float3 diffVec
|
---|
64 | )
|
---|
65 | {
|
---|
66 | // compute position from old frame for dynamic objects + translational portion
|
---|
67 | const float3 translatedPos = diffVec - oldEyePos + worldPos.xyz;
|
---|
68 |
|
---|
69 |
|
---|
70 | /////////////////
|
---|
71 | //-- reproject into old frame and calculate texture position of sample in old frame
|
---|
72 |
|
---|
73 | // note: the old model view matrix only holds the view orientation part
|
---|
74 | float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f));
|
---|
75 | backProjPos /= backProjPos.w;
|
---|
76 |
|
---|
77 | // fit from unit cube into 0 .. 1
|
---|
78 | const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
|
---|
79 | // retrieve the sample from the last frame
|
---|
80 | const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
|
---|
81 |
|
---|
82 | // the ssao value in the old frame
|
---|
83 | const float ssao = oldPixel.x;
|
---|
84 |
|
---|
85 | // calculate eye space position of sample in old frame
|
---|
86 | const float oldEyeSpaceDepth = oldPixel.w;
|
---|
87 |
|
---|
88 | // vector from eye pos to old sample
|
---|
89 | const float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
|
---|
90 | const float invLen = 1.0f / length(viewVec);
|
---|
91 | const float projectedEyeSpaceDepth = invLen * length(translatedPos);
|
---|
92 | //const float projectedEyeSpaceDepth = length(translatedPos);
|
---|
93 |
|
---|
94 | const float depthDif = abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth);
|
---|
95 |
|
---|
96 | // the weight of the accumulated samples from the previous frames
|
---|
97 | float w;
|
---|
98 |
|
---|
99 | //////////////
|
---|
100 | //-- reuse old value only if it was still valid in the old frame
|
---|
101 |
|
---|
102 | if (1
|
---|
103 | && (oldTexCoords.x > 0) && (oldTexCoords.x < 1.0f)
|
---|
104 | && (oldTexCoords.y > 0) && (oldTexCoords.y < 1.0f)
|
---|
105 | && (depthDif <= MIN_DEPTH_DIFF)
|
---|
106 | )
|
---|
107 | {
|
---|
108 | // pixel valid => retrieve the convergence weight
|
---|
109 | w = oldPixel.y;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | w = 0.0f;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return float2(ssao, w);
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /** The ssao shader returning the an intensity value between 0 and 1
|
---|
121 | This version of the ssao shader uses the dotproduct between pixel and
|
---|
122 | sample normal as weight.
|
---|
123 | */
|
---|
124 | float3 ssao2(fragment IN,
|
---|
125 | sampler2D colors,
|
---|
126 | sampler2D noiseTex,
|
---|
127 | float2 samples[NUM_SAMPLES],
|
---|
128 | float3 normal,
|
---|
129 | float3 centerPosition,
|
---|
130 | float scaleFactor,
|
---|
131 | float3 bl,
|
---|
132 | float3 br,
|
---|
133 | float3 tl,
|
---|
134 | float3 tr,
|
---|
135 | float3 viewDir,
|
---|
136 | sampler2D normalTex,
|
---|
137 | float sampleIntensity
|
---|
138 | )
|
---|
139 | {
|
---|
140 | float total_ao = .0f;
|
---|
141 | float numSamples = .0f;
|
---|
142 | float validSamples = .0f;
|
---|
143 |
|
---|
144 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
145 | {
|
---|
146 | const float2 offset = samples[i];
|
---|
147 |
|
---|
148 | #if 1
|
---|
149 | ////////////////////
|
---|
150 | //-- add random noise: reflect around random normal vector (rather slow!)
|
---|
151 |
|
---|
152 | const float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
|
---|
153 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
154 | #else
|
---|
155 | const float2 offsetTransformed = offset;
|
---|
156 | #endif
|
---|
157 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
158 | //const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor + jitter;
|
---|
159 | const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor;
|
---|
160 |
|
---|
161 | //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
|
---|
162 | float4 sampleColor = tex2Dlod(colors, float4(texcoord, 0, 0));
|
---|
163 |
|
---|
164 | const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
|
---|
165 | // the normal of the current sample
|
---|
166 | const float3 sampleNormal = tex2Dlod(normalTex, float4(texcoord, 0, 0)).xyz;
|
---|
167 |
|
---|
168 |
|
---|
169 | ////////////////
|
---|
170 | //-- compute contribution of sample using the direction and angle
|
---|
171 |
|
---|
172 | float3 dirSample = samplePos - centerPosition;
|
---|
173 |
|
---|
174 | const float sqrLen = max(SqrLen(dirSample), 1e-2f);
|
---|
175 | const float lengthToSample = sqrt(sqrLen);
|
---|
176 | //const float lengthToSample = max(length(dirSample), 1e-6f);
|
---|
177 |
|
---|
178 | dirSample /= lengthToSample; // normalize
|
---|
179 |
|
---|
180 | // angle between current normal and direction to sample controls AO intensity.
|
---|
181 | float cosAngle = .5f + dot(sampleNormal, -normal) * 0.5f;
|
---|
182 | // use binary decision to cull samples that are behind current shading point
|
---|
183 | cosAngle *= step(0.0f, dot(dirSample, normal));
|
---|
184 |
|
---|
185 | const float aoContrib = sampleIntensity / sqrLen;
|
---|
186 | //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
|
---|
187 |
|
---|
188 | total_ao += cosAngle * aoContrib;
|
---|
189 |
|
---|
190 | // check if the samples have been valid in the last frame
|
---|
191 | validSamples += (1.0f - step(1.0f, lengthToSample)) * sampleColor.x;
|
---|
192 |
|
---|
193 | ++ numSamples;
|
---|
194 | }
|
---|
195 |
|
---|
196 | total_ao /= numSamples;
|
---|
197 |
|
---|
198 | #if 1
|
---|
199 | // if surface normal perpenticular to view dir, approx. half of the samples will not count
|
---|
200 | // => compensate for this (on the other hand, projected sampling area could be larger!)
|
---|
201 | const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
|
---|
202 | total_ao += cosAngle * aoContrib * viewCorrection;
|
---|
203 |
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /** The ssao shader returning the an intensity value between 0 and 1.
|
---|
211 | This version of the ssao shader uses the dotproduct between
|
---|
212 | pixel-to-sample direction and sample normal as weight.
|
---|
213 |
|
---|
214 | The algorithm works like the following:
|
---|
215 | 1) Check in a circular area around the current position.
|
---|
216 | 2) Shoot vectors to the positions there, and check the angle to these positions.
|
---|
217 | 3) Summing up these angles gives an estimation of the occlusion at the current position.
|
---|
218 | */
|
---|
219 | float3 ssao(fragment IN,
|
---|
220 | sampler2D colors,
|
---|
221 | sampler2D noiseTex,
|
---|
222 | float2 samples[NUM_SAMPLES],
|
---|
223 | float3 normal,
|
---|
224 | float3 centerPosition,
|
---|
225 | float scaleFactor,
|
---|
226 | float3 bl,
|
---|
227 | float3 br,
|
---|
228 | float3 tl,
|
---|
229 | float3 tr,
|
---|
230 | float3 viewDir,
|
---|
231 | float convergence,
|
---|
232 | float sampleIntensity,
|
---|
233 | bool isMovingObject
|
---|
234 | )
|
---|
235 | {
|
---|
236 | float total_ao = .0f;
|
---|
237 | float validSamples = .0f;
|
---|
238 | float numSamples = .0f;
|
---|
239 |
|
---|
240 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
241 | {
|
---|
242 | float2 offset;
|
---|
243 |
|
---|
244 | ////////////////////
|
---|
245 | //-- add random noise: reflect around random normal vector
|
---|
246 | //-- (affects performance for some reason!)
|
---|
247 |
|
---|
248 | if (1)//convergence < 700)
|
---|
249 | {
|
---|
250 | float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy;
|
---|
251 | //offsetTransformed = myreflect(offset, mynoise);
|
---|
252 | offset = myrotate(samples[i], mynoise.x);
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | offset = samples[i];
|
---|
257 | }
|
---|
258 |
|
---|
259 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
260 | const float2 texcoord = IN.texCoord.xy + offset * scaleFactor;
|
---|
261 |
|
---|
262 | const float4 sampleColor = tex2Dlod(colors, float4(texcoord, .0f, .0f));
|
---|
263 | const float3 samplePos = ReconstructSamplePos(sampleColor.w, texcoord, bl, br, tl, tr);
|
---|
264 |
|
---|
265 |
|
---|
266 | ////////////////
|
---|
267 | //-- compute contribution of sample using the direction and angle
|
---|
268 |
|
---|
269 | float3 dirSample = samplePos - centerPosition;
|
---|
270 |
|
---|
271 | //const float sqrLen = max(SqrLen(dirSample), 1e-2f);
|
---|
272 | //const float lengthToSample = sqrt(sqrLen);
|
---|
273 | const float lengthToSample = max(length(dirSample), 1e-2f);
|
---|
274 |
|
---|
275 | dirSample /= lengthToSample; // normalize
|
---|
276 |
|
---|
277 | // angle between current normal and direction to sample controls AO intensity.
|
---|
278 | float cosAngle = dot(dirSample, normal);
|
---|
279 |
|
---|
280 | //const float aoContrib = sampleIntensity / sqrLen;
|
---|
281 | const float aoContrib = sampleIntensity / lengthToSample;
|
---|
282 | //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f;
|
---|
283 |
|
---|
284 | total_ao += max(cosAngle, 0) * aoContrib;
|
---|
285 |
|
---|
286 | ++ numSamples;
|
---|
287 |
|
---|
288 | // check if the samples have been valid in the last frame
|
---|
289 | // only mark sample as invalid if in the last / current frame
|
---|
290 | // they possibly have any influence on the ao
|
---|
291 | const float changeFactor = sampleColor.y;
|
---|
292 | const float pixelValid = sampleColor.x;
|
---|
293 |
|
---|
294 | // we check if the sample could have been near enough to the current pixel
|
---|
295 | // to have any influence in the current or last frame
|
---|
296 | const float tooFarAway = step(0.5f, lengthToSample - changeFactor);
|
---|
297 | validSamples = max(validSamples, (1.0f - tooFarAway) * pixelValid * step(-0.1f, cosAngle));
|
---|
298 |
|
---|
299 | #ifdef USE_GTX
|
---|
300 | // we can bail out early and use a minimal #samples)
|
---|
301 | // if some conditions are met as long as the hardware supports it
|
---|
302 | if (numSamples >= MIN_SAMPLES)
|
---|
303 | {
|
---|
304 | //break;
|
---|
305 | // if the pixel belongs to a static object and all the samples stay valid in the current frame
|
---|
306 | if (!isMovingObject && (validSamples < 1.0f)) break;
|
---|
307 | // if the pixel belongs to a dynamic object but the #accumulated samples for this pixel is sufficiently high
|
---|
308 | // (=> there was no discontinuity recently)
|
---|
309 | else if (isMovingObject && (convergence > NUM_SAMPLES * 5)) break;
|
---|
310 | }
|
---|
311 | #endif
|
---|
312 | }
|
---|
313 |
|
---|
314 | // "normalize" ao contribution
|
---|
315 | total_ao /= numSamples;
|
---|
316 |
|
---|
317 | #if 1
|
---|
318 | // if surface normal perpenticular to view dir, approx. half of the samples will not count
|
---|
319 | // => compensate for this (on the other hand, projected sampling area could be larger!)
|
---|
320 | const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f);
|
---|
321 | total_ao *= viewCorrection;
|
---|
322 | #endif
|
---|
323 |
|
---|
324 | //return float3(total_ao, validSamples, numSamples);
|
---|
325 | return float3(min(1.0f, total_ao), validSamples, numSamples);
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | /** The mrt shader for screen space ambient occlusion
|
---|
331 | */
|
---|
332 | pixel main(fragment IN,
|
---|
333 | uniform sampler2D colors,
|
---|
334 | uniform sampler2D normals,
|
---|
335 | uniform sampler2D noiseTex,
|
---|
336 | uniform float2 samples[NUM_SAMPLES],
|
---|
337 | uniform sampler2D oldTex,
|
---|
338 | uniform float4x4 modelViewProj,
|
---|
339 | uniform float4x4 oldModelViewProj,
|
---|
340 | uniform float temporalCoherence,
|
---|
341 | uniform float3 bl,
|
---|
342 | uniform float3 br,
|
---|
343 | uniform float3 tl,
|
---|
344 | uniform float3 tr,
|
---|
345 | uniform float3 oldEyePos,
|
---|
346 | uniform float3 oldbl,
|
---|
347 | uniform float3 oldbr,
|
---|
348 | uniform float3 oldtl,
|
---|
349 | uniform float3 oldtr,
|
---|
350 | uniform sampler2D attribsTex,
|
---|
351 | uniform float kernelRadius,
|
---|
352 | uniform float sampleIntensity
|
---|
353 | )
|
---|
354 | {
|
---|
355 | pixel OUT;
|
---|
356 |
|
---|
357 | //const float3 normal = normalize(tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz);
|
---|
358 | const float3 normal = tex2Dlod(normals, float4(IN.texCoord, 0 ,0)).xyz;
|
---|
359 |
|
---|
360 | // reconstruct position from the eye space depth
|
---|
361 | const float3 viewDir = IN.view;
|
---|
362 | const float eyeSpaceDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
363 | const float4 eyeSpacePos = float4(-viewDir * eyeSpaceDepth, 1.0f);
|
---|
364 |
|
---|
365 | float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz;
|
---|
366 |
|
---|
367 |
|
---|
368 | ////////////////
|
---|
369 | //-- calculcate the current projected posiion (also used for next frame)
|
---|
370 |
|
---|
371 | float4 projPos = mul(modelViewProj, eyeSpacePos);
|
---|
372 | const float invw = 1.0f / projPos.w;
|
---|
373 | projPos *= invw;
|
---|
374 | float scaleFactor = kernelRadius * invw;
|
---|
375 |
|
---|
376 | const float sqrMoveSpeed = SqrLen(diffVec);
|
---|
377 | const bool isMovingObject = (sqrMoveSpeed > DYNAMIC_OBJECTS_THRESHOLD);
|
---|
378 |
|
---|
379 |
|
---|
380 | /////////////////
|
---|
381 | //-- compute temporal reprojection
|
---|
382 |
|
---|
383 | float2 temporalVals = temporalSmoothing(eyeSpacePos, eyeSpaceDepth, IN.texCoord, oldEyePos,
|
---|
384 | oldTex, oldModelViewProj,
|
---|
385 | colors,
|
---|
386 | projPos.xyz,
|
---|
387 | invw,
|
---|
388 | oldbl, oldbr, oldtl, oldtr,
|
---|
389 | diffVec
|
---|
390 | );
|
---|
391 |
|
---|
392 | const float oldSsao = temporalVals.x;
|
---|
393 | float oldWeight = temporalVals.y;
|
---|
394 |
|
---|
395 | float3 ao;
|
---|
396 |
|
---|
397 | // cull background note: this should be done with the stencil buffer
|
---|
398 | if (eyeSpaceDepth < 1e10f)
|
---|
399 | {
|
---|
400 | ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject);
|
---|
401 | //ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals, sampleIntensity);
|
---|
402 | }
|
---|
403 | else
|
---|
404 | {
|
---|
405 | ao = float3(1.0f, 1.0f, 1.0f);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | ///////////
|
---|
410 | //-- check if we have to reset pixel because one of the sample points was invalid
|
---|
411 | //-- only do this if the current pixel does not belong to a moving object
|
---|
412 |
|
---|
413 | // the weight equals the number of sampled shot in this pass
|
---|
414 | const float newWeight = ao.z;
|
---|
415 |
|
---|
416 | // completely reset the ao in this pixel
|
---|
417 | const float completelyResetThres = 4.0f;
|
---|
418 | // don't fully reset the ao in this pixel, but give low weight to old solution
|
---|
419 | const float partlyResetThres = 1.0f;
|
---|
420 |
|
---|
421 | if (!isMovingObject)
|
---|
422 | {
|
---|
423 | if (ao.y > completelyResetThres)
|
---|
424 | {
|
---|
425 | oldWeight = .0f;
|
---|
426 | }
|
---|
427 | else if (ao.y > partlyResetThres)
|
---|
428 | {
|
---|
429 | oldWeight = min(oldWeight, 4.0f * newWeight);
|
---|
430 | //oldWeight = .0f;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | //////////
|
---|
435 | //-- blend ao between old and new samples (and avoid division by zero)
|
---|
436 |
|
---|
437 | OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight);// / (newWeight + oldWeight);//max(1e-6f, newWeight + oldWeight);
|
---|
438 |
|
---|
439 | OUT.illum_col.x /= (newWeight + oldWeight);
|
---|
440 |
|
---|
441 | // the new weight for the next frame
|
---|
442 | const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence);
|
---|
443 |
|
---|
444 | OUT.illum_col.y = combinedWeight;
|
---|
445 | // can be used to check if this pixel belongs to a moving object
|
---|
446 | OUT.illum_col.z = SqrLen(diffVec);
|
---|
447 | OUT.illum_col.w = eyeSpaceDepth;
|
---|
448 |
|
---|
449 | //OUT.illum_col.yzw = diffVec;
|
---|
450 |
|
---|
451 | return OUT;
|
---|
452 | }
|
---|