blob: 1dd4aabe1c56e8cd001ba166d18830c2ff78691b [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
Greg Daniel456f9b52020-03-05 19:14:18 +000013#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17#include "src/sksl/SkSLCPP.h"
18#include "src/sksl/SkSLUtil.h"
Michael Ludwig8f685082018-09-12 15:23:01 -040019class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLTwoPointConicalGradientLayout() {}
22 void emitCode(EmitArgs& args) override {
Mike Kleind6ab77a2019-03-21 08:18:24 -050023 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Michael Ludwig8f685082018-09-12 15:23:01 -040024 const GrTwoPointConicalGradientLayout& _outer =
25 args.fFp.cast<GrTwoPointConicalGradientLayout>();
26 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040027 auto type = _outer.type;
Michael Ludwig8f685082018-09-12 15:23:01 -040028 (void)type;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040029 auto isRadiusIncreasing = _outer.isRadiusIncreasing;
Michael Ludwig8f685082018-09-12 15:23:01 -040030 (void)isRadiusIncreasing;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040031 auto isFocalOnCircle = _outer.isFocalOnCircle;
Michael Ludwig8f685082018-09-12 15:23:01 -040032 (void)isFocalOnCircle;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040033 auto isWellBehaved = _outer.isWellBehaved;
Michael Ludwig8f685082018-09-12 15:23:01 -040034 (void)isWellBehaved;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040035 auto isSwapped = _outer.isSwapped;
Michael Ludwig8f685082018-09-12 15:23:01 -040036 (void)isSwapped;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040037 auto isNativelyFocal = _outer.isNativelyFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -040038 (void)isNativelyFocal;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040039 auto focalParams = _outer.focalParams;
Michael Ludwig8f685082018-09-12 15:23:01 -040040 (void)focalParams;
Ethan Nicholas16464c32020-04-06 13:53:05 -040041 focalParamsVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
42 kHalf2_GrSLType, "focalParams");
Michael Ludwig8f685082018-09-12 15:23:01 -040043 fragBuilder->codeAppendf(
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040044 R"SkSL(float t = -1.0;
John Stiles50819422020-06-18 13:00:38 -040045half v = 1.0;
46@switch (%d) {
47 case 1:
48 {
49 half r0_2 = %s.y;
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040050 t = float(r0_2) - %s.y * %s.y;
John Stiles50819422020-06-18 13:00:38 -040051 if (t >= 0.0) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040052 t = %s.x + sqrt(t);
John Stiles50819422020-06-18 13:00:38 -040053 } else {
54 v = -1.0;
55 }
56 }
57 break;
58 case 0:
59 {
60 half r0 = %s.x;
61 @if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040062 t = length(%s) - float(r0);
John Stiles50819422020-06-18 13:00:38 -040063 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040064 t = -length(%s) - float(r0);
John Stiles50819422020-06-18 13:00:38 -040065 }
66 }
67 break;
68 case 2:
69 {
70 half invR1 = %s.x;
71 half fx = %s.y;
72 float x_t = -1.0;
73 @if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040074 x_t = dot(%s, %s) / %s.x;
John Stiles50819422020-06-18 13:00:38 -040075 } else if (%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040076 x_t = length(%s) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040077 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040078 float temp = %s.x * %s.x - %s.y * %s.y;
John Stiles50819422020-06-18 13:00:38 -040079 if (temp >= 0.0) {
80 @if (%s || !%s) {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040081 x_t = -sqrt(temp) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040082 } else {
Michael Ludwigfc2fdf02020-06-29 17:20:13 -040083 x_t = sqrt(temp) - %s.x * float(invR1);
John Stiles50819422020-06-18 13:00:38 -040084 }
85 }
86 }
87 @if (!%s) {
88 if (x_t <= 0.0) {
89 v = -1.0;
90 }
91 }
92 @if (%s) {
93 @if (%s) {
94 t = x_t;
95 } else {
96 t = x_t + float(fx);
97 }
98 } else {
99 @if (%s) {
100 t = -x_t;
101 } else {
102 t = -x_t + float(fx);
103 }
104 }
105 @if (%s) {
106 t = 1.0 - t;
107 }
108 }
109 break;
110}
111%s = half4(half(t), v, 0.0, 0.0);
112)SkSL",
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400113 (int)_outer.type, args.fUniformHandler->getUniformCStr(focalParamsVar),
114 args.fSampleCoord, args.fSampleCoord, args.fSampleCoord,
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500115 args.fUniformHandler->getUniformCStr(focalParamsVar),
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400116 (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord,
117 args.fSampleCoord, args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400118 args.fUniformHandler->getUniformCStr(focalParamsVar),
Michael Ludwigfc2fdf02020-06-29 17:20:13 -0400119 (_outer.isFocalOnCircle ? "true" : "false"), args.fSampleCoord, args.fSampleCoord,
120 args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"), args.fSampleCoord,
121 args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, args.fSampleCoord,
122 args.fSampleCoord, (_outer.isSwapped ? "true" : "false"),
123 (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord,
124 args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400125 (_outer.isRadiusIncreasing ? "true" : "false"),
126 (_outer.isNativelyFocal ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400127 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
128 args.fOutputColor);
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 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400152 b->add32((int32_t)type);
153 b->add32((int32_t)isRadiusIncreasing);
154 b->add32((int32_t)isFocalOnCircle);
155 b->add32((int32_t)isWellBehaved);
156 b->add32((int32_t)isSwapped);
157 b->add32((int32_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 {
185 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
186}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400187GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
188#if GR_TEST_UTILS
189std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
190 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500191 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400192 SkScalar offset = scale / 32.0f;
193
Mike Kleind6ab77a2019-03-21 08:18:24 -0500194 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400195 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500196 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400197 d->fRandom->nextRangeScalar(0.0f, scale)};
198 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
199 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400200
201 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
202 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
203 // We won't treat isWellDefined and isRadiusIncreasing specially because they
204 // should have high probability to be turned on and off as we're getting random
205 // radii and centers.
206
207 int mask = d->fRandom->nextU();
208 int type = mask & kTestTypeMask;
209 if (type == static_cast<int>(Type::kRadial)) {
210 center2 = center1;
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 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500216 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400217 radius2 = radius1;
218 // Make sure that the centers are different
219 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400220 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400221 }
222 } else { // kFocal_Type
223 // Make sure that the centers are different
224 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400225 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400226 }
227
228 if (kTestNativelyFocalBit & mask) {
229 radius1 = 0;
230 }
231 if (kTestFocalOnCircleBit & mask) {
232 radius2 = radius1 + SkPoint::Distance(center1, center2);
233 }
234 if (kTestSwappedBit & mask) {
235 std::swap(radius1, radius2);
236 radius2 = 0;
237 }
238
239 // Make sure that the radii are different
240 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400241 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400242 }
243 }
244
245 if (SkScalarNearlyZero(radius1 - radius2) &&
246 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400247 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400248 }
249
250 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500251 auto shader = params.fUseColors4f
252 ? SkGradientShader::MakeTwoPointConical(
253 center1, radius1, center2, radius2, params.fColors4f,
254 params.fColorSpace, params.fStops, params.fColorCount,
255 params.fTileMode)
256 : SkGradientShader::MakeTwoPointConical(
257 center1, radius1, center2, radius2, params.fColors,
258 params.fStops, params.fColorCount, params.fTileMode);
259 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400260 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
261
Brian Salomon4dea72a2019-12-18 10:43:10 -0500262 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400263 return fp;
264}
265#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400266
267// .fp files do not let you reference outside enum definitions, so we have to explicitly map
268// between the two compatible enum defs
269GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
270 switch (type) {
271 case SkTwoPointConicalGradient::Type::kRadial:
272 return GrTwoPointConicalGradientLayout::Type::kRadial;
273 case SkTwoPointConicalGradient::Type::kStrip:
274 return GrTwoPointConicalGradientLayout::Type::kStrip;
275 case SkTwoPointConicalGradient::Type::kFocal:
276 return GrTwoPointConicalGradientLayout::Type::kFocal;
277 }
278 SkDEBUGFAIL("Should not be reachable");
279 return GrTwoPointConicalGradientLayout::Type::kRadial;
280}
281
282std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500283 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400284 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
285
286 // The focalData struct is only valid if isFocal is true
287 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500288 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400289
290 // Calculate optimization switches from gradient specification
291 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500292 bool isWellBehaved = isFocal && focalData.isWellBehaved();
293 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400294 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
295
296 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
297 // However, all types start with the total inverse local matrix calculated from the shader
298 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500299 bool isRadiusIncreasing;
300 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400301 SkMatrix matrix;
302
303 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400304 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400305 return nullptr;
306 }
307
308 if (isFocal) {
309 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
310
311 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
312
313 matrix.postConcat(grad.getGradientMatrix());
314 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500315 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400316 isRadiusIncreasing = dr >= 0;
317
318 SkScalar r0 = grad.getStartRadius() / dr;
319 focalParams.set(r0, r0 * r0);
320
321 // GPU radial matrix is different from the original matrix, since we map the diff radius
322 // to have |dr| = 1, so manually compute the final gradient matrix here.
323
324 // Map center to (0, 0)
325 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
326
327 // scale |diffRadius| to 1
328 matrix.postScale(1 / dr, 1 / dr);
329 } else { // kStrip
330 isRadiusIncreasing = false; // kStrip doesn't use this flag
331
332 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
333 focalParams.set(r0, r0 * r0);
334
335 matrix.postConcat(grad.getGradientMatrix());
336 }
337
Michael Ludwig1b7399b2020-06-26 09:56:48 -0400338 return GrMatrixEffect::Make(
339 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
340 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
341 isNativelyFocal, focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400342}