Chromium Code Reviews| 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 | 8 |
| 9 #include "SkLights.h" | 9 #include "SkLights.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 534 } | 534 } |
| 535 | 535 |
| 536 // larger is better (fewer times we have to loop), but we shouldn't | 536 // larger is better (fewer times we have to loop), but we shouldn't |
| 537 // take up too much stack-space (each one here costs 16 bytes) | 537 // take up too much stack-space (each one here costs 16 bytes) |
| 538 #define BUFFER_MAX 16 | 538 #define BUFFER_MAX 16 |
| 539 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, | 539 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, |
| 540 SkPMColor result[], int count) { | 540 SkPMColor result[], int count) { |
| 541 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); | 541 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); |
| 542 | 542 |
| 543 SkPMColor diffuse[BUFFER_MAX]; | 543 SkPMColor diffuse[BUFFER_MAX]; |
| 544 SkPMColor povDepth[BUFFER_MAX]; | |
| 544 | 545 |
| 545 do { | 546 do { |
| 546 int n = SkTMin(count, BUFFER_MAX); | 547 int n = SkTMin(count, BUFFER_MAX); |
| 547 | 548 |
| 548 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
| 549 fDiffuseContext->shadeSpan(x, y, diffuse, n); | 549 fDiffuseContext->shadeSpan(x, y, diffuse, n); |
| 550 fPovDepthContext->shadeSpan(x, y, povDepth, n); | |
| 550 | 551 |
| 551 for (int i = 0; i < n; ++i) { | 552 for (int i = 0; i < n; ++i) { |
| 553 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
| 554 SkColor povDepthColor = SkUnPreMultiply::PMColorToColor(povDepth[i]) ; | |
| 552 | 555 |
| 553 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 556 SkColor3f totalColor = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| 557 SkColor3f totalLight = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
| 558 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | |
| 554 | 559 |
| 555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
| 556 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | |
| 557 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 560 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 558 const SkLights::Light& light = lightShader.fLights->light(l); | 561 const SkLights::Light& light = lightShader.fLights->light(l); |
| 559 | 562 |
| 560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 563 if (light.type() == SkLights::Light::kDirectional_LightType) { |
| 561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 564 int xOffset = round(light.dir().fX * (SkScalar) SkColorGetB( povDepthColor)); |
|
jvanverth1
2016/08/16 00:57:18
SkScalarRoundToInt instead of round(), and SkIntTo
vjiaoblack
2016/08/17 13:25:02
Done.
| |
| 562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 565 int yOffset = round(light.dir().fY * (SkScalar) SkColorGetB( povDepthColor)); |
| 563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 566 |
| 564 } else { | 567 int shX = (x + i + xOffset); |
| 565 // scaling by fZ accounts for lighting direction | 568 int shY = (y + yOffset); |
| 566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 569 |
| 567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 570 if (shX < 0) { shX = 0; } |
|
jvanverth1
2016/08/16 00:57:18
shX = SkClampPos(shX);
vjiaoblack
2016/08/17 13:25:02
Done.
| |
| 568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 571 if (shY < 0) { shY = 0; } |
| 572 | |
| 573 if (shX >= light.getShadowMap()->width()) { | |
|
jvanverth1
2016/08/16 00:57:18
Or replace both the bounds checks with SkClampMax(
vjiaoblack
2016/08/17 13:25:02
Done.
| |
| 574 shX = light.getShadowMap()->width() - 1; | |
| 575 } | |
| 576 | |
| 577 if (shY >= light.getShadowMap()->height()) { | |
| 578 shY = light.getShadowMap()->height() - 1; | |
| 579 } | |
| 580 | |
| 581 SkPixmap* shPixels = new SkPixmap(); | |
| 582 int shDepth = 0; | |
| 583 int pvDepth = SkColorGetB(povDepthColor); | |
|
jvanverth1
2016/08/16 00:57:18
Why B?
vjiaoblack
2016/08/17 13:25:02
We stidk the depth into the blue channel. I'll mak
| |
| 584 | |
| 585 if (light.getShadowMap()->peekPixels(shPixels)) { | |
|
jvanverth1
2016/08/16 00:57:18
This seems very inefficient -- instead I suggest b
vjiaoblack
2016/08/17 13:25:02
Done.
| |
| 586 uint32_t pix = *shPixels->addr32(shX, shY); | |
|
jvanverth1
2016/08/16 00:57:18
This looks like it will do nearest neighbor sampli
vjiaoblack
2016/08/17 13:25:02
Well, we want it to alias - interpolation would be
| |
| 587 SkColor shColor(pix); | |
| 588 | |
| 589 shDepth += SkColorGetB(shColor); | |
| 590 } else { | |
| 591 // TODO: handle not being able to read shadow map pixels | |
| 592 } | |
| 593 | |
| 594 if (pvDepth >= shDepth) { | |
| 595 // assume object normals are pointing straight up | |
| 596 totalLight.fX += light.dir().fZ * light.color().fX; | |
| 597 totalLight.fY += light.dir().fZ * light.color().fY; | |
| 598 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
| 599 } | |
| 600 } else if (light.type() == SkLights::Light::kAmbient_LightType) { | |
| 601 totalLight += light.color(); | |
| 569 } | 602 } |
| 570 } | 603 } |
| 571 | 604 |
| 572 result[i] = convert(accum, SkColorGetA(diffColor)); | 605 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
| 606 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
| 607 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
| 608 | |
| 609 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
| 573 } | 610 } |
| 574 | 611 |
| 575 result += n; | 612 result += n; |
| 576 x += n; | 613 x += n; |
| 577 count -= n; | 614 count -= n; |
| 578 } while (count > 0); | 615 } while (count > 0); |
| 579 } | 616 } |
| 580 | 617 |
| 581 //////////////////////////////////////////////////////////////////////////// | 618 //////////////////////////////////////////////////////////////////////////// |
| 582 | 619 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 | 754 |
| 718 /////////////////////////////////////////////////////////////////////////////// | 755 /////////////////////////////////////////////////////////////////////////////// |
| 719 | 756 |
| 720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 757 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 758 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 759 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 723 | 760 |
| 724 /////////////////////////////////////////////////////////////////////////////// | 761 /////////////////////////////////////////////////////////////////////////////// |
| 725 | 762 |
| 726 #endif | 763 #endif |
| OLD | NEW |