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]; | |
| 545 | |
|
robertphillips
2016/08/18 17:33:16
Let's (you, me, Jim & maybe Mike) talk about chang
vjiaoblack
2016/08/24 14:34:06
Acknowledged.
| |
| 546 SkPixmap* shPixels[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | |
| 547 int nonAmbLightCnt = 0; | |
| 548 for (int i = 0; i < lightShader.fLights->numLights(); i++) { | |
| 549 if (lightShader.fLights->light(i).type() == SkLights::Light::kDirectiona l_LightType) { | |
|
robertphillips
2016/08/18 17:33:16
Eeek! A new&delete for each span, for each shadowe
vjiaoblack
2016/08/24 14:34:06
Done.
| |
| 550 shPixels[nonAmbLightCnt] = new SkPixmap(); | |
| 551 if (!lightShader.fLights->light(i).getShadowMap()-> | |
| 552 peekPixels(shPixels[nonAmbLightCnt])) { | |
| 553 shPixels[nonAmbLightCnt] = nullptr; | |
| 554 } | |
| 555 nonAmbLightCnt++; | |
| 556 } | |
| 557 } | |
| 544 | 558 |
| 545 do { | 559 do { |
| 546 int n = SkTMin(count, BUFFER_MAX); | 560 int n = SkTMin(count, BUFFER_MAX); |
| 547 | 561 |
| 548 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
| 549 fDiffuseContext->shadeSpan(x, y, diffuse, n); | 562 fDiffuseContext->shadeSpan(x, y, diffuse, n); |
| 563 fPovDepthContext->shadeSpan(x, y, povDepth, n); | |
| 550 | 564 |
| 551 for (int i = 0; i < n; ++i) { | 565 for (int i = 0; i < n; ++i) { |
| 566 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
|
robertphillips
2016/08/18 17:33:16
It seems like the depth map won't be in premul spa
vjiaoblack
2016/08/24 14:34:06
Done.
| |
| 567 SkColor povDepthColor = SkUnPreMultiply::PMColorToColor(povDepth[i]) ; | |
| 552 | 568 |
| 553 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 569 SkColor3f totalColor = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| 570 SkColor3f totalLight = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
| 571 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | |
| 554 | 572 |
| 555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 573 nonAmbLightCnt = 0; |
| 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) { | 574 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 558 const SkLights::Light& light = lightShader.fLights->light(l); | 575 const SkLights::Light& light = lightShader.fLights->light(l); |
| 559 | 576 |
| 560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 577 if (light.type() == SkLights::Light::kDirectional_LightType) { |
| 561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 578 int xOffset = SkScalarRoundToInt(light.dir().fX * |
| 562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 579 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
| 563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 580 int yOffset = SkScalarRoundToInt(light.dir().fY * |
| 564 } else { | 581 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
| 565 // scaling by fZ accounts for lighting direction | 582 |
| 566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 583 int shX = (x + i + xOffset); |
| 567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 584 int shY = (y + yOffset); |
| 568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 585 |
| 586 shX = SkClampPos(shX); | |
| 587 shY = SkClampPos(shY); | |
| 588 | |
| 589 shX = SkClampMax(shX, light.getShadowMap()->width() - 1); | |
| 590 shY = SkClampMax(shY, light.getShadowMap()->height() - 1); | |
| 591 | |
| 592 int shDepth = 0; | |
| 593 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel | |
| 594 | |
| 595 if (shPixels[nonAmbLightCnt]) { | |
| 596 uint32_t pix = *shPixels[nonAmbLightCnt]->addr32(shX, sh Y); | |
| 597 SkColor shColor(pix); | |
| 598 | |
| 599 shDepth += SkColorGetB(shColor); | |
| 600 } else { | |
| 601 // TODO: handle not being able to read shadow map pixels | |
| 602 } | |
| 603 | |
| 604 if (pvDepth >= shDepth) { | |
| 605 // assume object normals are pointing straight up | |
| 606 totalLight.fX += light.dir().fZ * light.color().fX; | |
| 607 totalLight.fY += light.dir().fZ * light.color().fY; | |
| 608 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
| 609 } | |
| 610 nonAmbLightCnt++; | |
| 611 } else if (light.type() == SkLights::Light::kAmbient_LightType) { | |
| 612 totalLight += light.color(); | |
| 569 } | 613 } |
| 570 } | 614 } |
| 571 | 615 |
| 572 result[i] = convert(accum, SkColorGetA(diffColor)); | 616 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
| 617 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
| 618 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
| 619 | |
| 620 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
| 573 } | 621 } |
| 574 | 622 |
| 575 result += n; | 623 result += n; |
| 576 x += n; | 624 x += n; |
| 577 count -= n; | 625 count -= n; |
| 578 } while (count > 0); | 626 } while (count > 0); |
| 627 | |
| 628 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
| 629 if (shPixels[i]) { | |
| 630 delete shPixels[i]; | |
| 631 } | |
| 632 } | |
| 579 } | 633 } |
| 580 | 634 |
| 581 //////////////////////////////////////////////////////////////////////////// | 635 //////////////////////////////////////////////////////////////////////////// |
| 582 | 636 |
| 583 #ifndef SK_IGNORE_TO_STRING | 637 #ifndef SK_IGNORE_TO_STRING |
| 584 void SkShadowShaderImpl::toString(SkString* str) const { | 638 void SkShadowShaderImpl::toString(SkString* str) const { |
| 585 str->appendf("ShadowShader: ()"); | 639 str->appendf("ShadowShader: ()"); |
| 586 } | 640 } |
| 587 #endif | 641 #endif |
| 588 | 642 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 | 771 |
| 718 /////////////////////////////////////////////////////////////////////////////// | 772 /////////////////////////////////////////////////////////////////////////////// |
| 719 | 773 |
| 720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 774 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 775 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 776 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 723 | 777 |
| 724 /////////////////////////////////////////////////////////////////////////////// | 778 /////////////////////////////////////////////////////////////////////////////// |
| 725 | 779 |
| 726 #endif | 780 #endif |
| OLD | NEW |