blob: 96e29e155a915945b2350a8ef2b2fbf67be5e66e [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(
John Stiles50819422020-06-18 13:00:38 -040044 R"SkSL(float2 p = %s;
45float t = -1.0;
46half v = 1.0;
47@switch (%d) {
48 case 1:
49 {
50 half r0_2 = %s.y;
51 t = float(r0_2) - p.y * p.y;
52 if (t >= 0.0) {
53 t = p.x + sqrt(t);
54 } else {
55 v = -1.0;
56 }
57 }
58 break;
59 case 0:
60 {
61 half r0 = %s.x;
62 @if (%s) {
63 t = length(p) - float(r0);
64 } else {
65 t = -length(p) - float(r0);
66 }
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) {
75 x_t = dot(p, p) / p.x;
76 } else if (%s) {
77 x_t = length(p) - p.x * float(invR1);
78 } else {
79 float temp = p.x * p.x - p.y * p.y;
80 if (temp >= 0.0) {
81 @if (%s || !%s) {
82 x_t = -sqrt(temp) - p.x * float(invR1);
83 } else {
84 x_t = sqrt(temp) - p.x * float(invR1);
85 }
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 Ludwige88320b2020-06-24 09:04:56 -0400114 args.fSampleCoord, (int)_outer.type,
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500115 args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400116 args.fUniformHandler->getUniformCStr(focalParamsVar),
John Stiles50819422020-06-18 13:00:38 -0400117 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400118 args.fUniformHandler->getUniformCStr(focalParamsVar),
119 args.fUniformHandler->getUniformCStr(focalParamsVar),
120 (_outer.isFocalOnCircle ? "true" : "false"),
121 (_outer.isWellBehaved ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400122 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400123 (_outer.isWellBehaved ? "true" : "false"),
124 (_outer.isRadiusIncreasing ? "true" : "false"),
125 (_outer.isNativelyFocal ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400126 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
127 args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -0400128 }
129
130private:
131 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500132 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -0400133 const GrTwoPointConicalGradientLayout& _outer =
134 _proc.cast<GrTwoPointConicalGradientLayout>();
135 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400136 const SkPoint& focalParamsValue = _outer.focalParams;
137 if (focalParamsPrev != focalParamsValue) {
138 focalParamsPrev = focalParamsValue;
139 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400140 }
141 }
142 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400143 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
144 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400145};
146GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
147 return new GrGLSLTwoPointConicalGradientLayout();
148}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500149void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400150 GrProcessorKeyBuilder* b) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400151 b->add32((int32_t)type);
152 b->add32((int32_t)isRadiusIncreasing);
153 b->add32((int32_t)isFocalOnCircle);
154 b->add32((int32_t)isWellBehaved);
155 b->add32((int32_t)isSwapped);
156 b->add32((int32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400157}
158bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
159 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
160 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400161 if (type != that.type) return false;
162 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
163 if (isFocalOnCircle != that.isFocalOnCircle) return false;
164 if (isWellBehaved != that.isWellBehaved) return false;
165 if (isSwapped != that.isSwapped) return false;
166 if (isNativelyFocal != that.isNativelyFocal) return false;
167 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400168 return true;
169}
170GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
171 const GrTwoPointConicalGradientLayout& src)
172 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400173 , type(src.type)
174 , isRadiusIncreasing(src.isRadiusIncreasing)
175 , isFocalOnCircle(src.isFocalOnCircle)
176 , isWellBehaved(src.isWellBehaved)
177 , isSwapped(src.isSwapped)
178 , isNativelyFocal(src.isNativelyFocal)
179 , focalParams(src.focalParams) {
Michael Ludwige88320b2020-06-24 09:04:56 -0400180 this->setUsesSampleCoordsDirectly();
Michael Ludwig8f685082018-09-12 15:23:01 -0400181}
182std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
183 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
184}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400185GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
186#if GR_TEST_UTILS
187std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
188 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500189 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400190 SkScalar offset = scale / 32.0f;
191
Mike Kleind6ab77a2019-03-21 08:18:24 -0500192 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400193 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500194 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400195 d->fRandom->nextRangeScalar(0.0f, scale)};
196 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
197 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400198
199 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
200 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
201 // We won't treat isWellDefined and isRadiusIncreasing specially because they
202 // should have high probability to be turned on and off as we're getting random
203 // radii and centers.
204
205 int mask = d->fRandom->nextU();
206 int type = mask & kTestTypeMask;
207 if (type == static_cast<int>(Type::kRadial)) {
208 center2 = center1;
209 // Make sure that the radii are different
210 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400211 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400212 }
213 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500214 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400215 radius2 = radius1;
216 // Make sure that the centers are different
217 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400218 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400219 }
220 } else { // kFocal_Type
221 // Make sure that the centers are different
222 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400223 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400224 }
225
226 if (kTestNativelyFocalBit & mask) {
227 radius1 = 0;
228 }
229 if (kTestFocalOnCircleBit & mask) {
230 radius2 = radius1 + SkPoint::Distance(center1, center2);
231 }
232 if (kTestSwappedBit & mask) {
233 std::swap(radius1, radius2);
234 radius2 = 0;
235 }
236
237 // Make sure that the radii are different
238 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400239 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400240 }
241 }
242
243 if (SkScalarNearlyZero(radius1 - radius2) &&
244 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400245 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400246 }
247
248 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500249 auto shader = params.fUseColors4f
250 ? SkGradientShader::MakeTwoPointConical(
251 center1, radius1, center2, radius2, params.fColors4f,
252 params.fColorSpace, params.fStops, params.fColorCount,
253 params.fTileMode)
254 : SkGradientShader::MakeTwoPointConical(
255 center1, radius1, center2, radius2, params.fColors,
256 params.fStops, params.fColorCount, params.fTileMode);
257 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400258 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
259
Brian Salomon4dea72a2019-12-18 10:43:10 -0500260 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400261 return fp;
262}
263#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400264
265// .fp files do not let you reference outside enum definitions, so we have to explicitly map
266// between the two compatible enum defs
267GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
268 switch (type) {
269 case SkTwoPointConicalGradient::Type::kRadial:
270 return GrTwoPointConicalGradientLayout::Type::kRadial;
271 case SkTwoPointConicalGradient::Type::kStrip:
272 return GrTwoPointConicalGradientLayout::Type::kStrip;
273 case SkTwoPointConicalGradient::Type::kFocal:
274 return GrTwoPointConicalGradientLayout::Type::kFocal;
275 }
276 SkDEBUGFAIL("Should not be reachable");
277 return GrTwoPointConicalGradientLayout::Type::kRadial;
278}
279
280std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500281 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400282 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
283
284 // The focalData struct is only valid if isFocal is true
285 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500286 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400287
288 // Calculate optimization switches from gradient specification
289 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500290 bool isWellBehaved = isFocal && focalData.isWellBehaved();
291 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400292 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
293
294 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
295 // However, all types start with the total inverse local matrix calculated from the shader
296 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500297 bool isRadiusIncreasing;
298 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400299 SkMatrix matrix;
300
301 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400302 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400303 return nullptr;
304 }
305
306 if (isFocal) {
307 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
308
309 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
310
311 matrix.postConcat(grad.getGradientMatrix());
312 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500313 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400314 isRadiusIncreasing = dr >= 0;
315
316 SkScalar r0 = grad.getStartRadius() / dr;
317 focalParams.set(r0, r0 * r0);
318
319 // GPU radial matrix is different from the original matrix, since we map the diff radius
320 // to have |dr| = 1, so manually compute the final gradient matrix here.
321
322 // Map center to (0, 0)
323 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
324
325 // scale |diffRadius| to 1
326 matrix.postScale(1 / dr, 1 / dr);
327 } else { // kStrip
328 isRadiusIncreasing = false; // kStrip doesn't use this flag
329
330 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
331 focalParams.set(r0, r0 * r0);
332
333 matrix.postConcat(grad.getGradientMatrix());
334 }
335
Michael Ludwig1b7399b2020-06-26 09:56:48 -0400336 return GrMatrixEffect::Make(
337 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
338 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
339 isNativelyFocal, focalParams)));
Michael Ludwig8f685082018-09-12 15:23:01 -0400340}