blob: 2dda47f60256633302b24ccbe55925478d08bfc6 [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) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400181 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400182}
183std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
184 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
185}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400186GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
187#if GR_TEST_UTILS
188std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
189 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500190 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400191 SkScalar offset = scale / 32.0f;
192
Mike Kleind6ab77a2019-03-21 08:18:24 -0500193 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400194 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500195 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400196 d->fRandom->nextRangeScalar(0.0f, scale)};
197 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
198 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400199
200 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
201 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
202 // We won't treat isWellDefined and isRadiusIncreasing specially because they
203 // should have high probability to be turned on and off as we're getting random
204 // radii and centers.
205
206 int mask = d->fRandom->nextU();
207 int type = mask & kTestTypeMask;
208 if (type == static_cast<int>(Type::kRadial)) {
209 center2 = center1;
210 // Make sure that the radii are different
211 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400212 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400213 }
214 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500215 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400216 radius2 = radius1;
217 // Make sure that the centers are different
218 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400219 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400220 }
221 } else { // kFocal_Type
222 // Make sure that the centers are different
223 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400224 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400225 }
226
227 if (kTestNativelyFocalBit & mask) {
228 radius1 = 0;
229 }
230 if (kTestFocalOnCircleBit & mask) {
231 radius2 = radius1 + SkPoint::Distance(center1, center2);
232 }
233 if (kTestSwappedBit & mask) {
234 std::swap(radius1, radius2);
235 radius2 = 0;
236 }
237
238 // Make sure that the radii are different
239 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400240 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400241 }
242 }
243
244 if (SkScalarNearlyZero(radius1 - radius2) &&
245 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400246 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400247 }
248
249 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500250 auto shader = params.fUseColors4f
251 ? SkGradientShader::MakeTwoPointConical(
252 center1, radius1, center2, radius2, params.fColors4f,
253 params.fColorSpace, params.fStops, params.fColorCount,
254 params.fTileMode)
255 : SkGradientShader::MakeTwoPointConical(
256 center1, radius1, center2, radius2, params.fColors,
257 params.fStops, params.fColorCount, params.fTileMode);
258 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400259 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
260
Brian Salomon4dea72a2019-12-18 10:43:10 -0500261 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400262 return fp;
263}
264#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400265
266// .fp files do not let you reference outside enum definitions, so we have to explicitly map
267// between the two compatible enum defs
268GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
269 switch (type) {
270 case SkTwoPointConicalGradient::Type::kRadial:
271 return GrTwoPointConicalGradientLayout::Type::kRadial;
272 case SkTwoPointConicalGradient::Type::kStrip:
273 return GrTwoPointConicalGradientLayout::Type::kStrip;
274 case SkTwoPointConicalGradient::Type::kFocal:
275 return GrTwoPointConicalGradientLayout::Type::kFocal;
276 }
277 SkDEBUGFAIL("Should not be reachable");
278 return GrTwoPointConicalGradientLayout::Type::kRadial;
279}
280
281std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500282 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400283 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
284
285 // The focalData struct is only valid if isFocal is true
286 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500287 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400288
289 // Calculate optimization switches from gradient specification
290 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500291 bool isWellBehaved = isFocal && focalData.isWellBehaved();
292 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400293 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
294
295 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
296 // However, all types start with the total inverse local matrix calculated from the shader
297 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500298 bool isRadiusIncreasing;
299 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400300 SkMatrix matrix;
301
302 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400303 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400304 return nullptr;
305 }
306
307 if (isFocal) {
308 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
309
310 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
311
312 matrix.postConcat(grad.getGradientMatrix());
313 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500314 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400315 isRadiusIncreasing = dr >= 0;
316
317 SkScalar r0 = grad.getStartRadius() / dr;
318 focalParams.set(r0, r0 * r0);
319
320 // GPU radial matrix is different from the original matrix, since we map the diff radius
321 // to have |dr| = 1, so manually compute the final gradient matrix here.
322
323 // Map center to (0, 0)
324 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
325
326 // scale |diffRadius| to 1
327 matrix.postScale(1 / dr, 1 / dr);
328 } else { // kStrip
329 isRadiusIncreasing = false; // kStrip doesn't use this flag
330
331 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
332 focalParams.set(r0, r0 * r0);
333
334 matrix.postConcat(grad.getGradientMatrix());
335 }
336
Michael Ludwig1b7399b2020-06-26 09:56:48 -0400337 return GrMatrixEffect::Make(
338 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
339 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
340 isNativelyFocal, focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400341}