1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 | ////////////////////
|
---|
4 | // Screen Spaced Ambient Occlusion shader
|
---|
5 | // based on shader of Alexander Kusternig
|
---|
6 |
|
---|
7 |
|
---|
8 | #define USE_EYE_SPACE_DEPTH 1
|
---|
9 |
|
---|
10 |
|
---|
11 | struct fragment
|
---|
12 | {
|
---|
13 | float2 texCoord: TEXCOORD0;
|
---|
14 | float3 view: TEXCOORD1;
|
---|
15 | };
|
---|
16 |
|
---|
17 |
|
---|
18 | struct pixel
|
---|
19 | {
|
---|
20 | float4 illum_col: COLOR0;
|
---|
21 | };
|
---|
22 |
|
---|
23 |
|
---|
24 | inline float2 myreflect(float2 pt, float2 n)
|
---|
25 | {
|
---|
26 | // distance to plane
|
---|
27 | float d = dot(n, pt);
|
---|
28 | // reflect around plane
|
---|
29 | float2 rpt = pt - d * 2.0f * n;
|
---|
30 |
|
---|
31 | return rpt;
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
36 | {
|
---|
37 | float3 x1 = lerp(bl, tl, w.y);
|
---|
38 | float3 x2 = lerp(br, tr, w.y);
|
---|
39 | float3 v = lerp(x1, x2, w.x);
|
---|
40 |
|
---|
41 | return v;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | // reconstruct world space position
|
---|
46 | inline float3 ReconstructSamplePos(uniform sampler2D colors,
|
---|
47 | float2 texcoord,
|
---|
48 | float3 bl, float3 br, float3 tl, float3 tr)
|
---|
49 | {
|
---|
50 | //const float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w;
|
---|
51 | const float eyeSpaceDepth = tex2D(colors, texcoord).w;
|
---|
52 | float3 viewVec = Interpol(texcoord, bl, br, tl, tr);
|
---|
53 | float3 samplePos = -viewVec * eyeSpaceDepth;
|
---|
54 |
|
---|
55 | return samplePos;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /** The ssao shader returning the an intensity value between 0 and 1
|
---|
60 | */
|
---|
61 | float2 ssao(fragment IN,
|
---|
62 | uniform sampler2D colors,
|
---|
63 | uniform sampler2D noiseTexture,
|
---|
64 | uniform float2 samples[NUM_SAMPLES],
|
---|
65 | uniform float3 currentNormal,
|
---|
66 | uniform float3 centerPosition,
|
---|
67 | uniform float scaleFactor,
|
---|
68 | uniform float3 bl,
|
---|
69 | uniform float3 br,
|
---|
70 | uniform float3 tl,
|
---|
71 | uniform float3 tr,
|
---|
72 | uniform float3 viewDir
|
---|
73 | )
|
---|
74 | {
|
---|
75 | // Check in a circular area around the current position.
|
---|
76 | // Shoot vectors to the positions there, and check the angle to these positions.
|
---|
77 | // Summing up these angles gives an estimation of the occlusion at the current position.
|
---|
78 |
|
---|
79 | float total_ao = 0.0;
|
---|
80 | float numSamples = 0;
|
---|
81 |
|
---|
82 |
|
---|
83 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
84 | {
|
---|
85 | const float2 offset = samples[i];
|
---|
86 |
|
---|
87 | #if 1
|
---|
88 | ////////////////////
|
---|
89 | // add random noise: reflect around random normal vector (warning: slow!)
|
---|
90 |
|
---|
91 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
92 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
93 | #else
|
---|
94 | const float2 offsetTransformed = offset;
|
---|
95 | #endif
|
---|
96 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
97 | const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor;
|
---|
98 |
|
---|
99 | //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
|
---|
100 |
|
---|
101 | const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr);
|
---|
102 |
|
---|
103 |
|
---|
104 | ////////////////
|
---|
105 | //-- compute contribution of sample using the direction and angle
|
---|
106 |
|
---|
107 | float3 dirSample = samplePos - centerPosition;
|
---|
108 | const float magSample = length(dirSample);
|
---|
109 | // normalize
|
---|
110 | dirSample /= magSample;
|
---|
111 |
|
---|
112 | // angle between current normal and direction to sample controls AO intensity.
|
---|
113 | const float cosAngle = max(dot(dirSample, currentNormal), 0.0f);
|
---|
114 |
|
---|
115 | // the distance_scale offset is used to avoid singularity that occurs at global illumination when
|
---|
116 | // the distance to a sample approaches zero
|
---|
117 | const float intensity = SAMPLE_INTENSITY / (DISTANCE_SCALE + magSample * magSample);
|
---|
118 |
|
---|
119 | #if 1
|
---|
120 | // if surface normal perpenticular to view dir, approx. half of the samples will not count
|
---|
121 | // => compensate for this (on the other hand, projected sampling area could be larger!)
|
---|
122 | const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal);
|
---|
123 | total_ao += cosAngle * intensity * viewCorrection;
|
---|
124 | #else
|
---|
125 | total_ao += cosAngle * intensity;
|
---|
126 | #endif
|
---|
127 | }
|
---|
128 |
|
---|
129 | return float2(max(0.0f, 1.0f - total_ao), numSamples);
|
---|
130 | }
|
---|
131 |
|
---|
132 | #pragma position_invariant main
|
---|
133 |
|
---|
134 | /** The mrt shader for screen space ambient occlusion
|
---|
135 | */
|
---|
136 | pixel main(fragment IN,
|
---|
137 | uniform sampler2D colors,
|
---|
138 | uniform sampler2D normals,
|
---|
139 | uniform sampler2D noise,
|
---|
140 | uniform float2 samples[NUM_SAMPLES],
|
---|
141 | uniform sampler2D oldTex,
|
---|
142 | const uniform float4x4 oldModelViewProj,
|
---|
143 | const uniform float4x4 modelViewProj,
|
---|
144 | uniform float temporalCoherence,
|
---|
145 | uniform float3 eyePos,
|
---|
146 | uniform float3 bl,
|
---|
147 | uniform float3 br,
|
---|
148 | uniform float3 tl,
|
---|
149 | uniform float3 tr
|
---|
150 | )
|
---|
151 | {
|
---|
152 | pixel OUT;
|
---|
153 |
|
---|
154 | float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0));
|
---|
155 | const float3 normal = normalize(norm.xyz);
|
---|
156 |
|
---|
157 | /// reconstruct position from the eye space depth
|
---|
158 | float3 viewDir = IN.view;
|
---|
159 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
160 | const float3 eyeSpacePos = -viewDir * eyeDepth;
|
---|
161 | const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f);
|
---|
162 |
|
---|
163 |
|
---|
164 | ////////////////
|
---|
165 | //-- calculcate the current projected depth for next frame
|
---|
166 |
|
---|
167 | float4 currentPos = mul(modelViewProj, worldPos);
|
---|
168 |
|
---|
169 | const float w = SAMPLE_RADIUS / currentPos.w;
|
---|
170 | currentPos /= currentPos.w;
|
---|
171 |
|
---|
172 | const float precisionScale = 1e-3f;
|
---|
173 | const float currentDepth = currentPos.z * precisionScale;
|
---|
174 |
|
---|
175 | const float2 ao = ssao(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
|
---|
176 |
|
---|
177 |
|
---|
178 | /////////////////
|
---|
179 | //-- compute temporally smoothing
|
---|
180 |
|
---|
181 |
|
---|
182 | // reproject new frame into old one
|
---|
183 |
|
---|
184 | // calculate projected depth
|
---|
185 | float4 projPos = mul(oldModelViewProj, worldPos);
|
---|
186 | projPos /= projPos.w;
|
---|
187 |
|
---|
188 | // the current depth projected into the old frame
|
---|
189 | const float projDepth = projPos.z * precisionScale;
|
---|
190 |
|
---|
191 | // fit from unit cube into 0 .. 1
|
---|
192 | const float2 tex = projPos.xy * 0.5f + 0.5f;
|
---|
193 |
|
---|
194 | // retrieve the sample from the last frame
|
---|
195 | float4 oldCol = tex2D(oldTex, tex);
|
---|
196 |
|
---|
197 | const float oldDepth = oldCol.z;
|
---|
198 | //const float depthDif = 1.0f - projDepth / oldDepth;
|
---|
199 | const float depthDif = projDepth - oldDepth;
|
---|
200 |
|
---|
201 |
|
---|
202 | //const float oldNumSamples = oldCol.y;
|
---|
203 | const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
|
---|
204 |
|
---|
205 | float newWeight;
|
---|
206 |
|
---|
207 | // the number of valid samples in this frame
|
---|
208 | //const float newNumSamples = ao.y;
|
---|
209 |
|
---|
210 | if ((temporalCoherence > 0) &&
|
---|
211 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
212 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
213 | (abs(depthDif) < MIN_DEPTH_DIFF)
|
---|
214 | // if visibility changed in the surrounding area we have to recompute
|
---|
215 | //&& (oldNumSamples > 0.8f * newNumSamples)
|
---|
216 | )
|
---|
217 | {
|
---|
218 | // increase the weight for convergence
|
---|
219 | newWeight = oldWeight + 1.0f;
|
---|
220 | OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
|
---|
221 | //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | OUT.illum_col.x = ao.x;
|
---|
226 | newWeight = 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | OUT.illum_col.y = newWeight;
|
---|
230 | OUT.illum_col.z = currentDepth;
|
---|
231 |
|
---|
232 | return OUT;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | float Filter(float2 texCoord,
|
---|
237 | uniform sampler2D ssaoTex,
|
---|
238 | uniform float2 filterOffs[NUM_DOWNSAMPLES],
|
---|
239 | uniform float filterWeights[NUM_DOWNSAMPLES]
|
---|
240 | )
|
---|
241 | {
|
---|
242 | float average = .0f;
|
---|
243 | float w = .0f;
|
---|
244 |
|
---|
245 | for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
|
---|
246 | {
|
---|
247 | average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x;
|
---|
248 | w += filterWeights[i];
|
---|
249 | }
|
---|
250 |
|
---|
251 | average *= 1.0f / (float)w;
|
---|
252 |
|
---|
253 | return average;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | pixel combine(fragment IN,
|
---|
258 | uniform sampler2D colors,
|
---|
259 | uniform sampler2D ssaoTex,
|
---|
260 | uniform float2 filterOffs[NUM_DOWNSAMPLES],
|
---|
261 | uniform float filterWeights[NUM_DOWNSAMPLES]
|
---|
262 | )
|
---|
263 | {
|
---|
264 | pixel OUT;
|
---|
265 |
|
---|
266 | float4 col = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
267 | float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
268 |
|
---|
269 | //if (ao.y < 2000.0f)
|
---|
270 | // ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
|
---|
271 |
|
---|
272 | OUT.illum_col = col * ao.x;
|
---|
273 | //OUT.illum_col.xyz = float3(ao.x,1-ao.y*1e-2f, 0);
|
---|
274 | OUT.illum_col.w = col.w;
|
---|
275 |
|
---|
276 | return OUT;
|
---|
277 | }
|
---|