1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct fragment
|
---|
5 | {
|
---|
6 | // normalized screen position
|
---|
7 | float4 pos: WPOS;
|
---|
8 | float2 texCoord: TEXCOORD0;
|
---|
9 | float3 view: TEXCOORD1;
|
---|
10 | };
|
---|
11 |
|
---|
12 |
|
---|
13 | struct pixel
|
---|
14 | {
|
---|
15 | float4 color: COLOR0;
|
---|
16 | };
|
---|
17 |
|
---|
18 |
|
---|
19 | float2 myreflect(float2 pt, float2 n)
|
---|
20 | {
|
---|
21 | // distance to plane
|
---|
22 | float d = dot(n, pt);
|
---|
23 | // reflect around plane
|
---|
24 | float2 rpt = pt - d * 2.0f * n;
|
---|
25 |
|
---|
26 | return rpt;
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | /** function for standard deferred shading
|
---|
31 | */
|
---|
32 | float4 shade(fragment IN,
|
---|
33 | uniform float4 color,
|
---|
34 | uniform float3 normal,
|
---|
35 | float3 lightDir)
|
---|
36 | {
|
---|
37 | // diffuse intensity
|
---|
38 | const float angle = saturate(dot(normal, lightDir));
|
---|
39 |
|
---|
40 | float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
41 | float4 diffuse = angle * lightDiffuse;
|
---|
42 |
|
---|
43 | // global ambient
|
---|
44 | const float4 ambient = glstate.light[0].ambient;
|
---|
45 |
|
---|
46 | float4 outColor;
|
---|
47 |
|
---|
48 | // hack: prevent shading the sky
|
---|
49 | if (color.w > 1e19f) outColor = color;
|
---|
50 | else outColor = (ambient + diffuse) * color;
|
---|
51 |
|
---|
52 | return outColor;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /** The mrt shader for standard rendering
|
---|
58 | */
|
---|
59 | pixel main(fragment IN,
|
---|
60 | uniform sampler2D colors,
|
---|
61 | uniform sampler2D normals,
|
---|
62 | uniform float3 lightDir
|
---|
63 | )
|
---|
64 | {
|
---|
65 | pixel OUT;
|
---|
66 |
|
---|
67 | float4 norm = tex2D(normals, IN.texCoord);
|
---|
68 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
69 |
|
---|
70 | float3 normal = normalize(norm.xyz);
|
---|
71 | float4 col = shade(IN, color, normal, lightDir);
|
---|
72 |
|
---|
73 | OUT.color = col;
|
---|
74 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
75 | OUT.color.w = color.w;// / length(IN.view);
|
---|
76 |
|
---|
77 | return OUT;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | float CalcShadowTerm(fragment IN,
|
---|
82 | uniform sampler2D shadowMap,
|
---|
83 | uniform float scale,
|
---|
84 | uniform float2 lightSpacePos,
|
---|
85 | uniform float depth,
|
---|
86 | uniform float2 samples[NUM_PCF_TABS],
|
---|
87 | uniform float weights[NUM_PCF_TABS],
|
---|
88 | uniform sampler2D noiseTexture
|
---|
89 | )
|
---|
90 | {
|
---|
91 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
92 | //return step(depth, shadowDepth);
|
---|
93 |
|
---|
94 | float total_d = .0f;
|
---|
95 | float total_w = .0f;
|
---|
96 |
|
---|
97 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
98 | {
|
---|
99 | const float2 offset = samples[i];
|
---|
100 | const float w = weights[i];
|
---|
101 |
|
---|
102 | #if 1
|
---|
103 | ////////////////////
|
---|
104 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
105 |
|
---|
106 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
107 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
108 | #else
|
---|
109 | const float2 offsetTransformed = offset;
|
---|
110 | #endif
|
---|
111 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
112 | float2 texcoord = lightSpacePos + offsetTransformed * scale;
|
---|
113 |
|
---|
114 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
115 |
|
---|
116 | total_d += w * step(depth, shadowDepth);
|
---|
117 | total_w += w;
|
---|
118 | }
|
---|
119 |
|
---|
120 | total_d /= (float)total_w;
|
---|
121 |
|
---|
122 | return total_d;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
127 | {
|
---|
128 | float3 x1 = lerp(bl, tl, w.y);
|
---|
129 | float3 x2 = lerp(br, tr, w.y);
|
---|
130 | float3 v = lerp(x1, x2, w.x);
|
---|
131 |
|
---|
132 | return v;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | pixel main_shadow(fragment IN,
|
---|
137 | uniform sampler2D colors,
|
---|
138 | uniform sampler2D positions,
|
---|
139 | uniform sampler2D normals,
|
---|
140 | uniform sampler2D shadowMap,
|
---|
141 | uniform float4x4 shadowMatrix,
|
---|
142 | uniform float sampleWidth,
|
---|
143 | uniform sampler2D noiseTex,
|
---|
144 | uniform float2 samples[NUM_PCF_TABS],
|
---|
145 | uniform float weights[NUM_PCF_TABS],
|
---|
146 | uniform float3 lightDir,
|
---|
147 | uniform float3 eyePos,
|
---|
148 | uniform float3 bl,
|
---|
149 | uniform float3 br,
|
---|
150 | uniform float3 tl,
|
---|
151 | uniform float3 tr
|
---|
152 | )
|
---|
153 | {
|
---|
154 | pixel OUT;
|
---|
155 |
|
---|
156 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
157 | const float3 normal = normalize(norm.xyz);
|
---|
158 |
|
---|
159 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
160 |
|
---|
161 | /// reconstruct position from the eye space depth
|
---|
162 | float3 viewDir = IN.view;
|
---|
163 | const float lenView = length(viewDir);
|
---|
164 | viewDir /= lenView;
|
---|
165 |
|
---|
166 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
167 |
|
---|
168 | const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
|
---|
169 |
|
---|
170 | // diffuse intensity
|
---|
171 | const float angle = saturate(dot(normal, lightDir));
|
---|
172 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
173 |
|
---|
174 | float4 diffuse = lightDiffuse * angle;
|
---|
175 |
|
---|
176 | // hack: prevent shadowing the sky
|
---|
177 | const bool useShading = (color.w < 1e19f);
|
---|
178 |
|
---|
179 | // calc diffuse illumination + shadow term
|
---|
180 | if (useShading &&
|
---|
181 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
182 | )
|
---|
183 | {
|
---|
184 | float4 lightSpacePos = mul(shadowMatrix, worldPos);
|
---|
185 | lightSpacePos /= lightSpacePos.w;
|
---|
186 |
|
---|
187 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTex);
|
---|
188 |
|
---|
189 | diffuse *= shadowTerm;
|
---|
190 | }
|
---|
191 |
|
---|
192 | // light ambient term
|
---|
193 | const float4 ambient = glstate.light[0].ambient;
|
---|
194 | // compute shading
|
---|
195 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
196 | // store scaled view vector from now on so wie don't have to normalize later (e.g., for ssao)
|
---|
197 | //OUT.color.w = color.w / lenView;
|
---|
198 |
|
---|
199 | return OUT;
|
---|
200 | }
|
---|
201 |
|
---|
202 | #if 0
|
---|
203 | /** This shader computes the reprojection and stores reprojected color / depth values
|
---|
204 | as well as a boolean that
|
---|
205 | */
|
---|
206 | pixel Reproject(fragment IN,
|
---|
207 | uniform sampler2D colors,
|
---|
208 | uniform sampler2D normals)
|
---|
209 | {
|
---|
210 | float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0));
|
---|
211 | const float3 normal = normalize(norm.xyz);
|
---|
212 |
|
---|
213 | /// reconstruct position from the eye space depth
|
---|
214 | float3 viewDir = IN.view;
|
---|
215 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
216 | const float3 eyeSpacePos = -viewDir * eyeDepth;
|
---|
217 | const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f);
|
---|
218 |
|
---|
219 |
|
---|
220 | ////////////////
|
---|
221 | //-- calculcate the current projected posiion (also used for next frame)
|
---|
222 |
|
---|
223 | float4 currentPos = mul(modelViewProj, worldPos);
|
---|
224 |
|
---|
225 | const float w = SAMPLE_RADIUS / currentPos.w;
|
---|
226 | currentPos /= currentPos.w;
|
---|
227 |
|
---|
228 | const float precisionScale = 1e-3f;
|
---|
229 | const float currentDepth = currentPos.z * precisionScale;
|
---|
230 |
|
---|
231 | const float2 ao = ssao(IN, colors, noiseTex, samples, normal,
|
---|
232 | eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
|
---|
233 |
|
---|
234 |
|
---|
235 | /////////////////
|
---|
236 | //-- compute temporally smoothing
|
---|
237 |
|
---|
238 |
|
---|
239 | // reproject new frame into old one
|
---|
240 |
|
---|
241 | // calculate projected depth
|
---|
242 | float4 projPos = mul(oldModelViewProj, worldPos);
|
---|
243 | projPos /= projPos.w;
|
---|
244 |
|
---|
245 | // the current depth projected into the old frame
|
---|
246 | const float projDepth = projPos.z * precisionScale;
|
---|
247 | // fit from unit cube into 0 .. 1
|
---|
248 | const float2 tex = projPos.xy * 0.5f + 0.5f;
|
---|
249 | // retrieve the sample from the last frame
|
---|
250 | float4 oldCol = tex2D(oldTex, tex);
|
---|
251 |
|
---|
252 | const float oldDepth = oldCol.z;
|
---|
253 | //const float depthDif = 1.0f - projDepth / oldDepth;
|
---|
254 | const float depthDif = projDepth - oldDepth;
|
---|
255 |
|
---|
256 | //const float oldNumSamples = oldCol.y;
|
---|
257 | const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
|
---|
258 |
|
---|
259 | float newWeight;
|
---|
260 |
|
---|
261 | // the number of valid samples in this frame
|
---|
262 | //const float newNumSamples = ao.y;
|
---|
263 |
|
---|
264 | if ((temporalCoherence > 0)
|
---|
265 | && (tex.x >= 0.0f) && (tex.x < 1.0f)
|
---|
266 | && (tex.y >= 0.0f) && (tex.y < 1.0f)
|
---|
267 | && (abs(depthDif) < MIN_DEPTH_DIFF)
|
---|
268 | && (abs(oldCol.x - ao.x) < 0.1f)
|
---|
269 | // if visibility changed in the surrounding area we have to recompute
|
---|
270 | //&& (oldNumSamples > 0.8f * newNumSamples)
|
---|
271 | )
|
---|
272 | {
|
---|
273 | // increase the weight for convergence
|
---|
274 | newWeight = oldWeight + 1.0f;
|
---|
275 | OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
|
---|
276 | //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | OUT.illum_col.x = ao.x;
|
---|
281 | newWeight = .0f;
|
---|
282 | }
|
---|
283 |
|
---|
284 | OUT.illum_col.y = newWeight;
|
---|
285 | OUT.illum_col.z = currentDepth;
|
---|
286 |
|
---|
287 | return OUT;
|
---|
288 | }
|
---|
289 |
|
---|
290 | #endif
|
---|
291 |
|
---|
292 |
|
---|
293 | float4 Output(fragment IN, uniform sampler2D colors): COLOR
|
---|
294 | {
|
---|
295 | return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | float4 ScaleDepth(fragment IN,
|
---|
300 | uniform sampler2D colors): COLOR
|
---|
301 | {
|
---|
302 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
303 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
304 | color.w /= length(IN.view);
|
---|
305 |
|
---|
306 | return color;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | float4 DownSample(fragment IN,
|
---|
311 | uniform sampler2D colors,
|
---|
312 | uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
|
---|
313 | {
|
---|
314 | // let bilinear filtering do its work
|
---|
315 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
316 | return color;
|
---|
317 | } |
---|