blob: 758af5a9c6969b4f2edb0af65e1574209633c99b [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}
112%s = half4(half(t), v, 0.0, 0.0);
113)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 Stiles50819422020-06-18 13:00:38 -0400128 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
129 args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -0400130 }
131
132private:
133 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500134 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -0400135 const GrTwoPointConicalGradientLayout& _outer =
136 _proc.cast<GrTwoPointConicalGradientLayout>();
137 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400138 const SkPoint& focalParamsValue = _outer.focalParams;
139 if (focalParamsPrev != focalParamsValue) {
140 focalParamsPrev = focalParamsValue;
141 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400142 }
143 }
144 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400145 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
146 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400147};
148GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
149 return new GrGLSLTwoPointConicalGradientLayout();
150}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500151void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400152 GrProcessorKeyBuilder* b) const {
John Stiles45f5b032020-07-27 17:31:29 -0400153 b->add32((uint32_t)type);
154 b->add32((uint32_t)isRadiusIncreasing);
155 b->add32((uint32_t)isFocalOnCircle);
156 b->add32((uint32_t)isWellBehaved);
157 b->add32((uint32_t)isSwapped);
158 b->add32((uint32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400159}
160bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
161 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
162 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400163 if (type != that.type) return false;
164 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
165 if (isFocalOnCircle != that.isFocalOnCircle) return false;
166 if (isWellBehaved != that.isWellBehaved) return false;
167 if (isSwapped != that.isSwapped) return false;
168 if (isNativelyFocal != that.isNativelyFocal) return false;
169 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400170 return true;
171}
172GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
173 const GrTwoPointConicalGradientLayout& src)
174 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400175 , type(src.type)
176 , isRadiusIncreasing(src.isRadiusIncreasing)
177 , isFocalOnCircle(src.isFocalOnCircle)
178 , isWellBehaved(src.isWellBehaved)
179 , isSwapped(src.isSwapped)
180 , isNativelyFocal(src.isNativelyFocal)
181 , focalParams(src.focalParams) {
Brian Osman12c5d292020-07-13 16:11:35 -0400182 this->cloneAndRegisterAllChildProcessors(src);
Michael Ludwige88320b2020-06-24 09:04:56 -0400183 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400184}
185std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
186 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
187}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400188GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
189#if GR_TEST_UTILS
190std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
191 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500192 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400193 SkScalar offset = scale / 32.0f;
194
Mike Kleind6ab77a2019-03-21 08:18:24 -0500195 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400196 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500197 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400198 d->fRandom->nextRangeScalar(0.0f, scale)};
199 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
200 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400201
202 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
203 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
204 // We won't treat isWellDefined and isRadiusIncreasing specially because they
205 // should have high probability to be turned on and off as we're getting random
206 // radii and centers.
207
208 int mask = d->fRandom->nextU();
209 int type = mask & kTestTypeMask;
210 if (type == static_cast<int>(Type::kRadial)) {
211 center2 = center1;
212 // Make sure that the radii are different
213 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400214 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400215 }
216 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500217 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400218 radius2 = radius1;
219 // Make sure that the centers are different
220 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400221 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400222 }
223 } else { // kFocal_Type
224 // Make sure that the centers are different
225 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400226 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400227 }
228
229 if (kTestNativelyFocalBit & mask) {
230 radius1 = 0;
231 }
232 if (kTestFocalOnCircleBit & mask) {
233 radius2 = radius1 + SkPoint::Distance(center1, center2);
234 }
235 if (kTestSwappedBit & mask) {
236 std::swap(radius1, radius2);
237 radius2 = 0;
238 }
239
240 // Make sure that the radii are different
241 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400242 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400243 }
244 }
245
246 if (SkScalarNearlyZero(radius1 - radius2) &&
247 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400248 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400249 }
250
251 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500252 auto shader = params.fUseColors4f
253 ? SkGradientShader::MakeTwoPointConical(
254 center1, radius1, center2, radius2, params.fColors4f,
255 params.fColorSpace, params.fStops, params.fColorCount,
256 params.fTileMode)
257 : SkGradientShader::MakeTwoPointConical(
258 center1, radius1, center2, radius2, params.fColors,
259 params.fStops, params.fColorCount, params.fTileMode);
260 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400261 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
262
Brian Salomon4dea72a2019-12-18 10:43:10 -0500263 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400264 return fp;
265}
266#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400267
268// .fp files do not let you reference outside enum definitions, so we have to explicitly map
269// between the two compatible enum defs
270GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
271 switch (type) {
272 case SkTwoPointConicalGradient::Type::kRadial:
273 return GrTwoPointConicalGradientLayout::Type::kRadial;
274 case SkTwoPointConicalGradient::Type::kStrip:
275 return GrTwoPointConicalGradientLayout::Type::kStrip;
276 case SkTwoPointConicalGradient::Type::kFocal:
277 return GrTwoPointConicalGradientLayout::Type::kFocal;
278 }
279 SkDEBUGFAIL("Should not be reachable");
280 return GrTwoPointConicalGradientLayout::Type::kRadial;
281}
282
283std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500284 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400285 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
286
287 // The focalData struct is only valid if isFocal is true
288 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500289 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400290
291 // Calculate optimization switches from gradient specification
292 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500293 bool isWellBehaved = isFocal && focalData.isWellBehaved();
294 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400295 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
296
297 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
298 // However, all types start with the total inverse local matrix calculated from the shader
299 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500300 bool isRadiusIncreasing;
301 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400302 SkMatrix matrix;
303
304 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400305 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400306 return nullptr;
307 }
308
309 if (isFocal) {
310 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
311
312 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
313
314 matrix.postConcat(grad.getGradientMatrix());
315 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500316 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400317 isRadiusIncreasing = dr >= 0;
318
319 SkScalar r0 = grad.getStartRadius() / dr;
320 focalParams.set(r0, r0 * r0);
321
322 // GPU radial matrix is different from the original matrix, since we map the diff radius
323 // to have |dr| = 1, so manually compute the final gradient matrix here.
324
325 // Map center to (0, 0)
326 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
327
328 // scale |diffRadius| to 1
329 matrix.postScale(1 / dr, 1 / dr);
330 } else { // kStrip
331 isRadiusIncreasing = false; // kStrip doesn't use this flag
332
333 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
334 focalParams.set(r0, r0 * r0);
335
336 matrix.postConcat(grad.getGradientMatrix());
337 }
338
Michael Ludwig1b7399b2020-06-26 09:56:48 -0400339 return GrMatrixEffect::Make(
340 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
341 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
342 isNativelyFocal, focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400343}