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 | |
| 546 SkPixmap* shPixels[SkShadowShader::kMaxNonAmbientLights]; | |
|
jvanverth1
2016/08/17 15:42:43
If you just put the Pixmap at shPixels[i], then yo
vjiaoblack
2016/08/18 15:33:28
Well, that'd be inefficient, because I'd have to m
| |
| 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) { | |
| 550 shPixels[nonAmbLightCnt] = new SkPixmap(); | |
| 551 if (!lightShader.fLights->light(i).getShadowMap()->peekPixels(shPixe ls[i])) { | |
| 552 shPixels[nonAmbLightCnt] = nullptr; | |
|
jvanverth1
2016/08/17 15:42:43
You have a memory leak here. Use sk_sp<SkPixmap> i
vjiaoblack
2016/08/18 15:33:28
Done.
| |
| 553 } | |
| 554 nonAmbLightCnt++; | |
| 555 } | |
| 556 } | |
| 544 | 557 |
| 545 do { | 558 do { |
| 546 int n = SkTMin(count, BUFFER_MAX); | 559 int n = SkTMin(count, BUFFER_MAX); |
| 547 | 560 |
| 548 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
| 549 fDiffuseContext->shadeSpan(x, y, diffuse, n); | 561 fDiffuseContext->shadeSpan(x, y, diffuse, n); |
| 562 fPovDepthContext->shadeSpan(x, y, povDepth, n); | |
| 550 | 563 |
| 551 for (int i = 0; i < n; ++i) { | 564 for (int i = 0; i < n; ++i) { |
| 565 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
| 566 SkColor povDepthColor = SkUnPreMultiply::PMColorToColor(povDepth[i]) ; | |
| 552 | 567 |
| 553 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 568 SkColor3f totalColor = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| 569 SkColor3f totalLight = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
| 570 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | |
| 554 | 571 |
| 555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 572 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) { | 573 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 558 const SkLights::Light& light = lightShader.fLights->light(l); | 574 const SkLights::Light& light = lightShader.fLights->light(l); |
| 559 | 575 |
| 560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 576 if (light.type() == SkLights::Light::kDirectional_LightType) { |
| 561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 577 int xOffset = SkScalarRoundToInt(light.dir().fX * |
| 562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 578 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
| 563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 579 int yOffset = SkScalarRoundToInt(light.dir().fY * |
| 564 } else { | 580 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
| 565 // scaling by fZ accounts for lighting direction | 581 |
| 566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 582 int shX = (x + i + xOffset); |
| 567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 583 int shY = (y + yOffset); |
| 568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 584 |
| 585 shX = SkClampPos(shX); | |
| 586 shY = SkClampPos(shY); | |
| 587 | |
| 588 shX = SkClampMax(shX, light.getShadowMap()->width() - 1); | |
| 589 shY = SkClampMax(shY, light.getShadowMap()->height() - 1); | |
| 590 | |
| 591 int shDepth = 0; | |
| 592 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel | |
| 593 | |
| 594 if (shPixels[i]) { | |
| 595 uint32_t pix = *shPixels[nonAmbLightCnt]->addr32(shX, sh Y); | |
| 596 SkColor shColor(pix); | |
| 597 | |
| 598 shDepth += SkColorGetB(shColor); | |
| 599 } else { | |
| 600 // TODO: handle not being able to read shadow map pixels | |
| 601 } | |
| 602 | |
| 603 if (pvDepth >= shDepth) { | |
| 604 // assume object normals are pointing straight up | |
| 605 totalLight.fX += light.dir().fZ * light.color().fX; | |
| 606 totalLight.fY += light.dir().fZ * light.color().fY; | |
| 607 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
| 608 } | |
| 609 nonAmbLightCnt++; | |
| 610 } else if (light.type() == SkLights::Light::kAmbient_LightType) { | |
| 611 totalLight += light.color(); | |
| 569 } | 612 } |
| 570 } | 613 } |
| 571 | 614 |
| 572 result[i] = convert(accum, SkColorGetA(diffColor)); | 615 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
| 616 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
| 617 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
| 618 | |
| 619 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
| 573 } | 620 } |
| 574 | 621 |
| 575 result += n; | 622 result += n; |
| 576 x += n; | 623 x += n; |
| 577 count -= n; | 624 count -= n; |
| 578 } while (count > 0); | 625 } while (count > 0); |
| 579 } | 626 } |
| 580 | 627 |
| 581 //////////////////////////////////////////////////////////////////////////// | 628 //////////////////////////////////////////////////////////////////////////// |
| 582 | 629 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 | 764 |
| 718 /////////////////////////////////////////////////////////////////////////////// | 765 /////////////////////////////////////////////////////////////////////////////// |
| 719 | 766 |
| 720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 767 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 768 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 769 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 723 | 770 |
| 724 /////////////////////////////////////////////////////////////////////////////// | 771 /////////////////////////////////////////////////////////////////////////////// |
| 725 | 772 |
| 726 #endif | 773 #endif |
| OLD | NEW |