Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: src/core/SkShadowShader.cpp

Issue 2248493002: raster shadows (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: compressed shadow-map indexing code Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 uint32_t getFlags() const override { return fFlags; } 55 uint32_t getFlags() const override { return fFlags; }
56 56
57 private: 57 private:
58 SkShader::Context* fPovDepthContext; 58 SkShader::Context* fPovDepthContext;
59 SkShader::Context* fDiffuseContext; 59 SkShader::Context* fDiffuseContext;
60 uint32_t fFlags; 60 uint32_t fFlags;
61 61
62 void* fHeapAllocated; 62 void* fHeapAllocated;
63 63
64 int fNonAmbLightCnt;
65 SkPixmap* fShadowMapPixels;
66
67
64 typedef SkShader::Context INHERITED; 68 typedef SkShader::Context INHERITED;
65 }; 69 };
66 70
67 SK_TO_STRING_OVERRIDE() 71 SK_TO_STRING_OVERRIDE()
68 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl) 72 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl)
69 73
70 protected: 74 protected:
71 void flatten(SkWriteBuffer&) const override; 75 void flatten(SkWriteBuffer&) const override;
72 size_t onContextSize(const ContextRec&) const override; 76 size_t onContextSize(const ContextRec&) const override;
73 Context* onCreateContext(const ContextRec&, void*) const override; 77 Context* onCreateContext(const ContextRec&, void*) const override;
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 , fHeapAllocated(heapAllocated) { 639 , fHeapAllocated(heapAllocated) {
636 bool isOpaque = shader.isOpaque(); 640 bool isOpaque = shader.isOpaque();
637 641
638 // update fFlags 642 // update fFlags
639 uint32_t flags = 0; 643 uint32_t flags = 0;
640 if (isOpaque && (255 == this->getPaintAlpha())) { 644 if (isOpaque && (255 == this->getPaintAlpha())) {
641 flags |= kOpaqueAlpha_Flag; 645 flags |= kOpaqueAlpha_Flag;
642 } 646 }
643 647
644 fFlags = flags; 648 fFlags = flags;
649
650 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader);
651
652 fNonAmbLightCnt = lightShader.fLights->numLights();
653 fShadowMapPixels = new SkPixmap[fNonAmbLightCnt];
654
655 for (int i = 0; i < fNonAmbLightCnt; i++) {
656 if (lightShader.fLights->light(i).type() == SkLights::Light::kDirectiona l_LightType) {
657 lightShader.fLights->light(i).getShadowMap()->
658 peekPixels(&fShadowMapPixels[i]);
659 }
660 }
645 } 661 }
646 662
647 SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() { 663 SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() {
664 delete[] fShadowMapPixels;
665
648 // The dependencies have been created outside of the context on memory that was allocated by 666 // The dependencies have been created outside of the context on memory that was allocated by
649 // the onCreateContext() method. Call the destructors and free the memory. 667 // the onCreateContext() method. Call the destructors and free the memory.
650 fPovDepthContext->~Context(); 668 fPovDepthContext->~Context();
651 fDiffuseContext->~Context(); 669 fDiffuseContext->~Context();
652 670
653 sk_free(fHeapAllocated); 671 sk_free(fHeapAllocated);
654 } 672 }
655 673
656 static inline SkPMColor convert(SkColor3f color, U8CPU a) { 674 static inline SkPMColor convert(SkColor3f color, U8CPU a) {
657 if (color.fX <= 0.0f) { 675 if (color.fX <= 0.0f) {
(...skipping 18 matching lines...) Expand all
676 } 694 }
677 695
678 // larger is better (fewer times we have to loop), but we shouldn't 696 // larger is better (fewer times we have to loop), but we shouldn't
679 // take up too much stack-space (each one here costs 16 bytes) 697 // take up too much stack-space (each one here costs 16 bytes)
680 #define BUFFER_MAX 16 698 #define BUFFER_MAX 16
681 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, 699 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y,
682 SkPMColor result[], int count) { 700 SkPMColor result[], int count) {
683 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); 701 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader);
684 702
685 SkPMColor diffuse[BUFFER_MAX]; 703 SkPMColor diffuse[BUFFER_MAX];
704 SkPMColor povDepth[BUFFER_MAX];
686 705
687 do { 706 do {
688 int n = SkTMin(count, BUFFER_MAX); 707 int n = SkTMin(count, BUFFER_MAX);
689 708
690 fPovDepthContext->shadeSpan(x, y, diffuse, n);
691 fDiffuseContext->shadeSpan(x, y, diffuse, n); 709 fDiffuseContext->shadeSpan(x, y, diffuse, n);
710 fPovDepthContext->shadeSpan(x, y, povDepth, n);
692 711
693 for (int i = 0; i < n; ++i) { 712 for (int i = 0; i < n; ++i) {
713 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]);
714 SkColor povDepthColor = povDepth[i];
694 715
695 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); 716 SkColor3f totalLight = lightShader.fLights->ambientLightColor();
696
697 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f);
698 // This is all done in linear unpremul color space (each component 0 ..255.0f though) 717 // This is all done in linear unpremul color space (each component 0 ..255.0f though)
699 718
700 accum.fX += lightShader.fLights->ambientLightColor().fX * SkColorGet R(diffColor);
701 accum.fY += lightShader.fLights->ambientLightColor().fY * SkColorGet G(diffColor);
702 accum.fZ += lightShader.fLights->ambientLightColor().fZ * SkColorGet B(diffColor);
703
704 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { 719 for (int l = 0; l < lightShader.fLights->numLights(); ++l) {
705 const SkLights::Light& light = lightShader.fLights->light(l); 720 const SkLights::Light& light = lightShader.fLights->light(l);
706 721
707 if (SkLights::Light::kDirectional_LightType == light.type()) { 722 if (light.type() == SkLights::Light::kDirectional_LightType) {
708 // scaling by fZ accounts for lighting direction 723 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel
709 accum.fX += light.color().makeScale(light.dir().fZ).fX * 724
710 SkColorGetR(diffColor); 725 int xOffset = SkScalarRoundToInt(light.dir().fX * pvDepth);
711 accum.fY += light.color().makeScale(light.dir().fZ).fY * 726 int yOffset = SkScalarRoundToInt(light.dir().fY * pvDepth);
712 SkColorGetG(diffColor); 727
713 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * 728 int shX = SkClampMax(x + i + xOffset, light.getShadowMap()-> width() - 1);
714 SkColorGetB(diffColor); 729 int shY = SkClampMax(y + yOffset, light.getShadowMap()->heig ht() - 1);
730
731 int shDepth = 0;
732
733 // pixmaps that point to things have nonzero heights
734 if (fShadowMapPixels[l].height() > 0) {
735 uint32_t pix = *fShadowMapPixels[l].addr32(shX, shY);
736 SkColor shColor(pix);
737
738 shDepth = SkColorGetB(shColor);
739 } else {
740 // Make lights w/o a shadow map receive the full light c ontribution
741 shDepth = pvDepth;
742 }
743
744 if (pvDepth >= shDepth) {
745 // assume object normals are pointing straight up
746 totalLight.fX += light.dir().fZ * light.color().fX;
747 totalLight.fY += light.dir().fZ * light.color().fY;
748 totalLight.fZ += light.dir().fZ * light.color().fZ;
749 }
715 } else { 750 } else {
716 accum.fX += light.color().fX * SkColorGetR(diffColor); 751 totalLight += light.color();
717 accum.fY += light.color().fY * SkColorGetG(diffColor);
718 accum.fZ += light.color().fZ * SkColorGetB(diffColor);
719 } 752 }
720
721 } 753 }
722 754
723 result[i] = convert(accum, SkColorGetA(diffColor)); 755 SkColor3f totalColor = SkColor3f::Make(SkColorGetR(diffColor) * tota lLight.fX,
756 SkColorGetG(diffColor) * tota lLight.fY,
757 SkColorGetB(diffColor) * tota lLight.fZ);
758
759 result[i] = convert(totalColor, SkColorGetA(diffColor));
724 } 760 }
725 761
726 result += n; 762 result += n;
727 x += n; 763 x += n;
728 count -= n; 764 count -= n;
729 } while (count > 0); 765 } while (count > 0);
730 } 766 }
731 767
732 //////////////////////////////////////////////////////////////////////////// 768 ////////////////////////////////////////////////////////////////////////////
733 769
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 872
837 /////////////////////////////////////////////////////////////////////////////// 873 ///////////////////////////////////////////////////////////////////////////////
838 874
839 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) 875 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader)
840 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) 876 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl)
841 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 877 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
842 878
843 /////////////////////////////////////////////////////////////////////////////// 879 ///////////////////////////////////////////////////////////////////////////////
844 880
845 #endif 881 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698