Index: src/core/SkShadowMapShader.cpp |
diff --git a/src/core/SkShadowMapShader.cpp b/src/core/SkShadowMapShader.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..98c9ae02bd513b47af7ae0fcdaba4a23050c8e2a |
--- /dev/null |
+++ b/src/core/SkShadowMapShader.cpp |
@@ -0,0 +1,581 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+ |
+#include "SkLights.h" |
+#include "SkReadBuffer.h" |
+#include "SkShadowMapShader.h" |
+#include "SkPoint3.h" |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+#ifdef SK_EXPERIMENTAL_SHADOWING |
+ |
+ |
+/** \class SkShadowMapShaderImpl |
+ This subclass of shader applies shadowing |
+*/ |
+class SkShadowMapShaderImpl : public SkShader { |
+public: |
+ /** Create a new shadowing shader that shadows |
+ @param to do to do |
+ */ |
+ SkShadowMapShaderImpl(sk_sp<SkShader> povDepthShader, |
+ sk_sp<SkLights> lights, |
+ int diffuseWidth, int diffuseHeight) |
+ : fPovDepthShader(std::move(povDepthShader)) |
+ , fLights(std::move(lights)) |
+ , fDiffuseWidth(diffuseWidth) |
+ , fDiffuseHeight(diffuseHeight) { } |
+ |
+ bool isOpaque() const override; |
+ |
+#if SK_SUPPORT_GPU |
+ sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override; |
+#endif |
+ |
+ class ShadowMapShaderContext : public SkShader::Context { |
+ public: |
+ // The context takes ownership of the states. It will call their destructors |
+ // but will NOT free the memory. |
+ ShadowMapShaderContext(const SkShadowMapShaderImpl&, const ContextRec&, |
+ SkShader::Context* povDepthContext, |
+ void* heapAllocated); |
+ |
+ ~ShadowMapShaderContext() override; |
+ |
+ void shadeSpan(int x, int y, SkPMColor[], int count) override; |
+ |
+ uint32_t getFlags() const override { return fFlags; } |
+ |
+ private: |
+ SkShader::Context* fPovDepthContext; |
+ uint32_t fFlags; |
+ |
+ void* fHeapAllocated; |
+ |
+ typedef SkShader::Context INHERITED; |
+ }; |
+ |
+ SK_TO_STRING_OVERRIDE() |
+SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowMapShaderImpl) |
+ |
+protected: |
+ void flatten(SkWriteBuffer&) const override; |
+ size_t onContextSize(const ContextRec&) const override; |
+ Context* onCreateContext(const ContextRec&, void*) const override; |
+ |
+private: |
+ sk_sp<SkShader> fPovDepthShader; |
+ sk_sp<SkLights> fLights; |
+ |
+ int fDiffuseWidth; |
+ int fDiffuseHeight; |
+ |
+ friend class SkShadowMapShader; |
+ |
+ typedef SkShader INHERITED; |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+#if SK_SUPPORT_GPU |
+ |
+#include "GrCoordTransform.h" |
+#include "GrFragmentProcessor.h" |
+#include "GrInvariantOutput.h" |
+#include "glsl/GrGLSLFragmentProcessor.h" |
+#include "glsl/GrGLSLFragmentShaderBuilder.h" |
+#include "SkGr.h" |
+#include "SkGrPriv.h" |
+#include "SkSpecialImage.h" |
+#include "SkImage_Base.h" |
+#include "GrContext.h" |
+ |
+class ShadowMapFP : public GrFragmentProcessor { |
+public: |
+ ShadowMapFP(sk_sp<GrFragmentProcessor> povDepth, |
+ sk_sp<SkLights> lights, |
+ int diffuseWidth, int diffuseHeight, |
+ GrContext* context) { |
+ |
+ // fuse all ambient lights into a single one |
+ |
+ fLightDir = lights->light(0).dir(); |
+ SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(0).getShadowMap()); |
+ |
+ // gets deleted when the ShadowMapFP is destroyed, and frees the GrTexture* |
+ fTexture = sk_sp<GrTexture>(shadowMap->asTextureRef(context, |
+ GrTextureParams::ClampNoFilter(), |
+ SkSourceGammaTreatment::kIgnore)); |
+ fDepthMapAccess.reset(fTexture.get()); |
+ this->addTextureAccess(&fDepthMapAccess); |
+ |
+ fDepthMapHeight = shadowMap->height(); |
+ fDepthMapWidth = shadowMap->width(); |
+ |
+ fWidth = diffuseWidth; |
+ fHeight = diffuseHeight; |
+ |
+ this->registerChildProcessor(std::move(povDepth)); |
+ this->initClassID<ShadowMapFP>(); |
+ } |
+ |
+ class GLSLShadowMapFP : public GrGLSLFragmentProcessor { |
+ public: |
+ GLSLShadowMapFP() { } |
+ |
+ void emitCode(EmitArgs& args) override { |
+ |
+ GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
+ GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
+ |
+ // add uniforms |
+ const char* lightDirUniName = nullptr; |
+ const char* depthMapWidthUniName = nullptr; |
+ const char* depthMapHeightUniName = nullptr; |
+ |
+ |
+ fLightDirUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kVec3f_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "lightDir", |
+ &lightDirUniName); |
+ |
+ fDepthMapWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kInt_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "dmapWidth", |
+ &depthMapWidthUniName); |
+ fDepthMapHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kInt_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "dmapHeight", |
+ &depthMapHeightUniName); |
+ |
+ |
+ const char* widthUniName = nullptr; |
+ const char* heightUniName = nullptr; |
+ |
+ fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kInt_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "width", &widthUniName); |
+ fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kInt_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "height", &heightUniName); |
+ |
+ |
+ SkString povDepth("povDepth"); |
+ this->emitChild(0, nullptr, &povDepth, args); |
+ |
+ SkString depthMap; |
+ |
+ SkString povCoord("povCoord"); |
+ |
+ // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. |
+ // povDepth.b * 255 scales it to 0 - 255, bringing it to world space, |
+ // and the / 400 brings it back to a sampler coordinate, 0 - 1 |
+ // The 400 comes from the shadowmaps GM. |
+ // TODO use real shadowmaps size |
+ SkString offset("offset"); |
+ SkString scaleVec("scaleVec"); |
+ SkString scaleOffsetVec("scaleOffsetVec"); |
+ |
+ fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n", |
+ offset.c_str(), lightDirUniName); |
+ |
+ fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s));\n", |
+ scaleVec.c_str(), |
+ widthUniName, heightUniName, |
+ depthMapWidthUniName, depthMapHeightUniName); |
+ |
+ fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_0_Stage0 + " |
+ "vec2(%s.x, 0 - %s.y)) " |
+ " * %s + vec2(0,1) * (1 - %s);\n", |
+ povCoord.c_str(), offset.c_str(), offset.c_str(), |
+ scaleVec.c_str(), scaleVec.c_str()); |
+// |
+// fragBuilder->codeAppendf(" %s *= %s;\n", |
+// povCoord.c_str(), scaleVec.c_str()); |
+ |
+// |
+ fragBuilder->appendTextureLookup(&depthMap, args.fTexSamplers[0], |
+ povCoord.c_str(), |
+ kVec2f_GrSLType); |
+// |
+ |
+// fragBuilder->codeAppendf("if (%s.b == %s.b) {" |
+// "%s = vec4(0,0,0,1);" |
+// "} else {", povDepth.c_str(), depthMap.c_str() |
+// , args.fOutputColor); |
+// fragBuilder->codeAppendf("%s = %s;" |
+// "}", args.fOutputColor, depthMap.c_str()); |
+ |
+ fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, depthMap.c_str()); |
+ |
+// |
+ |
+ |
+ } |
+ |
+ static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
+ GrProcessorKeyBuilder* b) { |
+ const ShadowMapFP& shadowMapFP = proc.cast<ShadowMapFP>(); |
+ b->add32(shadowMapFP.fNumDirLights); |
+ } |
+ |
+ protected: |
+ void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
+ const ShadowMapFP &shadowMapFP = proc.cast<ShadowMapFP>(); |
+ |
+ const SkVector3& lightDir = shadowMapFP.lightDir(); |
+ if (lightDir != fLightDir) { |
+ pdman.set3fv(fLightDirUni, 1, &lightDir.fX); |
+ fLightDir = lightDir; |
+ } |
+ |
+ int depthMapWidth = shadowMapFP.depthMapWidth(); |
+ if (depthMapWidth != fDepthMapWidth) { |
+ pdman.set1i(fDepthMapWidthUni, depthMapWidth); |
+ fDepthMapWidth = depthMapWidth; |
+ } |
+ int depthMapHeight = shadowMapFP.depthMapHeight(); |
+ if (depthMapHeight != fDepthMapHeight) { |
+ pdman.set1i(fDepthMapHeightUni, depthMapHeight); |
+ fDepthMapHeight = depthMapHeight; |
+ } |
+ |
+ int width = shadowMapFP.width(); |
+ if (width != fWidth) { |
+ pdman.set1i(fWidthUni, width); |
+ fWidth = width; |
+ } |
+ int height = shadowMapFP.height(); |
+ if (height != fHeight) { |
+ pdman.set1i(fHeightUni, height); |
+ fHeight = height; |
+ } |
+ |
+ } |
+ |
+ private: |
+ SkVector3 fLightDir; |
+ GrGLSLProgramDataManager::UniformHandle |
+ fLightDirUni; |
+ |
+ |
+ int fDepthMapWidth; |
+ GrGLSLProgramDataManager::UniformHandle |
+ fDepthMapWidthUni; |
+ |
+ int fDepthMapHeight; |
+ GrGLSLProgramDataManager::UniformHandle |
+ fDepthMapHeightUni; |
+ |
+ int fWidth; |
+ GrGLSLProgramDataManager::UniformHandle fWidthUni; |
+ int fHeight; |
+ GrGLSLProgramDataManager::UniformHandle fHeightUni; |
+ }; |
+ |
+ void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
+ GLSLShadowMapFP::GenKey(*this, caps, b); |
+ } |
+ |
+ const char* name() const override { return "shadowMapFP"; } |
+ |
+ void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
+ inout->mulByUnknownFourComponents(); |
+ } |
+ int32_t numLights() const { return fNumDirLights; } |
+ const SkColor3f& ambientColor() const { return fAmbientColor; } |
+ const SkVector3& lightDir() const { |
+ return fLightDir; |
+ } |
+ int depthMapWidth() const { |
+ return fDepthMapWidth; |
+ } |
+ int depthMapHeight() const { |
+ return fDepthMapHeight; |
+ } |
+ int width() const {return fWidth; } |
+ int height() const {return fHeight; } |
+ |
+private: |
+ GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowMapFP; } |
+ |
+ bool onIsEqual(const GrFragmentProcessor& proc) const override { |
+ const ShadowMapFP& shadowMapFP = proc.cast<ShadowMapFP>(); |
+ if (fAmbientColor != shadowMapFP.fAmbientColor || fNumDirLights != shadowMapFP.fNumDirLights) { |
+ return false; |
+ } |
+ |
+ if (fWidth != shadowMapFP.fWidth || fHeight != shadowMapFP.fHeight) { |
+ return false; |
+ } |
+ |
+ for (int i = 0; i < fNumDirLights; i++) { |
+ if (fLightDir != shadowMapFP.fLightDir) { |
+ return false; |
+ } |
+ |
+ if (fDepthMapWidth != shadowMapFP.fDepthMapWidth || |
+ fDepthMapHeight != shadowMapFP.fDepthMapHeight) { |
+ return false; |
+ } |
+ } |
+ |
+ return true; |
+ } |
+ |
+ int fNumDirLights; |
+ |
+ SkVector3 fLightDir; |
+ GrTextureAccess fDepthMapAccess; |
+ sk_sp<GrTexture> fTexture; |
+ |
+ int fDepthMapWidth; |
+ int fDepthMapHeight; |
+ |
+ int fHeight; |
+ int fWidth; |
+ |
+ SkColor3f fAmbientColor; |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+sk_sp<GrFragmentProcessor> SkShadowMapShaderImpl::asFragmentProcessor(const AsFPArgs& fpargs) const { |
+ |
+ sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor(fpargs); |
+ |
+ sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowMapFP>(std::move(povDepthFP), |
+ std::move(fLights), |
+ fDiffuseWidth, fDiffuseHeight, |
+ fpargs.fContext); |
+ return shadowfp; |
+} |
+ |
+ |
+#endif |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+bool SkShadowMapShaderImpl::isOpaque() const { |
+ return true; |
+} |
+ |
+SkShadowMapShaderImpl::ShadowMapShaderContext::ShadowMapShaderContext( |
+ const SkShadowMapShaderImpl& shader, const ContextRec& rec, |
+ SkShader::Context* povDepthContext, |
+ void* heapAllocated) |
+ : INHERITED(shader, rec) |
+ , fPovDepthContext(povDepthContext) |
+ , fHeapAllocated(heapAllocated) { |
+ bool isOpaque = shader.isOpaque(); |
+ |
+ // update fFlags |
+ uint32_t flags = 0; |
+ if (isOpaque && (255 == this->getPaintAlpha())) { |
+ flags |= kOpaqueAlpha_Flag; |
+ } |
+ |
+ fFlags = flags; |
+} |
+ |
+SkShadowMapShaderImpl::ShadowMapShaderContext::~ShadowMapShaderContext() { |
+ // The dependencies have been created outside of the context on memory that was allocated by |
+ // the onCreateContext() method. Call the destructors and free the memory. |
+ fPovDepthContext->~Context(); |
+ |
+ sk_free(fHeapAllocated); |
+} |
+ |
+static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
+ if (color.fX <= 0.0f) { |
+ color.fX = 0.0f; |
+ } else if (color.fX >= 255.0f) { |
+ color.fX = 255.0f; |
+ } |
+ |
+ if (color.fY <= 0.0f) { |
+ color.fY = 0.0f; |
+ } else if (color.fY >= 255.0f) { |
+ color.fY = 255.0f; |
+ } |
+ |
+ if (color.fZ <= 0.0f) { |
+ color.fZ = 0.0f; |
+ } else if (color.fZ >= 255.0f) { |
+ color.fZ = 255.0f; |
+ } |
+ |
+ return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ); |
+} |
+ |
+// larger is better (fewer times we have to loop), but we shouldn't |
+// take up too much stack-space (each one here costs 16 bytes) |
+#define BUFFER_MAX 16 |
+void SkShadowMapShaderImpl::ShadowMapShaderContext::shadeSpan(int x, int y, |
+ SkPMColor result[], int count) { |
+ const SkShadowMapShaderImpl& lightShader = static_cast<const SkShadowMapShaderImpl&>(fShader); |
+ |
+ SkPMColor diffuse[BUFFER_MAX]; |
+ |
+ do { |
+ int n = SkTMin(count, BUFFER_MAX); |
+ |
+ fPovDepthContext->shadeSpan(x, y, diffuse, n); |
+ |
+ for (int i = 0; i < n; ++i) { |
+ |
+ SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); |
+ |
+ SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
+ // This is all done in linear unpremul color space (each component 0..255.0f though) |
+ const SkLights::Light& light = lightShader.fLights->light(0); |
+ |
+ accum.fX += light.color().fX * SkColorGetR(diffColor); |
+ accum.fY += light.color().fY * SkColorGetG(diffColor); |
+ accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
+ |
+ result[i] = convert(accum, SkColorGetA(diffColor)); |
+ } |
+ |
+ result += n; |
+ x += n; |
+ count -= n; |
+ } while (count > 0); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+#ifndef SK_IGNORE_TO_STRING |
+void SkShadowMapShaderImpl::toString(SkString* str) const { |
+ str->appendf("ShadowMapShader: ()"); |
+} |
+#endif |
+ |
+sk_sp<SkFlattenable> SkShadowMapShaderImpl::CreateProc(SkReadBuffer& buf) { |
+ |
+ // Discarding SkShader flattenable params |
+ bool hasLocalMatrix = buf.readBool(); |
+ SkAssertResult(!hasLocalMatrix); |
+ |
+ SkLights::Builder builder; |
+ |
+ bool isAmbient = buf.readBool(); |
+ |
+ SkColor3f color; |
+ if (!buf.readScalarArray(&color.fX, 3)) { |
+ return nullptr; |
+ } |
+ |
+ if (isAmbient) { |
+ builder.add(SkLights::Light(color)); |
+ } else { |
+ SkVector3 dir; |
+ if (!buf.readScalarArray(&dir.fX, 3)) { |
+ return nullptr; |
+ } |
+ |
+ sk_sp<SkImage> depthMap; |
+ if (!(depthMap = sk_ref_sp<SkImage>(buf.readImage()))) { |
+ return nullptr; |
+ } |
+ |
+ SkLights::Light light = SkLights::Light(color, dir); |
+ light.setShadowMap(depthMap); |
+ |
+ builder.add(light); |
+ } |
+ |
+ sk_sp<SkLights> lights(builder.finish()); |
+ |
+ int diffuseWidth = buf.readInt(); |
+ int diffuseHeight = buf.readInt(); |
+ |
+ sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
+ |
+ return sk_make_sp<SkShadowMapShaderImpl>(std::move(povDepthShader), |
+ std::move(lights), |
+ diffuseWidth, diffuseHeight); |
+} |
+ |
+void SkShadowMapShaderImpl::flatten(SkWriteBuffer& buf) const { |
+ this->INHERITED::flatten(buf); |
+ |
+ buf.writeInt(fLights->numLights()); |
+ |
+ const SkLights::Light& light = fLights->light(0); |
+ |
+ bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
+ |
+ buf.writeBool(isAmbient); |
+ buf.writeScalarArray(&light.color().fX, 3); |
+ if (!isAmbient) { |
+ buf.writeScalarArray(&light.dir().fX, 3); |
+ } |
+ |
+ buf.writeImage(light.getShadowMap()); |
+ |
+ buf.writeInt(fDiffuseWidth); |
+ buf.writeInt(fDiffuseHeight); |
+ |
+ buf.writeFlattenable(fPovDepthShader.get()); |
+} |
+ |
+size_t SkShadowMapShaderImpl::onContextSize(const ContextRec& rec) const { |
+ return sizeof(ShadowMapShaderContext); |
+} |
+ |
+SkShader::Context* SkShadowMapShaderImpl::onCreateContext(const ContextRec& rec, |
+ void* storage) const { |
+ size_t heapRequired = fPovDepthShader->contextSize(rec); |
+ |
+ void* heapAllocated = sk_malloc_throw(heapRequired); |
+ |
+ void* povDepthContextStorage = heapAllocated; |
+ |
+ SkShader::Context* povDepthContext = |
+ fPovDepthShader->createContext(rec, povDepthContextStorage); |
+ |
+ if (!povDepthContext) { |
+ sk_free(heapAllocated); |
+ return nullptr; |
+ } |
+ |
+ return new (storage) ShadowMapShaderContext(*this, rec, povDepthContext, |
+ heapAllocated); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+sk_sp<SkShader> SkShadowMapShader::Make(sk_sp<SkShader> povDepthShader, |
+ sk_sp<SkLights> lights, |
+ int diffuseWidth, int diffuseHeight) { |
+ if (!povDepthShader) { |
+ // TODO: Use paint's color in absence of a diffuseShader |
+ // TODO: Use a default implementation of normalSource instead |
+ return nullptr; |
+ } |
+ |
+ return sk_make_sp<SkShadowMapShaderImpl>(std::move(povDepthShader), |
+ std::move(lights), |
+ diffuseWidth, diffuseHeight); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowMapShader) |
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowMapShaderImpl) |
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+#endif |