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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 scaleVec.appendf("%d", i); | 290 scaleVec.appendf("%d", i); |
291 | 291 |
292 fragBuilder->codeAppendf("vec2 %s;", offset.c_str()); | 292 fragBuilder->codeAppendf("vec2 %s;", offset.c_str()); |
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 , " |
301 "fragToLight%d = normalize(fragToLi ght%d);", | 301 "fragToLight%d );", |
302 i, i, i, i, i); | 302 i, i, i); |
303 fragBuilder->codeAppendf("%s = vec2(worldCor.x - %s.x, %s.y - worldCor.y) * " | 303 fragBuilder->codeAppendf("%s = vec2(-fragToLight%d) * povDep th.b;", |
304 "povDepth.b;", | 304 offset.c_str(), i); |
305 offset.c_str(), lightDirOrPosUniNam e[i], | 305 fragBuilder->codeAppendf("fragToLight%d = normalize(fragToLi ght%d);", |
306 lightDirOrPosUniName[i]); | 306 i, i); |
307 | |
robertphillips
2016/09/06 17:51:11
Where are the 0.375fs coming from ?
vjiaoblack
2016/09/06 19:30:32
Done.
| |
308 fragBuilder->codeAppendf("vec2 %s = ((vec2(%s, %s) *" | |
309 "vMatrixCoord_0_1_Stage0 +" | |
310 "vec2(0,%s - %s)" | |
311 "+ %s) / (vec2(%s, %s))) +" | |
312 "vec2(0.375, -0.375);", | |
313 povCoord.c_str(), | |
314 widthUniName, heightUniName, | |
315 depthMapHeightUniName[i], heightUni Name, | |
316 offset.c_str(), | |
317 depthMapWidthUniName[i], depthMapWi dthUniName[i]); | |
307 } else { | 318 } else { |
308 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * vec2( 255.0, -255.0);", | 319 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * vec2( 255.0, -255.0);", |
309 offset.c_str(), lightDirOrPosUniNam e[i]); | 320 offset.c_str(), lightDirOrPosUniNam e[i]); |
321 | |
322 fragBuilder->codeAppendf("vec2 %s = ((vec2(%s, %s) *" | |
323 "vMatrixCoord_0_1_Stage0 +" | |
324 "vec2(0,%s - %s)" | |
325 "+ %s) / vec2(%s, %s));", | |
326 povCoord.c_str(), | |
327 widthUniName, heightUniName, | |
328 depthMapHeightUniName[i], heightUni Name, | |
329 offset.c_str(), | |
330 depthMapWidthUniName[i], depthMapWi dthUniName[i]); | |
310 } | 331 } |
311 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) *" | |
312 "vMatrixCoord_0_1_Stage0 +" | |
313 "vec2(0,%s - %s)" | |
314 " + " | |
315 "%s) / vec2(%s, %s);", | |
316 povCoord.c_str(), | |
317 widthUniName, heightUniName, | |
318 depthMapHeightUniName[i], heightUniName , | |
319 offset.c_str(), | |
320 depthMapWidthUniName[i], depthMapHeight UniName[i]); | |
321 | 332 |
322 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 333 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
323 povCoord.c_str(), | 334 povCoord.c_str(), |
324 kVec2f_GrSLType); | 335 kVec2f_GrSLType); |
325 | 336 |
326 } | 337 } |
327 | 338 |
328 // helper variables for calculating shadowing | 339 // helper variables for calculating shadowing |
329 | 340 |
330 // variance of depth at this fragment in the context of surrounding area | 341 // variance of depth at this fragment in the context of surrounding area |
331 // (area size and weighting dependent on blur size and type) | 342 // (area size and weighting dependent on blur size and type) |
332 fragBuilder->codeAppendf("float variance;"); | 343 fragBuilder->codeAppendf("float variance;"); |
333 | 344 |
334 // the difference in depth between the user POV and light POV. | 345 // the difference in depth between the user POV and light POV. |
335 fragBuilder->codeAppendf("float d;"); | 346 fragBuilder->codeAppendf("float d;"); |
336 | 347 |
337 // add up light contributions from all lights to totalLightColor | 348 // add up light contributions from all lights to totalLightColor |
338 for (int i = 0; i < numLights; i++) { | 349 for (int i = 0; i < numLights; i++) { |
339 if (!shadowFP.isPointLight(i)) { | 350 fragBuilder->codeAppendf("lightProbability = 1;"); |
351 | |
352 // 1/512 == .00195... is less than half a pixel; imperceptible | |
353 fragBuilder->codeAppendf("if (%s.b <= %s.b + .001953125) {", | |
354 povDepth.c_str(), depthMaps[i].c_str()) ; | |
355 if (blurAlgorithm == SkShadowParams::kVariance_ShadowType) { | |
356 // We mess with depth and depth^2 in their given scales. | |
357 // (i.e. between 0 and 1) | |
358 fragBuilder->codeAppendf("vec2 moments%d = vec2(%s.b, %s.g); ", | |
359 i, depthMaps[i].c_str(), depthMaps[ i].c_str()); | |
360 | |
361 // variance biasing lessens light bleeding | |
362 fragBuilder->codeAppendf("variance = max(moments%d.y - " | |
363 "(moments%d.x * mome nts%d.x)," | |
364 "%s);", i, i, i, | |
365 minVarianceUniName); | |
366 | |
367 fragBuilder->codeAppendf("d = (%s.b) - moments%d.x;", | |
368 povDepth.c_str(), i); | |
369 fragBuilder->codeAppendf("lightProbability = " | |
370 "(variance / (variance + d * d));"); | |
371 | |
372 SkString clamp("clamp"); | |
373 clamp.appendf("%d", i); | |
374 | |
375 // choosing between light artifacts or correct shape shadows | |
376 // linstep | |
377 fragBuilder->codeAppendf("float %s = clamp((lightProbability - %s) /" | |
378 "(1 - %s), 0, 1);" , | |
379 clamp.c_str(), shBiasUniName, shBia sUniName); | |
380 | |
381 fragBuilder->codeAppendf("lightProbability = %s;", clamp.c_s tr()); | |
382 } else { | |
383 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | |
384 povDepth.c_str(), depthMaps[i].c_st r()); | |
340 fragBuilder->codeAppendf("lightProbability = 1;"); | 385 fragBuilder->codeAppendf("lightProbability = 1;"); |
386 fragBuilder->codeAppendf("} else { lightProbability = 0; }") ; | |
387 } | |
341 | 388 |
342 // 1/512 == .00195... is less than half a pixel; imperceptib le | 389 // VSM: The curved shadows near plane edges are artifacts from b lurring |
343 fragBuilder->codeAppendf("if (%s.b <= %s.b + .001953125) {", | 390 // lightDir.z is equal to the lightDir dot the surface normal. |
344 povDepth.c_str(), depthMaps[i].c_st r()); | 391 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 | 392 |
351 // variance biasing lessens light bleeding | 393 if (shadowFP.isPointLight(i)) { |
352 fragBuilder->codeAppendf("variance = max(moments%d.y - " | 394 fragBuilder->codeAppendf("totalLightColor += max(fragToLight %d.z, 0) * %s /" |
353 "(moments%d.x * moments%d.x)," | 395 "(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;", | 396 "lightProbabilit y;", |
397 i, lightColorUniName[i], i); | |
398 } else { | |
399 fragBuilder->codeAppendf("totalLightColor += %s.z * %s * lig htProbability;", | |
383 lightDirOrPosUniName[i], | 400 lightDirOrPosUniName[i], |
384 lightColorUniName[i]); | 401 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 } | 402 } |
391 } | 403 } |
392 | 404 |
393 fragBuilder->codeAppendf("totalLightColor += %s;", ambientColorUniNa me); | 405 fragBuilder->codeAppendf("totalLightColor += %s;", ambientColorUniNa me); |
394 fragBuilder->codeAppendf("%s = resultDiffuseColor * vec4(totalLightC olor, 1);", | 406 fragBuilder->codeAppendf("%s = resultDiffuseColor * vec4(totalLightC olor, 1);", |
395 args.fOutputColor); | 407 args.fOutputColor); |
396 } | 408 } |
397 | 409 |
398 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 410 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
399 GrProcessorKeyBuilder* b) { | 411 GrProcessorKeyBuilder* b) { |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 | 901 |
890 /////////////////////////////////////////////////////////////////////////////// | 902 /////////////////////////////////////////////////////////////////////////////// |
891 | 903 |
892 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 904 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
893 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 905 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
894 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 906 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
895 | 907 |
896 /////////////////////////////////////////////////////////////////////////////// | 908 /////////////////////////////////////////////////////////////////////////////// |
897 | 909 |
898 #endif | 910 #endif |
OLD | NEW |