blob: 632d17d504f43545e6e01173ed7f79d5ff80835c [file] [log] [blame]
Michael Ludwig8f685082018-09-12 15:23:01 -04001/*
2 * Copyright 2018 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 */
7
8/**************************************************************************************************
9 *** This file was autogenerated from GrTwoPointConicalGradientLayout.fp; do not modify.
10 **************************************************************************************************/
11#include "GrTwoPointConicalGradientLayout.h"
12#include "glsl/GrGLSLFragmentProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLProgramBuilder.h"
15#include "GrTexture.h"
16#include "SkSLCPP.h"
17#include "SkSLUtil.h"
18class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor {
19public:
20 GrGLSLTwoPointConicalGradientLayout() {}
21 void emitCode(EmitArgs& args) override {
Mike Kleind6ab77a2019-03-21 08:18:24 -050022 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwig8f685082018-09-12 15:23:01 -040023 const GrTwoPointConicalGradientLayout& _outer =
24 args.fFp.cast<GrTwoPointConicalGradientLayout>();
25 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040026 auto gradientMatrix = _outer.gradientMatrix;
Michael Ludwig8f685082018-09-12 15:23:01 -040027 (void)gradientMatrix;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040028 auto type = _outer.type;
Michael Ludwig8f685082018-09-12 15:23:01 -040029 (void)type;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040030 auto isRadiusIncreasing = _outer.isRadiusIncreasing;
Michael Ludwig8f685082018-09-12 15:23:01 -040031 (void)isRadiusIncreasing;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040032 auto isFocalOnCircle = _outer.isFocalOnCircle;
Michael Ludwig8f685082018-09-12 15:23:01 -040033 (void)isFocalOnCircle;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040034 auto isWellBehaved = _outer.isWellBehaved;
Michael Ludwig8f685082018-09-12 15:23:01 -040035 (void)isWellBehaved;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040036 auto isSwapped = _outer.isSwapped;
Michael Ludwig8f685082018-09-12 15:23:01 -040037 (void)isSwapped;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040038 auto isNativelyFocal = _outer.isNativelyFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -040039 (void)isNativelyFocal;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040040 auto focalParams = _outer.focalParams;
Michael Ludwig8f685082018-09-12 15:23:01 -040041 (void)focalParams;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040042 focalParamsVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
43 "focalParams");
Michael Ludwig8f685082018-09-12 15:23:01 -040044 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
45 fragBuilder->codeAppendf(
Michael Ludwigbf6bf392018-09-19 16:46:07 -040046 "float2 p = %s;\nfloat t = -1.0;\nhalf v = 1.0;\n@switch (%d) {\n case 1:\n "
Ethan Nicholase1f55022019-02-05 17:17:40 -050047 " {\n half r0_2 = %s.y;\n t = float(r0_2) - p.y * p.y;\n "
48 " if (t >= 0.0) {\n t = p.x + sqrt(t);\n } else "
49 "{\n v = -1.0;\n }\n }\n break;\n case "
50 "0:\n {\n half r0 = %s.x;\n @if (%s) {\n "
51 " t = length(p) - float(r0);\n } else {\n t = "
52 "-length(p) - float(r0);\n ",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040053 sk_TransformedCoords2D_0.c_str(), (int)_outer.type,
54 args.fUniformHandler->getUniformCStr(focalParamsVar),
55 args.fUniformHandler->getUniformCStr(focalParamsVar),
56 (_outer.isRadiusIncreasing ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040057 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050058 " }\n }\n break;\n case 2:\n {\n half invR1 "
59 "= %s.x;\n half fx = %s.y;\n float x_t = -1.0;\n "
60 "@if (%s) {\n x_t = dot(p, p) / p.x;\n } else if (%s) "
61 "{\n x_t = length(p) - p.x * float(invR1);\n } else {\n "
62 " float temp = p.x * p.x - p.y * p.y;\n if (temp >= "
63 "0.0) {\n @if (%s || !%s) {\n x_t = "
64 "-sqrt(temp) - p.x * float(invR1)",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040065 args.fUniformHandler->getUniformCStr(focalParamsVar),
66 args.fUniformHandler->getUniformCStr(focalParamsVar),
67 (_outer.isFocalOnCircle ? "true" : "false"),
68 (_outer.isWellBehaved ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
69 (_outer.isRadiusIncreasing ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040070 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050071 ";\n } else {\n x_t = sqrt(temp) - p.x * "
72 "float(invR1);\n }\n }\n }\n "
73 " @if (!%s) {\n if (x_t <= 0.0) {\n v = -1.0;\n "
74 " }\n }\n @if (%s) {\n @if (%s) "
75 "{\n t = x_t;\n } else {\n t "
76 "= x_t + float(fx);\n }\n } else {\n @if "
77 "(%s) {\n ",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040078 (_outer.isWellBehaved ? "true" : "false"),
79 (_outer.isRadiusIncreasing ? "true" : "false"),
80 (_outer.isNativelyFocal ? "true" : "false"),
81 (_outer.isNativelyFocal ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040082 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050083 " t = -x_t;\n } else {\n t = -x_t + "
Michael Ludwigbf6bf392018-09-19 16:46:07 -040084 "float(fx);\n }\n }\n @if (%s) {\n "
85 " t = 1.0 - t;\n }\n }\n break;\n}\n%s = "
86 "half4(half(t), v, 0.0, 0.0);\n",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040087 (_outer.isSwapped ? "true" : "false"), args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -040088 }
89
90private:
91 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050092 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -040093 const GrTwoPointConicalGradientLayout& _outer =
94 _proc.cast<GrTwoPointConicalGradientLayout>();
95 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040096 const SkPoint& focalParamsValue = _outer.focalParams;
97 if (focalParamsPrev != focalParamsValue) {
98 focalParamsPrev = focalParamsValue;
99 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400100 }
101 }
102 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400103 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
104 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400105};
106GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
107 return new GrGLSLTwoPointConicalGradientLayout();
108}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500109void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400110 GrProcessorKeyBuilder* b) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400111 b->add32((int32_t)type);
112 b->add32((int32_t)isRadiusIncreasing);
113 b->add32((int32_t)isFocalOnCircle);
114 b->add32((int32_t)isWellBehaved);
115 b->add32((int32_t)isSwapped);
116 b->add32((int32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400117}
118bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
119 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
120 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400121 if (gradientMatrix != that.gradientMatrix) return false;
122 if (type != that.type) return false;
123 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
124 if (isFocalOnCircle != that.isFocalOnCircle) return false;
125 if (isWellBehaved != that.isWellBehaved) return false;
126 if (isSwapped != that.isSwapped) return false;
127 if (isNativelyFocal != that.isNativelyFocal) return false;
128 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400129 return true;
130}
131GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
132 const GrTwoPointConicalGradientLayout& src)
133 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400134 , fCoordTransform0(src.fCoordTransform0)
135 , gradientMatrix(src.gradientMatrix)
136 , type(src.type)
137 , isRadiusIncreasing(src.isRadiusIncreasing)
138 , isFocalOnCircle(src.isFocalOnCircle)
139 , isWellBehaved(src.isWellBehaved)
140 , isSwapped(src.isSwapped)
141 , isNativelyFocal(src.isNativelyFocal)
142 , focalParams(src.focalParams) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400143 this->addCoordTransform(&fCoordTransform0);
144}
145std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
146 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
147}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400148GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
149#if GR_TEST_UTILS
150std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
151 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500152 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400153 SkScalar offset = scale / 32.0f;
154
Mike Kleind6ab77a2019-03-21 08:18:24 -0500155 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400156 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500157 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400158 d->fRandom->nextRangeScalar(0.0f, scale)};
159 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
160 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400161
162 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
163 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
164 // We won't treat isWellDefined and isRadiusIncreasing specially because they
165 // should have high probability to be turned on and off as we're getting random
166 // radii and centers.
167
168 int mask = d->fRandom->nextU();
169 int type = mask & kTestTypeMask;
170 if (type == static_cast<int>(Type::kRadial)) {
171 center2 = center1;
172 // Make sure that the radii are different
173 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400174 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400175 }
176 } else if (type == static_cast<int>(Type::kStrip)) {
177 radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
178 radius2 = radius1;
179 // Make sure that the centers are different
180 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400181 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400182 }
183 } else { // kFocal_Type
184 // Make sure that the centers are different
185 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400186 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400187 }
188
189 if (kTestNativelyFocalBit & mask) {
190 radius1 = 0;
191 }
192 if (kTestFocalOnCircleBit & mask) {
193 radius2 = radius1 + SkPoint::Distance(center1, center2);
194 }
195 if (kTestSwappedBit & mask) {
196 std::swap(radius1, radius2);
197 radius2 = 0;
198 }
199
200 // Make sure that the radii are different
201 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400202 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400203 }
204 }
205
206 if (SkScalarNearlyZero(radius1 - radius2) &&
207 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400208 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400209 }
210
211 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500212 auto shader = params.fUseColors4f
213 ? SkGradientShader::MakeTwoPointConical(
214 center1, radius1, center2, radius2, params.fColors4f,
215 params.fColorSpace, params.fStops, params.fColorCount,
216 params.fTileMode)
217 : SkGradientShader::MakeTwoPointConical(
218 center1, radius1, center2, radius2, params.fColors,
219 params.fStops, params.fColorCount, params.fTileMode);
220 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400221 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
222
223 GrAlwaysAssert(fp);
224 return fp;
225}
226#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400227
228// .fp files do not let you reference outside enum definitions, so we have to explicitly map
229// between the two compatible enum defs
230GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
231 switch (type) {
232 case SkTwoPointConicalGradient::Type::kRadial:
233 return GrTwoPointConicalGradientLayout::Type::kRadial;
234 case SkTwoPointConicalGradient::Type::kStrip:
235 return GrTwoPointConicalGradientLayout::Type::kStrip;
236 case SkTwoPointConicalGradient::Type::kFocal:
237 return GrTwoPointConicalGradientLayout::Type::kFocal;
238 }
239 SkDEBUGFAIL("Should not be reachable");
240 return GrTwoPointConicalGradientLayout::Type::kRadial;
241}
242
243std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500244 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400245 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
246
247 // The focalData struct is only valid if isFocal is true
248 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500249 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400250
251 // Calculate optimization switches from gradient specification
252 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500253 bool isWellBehaved = isFocal && focalData.isWellBehaved();
254 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400255 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
256
257 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
258 // However, all types start with the total inverse local matrix calculated from the shader
259 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500260 bool isRadiusIncreasing;
261 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400262 SkMatrix matrix;
263
264 // Initialize the base matrix
265 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
266 return nullptr;
267 }
268
269 if (isFocal) {
270 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
271
272 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
273
274 matrix.postConcat(grad.getGradientMatrix());
275 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500276 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400277 isRadiusIncreasing = dr >= 0;
278
279 SkScalar r0 = grad.getStartRadius() / dr;
280 focalParams.set(r0, r0 * r0);
281
282 // GPU radial matrix is different from the original matrix, since we map the diff radius
283 // to have |dr| = 1, so manually compute the final gradient matrix here.
284
285 // Map center to (0, 0)
286 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
287
288 // scale |diffRadius| to 1
289 matrix.postScale(1 / dr, 1 / dr);
290 } else { // kStrip
291 isRadiusIncreasing = false; // kStrip doesn't use this flag
292
293 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
294 focalParams.set(r0, r0 * r0);
295
296 matrix.postConcat(grad.getGradientMatrix());
297 }
298
Mike Kleind6ab77a2019-03-21 08:18:24 -0500299 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
300 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
301 isNativelyFocal, focalParams));
Michael Ludwig8f685082018-09-12 15:23:01 -0400302}