Index: src/core/Sk2DShadowMapShader.cpp |
diff --git a/src/core/Sk2DShadowMapShader.cpp b/src/core/Sk2DShadowMapShader.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5af7761702650d713c20ed75d127750f9a230fd5 |
--- /dev/null |
+++ b/src/core/Sk2DShadowMapShader.cpp |
@@ -0,0 +1,424 @@ |
+/* |
+ * 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 "Sk2DShadowMapShader.h" |
+#include "SkLights.h" |
+#include "SkPoint3.h" |
+#include "SkReadBuffer.h" |
+#include "SkShadowShader.h" |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+#ifdef SK_EXPERIMENTAL_SHADOWING |
+ |
+ |
+/** \class Sk2DShadowMapShaderImpl |
+ This subclass of shader applies shadowing |
jvanverth1
2016/09/07 14:47:23
Needs a little more detail here. "... shadowing ra
vjiaoblack
2016/09/07 15:21:39
Done.
|
+*/ |
+class Sk2DShadowMapShaderImpl : public SkShader { |
+public: |
+ /** Create a new shadowing shader that shadows |
jvanverth1
2016/09/07 14:47:23
"... shadows radially around a light"?
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ */ |
+ Sk2DShadowMapShaderImpl(sk_sp<SkShader> occluderShader, |
+ sk_sp<SkLights> lights, |
jvanverth1
2016/09/07 14:47:23
Nit: indent to (
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ int diffuseWidth, int diffuseHeight) |
+ : fOccluderShader(std::move(occluderShader)) |
jvanverth1
2016/09/07 14:47:23
Nit: indent 4 spaces
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ , fLight(std::move(lights)) |
+ , fWidth(diffuseWidth) |
+ , fHeight(diffuseHeight) { } |
+ |
+ bool isOpaque() const override; |
+ |
+#if SK_SUPPORT_GPU |
+ sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override; |
+#endif |
+ |
+ class ShadowMap2DShaderContext : public SkShader::Context { |
+ public: |
+ // The context takes ownership of the states. It will call their destructors |
+ // but will NOT free the memory. |
+ ShadowMap2DShaderContext(const Sk2DShadowMapShaderImpl&, const ContextRec&, |
+ SkShader::Context* occluderContext, |
jvanverth1
2016/09/07 14:47:22
Nit: indent to (
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ void* heapAllocated); |
+ |
+ ~ShadowMap2DShaderContext() override; |
+ |
+ void shadeSpan(int x, int y, SkPMColor[], int count) override; |
+ |
+ uint32_t getFlags() const override { return fFlags; } |
+ |
+ private: |
+ SkShader::Context* fOccluderContext; |
+ uint32_t fFlags; |
+ |
+ void* fHeapAllocated; |
+ |
+ typedef SkShader::Context INHERITED; |
+ }; |
+ |
+ SK_TO_STRING_OVERRIDE() |
+SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk2DShadowMapShaderImpl) |
jvanverth1
2016/09/07 14:47:23
Indent
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ |
+protected: |
+ void flatten(SkWriteBuffer&) const override; |
+ size_t onContextSize(const ContextRec&) const override; |
+ Context* onCreateContext(const ContextRec&, void*) const override; |
+ |
+private: |
+ sk_sp<SkShader> fOccluderShader; |
+ sk_sp<SkLights> fLight; |
+ |
+ int fWidth; |
+ int fHeight; |
+ |
+ friend class Sk2DShadowMapShader; |
+ |
+ typedef SkShader INHERITED; |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+#if SK_SUPPORT_GPU |
+ |
+#include "GrContext.h" |
+#include "GrCoordTransform.h" |
+#include "GrFragmentProcessor.h" |
+#include "glsl/GrGLSLFragmentProcessor.h" |
+#include "glsl/GrGLSLFragmentShaderBuilder.h" |
+#include "SkGr.h" |
+#include "SkGrPriv.h" |
+#include "SkImage_Base.h" |
+#include "GrInvariantOutput.h" |
+#include "SkSpecialImage.h" |
+ |
+class TwoDShadowMapFP : public GrFragmentProcessor { |
jvanverth1
2016/09/07 14:47:23
RadialShadowMapFP?
vjiaoblack
2016/09/07 15:21:39
Done.
|
+public: |
+ TwoDShadowMapFP(sk_sp<GrFragmentProcessor> occluder, |
+ sk_sp<SkLights> light, |
jvanverth1
2016/09/07 14:47:23
Indent to (
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ int diffuseWidth, int diffuseHeight, |
+ GrContext* context) { |
+ fLightDir = light->light(0).pos(); |
+ |
+ fWidth = diffuseWidth; |
+ fHeight = diffuseHeight; |
+ |
+ this->registerChildProcessor(std::move(occluder)); |
+ this->initClassID<TwoDShadowMapFP>(); |
+ } |
+ |
+ class GLSLTwoDShadowMapFP : public GrGLSLFragmentProcessor { |
+ public: |
+ GLSLTwoDShadowMapFP() { } |
+ |
+ void emitCode(EmitArgs& args) override { |
+ |
+ GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
+ GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
+ |
+ const char* lightDirUniName = nullptr; |
jvanverth1
2016/09/07 14:47:23
lightPosUniName?
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ |
+ fLightDirUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
+ kVec3f_GrSLType, |
+ kDefault_GrSLPrecision, |
+ "lightDir", |
+ &lightDirUniName); |
+ |
+ 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 occluder("occluder"); |
+ this->emitChild(0, nullptr, &occluder, args); |
+ |
+ // the above isn't good enough. We need to modify the indexing coordinate. |
jvanverth1
2016/09/07 14:47:22
Is this a TODO? Or do you plan to do it in this ch
vjiaoblack
2016/09/07 15:21:39
It's an explanation of what the code below does
|
+ |
+ fragBuilder->codeAppendf("float distHere;"); |
+ fragBuilder->codeAppendf("float closestDistHere = 0;"); |
+ fragBuilder->codeAppendf("vec2 coords = vMatrixCoord_0_0_Stage0;"); |
+ fragBuilder->codeAppendf("coords.y = 0;"); |
+ fragBuilder->codeAppendf("vec2 destCoords = vec2(0,0);"); |
+ fragBuilder->codeAppendf("float step = 0.0025;"); |
jvanverth1
2016/09/07 14:47:24
Is there an assumption of texture size here? Shoul
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ |
+ // assume that we are at 0, 0 light pos |
+ // TODO use correct light positions |
jvanverth1
2016/09/07 14:47:23
Could use a comment explaining what this loop is d
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ fragBuilder->codeAppendf("for (coords.y = 0; coords.y <= 1; coords.y += step) {"); |
+ |
+ fragBuilder->codeAppendf("vec2 norm = coords * 2.0 - 1.0;"); |
+ fragBuilder->codeAppendf("float theta = 3.1415 * 1.5 + norm.x * 3.1415;"); |
jvanverth1
2016/09/07 14:47:23
It looks like this will vary from pi/2 to 5*pi/2.
vjiaoblack
2016/09/07 15:21:39
Tis right for now. (The light is fixed)
|
+ fragBuilder->codeAppendf("float r = (1.0 + norm.y) * 0.5;"); |
jvanverth1
2016/09/07 14:47:23
I think this is just coords.y again.
|
+ fragBuilder->codeAppendf("destCoords = " |
+ "vec2(-r * sin(theta), -r * cos(theta)) /2.0 + 0.5;"); |
jvanverth1
2016/09/07 14:47:23
The -r is odd to me -- negative polar coords? And
vjiaoblack
2016/09/07 15:21:39
That compiles to + vec(0.5, 0.5)
|
+ fragBuilder->codeAppendf("distHere = texture(uTextureSampler0_Stage1, " |
+ "destCoords).b;"); |
+ fragBuilder->codeAppendf("if (distHere > 0.0) {" |
+ "closestDistHere = 1-coords.y;" |
+ "break;}"); |
+ fragBuilder->codeAppendf("}"); |
+ |
+ fragBuilder->codeAppendf("%s = vec4(vec3(closestDistHere),1);", args.fOutputColor); |
+ } |
+ |
+ static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
+ GrProcessorKeyBuilder* b) { |
+ b->add32(0); // nothing to add here |
+ } |
+ |
+ protected: |
+ void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
+ const TwoDShadowMapFP &twoDShadowMapFP = proc.cast<TwoDShadowMapFP>(); |
+ |
+ const SkVector3& lightDir = twoDShadowMapFP.lightDir(); |
+ if (lightDir != fLightDir) { |
+ pdman.set3fv(fLightDirUni, 1, &lightDir.fX); |
+ fLightDir = lightDir; |
+ } |
+ |
+ int width = twoDShadowMapFP.width(); |
+ if (width != fWidth) { |
+ pdman.set1i(fWidthUni, width); |
+ fWidth = width; |
+ } |
+ int height = twoDShadowMapFP.height(); |
+ if (height != fHeight) { |
+ pdman.set1i(fHeightUni, height); |
+ fHeight = height; |
+ } |
+ } |
+ |
+ private: |
+ SkVector3 fLightDir; |
+ GrGLSLProgramDataManager::UniformHandle fLightDirUni; |
+ |
+ int fWidth; |
+ GrGLSLProgramDataManager::UniformHandle fWidthUni; |
+ int fHeight; |
+ GrGLSLProgramDataManager::UniformHandle fHeightUni; |
+ }; |
+ |
+ void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
+ GLSLTwoDShadowMapFP::GenKey(*this, caps, b); |
+ } |
+ |
+ const char* name() const override { return "twoDShadowMapFP"; } |
jvanverth1
2016/09/07 14:47:23
"radialShadowMapFP"
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ |
+ void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
+ inout->mulByUnknownFourComponents(); |
+ } |
+ const SkVector3& lightDir() const { |
+ return fLightDir; |
+ } |
+ |
+ int width() const {return fWidth; } |
+ int height() const {return fHeight; } |
+ |
+private: |
+ GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
+ return new GLSLTwoDShadowMapFP; |
+ } |
+ |
+ bool onIsEqual(const GrFragmentProcessor& proc) const override { |
+ const TwoDShadowMapFP& twoDShadowMapFP = proc.cast<TwoDShadowMapFP>(); |
+ |
+ if (fWidth != twoDShadowMapFP.fWidth || fHeight != twoDShadowMapFP.fHeight) { |
+ return false; |
+ } |
+ |
+ if (fLightDir != twoDShadowMapFP.fLightDir) { |
+ return false; |
+ } |
+ |
+ return true; |
+ } |
+ |
+ SkVector3 fLightDir; |
jvanverth1
2016/09/07 14:47:23
There's no direction in this case, so fLightPos?
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ |
+ int fHeight; |
+ int fWidth; |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+sk_sp<GrFragmentProcessor> Sk2DShadowMapShaderImpl::asFragmentProcessor(const AsFPArgs& fpargs) |
+ const { |
+ |
+ sk_sp<GrFragmentProcessor> occluderFP = fOccluderShader->asFragmentProcessor(fpargs); |
+ |
+ sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<TwoDShadowMapFP>(std::move(occluderFP), |
+ std::move(fLight), |
+ fWidth, fHeight, |
+ fpargs.fContext); |
+ return shadowfp; |
+} |
+ |
+#endif |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+bool Sk2DShadowMapShaderImpl::isOpaque() const { |
+ return fOccluderShader->isOpaque(); |
+} |
+ |
+Sk2DShadowMapShaderImpl::ShadowMap2DShaderContext::ShadowMap2DShaderContext( |
jvanverth1
2016/09/07 14:47:23
SkRadialShadowMapShaderImpl
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ const Sk2DShadowMapShaderImpl& shader, const ContextRec& rec, |
+ SkShader::Context* occluderContext, |
+ void* heapAllocated) |
+ : INHERITED(shader, rec) |
+ , fOccluderContext(occluderContext) |
+ , fHeapAllocated(heapAllocated) { |
+ bool isOpaque = shader.isOpaque(); |
+ |
+ // update fFlags |
+ uint32_t flags = 0; |
+ if (isOpaque && (255 == this->getPaintAlpha())) { |
+ flags |= kOpaqueAlpha_Flag; |
+ } |
+ |
+ fFlags = flags; |
+} |
+ |
+Sk2DShadowMapShaderImpl::ShadowMap2DShaderContext::~ShadowMap2DShaderContext() { |
+ // 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. |
+ fOccluderContext->~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 Sk2DShadowMapShaderImpl::ShadowMap2DShaderContext::shadeSpan(int x, int y, |
+ SkPMColor result[], int count) { |
+ do { |
+ int n = SkTMin(count, BUFFER_MAX); |
+ |
+ for (int i = 0; i < n; ++i) { |
jvanverth1
2016/09/07 14:47:23
// Fill with white for now
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ SkColor3f accum = SkColor3f::Make(1.0f, 1.0f, 1.0f); |
jvanverth1
2016/09/07 14:47:23
This and the convert() could be outside the loop.
vjiaoblack
2016/09/07 15:21:39
Done.
|
+ result[i] = convert(accum, 0xFF); |
+ } |
+ |
+ result += n; |
+ x += n; |
+ count -= n; |
+ } while (count > 0); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////// |
+ |
+#ifndef SK_IGNORE_TO_STRING |
+void Sk2DShadowMapShaderImpl::toString(SkString* str) const { |
+ str->appendf("2DShadowMapShader: ()"); |
+} |
+#endif |
+ |
+sk_sp<SkFlattenable> Sk2DShadowMapShaderImpl::CreateProc(SkReadBuffer& buf) { |
+ |
+ // Discarding SkShader flattenable params |
+ bool hasLocalMatrix = buf.readBool(); |
+ SkAssertResult(!hasLocalMatrix); |
+ |
+ sk_sp<SkLights> light = SkLights::MakeFromBuffer(buf); |
+ |
+ int diffuseWidth = buf.readInt(); |
+ int diffuseHeight = buf.readInt(); |
+ |
+ sk_sp<SkShader> occluderShader(buf.readFlattenable<SkShader>()); |
+ |
+ return sk_make_sp<Sk2DShadowMapShaderImpl>(std::move(occluderShader), |
+ std::move(light), |
+ diffuseWidth, diffuseHeight); |
+} |
+ |
+void Sk2DShadowMapShaderImpl::flatten(SkWriteBuffer& buf) const { |
+ this->INHERITED::flatten(buf); |
+ |
+ fLight->flatten(buf); |
+ |
+ buf.writeInt(fWidth); |
+ buf.writeInt(fHeight); |
+ |
+ buf.writeFlattenable(fOccluderShader.get()); |
+} |
+ |
+size_t Sk2DShadowMapShaderImpl::onContextSize(const ContextRec& rec) const { |
+ return sizeof(ShadowMap2DShaderContext); |
+} |
+ |
+SkShader::Context* Sk2DShadowMapShaderImpl::onCreateContext(const ContextRec& rec, |
+ void* storage) const { |
+ size_t heapRequired = fOccluderShader->contextSize(rec); |
+ |
+ void* heapAllocated = sk_malloc_throw(heapRequired); |
+ |
+ void* occluderContextStorage = heapAllocated; |
+ |
+ SkShader::Context* occluderContext = |
+ fOccluderShader->createContext(rec, occluderContextStorage); |
+ |
+ if (!occluderContext) { |
+ sk_free(heapAllocated); |
+ return nullptr; |
+ } |
+ |
+ return new (storage) ShadowMap2DShaderContext(*this, rec, occluderContext, heapAllocated); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+sk_sp<SkShader> Sk2DShadowMapShader::Make(sk_sp<SkShader> occluderShader, |
+ sk_sp<SkLights> light, |
+ int diffuseWidth, int diffuseHeight) { |
+ if (!occluderShader) { |
+ // TODO: Use paint's color in absence of a diffuseShader |
+ // TODO: Use a default implementation of normalSource instead |
+ return nullptr; |
+ } |
+ |
+ return sk_make_sp<Sk2DShadowMapShaderImpl>(std::move(occluderShader), |
+ std::move(light), |
+ diffuseWidth, diffuseHeight); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(Sk2DShadowMapShader) |
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Sk2DShadowMapShaderImpl) |
jvanverth1
2016/09/07 14:47:23
Indent
vjiaoblack
2016/09/07 15:21:39
Done.
|
+SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+#endif |