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