diff options
Diffstat (limited to 'src/gpu/effects')
-rw-r--r-- | src/gpu/effects/GrBezierEffect.cpp | 92 | ||||
-rw-r--r-- | src/gpu/effects/GrBezierEffect.h | 31 |
2 files changed, 66 insertions, 57 deletions
diff --git a/src/gpu/effects/GrBezierEffect.cpp b/src/gpu/effects/GrBezierEffect.cpp index 58243ea635..54a2e8576e 100644 --- a/src/gpu/effects/GrBezierEffect.cpp +++ b/src/gpu/effects/GrBezierEffect.cpp @@ -499,21 +499,31 @@ public: pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); } + if (!fDevKLMMatrix.cheapEqualTo(ce.devKLMMatrix())) { + fDevKLMMatrix = ce.devKLMMatrix(); + float devKLMMatrix[3 * 3]; + GrGLSLGetMatrix<3>(devKLMMatrix, fDevKLMMatrix); + pdman.setMatrix3f(fDevKLMUniform, devKLMMatrix); + } + if (ce.color() != fColor) { float c[4]; GrColorToRGBAFloat(ce.color(), c); pdman.set4fv(fColorUniform, 1, c); fColor = ce.color(); } + this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); } private: SkMatrix fViewMatrix; + SkMatrix fDevKLMMatrix; GrColor fColor; GrPrimitiveEdgeType fEdgeType; UniformHandle fColorUniform; UniformHandle fViewMatrixUniform; + UniformHandle fDevKLMUniform; typedef GrGLSLGeometryProcessor INHERITED; }; @@ -533,10 +543,6 @@ void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { // emit attributes varyingHandler->emitAttributes(gp); - GrGLSLVertToFrag v(kVec4f_GrSLType); - varyingHandler->addVarying("CubicCoeffs", &v, kHigh_GrSLPrecision); - vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inCubicCoeffs()->fName); - GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; // Setup pass through color if (!gp.colorIgnored()) { @@ -551,6 +557,30 @@ void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { gp.viewMatrix(), &fViewMatrixUniform); + // Setup KLM + const char* devkLMMatrixName; + fDevKLMUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, kMat33f_GrSLType, + kHigh_GrSLPrecision, "KLM", &devkLMMatrixName); + GrGLSLVertToFrag v(kVec3f_GrSLType); + varyingHandler->addVarying("CubicCoeffs", &v, kHigh_GrSLPrecision); + vertBuilder->codeAppendf("%s = %s * vec3(%s, 1);", + v.vsOut(), devkLMMatrixName, gpArgs->fPositionVar.c_str()); + + + GrGLSLVertToFrag gradCoeffs(kVec4f_GrSLType); + if (kFillAA_GrProcessorEdgeType == fEdgeType || kHairlineAA_GrProcessorEdgeType == fEdgeType) { + varyingHandler->addVarying("GradCoeffs", &gradCoeffs, kHigh_GrSLPrecision); + vertBuilder->codeAppendf("highp float k = %s[0], l = %s[1], m = %s[2];", + v.vsOut(), v.vsOut(), v.vsOut()); + vertBuilder->codeAppendf("highp vec2 gk = vec2(%s[0][0], %s[1][0]), " + "gl = vec2(%s[0][1], %s[1][1]), " + "gm = vec2(%s[0][2], %s[1][2]);", + devkLMMatrixName, devkLMMatrixName, devkLMMatrixName, + devkLMMatrixName, devkLMMatrixName, devkLMMatrixName); + vertBuilder->codeAppendf("%s = vec4(3 * k * gk, -m * gl - l * gm);", + gradCoeffs.vsOut()); + } + // emit transforms with position this->emitTransforms(vertBuilder, varyingHandler, @@ -561,42 +591,23 @@ void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { GrShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0, kHigh_GrSLPrecision); - GrShaderVar dklmdx("dklmdx", kVec3f_GrSLType, 0, kHigh_GrSLPrecision); - GrShaderVar dklmdy("dklmdy", kVec3f_GrSLType, 0, kHigh_GrSLPrecision); - GrShaderVar dfdx("dfdx", kFloat_GrSLType, 0, kHigh_GrSLPrecision); - GrShaderVar dfdy("dfdy", kFloat_GrSLType, 0, kHigh_GrSLPrecision); GrShaderVar gF("gF", kVec2f_GrSLType, 0, kHigh_GrSLPrecision); - GrShaderVar gFM("gFM", kFloat_GrSLType, 0, kHigh_GrSLPrecision); GrShaderVar func("func", kFloat_GrSLType, 0, kHigh_GrSLPrecision); fragBuilder->declAppend(edgeAlpha); - fragBuilder->declAppend(dklmdx); - fragBuilder->declAppend(dklmdy); - fragBuilder->declAppend(dfdx); - fragBuilder->declAppend(dfdy); fragBuilder->declAppend(gF); - fragBuilder->declAppend(gFM); fragBuilder->declAppend(func); switch (fEdgeType) { case kHairlineAA_GrProcessorEdgeType: { - fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn()); - fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn()); - fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", - dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(), - dklmdx.c_str(), v.fsIn(), dklmdx.c_str()); - fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", - dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(), - dklmdy.c_str(), v.fsIn(), dklmdy.c_str()); - fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str()); - fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", - gFM.c_str(), gF.c_str(), gF.c_str()); + fragBuilder->codeAppendf("%s = %s.x * %s.xy + %s.zw;", + gF.c_str(), v.fsIn(), gradCoeffs.fsIn(), gradCoeffs.fsIn()); fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;", func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str()); - fragBuilder->codeAppendf("%s = %s / %s;", - edgeAlpha.c_str(), func.c_str(), gFM.c_str()); + fragBuilder->codeAppendf("%s = %s * inversesqrt(dot(%s, %s));", + edgeAlpha.c_str(), func.c_str(), gF.c_str(), gF.c_str()); fragBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);", edgeAlpha.c_str(), edgeAlpha.c_str()); // Add line below for smooth cubic ramp @@ -606,23 +617,13 @@ void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { break; } case kFillAA_GrProcessorEdgeType: { - fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn()); - fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn()); - fragBuilder->codeAppendf("%s =" - "3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", - dfdx.c_str(), v.fsIn(), v.fsIn(), dklmdx.c_str(), v.fsIn(), - dklmdx.c_str(), v.fsIn(), dklmdx.c_str()); - fragBuilder->codeAppendf("%s = 3.0 * %s.x * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", - dfdy.c_str(), v.fsIn(), v.fsIn(), dklmdy.c_str(), v.fsIn(), - dklmdy.c_str(), v.fsIn(), dklmdy.c_str()); - fragBuilder->codeAppendf("%s = vec2(%s, %s);", gF.c_str(), dfdx.c_str(), dfdy.c_str()); - fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", - gFM.c_str(), gF.c_str(), gF.c_str()); + fragBuilder->codeAppendf("%s = %s.x * %s.xy + %s.zw;", + gF.c_str(), v.fsIn(), gradCoeffs.fsIn(), gradCoeffs.fsIn()); fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;", func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); - fragBuilder->codeAppendf("%s = %s / %s;", - edgeAlpha.c_str(), func.c_str(), gFM.c_str()); + fragBuilder->codeAppendf("%s = %s * inversesqrt(dot(%s, %s));", + edgeAlpha.c_str(), func.c_str(), gF.c_str(), gF.c_str()); fragBuilder->codeAppendf("%s = clamp(0.5 - %s, 0.0, 1.0);", edgeAlpha.c_str(), edgeAlpha.c_str()); // Add line below for smooth cubic ramp @@ -667,15 +668,15 @@ GrGLSLPrimitiveProcessor* GrCubicEffect::createGLSLInstance(const GrShaderCaps&) return new GrGLCubicEffect(*this); } -GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix, - GrPrimitiveEdgeType edgeType) +GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix, const SkMatrix& + devKLMMatrix, GrPrimitiveEdgeType edgeType) : fColor(color) , fViewMatrix(viewMatrix) + , fDevKLMMatrix(devKLMMatrix) , fEdgeType(edgeType) { this->initClassID<GrCubicEffect>(); fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, kHigh_GrSLPrecision); - fInCubicCoeffs = &this->addVertexAttrib("inCubicCoeffs", kVec4f_GrVertexAttribType); } ////////////////////////////////////////////////////////////////////////////// @@ -690,7 +691,8 @@ sk_sp<GrGeometryProcessor> GrCubicEffect::TestCreate(GrProcessorTestData* d) { static_cast<GrPrimitiveEdgeType>( d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); gp = GrCubicEffect::Make(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom), - edgeType, *d->caps()); + GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool(), edgeType, + *d->caps()); } while (nullptr == gp); return gp; } diff --git a/src/gpu/effects/GrBezierEffect.h b/src/gpu/effects/GrBezierEffect.h index 3a130ec187..3a97506396 100644 --- a/src/gpu/effects/GrBezierEffect.h +++ b/src/gpu/effects/GrBezierEffect.h @@ -225,24 +225,30 @@ class GrCubicEffect : public GrGeometryProcessor { public: static sk_sp<GrGeometryProcessor> Make(GrColor color, const SkMatrix& viewMatrix, + const SkMatrix& klm, + bool flipKL, const GrPrimitiveEdgeType edgeType, const GrCaps& caps) { + // Map KLM to something that operates in device space. + SkMatrix devKLM; + if (!viewMatrix.invert(&devKLM)) { + return nullptr; + } + devKLM.postConcat(klm); + if (flipKL) { + devKLM.postScale(-1, -1); + } + switch (edgeType) { case kFillAA_GrProcessorEdgeType: - if (!caps.shaderCaps()->shaderDerivativeSupport()) { - return nullptr; - } return sk_sp<GrGeometryProcessor>( - new GrCubicEffect(color, viewMatrix, kFillAA_GrProcessorEdgeType)); + new GrCubicEffect(color, viewMatrix, devKLM, kFillAA_GrProcessorEdgeType)); case kHairlineAA_GrProcessorEdgeType: - if (!caps.shaderCaps()->shaderDerivativeSupport()) { - return nullptr; - } return sk_sp<GrGeometryProcessor>( - new GrCubicEffect(color, viewMatrix, kHairlineAA_GrProcessorEdgeType)); + new GrCubicEffect(color, viewMatrix, devKLM, kHairlineAA_GrProcessorEdgeType)); case kFillBW_GrProcessorEdgeType: return sk_sp<GrGeometryProcessor>( - new GrCubicEffect(color, viewMatrix, kFillBW_GrProcessorEdgeType)); + new GrCubicEffect(color, viewMatrix, devKLM, kFillBW_GrProcessorEdgeType)); default: return nullptr; } @@ -253,26 +259,27 @@ public: const char* name() const override { return "Cubic"; } inline const Attribute* inPosition() const { return fInPosition; } - inline const Attribute* inCubicCoeffs() const { return fInCubicCoeffs; } inline bool isAntiAliased() const { return GrProcessorEdgeTypeIsAA(fEdgeType); } inline bool isFilled() const { return GrProcessorEdgeTypeIsFill(fEdgeType); } inline GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } GrColor color() const { return fColor; } bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } const SkMatrix& viewMatrix() const { return fViewMatrix; } + const SkMatrix& devKLMMatrix() const { return fDevKLMMatrix; } void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; private: - GrCubicEffect(GrColor, const SkMatrix& viewMatrix, GrPrimitiveEdgeType); + GrCubicEffect(GrColor, const SkMatrix& viewMatrix, const SkMatrix& devKLMMatrix, + GrPrimitiveEdgeType); GrColor fColor; SkMatrix fViewMatrix; + SkMatrix fDevKLMMatrix; GrPrimitiveEdgeType fEdgeType; const Attribute* fInPosition; - const Attribute* fInCubicCoeffs; GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |