blob: adde160e1456ad3a548ca94deb709a448bfb2867 [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 gradientMatrix = _outer.gradientMatrix;
Michael Ludwig8f685082018-09-12 15:23:01 -040028 (void)gradientMatrix;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040029 auto type = _outer.type;
Michael Ludwig8f685082018-09-12 15:23:01 -040030 (void)type;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040031 auto isRadiusIncreasing = _outer.isRadiusIncreasing;
Michael Ludwig8f685082018-09-12 15:23:01 -040032 (void)isRadiusIncreasing;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040033 auto isFocalOnCircle = _outer.isFocalOnCircle;
Michael Ludwig8f685082018-09-12 15:23:01 -040034 (void)isFocalOnCircle;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040035 auto isWellBehaved = _outer.isWellBehaved;
Michael Ludwig8f685082018-09-12 15:23:01 -040036 (void)isWellBehaved;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040037 auto isSwapped = _outer.isSwapped;
Michael Ludwig8f685082018-09-12 15:23:01 -040038 (void)isSwapped;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040039 auto isNativelyFocal = _outer.isNativelyFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -040040 (void)isNativelyFocal;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040041 auto focalParams = _outer.focalParams;
Michael Ludwig8f685082018-09-12 15:23:01 -040042 (void)focalParams;
Ethan Nicholas16464c32020-04-06 13:53:05 -040043 focalParamsVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
44 kHalf2_GrSLType, "focalParams");
Michael Ludwig8f685082018-09-12 15:23:01 -040045 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040046 R"SkSL(float2 p = %s;
47float t = -1.0;
48half v = 1.0;
49@switch (%d) {
50 case 1:
51 {
52 half r0_2 = %s.y;
53 t = float(r0_2) - p.y * p.y;
54 if (t >= 0.0) {
55 t = p.x + sqrt(t);
56 } else {
57 v = -1.0;
58 }
59 }
60 break;
61 case 0:
62 {
63 half r0 = %s.x;
64 @if (%s) {
65 t = length(p) - float(r0);
66 } else {
67 t = -length(p) - float(r0);
68 }
69 }
70 break;
71 case 2:
72 {
73 half invR1 = %s.x;
74 half fx = %s.y;
75 float x_t = -1.0;
76 @if (%s) {
77 x_t = dot(p, p) / p.x;
78 } else if (%s) {
79 x_t = length(p) - p.x * float(invR1);
80 } else {
81 float temp = p.x * p.x - p.y * p.y;
82 if (temp >= 0.0) {
83 @if (%s || !%s) {
84 x_t = -sqrt(temp) - p.x * float(invR1);
85 } else {
86 x_t = sqrt(temp) - p.x * float(invR1);
87 }
88 }
89 }
90 @if (!%s) {
91 if (x_t <= 0.0) {
92 v = -1.0;
93 }
94 }
95 @if (%s) {
96 @if (%s) {
97 t = x_t;
98 } else {
99 t = x_t + float(fx);
100 }
101 } else {
102 @if (%s) {
103 t = -x_t;
104 } else {
105 t = -x_t + float(fx);
106 }
107 }
108 @if (%s) {
109 t = 1.0 - t;
110 }
111 }
112 break;
113}
114%s = half4(half(t), v, 0.0, 0.0);
115)SkSL",
Michael Ludwige88320b2020-06-24 09:04:56 -0400116 args.fSampleCoord, (int)_outer.type,
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500117 args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400118 args.fUniformHandler->getUniformCStr(focalParamsVar),
John Stiles50819422020-06-18 13:00:38 -0400119 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400120 args.fUniformHandler->getUniformCStr(focalParamsVar),
121 args.fUniformHandler->getUniformCStr(focalParamsVar),
122 (_outer.isFocalOnCircle ? "true" : "false"),
123 (_outer.isWellBehaved ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400124 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400125 (_outer.isWellBehaved ? "true" : "false"),
126 (_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 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400153 b->add32((int32_t)type);
154 b->add32((int32_t)isRadiusIncreasing);
155 b->add32((int32_t)isFocalOnCircle);
156 b->add32((int32_t)isWellBehaved);
157 b->add32((int32_t)isSwapped);
158 b->add32((int32_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 (gradientMatrix != that.gradientMatrix) return false;
164 if (type != that.type) return false;
165 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
166 if (isFocalOnCircle != that.isFocalOnCircle) return false;
167 if (isWellBehaved != that.isWellBehaved) return false;
168 if (isSwapped != that.isSwapped) return false;
169 if (isNativelyFocal != that.isNativelyFocal) return false;
170 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400171 return true;
172}
173GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
174 const GrTwoPointConicalGradientLayout& src)
175 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400176 , fCoordTransform0(src.fCoordTransform0)
177 , gradientMatrix(src.gradientMatrix)
178 , type(src.type)
179 , isRadiusIncreasing(src.isRadiusIncreasing)
180 , isFocalOnCircle(src.isFocalOnCircle)
181 , isWellBehaved(src.isWellBehaved)
182 , isSwapped(src.isSwapped)
183 , isNativelyFocal(src.isNativelyFocal)
184 , focalParams(src.focalParams) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400185 this->addCoordTransform(&fCoordTransform0);
Michael Ludwige88320b2020-06-24 09:04:56 -0400186 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400187}
188std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
189 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
190}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400191GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
192#if GR_TEST_UTILS
193std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
194 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500195 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400196 SkScalar offset = scale / 32.0f;
197
Mike Kleind6ab77a2019-03-21 08:18:24 -0500198 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400199 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500200 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400201 d->fRandom->nextRangeScalar(0.0f, scale)};
202 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
203 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400204
205 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
206 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
207 // We won't treat isWellDefined and isRadiusIncreasing specially because they
208 // should have high probability to be turned on and off as we're getting random
209 // radii and centers.
210
211 int mask = d->fRandom->nextU();
212 int type = mask & kTestTypeMask;
213 if (type == static_cast<int>(Type::kRadial)) {
214 center2 = center1;
215 // Make sure that the radii are different
216 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400217 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400218 }
219 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500220 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400221 radius2 = radius1;
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 } else { // kFocal_Type
227 // Make sure that the centers are different
228 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400229 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400230 }
231
232 if (kTestNativelyFocalBit & mask) {
233 radius1 = 0;
234 }
235 if (kTestFocalOnCircleBit & mask) {
236 radius2 = radius1 + SkPoint::Distance(center1, center2);
237 }
238 if (kTestSwappedBit & mask) {
239 std::swap(radius1, radius2);
240 radius2 = 0;
241 }
242
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 }
248
249 if (SkScalarNearlyZero(radius1 - radius2) &&
250 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400251 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400252 }
253
254 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500255 auto shader = params.fUseColors4f
256 ? SkGradientShader::MakeTwoPointConical(
257 center1, radius1, center2, radius2, params.fColors4f,
258 params.fColorSpace, params.fStops, params.fColorCount,
259 params.fTileMode)
260 : SkGradientShader::MakeTwoPointConical(
261 center1, radius1, center2, radius2, params.fColors,
262 params.fStops, params.fColorCount, params.fTileMode);
263 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400264 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
265
Brian Salomon4dea72a2019-12-18 10:43:10 -0500266 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400267 return fp;
268}
269#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400270
271// .fp files do not let you reference outside enum definitions, so we have to explicitly map
272// between the two compatible enum defs
273GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
274 switch (type) {
275 case SkTwoPointConicalGradient::Type::kRadial:
276 return GrTwoPointConicalGradientLayout::Type::kRadial;
277 case SkTwoPointConicalGradient::Type::kStrip:
278 return GrTwoPointConicalGradientLayout::Type::kStrip;
279 case SkTwoPointConicalGradient::Type::kFocal:
280 return GrTwoPointConicalGradientLayout::Type::kFocal;
281 }
282 SkDEBUGFAIL("Should not be reachable");
283 return GrTwoPointConicalGradientLayout::Type::kRadial;
284}
285
286std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500287 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400288 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
289
290 // The focalData struct is only valid if isFocal is true
291 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500292 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400293
294 // Calculate optimization switches from gradient specification
295 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500296 bool isWellBehaved = isFocal && focalData.isWellBehaved();
297 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400298 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
299
300 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
301 // However, all types start with the total inverse local matrix calculated from the shader
302 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500303 bool isRadiusIncreasing;
304 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400305 SkMatrix matrix;
306
307 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400308 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400309 return nullptr;
310 }
311
312 if (isFocal) {
313 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
314
315 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
316
317 matrix.postConcat(grad.getGradientMatrix());
318 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500319 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400320 isRadiusIncreasing = dr >= 0;
321
322 SkScalar r0 = grad.getStartRadius() / dr;
323 focalParams.set(r0, r0 * r0);
324
325 // GPU radial matrix is different from the original matrix, since we map the diff radius
326 // to have |dr| = 1, so manually compute the final gradient matrix here.
327
328 // Map center to (0, 0)
329 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
330
331 // scale |diffRadius| to 1
332 matrix.postScale(1 / dr, 1 / dr);
333 } else { // kStrip
334 isRadiusIncreasing = false; // kStrip doesn't use this flag
335
336 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
337 focalParams.set(r0, r0 * r0);
338
339 matrix.postConcat(grad.getGradientMatrix());
340 }
341
Mike Kleind6ab77a2019-03-21 08:18:24 -0500342 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
343 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
344 isNativelyFocal, focalParams));
Michael Ludwig8f685082018-09-12 15:23:01 -0400345}