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