blob: 745191dde33fc01f076eff10a6ad3ca5823923cf [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;
John Stiles9b660ef2021-03-04 15:45:00 -050042 focalParamsVar = args.fUniformHandler->addUniform(
43 &_outer, kFragment_GrShaderFlag, 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",
John Stiles9b660ef2021-03-04 15:45:00 -0500114 (int)_outer.type,
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500115 args.fUniformHandler->getUniformCStr(focalParamsVar),
John Stiles9b660ef2021-03-04 15:45:00 -0500116 args.fSampleCoord,
117 args.fSampleCoord,
118 args.fSampleCoord,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400119 args.fUniformHandler->getUniformCStr(focalParamsVar),
John Stiles9b660ef2021-03-04 15:45:00 -0500120 (_outer.isRadiusIncreasing ? "true" : "false"),
121 args.fSampleCoord,
122 args.fSampleCoord,
123 args.fUniformHandler->getUniformCStr(focalParamsVar),
124 args.fUniformHandler->getUniformCStr(focalParamsVar),
125 (_outer.isFocalOnCircle ? "true" : "false"),
126 args.fSampleCoord,
127 args.fSampleCoord,
128 args.fSampleCoord,
129 (_outer.isWellBehaved ? "true" : "false"),
130 args.fSampleCoord,
131 args.fSampleCoord,
132 args.fSampleCoord,
133 args.fSampleCoord,
134 args.fSampleCoord,
135 args.fSampleCoord,
136 (_outer.isSwapped ? "true" : "false"),
137 (_outer.isRadiusIncreasing ? "true" : "false"),
138 args.fSampleCoord,
139 args.fSampleCoord,
140 (_outer.isWellBehaved ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400141 (_outer.isRadiusIncreasing ? "true" : "false"),
142 (_outer.isNativelyFocal ? "true" : "false"),
John Stiles9b660ef2021-03-04 15:45:00 -0500143 (_outer.isNativelyFocal ? "true" : "false"),
144 (_outer.isSwapped ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -0400145 }
146
147private:
148 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500149 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -0400150 const GrTwoPointConicalGradientLayout& _outer =
151 _proc.cast<GrTwoPointConicalGradientLayout>();
152 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400153 const SkPoint& focalParamsValue = _outer.focalParams;
154 if (focalParamsPrev != focalParamsValue) {
155 focalParamsPrev = focalParamsValue;
156 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400157 }
158 }
159 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400160 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
161 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400162};
Brian Salomon18ab2032021-02-23 10:07:05 -0500163std::unique_ptr<GrGLSLFragmentProcessor> GrTwoPointConicalGradientLayout::onMakeProgramImpl()
164 const {
165 return std::make_unique<GrGLSLTwoPointConicalGradientLayout>();
Michael Ludwig8f685082018-09-12 15:23:01 -0400166}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500167void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400168 GrProcessorKeyBuilder* b) const {
Brian Osman4c594372021-03-02 16:57:09 -0500169 b->addBits(2, (uint32_t)type, "type");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500170 b->addBool(isRadiusIncreasing, "isRadiusIncreasing");
171 b->addBool(isFocalOnCircle, "isFocalOnCircle");
172 b->addBool(isWellBehaved, "isWellBehaved");
173 b->addBool(isSwapped, "isSwapped");
174 b->addBool(isNativelyFocal, "isNativelyFocal");
Michael Ludwig8f685082018-09-12 15:23:01 -0400175}
176bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
177 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
178 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400179 if (type != that.type) return false;
180 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
181 if (isFocalOnCircle != that.isFocalOnCircle) return false;
182 if (isWellBehaved != that.isWellBehaved) return false;
183 if (isSwapped != that.isSwapped) return false;
184 if (isNativelyFocal != that.isNativelyFocal) return false;
185 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400186 return true;
187}
188GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
189 const GrTwoPointConicalGradientLayout& src)
190 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400191 , type(src.type)
192 , isRadiusIncreasing(src.isRadiusIncreasing)
193 , isFocalOnCircle(src.isFocalOnCircle)
194 , isWellBehaved(src.isWellBehaved)
195 , isSwapped(src.isSwapped)
196 , isNativelyFocal(src.isNativelyFocal)
197 , focalParams(src.focalParams) {
Brian Osman12c5d292020-07-13 16:11:35 -0400198 this->cloneAndRegisterAllChildProcessors(src);
Michael Ludwige88320b2020-06-24 09:04:56 -0400199 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400200}
201std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400202 return std::make_unique<GrTwoPointConicalGradientLayout>(*this);
Michael Ludwig8f685082018-09-12 15:23:01 -0400203}
John Stiles8d9bf642020-08-12 15:07:45 -0400204#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400205SkString GrTwoPointConicalGradientLayout::onDumpInfo() const {
John Stiles47b4e222020-08-12 09:56:50 -0400206 return SkStringPrintf(
John Stilescab58862020-08-12 15:47:06 -0400207 "(type=%d, isRadiusIncreasing=%s, isFocalOnCircle=%s, isWellBehaved=%s, isSwapped=%s, "
208 "isNativelyFocal=%s, focalParams=half2(%f, %f))",
John Stiles9b660ef2021-03-04 15:45:00 -0500209 (int)type,
210 (isRadiusIncreasing ? "true" : "false"),
211 (isFocalOnCircle ? "true" : "false"),
212 (isWellBehaved ? "true" : "false"),
213 (isSwapped ? "true" : "false"),
214 (isNativelyFocal ? "true" : "false"),
215 focalParams.fX,
John Stiles47b4e222020-08-12 09:56:50 -0400216 focalParams.fY);
217}
218#endif
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400219GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
220#if GR_TEST_UTILS
221std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
222 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500223 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400224 SkScalar offset = scale / 32.0f;
225
Mike Kleind6ab77a2019-03-21 08:18:24 -0500226 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400227 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500228 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400229 d->fRandom->nextRangeScalar(0.0f, scale)};
230 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
231 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400232
233 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
234 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
235 // We won't treat isWellDefined and isRadiusIncreasing specially because they
236 // should have high probability to be turned on and off as we're getting random
237 // radii and centers.
238
239 int mask = d->fRandom->nextU();
240 int type = mask & kTestTypeMask;
241 if (type == static_cast<int>(Type::kRadial)) {
242 center2 = center1;
243 // Make sure that the radii are different
244 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400245 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400246 }
247 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500248 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400249 radius2 = radius1;
250 // Make sure that the centers are different
251 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400252 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400253 }
254 } else { // kFocal_Type
255 // Make sure that the centers are different
256 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400257 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400258 }
259
260 if (kTestNativelyFocalBit & mask) {
261 radius1 = 0;
262 }
263 if (kTestFocalOnCircleBit & mask) {
264 radius2 = radius1 + SkPoint::Distance(center1, center2);
265 }
266 if (kTestSwappedBit & mask) {
267 std::swap(radius1, radius2);
268 radius2 = 0;
269 }
270
271 // Make sure that the radii are different
272 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400273 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400274 }
275 }
276
277 if (SkScalarNearlyZero(radius1 - radius2) &&
278 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400279 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400280 }
281
282 GrGradientShader::RandomParams params(d->fRandom);
John Stiles9b660ef2021-03-04 15:45:00 -0500283 auto shader = params.fUseColors4f ? SkGradientShader::MakeTwoPointConical(center1,
284 radius1,
285 center2,
286 radius2,
287 params.fColors4f,
288 params.fColorSpace,
289 params.fStops,
290 params.fColorCount,
291 params.fTileMode)
292 : SkGradientShader::MakeTwoPointConical(center1,
293 radius1,
294 center2,
295 radius2,
296 params.fColors,
297 params.fStops,
298 params.fColorCount,
299 params.fTileMode);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500300 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400301 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
302
Brian Salomon4dea72a2019-12-18 10:43:10 -0500303 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400304 return fp;
305}
306#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400307
308// .fp files do not let you reference outside enum definitions, so we have to explicitly map
309// between the two compatible enum defs
310GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
311 switch (type) {
312 case SkTwoPointConicalGradient::Type::kRadial:
313 return GrTwoPointConicalGradientLayout::Type::kRadial;
314 case SkTwoPointConicalGradient::Type::kStrip:
315 return GrTwoPointConicalGradientLayout::Type::kStrip;
316 case SkTwoPointConicalGradient::Type::kFocal:
317 return GrTwoPointConicalGradientLayout::Type::kFocal;
318 }
319 SkDEBUGFAIL("Should not be reachable");
320 return GrTwoPointConicalGradientLayout::Type::kRadial;
321}
322
323std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500324 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400325 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
326
327 // The focalData struct is only valid if isFocal is true
328 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500329 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400330
331 // Calculate optimization switches from gradient specification
332 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500333 bool isWellBehaved = isFocal && focalData.isWellBehaved();
334 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400335 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
336
337 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
338 // However, all types start with the total inverse local matrix calculated from the shader
339 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500340 bool isRadiusIncreasing;
341 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400342 SkMatrix matrix;
343
344 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400345 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400346 return nullptr;
347 }
348
349 if (isFocal) {
350 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
351
352 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
353
354 matrix.postConcat(grad.getGradientMatrix());
355 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500356 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400357 isRadiusIncreasing = dr >= 0;
358
359 SkScalar r0 = grad.getStartRadius() / dr;
360 focalParams.set(r0, r0 * r0);
361
362 // GPU radial matrix is different from the original matrix, since we map the diff radius
363 // to have |dr| = 1, so manually compute the final gradient matrix here.
364
365 // Map center to (0, 0)
366 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
367
368 // scale |diffRadius| to 1
369 matrix.postScale(1 / dr, 1 / dr);
370 } else { // kStrip
371 isRadiusIncreasing = false; // kStrip doesn't use this flag
372
373 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
374 focalParams.set(r0, r0 * r0);
375
376 matrix.postConcat(grad.getGradientMatrix());
377 }
378
John Stiles9b660ef2021-03-04 15:45:00 -0500379 return GrMatrixEffect::Make(matrix,
380 std::unique_ptr<GrFragmentProcessor>(
381 new GrTwoPointConicalGradientLayout(grType,
382 isRadiusIncreasing,
383 isFocalOnCircle,
384 isWellBehaved,
385 isSwapped,
386 isNativelyFocal,
387 focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400388}