blob: fb2bb00e7c04619999ab1e639bdbf3b5b4e208b2 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012
John Stiles45f5b032020-07-27 17:31:29 -040013#include "src/core/SkUtils.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
18#include "src/sksl/SkSLCPP.h"
19#include "src/sksl/SkSLUtil.h"
Michael Ludwig8f685082018-09-12 15:23:01 -040020class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLTwoPointConicalGradientLayout() {}
23 void emitCode(EmitArgs& args) override {
Mike Kleind6ab77a2019-03-21 08:18:24 -050024 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwig8f685082018-09-12 15:23:01 -040025 const GrTwoPointConicalGradientLayout& _outer =
26 args.fFp.cast<GrTwoPointConicalGradientLayout>();
27 (void)_outer;
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 Nicholas16464c32020-04-06 13:53:05 -040042 focalParamsVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
43 kHalf2_GrSLType, "focalParams");
Michael Ludwig8f685082018-09-12 15:23:01 -040044 fragBuilder->codeAppendf(
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040045 R"SkSL(float t = -1.0;
John Stiles50819422020-06-18 13:00:38 -040046half v = 1.0;
47@switch (%d) {
48 case 1:
49 {
50 half r0_2 = %s.y;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040051 t = float(r0_2) - %s.y * %s.y;
John Stiles50819422020-06-18 13:00:38 -040052 if (t >= 0.0) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040053 t = %s.x + sqrt(t);
John Stiles50819422020-06-18 13:00:38 -040054 } else {
55 v = -1.0;
56 }
57 }
58 break;
59 case 0:
60 {
61 half r0 = %s.x;
62 @if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040063 t = length(%s) - float(r0);
John Stiles50819422020-06-18 13:00:38 -040064 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040065 t = -length(%s) - float(r0);
John Stiles50819422020-06-18 13:00:38 -040066 }
67 }
68 break;
69 case 2:
70 {
71 half invR1 = %s.x;
72 half fx = %s.y;
73 float x_t = -1.0;
74 @if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040075 x_t = dot(%s, %s) / %s.x;
John Stiles50819422020-06-18 13:00:38 -040076 } else if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040077 x_t = length(%s) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040078 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040079 float temp = %s.x * %s.x - %s.y * %s.y;
John Stiles50819422020-06-18 13:00:38 -040080 if (temp >= 0.0) {
81 @if (%s || !%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040082 x_t = -sqrt(temp) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040083 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040084 x_t = sqrt(temp) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040085 }
86 }
87 }
88 @if (!%s) {
89 if (x_t <= 0.0) {
90 v = -1.0;
91 }
92 }
93 @if (%s) {
94 @if (%s) {
95 t = x_t;
96 } else {
97 t = x_t + float(fx);
98 }
99 } else {
100 @if (%s) {
101 t = -x_t;
102 } else {
103 t = -x_t + float(fx);
104 }
105 }
106 @if (%s) {
107 t = 1.0 - t;
108 }
109 }
110 break;
111}
John Stiles118706a2020-10-09 10:07:38 -0400112return half4(half(t), v, 0.0, 0.0);
John Stiles50819422020-06-18 13:00:38 -0400113)SkSL",
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400114 (int)_outer.type, args.fUniformHandler->getUniformCStr(focalParamsVar),
115 args.fSampleCoord, args.fSampleCoord, args.fSampleCoord,
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500116 args.fUniformHandler->getUniformCStr(focalParamsVar),
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400117 (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord,
118 args.fSampleCoord, args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400119 args.fUniformHandler->getUniformCStr(focalParamsVar),
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400120 (_outer.isFocalOnCircle ? "true" : "false"), args.fSampleCoord, args.fSampleCoord,
121 args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"), args.fSampleCoord,
122 args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, args.fSampleCoord,
123 args.fSampleCoord, (_outer.isSwapped ? "true" : "false"),
124 (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord,
125 args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400126 (_outer.isRadiusIncreasing ? "true" : "false"),
127 (_outer.isNativelyFocal ? "true" : "false"),
John Stiles118706a2020-10-09 10:07:38 -0400128 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -0400129 }
130
131private:
132 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500133 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -0400134 const GrTwoPointConicalGradientLayout& _outer =
135 _proc.cast<GrTwoPointConicalGradientLayout>();
136 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400137 const SkPoint& focalParamsValue = _outer.focalParams;
138 if (focalParamsPrev != focalParamsValue) {
139 focalParamsPrev = focalParamsValue;
140 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400141 }
142 }
143 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400144 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
145 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400146};
147GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
148 return new GrGLSLTwoPointConicalGradientLayout();
149}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500150void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400151 GrProcessorKeyBuilder* b) const {
John Stiles45f5b032020-07-27 17:31:29 -0400152 b->add32((uint32_t)type);
153 b->add32((uint32_t)isRadiusIncreasing);
154 b->add32((uint32_t)isFocalOnCircle);
155 b->add32((uint32_t)isWellBehaved);
156 b->add32((uint32_t)isSwapped);
157 b->add32((uint32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400158}
159bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
160 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
161 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400162 if (type != that.type) return false;
163 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
164 if (isFocalOnCircle != that.isFocalOnCircle) return false;
165 if (isWellBehaved != that.isWellBehaved) return false;
166 if (isSwapped != that.isSwapped) return false;
167 if (isNativelyFocal != that.isNativelyFocal) return false;
168 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400169 return true;
170}
171GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
172 const GrTwoPointConicalGradientLayout& src)
173 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400174 , type(src.type)
175 , isRadiusIncreasing(src.isRadiusIncreasing)
176 , isFocalOnCircle(src.isFocalOnCircle)
177 , isWellBehaved(src.isWellBehaved)
178 , isSwapped(src.isSwapped)
179 , isNativelyFocal(src.isNativelyFocal)
180 , focalParams(src.focalParams) {
Brian Osman12c5d292020-07-13 16:11:35 -0400181 this->cloneAndRegisterAllChildProcessors(src);
Michael Ludwige88320b2020-06-24 09:04:56 -0400182 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400183}
184std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400185 return std::make_unique<GrTwoPointConicalGradientLayout>(*this);
Michael Ludwig8f685082018-09-12 15:23:01 -0400186}
John Stiles8d9bf642020-08-12 15:07:45 -0400187#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400188SkString GrTwoPointConicalGradientLayout::onDumpInfo() const {
John Stiles47b4e222020-08-12 09:56:50 -0400189 return SkStringPrintf(
John Stilescab58862020-08-12 15:47:06 -0400190 "(type=%d, isRadiusIncreasing=%s, isFocalOnCircle=%s, isWellBehaved=%s, isSwapped=%s, "
191 "isNativelyFocal=%s, focalParams=half2(%f, %f))",
John Stiles47b4e222020-08-12 09:56:50 -0400192 (int)type, (isRadiusIncreasing ? "true" : "false"),
193 (isFocalOnCircle ? "true" : "false"), (isWellBehaved ? "true" : "false"),
194 (isSwapped ? "true" : "false"), (isNativelyFocal ? "true" : "false"), focalParams.fX,
195 focalParams.fY);
196}
197#endif
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400198GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
199#if GR_TEST_UTILS
200std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
201 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500202 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400203 SkScalar offset = scale / 32.0f;
204
Mike Kleind6ab77a2019-03-21 08:18:24 -0500205 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400206 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500207 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400208 d->fRandom->nextRangeScalar(0.0f, scale)};
209 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
210 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400211
212 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
213 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
214 // We won't treat isWellDefined and isRadiusIncreasing specially because they
215 // should have high probability to be turned on and off as we're getting random
216 // radii and centers.
217
218 int mask = d->fRandom->nextU();
219 int type = mask & kTestTypeMask;
220 if (type == static_cast<int>(Type::kRadial)) {
221 center2 = center1;
222 // Make sure that the radii are different
223 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400224 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400225 }
226 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500227 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400228 radius2 = radius1;
229 // Make sure that the centers are different
230 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400231 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400232 }
233 } else { // kFocal_Type
234 // Make sure that the centers are different
235 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400236 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400237 }
238
239 if (kTestNativelyFocalBit & mask) {
240 radius1 = 0;
241 }
242 if (kTestFocalOnCircleBit & mask) {
243 radius2 = radius1 + SkPoint::Distance(center1, center2);
244 }
245 if (kTestSwappedBit & mask) {
246 std::swap(radius1, radius2);
247 radius2 = 0;
248 }
249
250 // Make sure that the radii are different
251 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400252 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400253 }
254 }
255
256 if (SkScalarNearlyZero(radius1 - radius2) &&
257 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400258 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400259 }
260
261 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500262 auto shader = params.fUseColors4f
263 ? SkGradientShader::MakeTwoPointConical(
264 center1, radius1, center2, radius2, params.fColors4f,
265 params.fColorSpace, params.fStops, params.fColorCount,
266 params.fTileMode)
267 : SkGradientShader::MakeTwoPointConical(
268 center1, radius1, center2, radius2, params.fColors,
269 params.fStops, params.fColorCount, params.fTileMode);
270 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400271 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
272
Brian Salomon4dea72a2019-12-18 10:43:10 -0500273 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400274 return fp;
275}
276#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400277
278// .fp files do not let you reference outside enum definitions, so we have to explicitly map
279// between the two compatible enum defs
280GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
281 switch (type) {
282 case SkTwoPointConicalGradient::Type::kRadial:
283 return GrTwoPointConicalGradientLayout::Type::kRadial;
284 case SkTwoPointConicalGradient::Type::kStrip:
285 return GrTwoPointConicalGradientLayout::Type::kStrip;
286 case SkTwoPointConicalGradient::Type::kFocal:
287 return GrTwoPointConicalGradientLayout::Type::kFocal;
288 }
289 SkDEBUGFAIL("Should not be reachable");
290 return GrTwoPointConicalGradientLayout::Type::kRadial;
291}
292
293std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500294 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400295 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
296
297 // The focalData struct is only valid if isFocal is true
298 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500299 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400300
301 // Calculate optimization switches from gradient specification
302 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500303 bool isWellBehaved = isFocal && focalData.isWellBehaved();
304 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400305 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
306
307 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
308 // However, all types start with the total inverse local matrix calculated from the shader
309 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500310 bool isRadiusIncreasing;
311 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400312 SkMatrix matrix;
313
314 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400315 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400316 return nullptr;
317 }
318
319 if (isFocal) {
320 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
321
322 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
323
324 matrix.postConcat(grad.getGradientMatrix());
325 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500326 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400327 isRadiusIncreasing = dr >= 0;
328
329 SkScalar r0 = grad.getStartRadius() / dr;
330 focalParams.set(r0, r0 * r0);
331
332 // GPU radial matrix is different from the original matrix, since we map the diff radius
333 // to have |dr| = 1, so manually compute the final gradient matrix here.
334
335 // Map center to (0, 0)
336 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
337
338 // scale |diffRadius| to 1
339 matrix.postScale(1 / dr, 1 / dr);
340 } else { // kStrip
341 isRadiusIncreasing = false; // kStrip doesn't use this flag
342
343 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
344 focalParams.set(r0, r0 * r0);
345
346 matrix.postConcat(grad.getGradientMatrix());
347 }
348
Michael Ludwig1b7399b2020-06-26 09:56:48 -0400349 return GrMatrixEffect::Make(
350 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
351 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
352 isNativelyFocal, focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400353}