OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkShadowShader.h" | 10 #include "SkShadowShader.h" |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 293 |
294 // note that we flip the y-coord of the offset and then later ad
d | 294 // note that we flip the y-coord of the offset and then later ad
d |
295 // a value just to the y-coord of povCoord. This is to account f
or | 295 // a value just to the y-coord of povCoord. This is to account f
or |
296 // the shifted origins from switching from raster into GPU. | 296 // the shifted origins from switching from raster into GPU. |
297 if (shadowFP.fIsPointLight[i]) { | 297 if (shadowFP.fIsPointLight[i]) { |
298 fragBuilder->codeAppendf("vec3 fragToLight%d = %s - worldCor
;", | 298 fragBuilder->codeAppendf("vec3 fragToLight%d = %s - worldCor
;", |
299 i, lightDirOrPosUniName[i]); | 299 i, lightDirOrPosUniName[i]); |
300 fragBuilder->codeAppendf("float distsq%d = dot(fragToLight%d
, fragToLight%d);" | 300 fragBuilder->codeAppendf("float distsq%d = dot(fragToLight%d
, fragToLight%d);" |
301 "fragToLight%d = normalize(fragToLi
ght%d);", | 301 "fragToLight%d = normalize(fragToLi
ght%d);", |
302 i, i, i, i, i); | 302 i, i, i, i, i); |
303 fragBuilder->codeAppendf("%s = vec2(worldCor.x - %s.x, %s.y
- worldCor.y) * " | 303 fragBuilder->codeAppendf("%s = vec2(worldCor - %s) * povDept
h.b;", |
304 "povDepth.b;", | 304 offset.c_str(), lightDirOrPosUniNam
e[i]); |
305 offset.c_str(), lightDirOrPosUniNam
e[i], | |
306 lightDirOrPosUniName[i]); | |
307 } else { | 305 } else { |
308 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * vec2(
255.0, -255.0);", | 306 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * vec2(
255.0, -255.0);", |
309 offset.c_str(), lightDirOrPosUniNam
e[i]); | 307 offset.c_str(), lightDirOrPosUniNam
e[i]); |
310 } | 308 } |
311 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) *" | 309 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) *" |
312 "vMatrixCoord_0_1_Stage0 +" | 310 "vMatrixCoord_0_1_Stage0 +" |
313 "vec2(0,%s - %s)" | 311 "vec2(0,%s - %s)" |
314 " + " | 312 "+ %s) / vec2(%s, %s);", |
315 "%s) / vec2(%s, %s);", | |
316 povCoord.c_str(), | 313 povCoord.c_str(), |
317 widthUniName, heightUniName, | 314 widthUniName, heightUniName, |
318 depthMapHeightUniName[i], heightUniName
, | 315 depthMapHeightUniName[i], heightUniName
, |
319 offset.c_str(), | 316 offset.c_str(), |
320 depthMapWidthUniName[i], depthMapHeight
UniName[i]); | 317 depthMapWidthUniName[i], depthMapWidthU
niName[i]); |
| 318 |
| 319 |
321 | 320 |
322 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler
s[i], | 321 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler
s[i], |
323 povCoord.c_str(), | 322 povCoord.c_str(), |
324 kVec2f_GrSLType); | 323 kVec2f_GrSLType); |
325 | 324 |
326 } | 325 } |
327 | 326 |
328 // helper variables for calculating shadowing | 327 // helper variables for calculating shadowing |
329 | 328 |
330 // variance of depth at this fragment in the context of surrounding
area | 329 // variance of depth at this fragment in the context of surrounding
area |
331 // (area size and weighting dependent on blur size and type) | 330 // (area size and weighting dependent on blur size and type) |
332 fragBuilder->codeAppendf("float variance;"); | 331 fragBuilder->codeAppendf("float variance;"); |
333 | 332 |
334 // the difference in depth between the user POV and light POV. | 333 // the difference in depth between the user POV and light POV. |
335 fragBuilder->codeAppendf("float d;"); | 334 fragBuilder->codeAppendf("float d;"); |
336 | 335 |
337 // add up light contributions from all lights to totalLightColor | 336 // add up light contributions from all lights to totalLightColor |
338 for (int i = 0; i < numLights; i++) { | 337 for (int i = 0; i < numLights; i++) { |
339 if (!shadowFP.isPointLight(i)) { | 338 fragBuilder->codeAppendf("lightProbability = 1;"); |
| 339 |
| 340 // 1/512 == .00195... is less than half a pixel; imperceptible |
| 341 fragBuilder->codeAppendf("if (%s.b <= %s.b + .001953125) {", |
| 342 povDepth.c_str(), depthMaps[i].c_str())
; |
| 343 if (blurAlgorithm == SkShadowParams::kVariance_ShadowType) { |
| 344 // We mess with depth and depth^2 in their given scales. |
| 345 // (i.e. between 0 and 1) |
| 346 fragBuilder->codeAppendf("vec2 moments%d = vec2(%s.b, %s.g);
", |
| 347 i, depthMaps[i].c_str(), depthMaps[
i].c_str()); |
| 348 |
| 349 // variance biasing lessens light bleeding |
| 350 fragBuilder->codeAppendf("variance = max(moments%d.y - " |
| 351 "(moments%d.x * mome
nts%d.x)," |
| 352 "%s);", i, i, i, |
| 353 minVarianceUniName); |
| 354 |
| 355 fragBuilder->codeAppendf("d = (%s.b) - moments%d.x;", |
| 356 povDepth.c_str(), i); |
| 357 fragBuilder->codeAppendf("lightProbability = " |
| 358 "(variance / (variance + d
* d));"); |
| 359 |
| 360 SkString clamp("clamp"); |
| 361 clamp.appendf("%d", i); |
| 362 |
| 363 // choosing between light artifacts or correct shape shadows |
| 364 // linstep |
| 365 fragBuilder->codeAppendf("float %s = clamp((lightProbability
- %s) /" |
| 366 "(1 - %s), 0, 1);"
, |
| 367 clamp.c_str(), shBiasUniName, shBia
sUniName); |
| 368 |
| 369 fragBuilder->codeAppendf("lightProbability = %s;", clamp.c_s
tr()); |
| 370 } else { |
| 371 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
| 372 povDepth.c_str(), depthMaps[i].c_st
r()); |
340 fragBuilder->codeAppendf("lightProbability = 1;"); | 373 fragBuilder->codeAppendf("lightProbability = 1;"); |
| 374 fragBuilder->codeAppendf("} else { lightProbability = 0; }")
; |
| 375 } |
341 | 376 |
342 // 1/512 == .00195... is less than half a pixel; imperceptib
le | 377 // VSM: The curved shadows near plane edges are artifacts from b
lurring |
343 fragBuilder->codeAppendf("if (%s.b <= %s.b + .001953125) {", | 378 // lightDir.z is equal to the lightDir dot the surface normal. |
344 povDepth.c_str(), depthMaps[i].c_st
r()); | 379 fragBuilder->codeAppendf("}"); |
345 if (blurAlgorithm == SkShadowParams::kVariance_ShadowType) { | |
346 // We mess with depth and depth^2 in their given scales. | |
347 // (i.e. between 0 and 1) | |
348 fragBuilder->codeAppendf("vec2 moments%d = vec2(%s.b, %s
.g);", | |
349 i, depthMaps[i].c_str(), depthM
aps[i].c_str()); | |
350 | 380 |
351 // variance biasing lessens light bleeding | 381 if (shadowFP.isPointLight(i)) { |
352 fragBuilder->codeAppendf("variance = max(moments%d.y - " | 382 fragBuilder->codeAppendf("totalLightColor += max(fragToLight
%d.z, 0) * %s /" |
353 "(moments%d.x *
moments%d.x)," | 383 "(1 + distsq%d)
*" |
354 "%s);", i, i, i, | |
355 minVarianceUniName); | |
356 | |
357 fragBuilder->codeAppendf("d = (%s.b) - moments%d.x;", | |
358 povDepth.c_str(), i); | |
359 fragBuilder->codeAppendf("lightProbability = " | |
360 "(variance / (variance
+ d * d));"); | |
361 | |
362 SkString clamp("clamp"); | |
363 clamp.appendf("%d", i); | |
364 | |
365 // choosing between light artifacts or correct shape sha
dows | |
366 // linstep | |
367 fragBuilder->codeAppendf("float %s = clamp((lightProbabi
lity - %s) /" | |
368 "(1 - %s), 0,
1);", | |
369 clamp.c_str(), shBiasUniName, s
hBiasUniName); | |
370 | |
371 fragBuilder->codeAppendf("lightProbability = %s;", clamp
.c_str()); | |
372 } else { | |
373 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | |
374 povDepth.c_str(), depthMaps[i].
c_str()); | |
375 fragBuilder->codeAppendf("lightProbability = 1;"); | |
376 fragBuilder->codeAppendf("} else { lightProbability = 0;
}"); | |
377 } | |
378 | |
379 // VSM: The curved shadows near plane edges are artifacts fr
om blurring | |
380 fragBuilder->codeAppendf("}"); | |
381 fragBuilder->codeAppendf("totalLightColor += %s.z * %s * " | |
382 "lightProbabilit
y;", | 384 "lightProbabilit
y;", |
| 385 i, lightColorUniName[i], i); |
| 386 } else { |
| 387 fragBuilder->codeAppendf("totalLightColor += %s.z * %s * lig
htProbability;", |
383 lightDirOrPosUniName[i], | 388 lightDirOrPosUniName[i], |
384 lightColorUniName[i]); | 389 lightColorUniName[i]); |
385 } else { | |
386 // fragToLight%d.z is equal to the fragToLight dot the surfa
ce normal. | |
387 fragBuilder->codeAppendf("totalLightColor += max(fragToLight
%d.z, 0) * %s /" | |
388 "(1 + distsq%d);
", | |
389 i, lightColorUniName[i], i); | |
390 } | 390 } |
391 } | 391 } |
392 | 392 |
393 fragBuilder->codeAppendf("totalLightColor += %s;", ambientColorUniNa
me); | 393 fragBuilder->codeAppendf("totalLightColor += %s;", ambientColorUniNa
me); |
394 fragBuilder->codeAppendf("%s = resultDiffuseColor * vec4(totalLightC
olor, 1);", | 394 fragBuilder->codeAppendf("%s = resultDiffuseColor * vec4(totalLightC
olor, 1);", |
395 args.fOutputColor); | 395 args.fOutputColor); |
396 } | 396 } |
397 | 397 |
398 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 398 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
399 GrProcessorKeyBuilder* b) { | 399 GrProcessorKeyBuilder* b) { |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 | 889 |
890 /////////////////////////////////////////////////////////////////////////////// | 890 /////////////////////////////////////////////////////////////////////////////// |
891 | 891 |
892 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 892 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
893 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 893 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
894 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 894 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
895 | 895 |
896 /////////////////////////////////////////////////////////////////////////////// | 896 /////////////////////////////////////////////////////////////////////////////// |
897 | 897 |
898 #endif | 898 #endif |
OLD | NEW |