commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkTwoPointConicalGradient.h" |
| 10 | |
commit-bot@chromium.org | ef93d29 | 2014-04-10 15:37:52 +0000 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "GrCoordTransform.h" |
| 13 | #include "GrInvariantOutput.h" |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 14 | #include "GrPaint.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 16 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUniformHandler.h" |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 18 | #include "SkTwoPointConicalGradient_gpu.h" |
| 19 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 20 | // For brevity |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 21 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 23 | static const SkScalar kErrorTol = 0.00001f; |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 24 | static const SkScalar kEdgeErrorTol = 5.f * kErrorTol; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * We have three general cases for 2pt conical gradients. First we always assume that |
| 28 | * the start radius <= end radius. Our first case (kInside_) is when the start circle |
| 29 | * is completely enclosed by the end circle. The second case (kOutside_) is the case |
| 30 | * when the start circle is either completely outside the end circle or the circles |
| 31 | * overlap. The final case (kEdge_) is when the start circle is inside the end one, |
| 32 | * but the two are just barely touching at 1 point along their edges. |
| 33 | */ |
| 34 | enum ConicalType { |
| 35 | kInside_ConicalType, |
| 36 | kOutside_ConicalType, |
| 37 | kEdge_ConicalType, |
| 38 | }; |
| 39 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 40 | ////////////////////////////////////////////////////////////////////////////// |
| 41 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 42 | static void set_matrix_edge_conical(const SkTwoPointConicalGradient& shader, |
| 43 | SkMatrix* invLMatrix) { |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 44 | // Inverse of the current local matrix is passed in then, |
| 45 | // translate to center1, rotate so center2 is on x axis. |
| 46 | const SkPoint& center1 = shader.getStartCenter(); |
| 47 | const SkPoint& center2 = shader.getEndCenter(); |
| 48 | |
| 49 | invLMatrix->postTranslate(-center1.fX, -center1.fY); |
| 50 | |
| 51 | SkPoint diff = center2 - center1; |
| 52 | SkScalar diffLen = diff.length(); |
| 53 | if (0 != diffLen) { |
| 54 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 55 | SkMatrix rot; |
| 56 | rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY), |
| 57 | SkScalarMul(invDiffLen, diff.fX)); |
| 58 | invLMatrix->postConcat(rot); |
| 59 | } |
| 60 | } |
| 61 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 62 | class Edge2PtConicalEffect : public GrGradientEffect { |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 63 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 64 | class GLSLEdge2PtConicalProcessor; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 65 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 66 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) { |
| 67 | return sk_sp<GrFragmentProcessor>(new Edge2PtConicalEffect(args)); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 68 | } |
| 69 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 70 | virtual ~Edge2PtConicalEffect() {} |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 73 | return "Two-Point Conical Gradient Edge Touching"; |
| 74 | } |
| 75 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 76 | // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 77 | SkScalar center() const { return fCenterX1; } |
| 78 | SkScalar diffRadius() const { return fDiffRadius; } |
| 79 | SkScalar radius() const { return fRadius0; } |
| 80 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 81 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 82 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 83 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 84 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 87 | const Edge2PtConicalEffect& s = sBase.cast<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 88 | return (INHERITED::onIsEqual(sBase) && |
| 89 | this->fCenterX1 == s.fCenterX1 && |
| 90 | this->fRadius0 == s.fRadius0 && |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 91 | this->fDiffRadius == s.fDiffRadius); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 94 | Edge2PtConicalEffect(const CreateArgs& args) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 95 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 96 | const SkTwoPointConicalGradient& shader = |
| 97 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
| 98 | fCenterX1 = shader.getCenterX1(); |
| 99 | fRadius0 = shader.getStartRadius(); |
| 100 | fDiffRadius = shader.getDiffRadius(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 101 | this->initClassID<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 102 | // We should only be calling this shader if we are degenerate case with touching circles |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 103 | // When deciding if we are in edge case, we scaled by the end radius for cases when the |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 104 | // start radius was close to zero, otherwise we scaled by the start radius. In addition |
| 105 | // Our test for the edge case in set_matrix_circle_conical has a higher tolerance so we |
| 106 | // need the sqrt value below |
| 107 | SkASSERT(SkScalarAbs(SkScalarAbs(fDiffRadius) - fCenterX1) < |
| 108 | (fRadius0 < kErrorTol ? shader.getEndRadius() * kEdgeErrorTol : |
| 109 | fRadius0 * sqrt(kEdgeErrorTol))); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 111 | // We pass the linear part of the quadratic as a varying. |
| 112 | // float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z) |
| 113 | fBTransform = this->getCoordTransform(); |
| 114 | SkMatrix& bMatrix = *fBTransform.accessMatrix(); |
| 115 | SkScalar r0dr = SkScalarMul(fRadius0, fDiffRadius); |
| 116 | bMatrix[SkMatrix::kMScaleX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) + |
| 117 | SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp0])); |
| 118 | bMatrix[SkMatrix::kMSkewX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) + |
| 119 | SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp1])); |
| 120 | bMatrix[SkMatrix::kMTransX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) + |
| 121 | SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp2])); |
| 122 | this->addCoordTransform(&fBTransform); |
| 123 | } |
| 124 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 125 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 126 | |
| 127 | // @{ |
| 128 | // Cache of values - these can change arbitrarily, EXCEPT |
| 129 | // we shouldn't change between degenerate and non-degenerate?! |
| 130 | |
| 131 | GrCoordTransform fBTransform; |
| 132 | SkScalar fCenterX1; |
| 133 | SkScalar fRadius0; |
| 134 | SkScalar fDiffRadius; |
| 135 | |
| 136 | // @} |
| 137 | |
| 138 | typedef GrGradientEffect INHERITED; |
| 139 | }; |
| 140 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 141 | class Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 142 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 143 | GLSLEdge2PtConicalProcessor(const GrProcessor&); |
| 144 | virtual ~GLSLEdge2PtConicalProcessor() { } |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 145 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 146 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 147 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 148 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 149 | |
| 150 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 151 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 152 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 153 | UniformHandle fParamUni; |
| 154 | |
| 155 | const char* fVSVaryingName; |
| 156 | const char* fFSVaryingName; |
| 157 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 158 | // @{ |
| 159 | /// Values last uploaded as uniforms |
| 160 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 161 | SkScalar fCachedRadius; |
| 162 | SkScalar fCachedDiffRadius; |
| 163 | |
| 164 | // @} |
| 165 | |
| 166 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 167 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 168 | |
| 169 | }; |
| 170 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 171 | void Edge2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 172 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 173 | Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 174 | } |
| 175 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 176 | GrGLSLFragmentProcessor* Edge2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 177 | return new Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor(*this); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 178 | } |
skia.committer@gmail.com | 221b911 | 2014-04-04 03:04:32 +0000 | [diff] [blame] | 179 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 180 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Edge2PtConicalEffect); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 181 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 182 | /* |
| 183 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 184 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 185 | sk_sp<GrFragmentProcessor> Edge2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 186 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 187 | SkScalar radius1 = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 188 | SkPoint center2; |
| 189 | SkScalar radius2; |
| 190 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 191 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 192 | // If the circles are identical the factory will give us an empty shader. |
| 193 | // This will happen if we pick identical centers |
| 194 | } while (center1 == center2); |
| 195 | |
| 196 | // Below makes sure that circle one is contained within circle two |
| 197 | // and both circles are touching on an edge |
| 198 | SkPoint diff = center2 - center1; |
| 199 | SkScalar diffLen = diff.length(); |
| 200 | radius2 = radius1 + diffLen; |
| 201 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 202 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 203 | auto shader = params.fUseColors4f ? |
| 204 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 205 | params.fColors4f, params.fColorSpace, params.fStops, |
| 206 | params.fColorCount, params.fTileMode) : |
| 207 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 208 | params.fColors, params.fStops, |
| 209 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 210 | GrTest::TestAsFPArgs asFPArgs(d); |
| 211 | sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 212 | GrAlwaysAssert(fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 213 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 214 | } |
| 215 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 216 | Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GLSLEdge2PtConicalProcessor(const GrProcessor&) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | : fVSVaryingName(nullptr) |
| 218 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 219 | , fCachedRadius(-SK_ScalarMax) |
| 220 | , fCachedDiffRadius(-SK_ScalarMax) {} |
| 221 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 222 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 223 | const Edge2PtConicalEffect& ge = args.fFp.cast<Edge2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 224 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 225 | this->emitUniforms(uniformHandler, ge); |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 226 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 227 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 228 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 229 | |
| 230 | SkString cName("c"); |
| 231 | SkString tName("t"); |
| 232 | SkString p0; // start radius |
| 233 | SkString p1; // start radius squared |
| 234 | SkString p2; // difference in radii (r1 - r0) |
| 235 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 236 | |
| 237 | p0.appendf("%s.x", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 238 | p1.appendf("%s.y", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 239 | p2.appendf("%s.z", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 240 | |
| 241 | // We interpolate the linear component in coords[1]. |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 242 | SkASSERT(args.fTransformedCoords[0].getType() == args.fTransformedCoords[1].getType()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 243 | const char* coords2D; |
| 244 | SkString bVar; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 245 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 246 | if (kVec3f_GrSLType == args.fTransformedCoords[0].getType()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 247 | fragBuilder->codeAppendf("\tvec3 interpolants = vec3(%s.xy / %s.z, %s.x / %s.z);\n", |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 248 | args.fTransformedCoords[0].c_str(), |
| 249 | args.fTransformedCoords[0].c_str(), |
| 250 | args.fTransformedCoords[1].c_str(), |
| 251 | args.fTransformedCoords[1].c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 252 | coords2D = "interpolants.xy"; |
| 253 | bVar = "interpolants.z"; |
| 254 | } else { |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 255 | coords2D = args.fTransformedCoords[0].c_str(); |
| 256 | bVar.printf("%s.x", args.fTransformedCoords[1].c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // output will default to transparent black (we simply won't write anything |
| 260 | // else to it if invalid, instead of discarding or returning prematurely) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 261 | fragBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 262 | |
| 263 | // c = (x^2)+(y^2) - params[1] |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 264 | fragBuilder->codeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 265 | cName.c_str(), coords2D, coords2D, p1.c_str()); |
| 266 | |
| 267 | // linear case: t = -c/b |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 268 | fragBuilder->codeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 269 | cName.c_str(), bVar.c_str()); |
| 270 | |
| 271 | // if r(t) > 0, then t will be the x coordinate |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 272 | fragBuilder->codeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 273 | p2.c_str(), p0.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 274 | fragBuilder->codeAppend("\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 275 | this->emitColor(fragBuilder, |
| 276 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 277 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 278 | ge, |
| 279 | tName.c_str(), |
| 280 | args.fOutputColor, |
| 281 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 282 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 283 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 284 | } |
| 285 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 286 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::onSetData( |
| 287 | const GrGLSLProgramDataManager& pdman, |
| 288 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 289 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 290 | const Edge2PtConicalEffect& data = processor.cast<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 291 | SkScalar radius0 = data.radius(); |
| 292 | SkScalar diffRadius = data.diffRadius(); |
| 293 | |
| 294 | if (fCachedRadius != radius0 || |
| 295 | fCachedDiffRadius != diffRadius) { |
| 296 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 297 | pdman.set3f(fParamUni, SkScalarToFloat(radius0), |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 298 | SkScalarToFloat(SkScalarMul(radius0, radius0)), SkScalarToFloat(diffRadius)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 299 | fCachedRadius = radius0; |
| 300 | fCachedDiffRadius = diffRadius; |
| 301 | } |
| 302 | } |
| 303 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 304 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GenKey(const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 305 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 306 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | ////////////////////////////////////////////////////////////////////////////// |
| 310 | // Focal Conical Gradients |
| 311 | ////////////////////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | static ConicalType set_matrix_focal_conical(const SkTwoPointConicalGradient& shader, |
| 314 | SkMatrix* invLMatrix, SkScalar* focalX) { |
| 315 | // Inverse of the current local matrix is passed in then, |
| 316 | // translate, scale, and rotate such that endCircle is unit circle on x-axis, |
| 317 | // and focal point is at the origin. |
| 318 | ConicalType conicalType; |
| 319 | const SkPoint& focal = shader.getStartCenter(); |
| 320 | const SkPoint& centerEnd = shader.getEndCenter(); |
| 321 | SkScalar radius = shader.getEndRadius(); |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 322 | SkScalar invRadius = 1.f / radius; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 323 | |
| 324 | SkMatrix matrix; |
| 325 | |
| 326 | matrix.setTranslate(-centerEnd.fX, -centerEnd.fY); |
| 327 | matrix.postScale(invRadius, invRadius); |
| 328 | |
| 329 | SkPoint focalTrans; |
| 330 | matrix.mapPoints(&focalTrans, &focal, 1); |
| 331 | *focalX = focalTrans.length(); |
| 332 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 333 | if (0.f != *focalX) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 334 | SkScalar invFocalX = SkScalarInvert(*focalX); |
| 335 | SkMatrix rot; |
| 336 | rot.setSinCos(-SkScalarMul(invFocalX, focalTrans.fY), |
| 337 | SkScalarMul(invFocalX, focalTrans.fX)); |
| 338 | matrix.postConcat(rot); |
| 339 | } |
| 340 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 341 | matrix.postTranslate(-(*focalX), 0.f); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 342 | |
| 343 | // If the focal point is touching the edge of the circle it will |
| 344 | // cause a degenerate case that must be handled separately |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 345 | // kEdgeErrorTol = 5 * kErrorTol was picked after manual testing the |
| 346 | // stability trade off versus the linear approx used in the Edge Shader |
| 347 | if (SkScalarAbs(1.f - (*focalX)) < kEdgeErrorTol) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 348 | return kEdge_ConicalType; |
| 349 | } |
| 350 | |
| 351 | // Scale factor 1 / (1 - focalX * focalX) |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 352 | SkScalar oneMinusF2 = 1.f - SkScalarMul(*focalX, *focalX); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 353 | SkScalar s = SkScalarInvert(oneMinusF2); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 354 | |
| 355 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 356 | if (s >= 0.f) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 357 | conicalType = kInside_ConicalType; |
| 358 | matrix.postScale(s, s * SkScalarSqrt(oneMinusF2)); |
| 359 | } else { |
| 360 | conicalType = kOutside_ConicalType; |
| 361 | matrix.postScale(s, s); |
| 362 | } |
| 363 | |
| 364 | invLMatrix->postConcat(matrix); |
| 365 | |
| 366 | return conicalType; |
| 367 | } |
| 368 | |
| 369 | ////////////////////////////////////////////////////////////////////////////// |
| 370 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 371 | class FocalOutside2PtConicalEffect : public GrGradientEffect { |
| 372 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 373 | class GLSLFocalOutside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 374 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 375 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 376 | return sk_sp<GrFragmentProcessor>( |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 377 | new FocalOutside2PtConicalEffect(args, focalX)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | virtual ~FocalOutside2PtConicalEffect() { } |
| 381 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 382 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 383 | return "Two-Point Conical Gradient Focal Outside"; |
| 384 | } |
| 385 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 386 | bool isFlipped() const { return fIsFlipped; } |
| 387 | SkScalar focal() const { return fFocalX; } |
| 388 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 389 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 390 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 391 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 392 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 393 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 394 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 395 | const FocalOutside2PtConicalEffect& s = sBase.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 396 | return (INHERITED::onIsEqual(sBase) && |
| 397 | this->fFocalX == s.fFocalX && |
| 398 | this->fIsFlipped == s.fIsFlipped); |
| 399 | } |
| 400 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 401 | static bool IsFlipped(const CreateArgs& args) { |
| 402 | // eww. |
| 403 | return static_cast<const SkTwoPointConicalGradient*>(args.fShader)->isFlippedGrad(); |
| 404 | } |
| 405 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 406 | FocalOutside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 407 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) |
| 408 | , fFocalX(focalX) |
| 409 | , fIsFlipped(IsFlipped(args)) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 410 | this->initClassID<FocalOutside2PtConicalEffect>(); |
| 411 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 412 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 413 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 414 | |
| 415 | SkScalar fFocalX; |
| 416 | bool fIsFlipped; |
| 417 | |
| 418 | typedef GrGradientEffect INHERITED; |
| 419 | }; |
| 420 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 421 | class FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 422 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 423 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 424 | GLSLFocalOutside2PtConicalProcessor(const GrProcessor&); |
| 425 | virtual ~GLSLFocalOutside2PtConicalProcessor() { } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 426 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 427 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 428 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 429 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 430 | |
| 431 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 432 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 433 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 434 | UniformHandle fParamUni; |
| 435 | |
| 436 | const char* fVSVaryingName; |
| 437 | const char* fFSVaryingName; |
| 438 | |
| 439 | bool fIsFlipped; |
| 440 | |
| 441 | // @{ |
| 442 | /// Values last uploaded as uniforms |
| 443 | |
| 444 | SkScalar fCachedFocal; |
| 445 | |
| 446 | // @} |
| 447 | |
| 448 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 449 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 450 | |
| 451 | }; |
| 452 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 453 | void FocalOutside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 454 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 455 | FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 456 | } |
| 457 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 458 | GrGLSLFragmentProcessor* FocalOutside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 459 | return new FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 460 | } |
| 461 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 462 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalOutside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 463 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 464 | /* |
| 465 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 466 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 467 | sk_sp<GrFragmentProcessor> FocalOutside2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 468 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 469 | SkScalar radius1 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 470 | SkPoint center2; |
| 471 | SkScalar radius2; |
| 472 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 473 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 474 | // Need to make sure the centers are not the same or else focal point will be inside |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 475 | } while (center1 == center2); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 476 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 477 | SkPoint diff = center2 - center1; |
| 478 | SkScalar diffLen = diff.length(); |
| 479 | // Below makes sure that the focal point is not contained within circle two |
| 480 | radius2 = d->fRandom->nextRangeF(0.f, diffLen); |
| 481 | |
| 482 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 483 | auto shader = params.fUseColors4f ? |
| 484 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 485 | params.fColors4f, params.fColorSpace, params.fStops, |
| 486 | params.fColorCount, params.fTileMode) : |
| 487 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 488 | params.fColors, params.fStops, |
| 489 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 490 | GrTest::TestAsFPArgs asFPArgs(d); |
| 491 | sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 492 | GrAlwaysAssert(fp); |
| 493 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 494 | } |
| 495 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 496 | FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor |
| 497 | ::GLSLFocalOutside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 498 | : fVSVaryingName(nullptr) |
| 499 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 500 | , fCachedFocal(SK_ScalarMax) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 501 | const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 502 | fIsFlipped = data.isFlipped(); |
| 503 | } |
| 504 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 505 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 506 | const FocalOutside2PtConicalEffect& ge = args.fFp.cast<FocalOutside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 507 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 508 | this->emitUniforms(uniformHandler, ge); |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 509 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 510 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 511 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 512 | SkString tName("t"); |
| 513 | SkString p0; // focalX |
| 514 | SkString p1; // 1 - focalX * focalX |
| 515 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 516 | p0.appendf("%s.x", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 517 | p1.appendf("%s.y", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 518 | |
| 519 | // if we have a vec3 from being in perspective, convert it to a vec2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 520 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 521 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 522 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 523 | |
| 524 | // t = p.x * focal.x +/- sqrt(p.x^2 + (1 - focal.x^2) * p.y^2) |
| 525 | |
| 526 | // output will default to transparent black (we simply won't write anything |
| 527 | // else to it if invalid, instead of discarding or returning prematurely) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 528 | fragBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 529 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 530 | fragBuilder->codeAppendf("\tfloat xs = %s.x * %s.x;\n", coords2D, coords2D); |
| 531 | fragBuilder->codeAppendf("\tfloat ys = %s.y * %s.y;\n", coords2D, coords2D); |
| 532 | fragBuilder->codeAppendf("\tfloat d = xs + %s * ys;\n", p1.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 533 | |
| 534 | // Must check to see if we flipped the circle order (to make sure start radius < end radius) |
| 535 | // If so we must also flip sign on sqrt |
| 536 | if (!fIsFlipped) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 537 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s + sqrt(d);\n", tName.c_str(), |
| 538 | coords2D, p0.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 539 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 540 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s - sqrt(d);\n", tName.c_str(), |
| 541 | coords2D, p0.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 542 | } |
| 543 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 544 | fragBuilder->codeAppendf("\tif (%s >= 0.0 && d >= 0.0) {\n", tName.c_str()); |
| 545 | fragBuilder->codeAppend("\t\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 546 | this->emitColor(fragBuilder, |
| 547 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 548 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 549 | ge, |
| 550 | tName.c_str(), |
| 551 | args.fOutputColor, |
| 552 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 553 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 554 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 555 | } |
| 556 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 557 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::onSetData( |
| 558 | const GrGLSLProgramDataManager& pdman, |
| 559 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 560 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 561 | const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 562 | SkASSERT(data.isFlipped() == fIsFlipped); |
| 563 | SkScalar focal = data.focal(); |
| 564 | |
| 565 | if (fCachedFocal != focal) { |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 566 | SkScalar oneMinus2F = 1.f - SkScalarMul(focal, focal); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 567 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 568 | pdman.set2f(fParamUni, SkScalarToFloat(focal), SkScalarToFloat(oneMinus2F)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 569 | fCachedFocal = focal; |
| 570 | } |
| 571 | } |
| 572 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 573 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::GenKey( |
| 574 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 575 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 576 | uint32_t* key = b->add32n(2); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 577 | key[0] = GenBaseGradientKey(processor); |
| 578 | key[1] = processor.cast<FocalOutside2PtConicalEffect>().isFlipped(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | ////////////////////////////////////////////////////////////////////////////// |
| 582 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 583 | class FocalInside2PtConicalEffect : public GrGradientEffect { |
| 584 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 585 | class GLSLFocalInside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 586 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 587 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 588 | return sk_sp<GrFragmentProcessor>( |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 589 | new FocalInside2PtConicalEffect(args, focalX)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | virtual ~FocalInside2PtConicalEffect() {} |
| 593 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 594 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 595 | return "Two-Point Conical Gradient Focal Inside"; |
| 596 | } |
| 597 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 598 | SkScalar focal() const { return fFocalX; } |
| 599 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 600 | typedef FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor GLSLProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 601 | |
| 602 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 603 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 604 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 605 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 606 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 607 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 608 | const FocalInside2PtConicalEffect& s = sBase.cast<FocalInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 609 | return (INHERITED::onIsEqual(sBase) && |
| 610 | this->fFocalX == s.fFocalX); |
| 611 | } |
| 612 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 613 | FocalInside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 614 | : INHERITED(args, args.fShader->colorsAreOpaque()), fFocalX(focalX) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 615 | this->initClassID<FocalInside2PtConicalEffect>(); |
| 616 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 617 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 618 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 619 | |
| 620 | SkScalar fFocalX; |
| 621 | |
| 622 | typedef GrGradientEffect INHERITED; |
| 623 | }; |
| 624 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 625 | class FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor |
| 626 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 627 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 628 | GLSLFocalInside2PtConicalProcessor(const GrProcessor&); |
| 629 | virtual ~GLSLFocalInside2PtConicalProcessor() {} |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 630 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 631 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 632 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 633 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 634 | |
| 635 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 636 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 637 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 638 | UniformHandle fFocalUni; |
| 639 | |
| 640 | const char* fVSVaryingName; |
| 641 | const char* fFSVaryingName; |
| 642 | |
| 643 | // @{ |
| 644 | /// Values last uploaded as uniforms |
| 645 | |
| 646 | SkScalar fCachedFocal; |
| 647 | |
| 648 | // @} |
| 649 | |
| 650 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 651 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 652 | |
| 653 | }; |
| 654 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 655 | void FocalInside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 656 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 657 | FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 658 | } |
| 659 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 660 | GrGLSLFragmentProcessor* FocalInside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 661 | return new FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 662 | } |
| 663 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 664 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalInside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 665 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 666 | /* |
| 667 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 668 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 669 | sk_sp<GrFragmentProcessor> FocalInside2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 670 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 671 | SkScalar radius1 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 672 | SkPoint center2; |
| 673 | SkScalar radius2; |
| 674 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 675 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 676 | // Below makes sure radius2 is larger enouch such that the focal point |
| 677 | // is inside the end circle |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 678 | SkScalar increase = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 679 | SkPoint diff = center2 - center1; |
| 680 | SkScalar diffLen = diff.length(); |
| 681 | radius2 = diffLen + increase; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 682 | // If the circles are identical the factory will give us an empty shader. |
| 683 | } while (radius1 == radius2 && center1 == center2); |
| 684 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 685 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 686 | auto shader = params.fUseColors4f ? |
| 687 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 688 | params.fColors4f, params.fColorSpace, params.fStops, |
| 689 | params.fColorCount, params.fTileMode) : |
| 690 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 691 | params.fColors, params.fStops, |
| 692 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 693 | GrTest::TestAsFPArgs asFPArgs(d); |
| 694 | sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 695 | GrAlwaysAssert(fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 696 | return fp; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 697 | } |
| 698 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 699 | FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor |
| 700 | ::GLSLFocalInside2PtConicalProcessor(const GrProcessor&) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 701 | : fVSVaryingName(nullptr) |
| 702 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 703 | , fCachedFocal(SK_ScalarMax) {} |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 704 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 705 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 706 | const FocalInside2PtConicalEffect& ge = args.fFp.cast<FocalInside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 707 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 708 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 709 | fFocalUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 710 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 711 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 712 | SkString tName("t"); |
| 713 | |
| 714 | // this is the distance along x-axis from the end center to focal point in |
| 715 | // transformed coordinates |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 716 | GrShaderVar focal = uniformHandler->getUniformVariable(fFocalUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 717 | |
| 718 | // if we have a vec3 from being in perspective, convert it to a vec2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 719 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 720 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 721 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 722 | |
| 723 | // t = p.x * focalX + length(p) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 724 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s + length(%s);\n", tName.c_str(), |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 725 | coords2D, focal.c_str(), coords2D); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 726 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 727 | this->emitColor(fragBuilder, |
| 728 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 729 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 730 | ge, |
| 731 | tName.c_str(), |
| 732 | args.fOutputColor, |
| 733 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 734 | args.fTexSamplers); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 735 | } |
| 736 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 737 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::onSetData( |
| 738 | const GrGLSLProgramDataManager& pdman, |
| 739 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 740 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 741 | const FocalInside2PtConicalEffect& data = processor.cast<FocalInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 742 | SkScalar focal = data.focal(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 743 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 744 | if (fCachedFocal != focal) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 745 | pdman.set1f(fFocalUni, SkScalarToFloat(focal)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 746 | fCachedFocal = focal; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 747 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 748 | } |
| 749 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 750 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::GenKey( |
| 751 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 752 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 753 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | ////////////////////////////////////////////////////////////////////////////// |
| 757 | // Circle Conical Gradients |
| 758 | ////////////////////////////////////////////////////////////////////////////// |
| 759 | |
| 760 | struct CircleConicalInfo { |
| 761 | SkPoint fCenterEnd; |
| 762 | SkScalar fA; |
| 763 | SkScalar fB; |
| 764 | SkScalar fC; |
| 765 | }; |
| 766 | |
| 767 | // Returns focal distance along x-axis in transformed coords |
| 768 | static ConicalType set_matrix_circle_conical(const SkTwoPointConicalGradient& shader, |
| 769 | SkMatrix* invLMatrix, CircleConicalInfo* info) { |
| 770 | // Inverse of the current local matrix is passed in then, |
| 771 | // translate and scale such that start circle is on the origin and has radius 1 |
| 772 | const SkPoint& centerStart = shader.getStartCenter(); |
| 773 | const SkPoint& centerEnd = shader.getEndCenter(); |
| 774 | SkScalar radiusStart = shader.getStartRadius(); |
| 775 | SkScalar radiusEnd = shader.getEndRadius(); |
| 776 | |
| 777 | SkMatrix matrix; |
| 778 | |
| 779 | matrix.setTranslate(-centerStart.fX, -centerStart.fY); |
| 780 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 781 | SkScalar invStartRad = 1.f / radiusStart; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 782 | matrix.postScale(invStartRad, invStartRad); |
| 783 | |
| 784 | radiusEnd /= radiusStart; |
| 785 | |
| 786 | SkPoint centerEndTrans; |
| 787 | matrix.mapPoints(¢erEndTrans, ¢erEnd, 1); |
| 788 | |
| 789 | SkScalar A = centerEndTrans.fX * centerEndTrans.fX + centerEndTrans.fY * centerEndTrans.fY |
| 790 | - radiusEnd * radiusEnd + 2 * radiusEnd - 1; |
| 791 | |
| 792 | // Check to see if start circle is inside end circle with edges touching. |
| 793 | // If touching we return that it is of kEdge_ConicalType, and leave the matrix setting |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 794 | // to the edge shader. kEdgeErrorTol = 5 * kErrorTol was picked after manual testing |
| 795 | // so that C = 1 / A is stable, and the linear approximation used in the Edge shader is |
| 796 | // still accurate. |
| 797 | if (SkScalarAbs(A) < kEdgeErrorTol) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 798 | return kEdge_ConicalType; |
| 799 | } |
| 800 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 801 | SkScalar C = 1.f / A; |
| 802 | SkScalar B = (radiusEnd - 1.f) * C; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 803 | |
| 804 | matrix.postScale(C, C); |
| 805 | |
| 806 | invLMatrix->postConcat(matrix); |
| 807 | |
| 808 | info->fCenterEnd = centerEndTrans; |
| 809 | info->fA = A; |
| 810 | info->fB = B; |
| 811 | info->fC = C; |
| 812 | |
| 813 | // if A ends up being negative, the start circle is contained completely inside the end cirlce |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 814 | if (A < 0.f) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 815 | return kInside_ConicalType; |
| 816 | } |
| 817 | return kOutside_ConicalType; |
| 818 | } |
| 819 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 820 | class CircleInside2PtConicalEffect : public GrGradientEffect { |
| 821 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 822 | class GLSLCircleInside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 823 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 824 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, const CircleConicalInfo& info) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 825 | return sk_sp<GrFragmentProcessor>( |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 826 | new CircleInside2PtConicalEffect(args, info)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | virtual ~CircleInside2PtConicalEffect() {} |
| 830 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 831 | const char* name() const override { return "Two-Point Conical Gradient Inside"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 832 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 833 | SkScalar centerX() const { return fInfo.fCenterEnd.fX; } |
| 834 | SkScalar centerY() const { return fInfo.fCenterEnd.fY; } |
| 835 | SkScalar A() const { return fInfo.fA; } |
| 836 | SkScalar B() const { return fInfo.fB; } |
| 837 | SkScalar C() const { return fInfo.fC; } |
| 838 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 839 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 840 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 841 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 842 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 843 | GrProcessorKeyBuilder* b) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 844 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 845 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 846 | const CircleInside2PtConicalEffect& s = sBase.cast<CircleInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 847 | return (INHERITED::onIsEqual(sBase) && |
| 848 | this->fInfo.fCenterEnd == s.fInfo.fCenterEnd && |
| 849 | this->fInfo.fA == s.fInfo.fA && |
| 850 | this->fInfo.fB == s.fInfo.fB && |
| 851 | this->fInfo.fC == s.fInfo.fC); |
| 852 | } |
| 853 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 854 | CircleInside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 855 | : INHERITED(args, args.fShader->colorsAreOpaque()), fInfo(info) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 856 | this->initClassID<CircleInside2PtConicalEffect>(); |
| 857 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 858 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 859 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 860 | |
| 861 | const CircleConicalInfo fInfo; |
| 862 | |
| 863 | typedef GrGradientEffect INHERITED; |
| 864 | }; |
| 865 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 866 | class CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor |
| 867 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 868 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 869 | GLSLCircleInside2PtConicalProcessor(const GrProcessor&); |
| 870 | virtual ~GLSLCircleInside2PtConicalProcessor() {} |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 871 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 872 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 873 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 874 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 875 | |
| 876 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 877 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 878 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 879 | UniformHandle fCenterUni; |
| 880 | UniformHandle fParamUni; |
| 881 | |
| 882 | const char* fVSVaryingName; |
| 883 | const char* fFSVaryingName; |
| 884 | |
| 885 | // @{ |
| 886 | /// Values last uploaded as uniforms |
| 887 | |
| 888 | SkScalar fCachedCenterX; |
| 889 | SkScalar fCachedCenterY; |
| 890 | SkScalar fCachedA; |
| 891 | SkScalar fCachedB; |
| 892 | SkScalar fCachedC; |
| 893 | |
| 894 | // @} |
| 895 | |
| 896 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 897 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 898 | |
| 899 | }; |
| 900 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 901 | void CircleInside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 902 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 903 | CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 904 | } |
| 905 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 906 | GrGLSLFragmentProcessor* CircleInside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 907 | return new CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 908 | } |
| 909 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 910 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleInside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 911 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 912 | /* |
| 913 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 914 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 915 | sk_sp<GrFragmentProcessor> CircleInside2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 916 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 917 | SkScalar radius1 = d->fRandom->nextUScalar1() + 0.0001f; // make sure radius1 != 0 |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 918 | SkPoint center2; |
| 919 | SkScalar radius2; |
| 920 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 921 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 922 | // Below makes sure that circle one is contained within circle two |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 923 | SkScalar increase = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 924 | SkPoint diff = center2 - center1; |
| 925 | SkScalar diffLen = diff.length(); |
| 926 | radius2 = radius1 + diffLen + increase; |
| 927 | // If the circles are identical the factory will give us an empty shader. |
| 928 | } while (radius1 == radius2 && center1 == center2); |
| 929 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 930 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 931 | auto shader = params.fUseColors4f ? |
| 932 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 933 | params.fColors4f, params.fColorSpace, params.fStops, |
| 934 | params.fColorCount, params.fTileMode) : |
| 935 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 936 | params.fColors, params.fStops, |
| 937 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 938 | GrTest::TestAsFPArgs asFPArgs(d); |
| 939 | sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 940 | GrAlwaysAssert(fp); |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 941 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 942 | } |
| 943 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 944 | CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor |
| 945 | ::GLSLCircleInside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 946 | : fVSVaryingName(nullptr) |
| 947 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 948 | , fCachedCenterX(SK_ScalarMax) |
| 949 | , fCachedCenterY(SK_ScalarMax) |
| 950 | , fCachedA(SK_ScalarMax) |
| 951 | , fCachedB(SK_ScalarMax) |
| 952 | , fCachedC(SK_ScalarMax) {} |
| 953 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 954 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 955 | const CircleInside2PtConicalEffect& ge = args.fFp.cast<CircleInside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 956 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 957 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 958 | fCenterUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 959 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 960 | "Conical2FSCenter"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 961 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 962 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 963 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 964 | SkString tName("t"); |
| 965 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 966 | GrShaderVar center = uniformHandler->getUniformVariable(fCenterUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 967 | // params.x = A |
| 968 | // params.y = B |
| 969 | // params.z = C |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 970 | GrShaderVar params = uniformHandler->getUniformVariable(fParamUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 971 | |
| 972 | // if we have a vec3 from being in perspective, convert it to a vec2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 973 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 974 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 975 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 976 | |
| 977 | // p = coords2D |
| 978 | // e = center end |
| 979 | // r = radius end |
| 980 | // A = dot(e, e) - r^2 + 2 * r - 1 |
| 981 | // B = (r -1) / A |
| 982 | // C = 1 / A |
| 983 | // d = dot(e, p) + B |
| 984 | // t = d +/- sqrt(d^2 - A * dot(p, p) + C) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 985 | fragBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D); |
| 986 | fragBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(), |
| 987 | params.c_str()); |
| 988 | fragBuilder->codeAppendf("\tfloat %s = d + sqrt(d * d - %s.x * pDotp + %s.z);\n", |
| 989 | tName.c_str(), params.c_str(), params.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 990 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 991 | this->emitColor(fragBuilder, |
| 992 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 993 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 994 | ge, |
| 995 | tName.c_str(), |
| 996 | args.fOutputColor, |
| 997 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 998 | args.fTexSamplers); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1001 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::onSetData( |
| 1002 | const GrGLSLProgramDataManager& pdman, |
| 1003 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1004 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1005 | const CircleInside2PtConicalEffect& data = processor.cast<CircleInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1006 | SkScalar centerX = data.centerX(); |
| 1007 | SkScalar centerY = data.centerY(); |
| 1008 | SkScalar A = data.A(); |
| 1009 | SkScalar B = data.B(); |
| 1010 | SkScalar C = data.C(); |
| 1011 | |
| 1012 | if (fCachedCenterX != centerX || fCachedCenterY != centerY || |
| 1013 | fCachedA != A || fCachedB != B || fCachedC != C) { |
| 1014 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1015 | pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY)); |
| 1016 | pdman.set3f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1017 | |
| 1018 | fCachedCenterX = centerX; |
| 1019 | fCachedCenterY = centerY; |
| 1020 | fCachedA = A; |
| 1021 | fCachedB = B; |
| 1022 | fCachedC = C; |
| 1023 | } |
| 1024 | } |
| 1025 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1026 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::GenKey( |
| 1027 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1028 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1029 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | ////////////////////////////////////////////////////////////////////////////// |
| 1033 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1034 | class CircleOutside2PtConicalEffect : public GrGradientEffect { |
| 1035 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1036 | class GLSLCircleOutside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1037 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1038 | static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, const CircleConicalInfo& info) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1039 | return sk_sp<GrFragmentProcessor>( |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1040 | new CircleOutside2PtConicalEffect(args, info)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | virtual ~CircleOutside2PtConicalEffect() {} |
| 1044 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1045 | const char* name() const override { return "Two-Point Conical Gradient Outside"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1046 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1047 | SkScalar centerX() const { return fInfo.fCenterEnd.fX; } |
| 1048 | SkScalar centerY() const { return fInfo.fCenterEnd.fY; } |
| 1049 | SkScalar A() const { return fInfo.fA; } |
| 1050 | SkScalar B() const { return fInfo.fB; } |
| 1051 | SkScalar C() const { return fInfo.fC; } |
| 1052 | SkScalar tLimit() const { return fTLimit; } |
| 1053 | bool isFlipped() const { return fIsFlipped; } |
| 1054 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1055 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1056 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1057 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1058 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 1059 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1060 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1061 | const CircleOutside2PtConicalEffect& s = sBase.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1062 | return (INHERITED::onIsEqual(sBase) && |
| 1063 | this->fInfo.fCenterEnd == s.fInfo.fCenterEnd && |
| 1064 | this->fInfo.fA == s.fInfo.fA && |
| 1065 | this->fInfo.fB == s.fInfo.fB && |
| 1066 | this->fInfo.fC == s.fInfo.fC && |
| 1067 | this->fTLimit == s.fTLimit && |
| 1068 | this->fIsFlipped == s.fIsFlipped); |
| 1069 | } |
| 1070 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1071 | CircleOutside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame^] | 1072 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) |
| 1073 | , fInfo(info) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1074 | this->initClassID<CircleOutside2PtConicalEffect>(); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1075 | const SkTwoPointConicalGradient& shader = |
| 1076 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1077 | if (shader.getStartRadius() != shader.getEndRadius()) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1078 | fTLimit = shader.getStartRadius() / (shader.getStartRadius() - shader.getEndRadius()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1079 | } else { |
| 1080 | fTLimit = SK_ScalarMin; |
| 1081 | } |
| 1082 | |
| 1083 | fIsFlipped = shader.isFlippedGrad(); |
| 1084 | } |
| 1085 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1086 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1087 | |
| 1088 | const CircleConicalInfo fInfo; |
| 1089 | SkScalar fTLimit; |
| 1090 | bool fIsFlipped; |
| 1091 | |
| 1092 | typedef GrGradientEffect INHERITED; |
| 1093 | }; |
| 1094 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1095 | class CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor |
| 1096 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1097 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1098 | GLSLCircleOutside2PtConicalProcessor(const GrProcessor&); |
| 1099 | virtual ~GLSLCircleOutside2PtConicalProcessor() {} |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1100 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1101 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1102 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1103 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1104 | |
| 1105 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1106 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1107 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1108 | UniformHandle fCenterUni; |
| 1109 | UniformHandle fParamUni; |
| 1110 | |
| 1111 | const char* fVSVaryingName; |
| 1112 | const char* fFSVaryingName; |
| 1113 | |
| 1114 | bool fIsFlipped; |
| 1115 | |
| 1116 | // @{ |
| 1117 | /// Values last uploaded as uniforms |
| 1118 | |
| 1119 | SkScalar fCachedCenterX; |
| 1120 | SkScalar fCachedCenterY; |
| 1121 | SkScalar fCachedA; |
| 1122 | SkScalar fCachedB; |
| 1123 | SkScalar fCachedC; |
| 1124 | SkScalar fCachedTLimit; |
| 1125 | |
| 1126 | // @} |
| 1127 | |
| 1128 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1129 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1130 | |
| 1131 | }; |
| 1132 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1133 | void CircleOutside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1134 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1135 | CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1138 | GrGLSLFragmentProcessor* CircleOutside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1139 | return new CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1142 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleOutside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1143 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 1146 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 1147 | sk_sp<GrFragmentProcessor> CircleOutside2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1148 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 1149 | SkScalar radius1 = d->fRandom->nextUScalar1() + 0.0001f; // make sure radius1 != 0 |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1150 | SkPoint center2; |
| 1151 | SkScalar radius2; |
| 1152 | SkScalar diffLen; |
| 1153 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1154 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1155 | // If the circles share a center than we can't be in the outside case |
| 1156 | } while (center1 == center2); |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 1157 | SkPoint diff = center2 - center1; |
| 1158 | diffLen = diff.length(); |
| 1159 | // Below makes sure that circle one is not contained within circle two |
| 1160 | // and have radius2 >= radius to match sorting on cpu side |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1161 | radius2 = radius1 + d->fRandom->nextRangeF(0.f, diffLen); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1162 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1163 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1164 | auto shader = params.fUseColors4f ? |
| 1165 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 1166 | params.fColors4f, params.fColorSpace, params.fStops, |
| 1167 | params.fColorCount, params.fTileMode) : |
| 1168 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 1169 | params.fColors, params.fStops, |
| 1170 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 1171 | GrTest::TestAsFPArgs asFPArgs(d); |
| 1172 | sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1173 | GrAlwaysAssert(fp); |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 1174 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1177 | CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor |
| 1178 | ::GLSLCircleOutside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1179 | : fVSVaryingName(nullptr) |
| 1180 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1181 | , fCachedCenterX(SK_ScalarMax) |
| 1182 | , fCachedCenterY(SK_ScalarMax) |
| 1183 | , fCachedA(SK_ScalarMax) |
| 1184 | , fCachedB(SK_ScalarMax) |
| 1185 | , fCachedC(SK_ScalarMax) |
| 1186 | , fCachedTLimit(SK_ScalarMax) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1187 | const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1188 | fIsFlipped = data.isFlipped(); |
| 1189 | } |
| 1190 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1191 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1192 | const CircleOutside2PtConicalEffect& ge = args.fFp.cast<CircleOutside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1193 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 1194 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1195 | fCenterUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1196 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 1197 | "Conical2FSCenter"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1198 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1199 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 1200 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1201 | SkString tName("t"); |
| 1202 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1203 | GrShaderVar center = uniformHandler->getUniformVariable(fCenterUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1204 | // params.x = A |
| 1205 | // params.y = B |
| 1206 | // params.z = C |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1207 | GrShaderVar params = uniformHandler->getUniformVariable(fParamUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1208 | |
| 1209 | // if we have a vec3 from being in perspective, convert it to a vec2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1210 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 1211 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 1212 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1213 | |
| 1214 | // output will default to transparent black (we simply won't write anything |
| 1215 | // else to it if invalid, instead of discarding or returning prematurely) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1216 | fragBuilder->codeAppendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1217 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1218 | // p = coords2D |
| 1219 | // e = center end |
| 1220 | // r = radius end |
| 1221 | // A = dot(e, e) - r^2 + 2 * r - 1 |
| 1222 | // B = (r -1) / A |
| 1223 | // C = 1 / A |
| 1224 | // d = dot(e, p) + B |
| 1225 | // t = d +/- sqrt(d^2 - A * dot(p, p) + C) |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1226 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1227 | fragBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D); |
| 1228 | fragBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(), |
| 1229 | params.c_str()); |
| 1230 | fragBuilder->codeAppendf("\tfloat deter = d * d - %s.x * pDotp + %s.z;\n", params.c_str(), |
| 1231 | params.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1232 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1233 | // Must check to see if we flipped the circle order (to make sure start radius < end radius) |
| 1234 | // If so we must also flip sign on sqrt |
| 1235 | if (!fIsFlipped) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1236 | fragBuilder->codeAppendf("\tfloat %s = d + sqrt(deter);\n", tName.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1237 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1238 | fragBuilder->codeAppendf("\tfloat %s = d - sqrt(deter);\n", tName.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1239 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1240 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1241 | fragBuilder->codeAppendf("\tif (%s >= %s.w && deter >= 0.0) {\n", |
| 1242 | tName.c_str(), params.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1243 | fragBuilder->codeAppend("\t\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1244 | this->emitColor(fragBuilder, |
| 1245 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1246 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1247 | ge, |
| 1248 | tName.c_str(), |
| 1249 | args.fOutputColor, |
| 1250 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 1251 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1252 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1255 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::onSetData( |
| 1256 | const GrGLSLProgramDataManager& pdman, |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1257 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1258 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1259 | const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1260 | SkASSERT(data.isFlipped() == fIsFlipped); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1261 | SkScalar centerX = data.centerX(); |
| 1262 | SkScalar centerY = data.centerY(); |
| 1263 | SkScalar A = data.A(); |
| 1264 | SkScalar B = data.B(); |
| 1265 | SkScalar C = data.C(); |
| 1266 | SkScalar tLimit = data.tLimit(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1267 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1268 | if (fCachedCenterX != centerX || fCachedCenterY != centerY || |
| 1269 | fCachedA != A || fCachedB != B || fCachedC != C || fCachedTLimit != tLimit) { |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1270 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1271 | pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY)); |
| 1272 | pdman.set4f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1273 | SkScalarToFloat(tLimit)); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1274 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1275 | fCachedCenterX = centerX; |
| 1276 | fCachedCenterY = centerY; |
| 1277 | fCachedA = A; |
| 1278 | fCachedB = B; |
| 1279 | fCachedC = C; |
| 1280 | fCachedTLimit = tLimit; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1284 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey( |
| 1285 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1286 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1287 | uint32_t* key = b->add32n(2); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1288 | key[0] = GenBaseGradientKey(processor); |
| 1289 | key[1] = processor.cast<CircleOutside2PtConicalEffect>().isFlipped(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1290 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1291 | |
| 1292 | ////////////////////////////////////////////////////////////////////////////// |
| 1293 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1294 | sk_sp<GrFragmentProcessor> Gr2PtConicalGradientEffect::Make( |
| 1295 | const GrGradientEffect::CreateArgs& args) { |
| 1296 | const SkTwoPointConicalGradient& shader = |
| 1297 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
| 1298 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1299 | SkMatrix matrix; |
| 1300 | if (!shader.getLocalMatrix().invert(&matrix)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1301 | return nullptr; |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1302 | } |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1303 | if (args.fMatrix) { |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1304 | SkMatrix inv; |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1305 | if (!args.fMatrix->invert(&inv)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1306 | return nullptr; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1307 | } |
| 1308 | matrix.postConcat(inv); |
| 1309 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1310 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1311 | GrGradientEffect::CreateArgs newArgs(args.fContext, args.fShader, &matrix, args.fTileMode, |
| 1312 | std::move(args.fColorSpaceXform), args.fGammaCorrect); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1313 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1314 | if (shader.getStartRadius() < kErrorTol) { |
| 1315 | SkScalar focalX; |
| 1316 | ConicalType type = set_matrix_focal_conical(shader, &matrix, &focalX); |
| 1317 | if (type == kInside_ConicalType) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1318 | return FocalInside2PtConicalEffect::Make(newArgs, focalX); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1319 | } else if(type == kEdge_ConicalType) { |
| 1320 | set_matrix_edge_conical(shader, &matrix); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1321 | return Edge2PtConicalEffect::Make(newArgs); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1322 | } else { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1323 | return FocalOutside2PtConicalEffect::Make(newArgs, focalX); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | CircleConicalInfo info; |
| 1328 | ConicalType type = set_matrix_circle_conical(shader, &matrix, &info); |
| 1329 | |
| 1330 | if (type == kInside_ConicalType) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1331 | return CircleInside2PtConicalEffect::Make(newArgs, info); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1332 | } else if (type == kEdge_ConicalType) { |
| 1333 | set_matrix_edge_conical(shader, &matrix); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1334 | return Edge2PtConicalEffect::Make(newArgs); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1335 | } else { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1336 | return CircleOutside2PtConicalEffect::Make(newArgs, info); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1337 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1340 | #endif |