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