blob: d234c3db04d54657073bad79f3f8d6c98907448d [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 Kleina2a5cc02019-03-19 11:30:11 -050022 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwig8f685082018-09-12 15:23:01 -040023 const GrTwoPointConicalGradientLayout& _outer =
24 args.fFp.cast<GrTwoPointConicalGradientLayout>();
25 (void)_outer;
26 auto gradientMatrix = _outer.gradientMatrix();
27 (void)gradientMatrix;
28 auto type = _outer.type();
29 (void)type;
30 auto isRadiusIncreasing = _outer.isRadiusIncreasing();
31 (void)isRadiusIncreasing;
32 auto isFocalOnCircle = _outer.isFocalOnCircle();
33 (void)isFocalOnCircle;
34 auto isWellBehaved = _outer.isWellBehaved();
35 (void)isWellBehaved;
36 auto isSwapped = _outer.isSwapped();
37 (void)isSwapped;
38 auto isNativelyFocal = _outer.isNativelyFocal();
39 (void)isNativelyFocal;
40 auto focalParams = _outer.focalParams();
41 (void)focalParams;
Mike Kleina2a5cc02019-03-19 11:30:11 -050042 fFocalParamsVar = args.fUniformHandler->addUniform(
43 kFragment_GrShaderFlag, kHalf2_GrSLType, "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 ",
Mike Kleina2a5cc02019-03-19 11:30:11 -050053 sk_TransformedCoords2D_0.c_str(),
54 (int)_outer.type(),
Michael Ludwig8f685082018-09-12 15:23:01 -040055 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
56 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
57 (_outer.isRadiusIncreasing() ? "true" : "false"));
58 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050059 " }\n }\n break;\n case 2:\n {\n half invR1 "
60 "= %s.x;\n half fx = %s.y;\n float x_t = -1.0;\n "
61 "@if (%s) {\n x_t = dot(p, p) / p.x;\n } else if (%s) "
62 "{\n x_t = length(p) - p.x * float(invR1);\n } else {\n "
63 " float temp = p.x * p.x - p.y * p.y;\n if (temp >= "
64 "0.0) {\n @if (%s || !%s) {\n x_t = "
65 "-sqrt(temp) - p.x * float(invR1)",
Michael Ludwig8f685082018-09-12 15:23:01 -040066 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
67 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
68 (_outer.isFocalOnCircle() ? "true" : "false"),
69 (_outer.isWellBehaved() ? "true" : "false"),
70 (_outer.isSwapped() ? "true" : "false"),
71 (_outer.isRadiusIncreasing() ? "true" : "false"));
72 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050073 ";\n } else {\n x_t = sqrt(temp) - p.x * "
74 "float(invR1);\n }\n }\n }\n "
75 " @if (!%s) {\n if (x_t <= 0.0) {\n v = -1.0;\n "
76 " }\n }\n @if (%s) {\n @if (%s) "
77 "{\n t = x_t;\n } else {\n t "
78 "= x_t + float(fx);\n }\n } else {\n @if "
79 "(%s) {\n ",
Michael Ludwig8f685082018-09-12 15:23:01 -040080 (_outer.isWellBehaved() ? "true" : "false"),
81 (_outer.isRadiusIncreasing() ? "true" : "false"),
82 (_outer.isNativelyFocal() ? "true" : "false"),
83 (_outer.isNativelyFocal() ? "true" : "false"));
84 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050085 " t = -x_t;\n } else {\n t = -x_t + "
Michael Ludwigbf6bf392018-09-19 16:46:07 -040086 "float(fx);\n }\n }\n @if (%s) {\n "
87 " t = 1.0 - t;\n }\n }\n break;\n}\n%s = "
88 "half4(half(t), v, 0.0, 0.0);\n",
Mike Kleina2a5cc02019-03-19 11:30:11 -050089 (_outer.isSwapped() ? "true" : "false"),
90 args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -040091 }
92
93private:
94 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleina2a5cc02019-03-19 11:30:11 -050095 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -040096 const GrTwoPointConicalGradientLayout& _outer =
97 _proc.cast<GrTwoPointConicalGradientLayout>();
98 {
99 const SkPoint& focalParamsValue = _outer.focalParams();
100 if (fFocalParamsPrev != focalParamsValue) {
101 fFocalParamsPrev = focalParamsValue;
102 pdman.set2f(fFocalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
103 }
104 }
105 }
Mike Kleina2a5cc02019-03-19 11:30:11 -0500106 SkPoint fFocalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
Michael Ludwig8f685082018-09-12 15:23:01 -0400107 UniformHandle fFocalParamsVar;
108};
109GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
110 return new GrGLSLTwoPointConicalGradientLayout();
111}
Mike Kleina2a5cc02019-03-19 11:30:11 -0500112void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400113 GrProcessorKeyBuilder* b) const {
114 b->add32((int32_t)fType);
115 b->add32((int32_t)fIsRadiusIncreasing);
116 b->add32((int32_t)fIsFocalOnCircle);
117 b->add32((int32_t)fIsWellBehaved);
118 b->add32((int32_t)fIsSwapped);
119 b->add32((int32_t)fIsNativelyFocal);
120}
121bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
122 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
123 (void)that;
Mike Kleina2a5cc02019-03-19 11:30:11 -0500124 if (fGradientMatrix != that.fGradientMatrix)
125 return false;
126 if (fType != that.fType)
127 return false;
128 if (fIsRadiusIncreasing != that.fIsRadiusIncreasing)
129 return false;
130 if (fIsFocalOnCircle != that.fIsFocalOnCircle)
131 return false;
132 if (fIsWellBehaved != that.fIsWellBehaved)
133 return false;
134 if (fIsSwapped != that.fIsSwapped)
135 return false;
136 if (fIsNativelyFocal != that.fIsNativelyFocal)
137 return false;
138 if (fFocalParams != that.fFocalParams)
139 return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400140 return true;
141}
142GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
143 const GrTwoPointConicalGradientLayout& src)
144 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
145 , fGradientMatrix(src.fGradientMatrix)
146 , fType(src.fType)
147 , fIsRadiusIncreasing(src.fIsRadiusIncreasing)
148 , fIsFocalOnCircle(src.fIsFocalOnCircle)
149 , fIsWellBehaved(src.fIsWellBehaved)
150 , fIsSwapped(src.fIsSwapped)
151 , fIsNativelyFocal(src.fIsNativelyFocal)
152 , fFocalParams(src.fFocalParams)
153 , fCoordTransform0(src.fCoordTransform0) {
154 this->addCoordTransform(&fCoordTransform0);
155}
156std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
157 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
158}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400159GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
160#if GR_TEST_UTILS
161std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
162 GrProcessorTestData* d) {
Mike Kleina2a5cc02019-03-19 11:30:11 -0500163 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400164 SkScalar offset = scale / 32.0f;
165
Mike Kleina2a5cc02019-03-19 11:30:11 -0500166 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400167 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleina2a5cc02019-03-19 11:30:11 -0500168 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400169 d->fRandom->nextRangeScalar(0.0f, scale)};
170 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
171 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400172
173 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
174 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
175 // We won't treat isWellDefined and isRadiusIncreasing specially because they
176 // should have high probability to be turned on and off as we're getting random
177 // radii and centers.
178
179 int mask = d->fRandom->nextU();
180 int type = mask & kTestTypeMask;
181 if (type == static_cast<int>(Type::kRadial)) {
182 center2 = center1;
183 // Make sure that the radii are different
184 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400185 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400186 }
187 } else if (type == static_cast<int>(Type::kStrip)) {
188 radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
189 radius2 = radius1;
190 // Make sure that the centers are different
191 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400192 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400193 }
194 } else { // kFocal_Type
195 // Make sure that the centers are different
196 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400197 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400198 }
199
200 if (kTestNativelyFocalBit & mask) {
201 radius1 = 0;
202 }
203 if (kTestFocalOnCircleBit & mask) {
204 radius2 = radius1 + SkPoint::Distance(center1, center2);
205 }
206 if (kTestSwappedBit & mask) {
207 std::swap(radius1, radius2);
208 radius2 = 0;
209 }
210
211 // Make sure that the radii are different
212 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400213 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400214 }
215 }
216
217 if (SkScalarNearlyZero(radius1 - radius2) &&
218 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400219 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400220 }
221
222 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleina2a5cc02019-03-19 11:30:11 -0500223 auto shader = params.fUseColors4f ? SkGradientShader::MakeTwoPointConical(center1,
224 radius1,
225 center2,
226 radius2,
227 params.fColors4f,
228 params.fColorSpace,
229 params.fStops,
230 params.fColorCount,
231 params.fTileMode)
232 : SkGradientShader::MakeTwoPointConical(center1,
233 radius1,
234 center2,
235 radius2,
236 params.fColors,
237 params.fStops,
238 params.fColorCount,
239 params.fTileMode);
240 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400241 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
242
243 GrAlwaysAssert(fp);
244 return fp;
245}
246#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400247
248// .fp files do not let you reference outside enum definitions, so we have to explicitly map
249// between the two compatible enum defs
250GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
251 switch (type) {
252 case SkTwoPointConicalGradient::Type::kRadial:
253 return GrTwoPointConicalGradientLayout::Type::kRadial;
254 case SkTwoPointConicalGradient::Type::kStrip:
255 return GrTwoPointConicalGradientLayout::Type::kStrip;
256 case SkTwoPointConicalGradient::Type::kFocal:
257 return GrTwoPointConicalGradientLayout::Type::kFocal;
258 }
259 SkDEBUGFAIL("Should not be reachable");
260 return GrTwoPointConicalGradientLayout::Type::kRadial;
261}
262
263std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleina2a5cc02019-03-19 11:30:11 -0500264 const SkTwoPointConicalGradient& grad,
265 const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400266 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
267
268 // The focalData struct is only valid if isFocal is true
269 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleina2a5cc02019-03-19 11:30:11 -0500270 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400271
272 // Calculate optimization switches from gradient specification
273 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleina2a5cc02019-03-19 11:30:11 -0500274 bool isWellBehaved = isFocal && focalData.isWellBehaved();
275 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400276 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
277
278 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
279 // However, all types start with the total inverse local matrix calculated from the shader
280 // and args
Mike Kleina2a5cc02019-03-19 11:30:11 -0500281 bool isRadiusIncreasing;
282 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400283 SkMatrix matrix;
284
285 // Initialize the base matrix
286 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
287 return nullptr;
288 }
289
290 if (isFocal) {
291 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
292
293 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
294
295 matrix.postConcat(grad.getGradientMatrix());
296 } else if (grType == Type::kRadial) {
Mike Kleina2a5cc02019-03-19 11:30:11 -0500297 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400298 isRadiusIncreasing = dr >= 0;
299
300 SkScalar r0 = grad.getStartRadius() / dr;
301 focalParams.set(r0, r0 * r0);
302
303 // GPU radial matrix is different from the original matrix, since we map the diff radius
304 // to have |dr| = 1, so manually compute the final gradient matrix here.
305
306 // Map center to (0, 0)
307 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
308
309 // scale |diffRadius| to 1
310 matrix.postScale(1 / dr, 1 / dr);
311 } else { // kStrip
312 isRadiusIncreasing = false; // kStrip doesn't use this flag
313
314 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
315 focalParams.set(r0, r0 * r0);
316
317 matrix.postConcat(grad.getGradientMatrix());
318 }
319
Mike Kleina2a5cc02019-03-19 11:30:11 -0500320 return std::unique_ptr<GrFragmentProcessor>(
321 new GrTwoPointConicalGradientLayout(matrix,
322 grType,
323 isRadiusIncreasing,
324 isFocalOnCircle,
325 isWellBehaved,
326 isSwapped,
327 isNativelyFocal,
328 focalParams));
Michael Ludwig8f685082018-09-12 15:23:01 -0400329}