blob: fa172c76c295f7f7dc6a4651aa5204e13c4ef06e [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");
Ethan Nicholas58430122020-04-14 09:54:02 -040045 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(
46 args.fTransformedCoords[0].fVaryingPoint, _outer.sampleMatrix());
Michael Ludwig8f685082018-09-12 15:23:01 -040047 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040048 R"SkSL(float2 p = %s;
49float t = -1.0;
50half v = 1.0;
51@switch (%d) {
52 case 1:
53 {
54 half r0_2 = %s.y;
55 t = float(r0_2) - p.y * p.y;
56 if (t >= 0.0) {
57 t = p.x + sqrt(t);
58 } else {
59 v = -1.0;
60 }
61 }
62 break;
63 case 0:
64 {
65 half r0 = %s.x;
66 @if (%s) {
67 t = length(p) - float(r0);
68 } else {
69 t = -length(p) - float(r0);
70 }
71 }
72 break;
73 case 2:
74 {
75 half invR1 = %s.x;
76 half fx = %s.y;
77 float x_t = -1.0;
78 @if (%s) {
79 x_t = dot(p, p) / p.x;
80 } else if (%s) {
81 x_t = length(p) - p.x * float(invR1);
82 } else {
83 float temp = p.x * p.x - p.y * p.y;
84 if (temp >= 0.0) {
85 @if (%s || !%s) {
86 x_t = -sqrt(temp) - p.x * float(invR1);
87 } else {
88 x_t = sqrt(temp) - p.x * float(invR1);
89 }
90 }
91 }
92 @if (!%s) {
93 if (x_t <= 0.0) {
94 v = -1.0;
95 }
96 }
97 @if (%s) {
98 @if (%s) {
99 t = x_t;
100 } else {
101 t = x_t + float(fx);
102 }
103 } else {
104 @if (%s) {
105 t = -x_t;
106 } else {
107 t = -x_t + float(fx);
108 }
109 }
110 @if (%s) {
111 t = 1.0 - t;
112 }
113 }
114 break;
115}
116%s = half4(half(t), v, 0.0, 0.0);
117)SkSL",
Brian Salomonbf5c0c02019-11-11 14:55:28 -0500118 sk_TransformedCoords2D_0.c_str(), (int)_outer.type,
119 args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400120 args.fUniformHandler->getUniformCStr(focalParamsVar),
John Stiles50819422020-06-18 13:00:38 -0400121 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400122 args.fUniformHandler->getUniformCStr(focalParamsVar),
123 args.fUniformHandler->getUniformCStr(focalParamsVar),
124 (_outer.isFocalOnCircle ? "true" : "false"),
125 (_outer.isWellBehaved ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400126 (_outer.isRadiusIncreasing ? "true" : "false"),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400127 (_outer.isWellBehaved ? "true" : "false"),
128 (_outer.isRadiusIncreasing ? "true" : "false"),
129 (_outer.isNativelyFocal ? "true" : "false"),
John Stiles50819422020-06-18 13:00:38 -0400130 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
131 args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -0400132 }
133
134private:
135 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500136 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -0400137 const GrTwoPointConicalGradientLayout& _outer =
138 _proc.cast<GrTwoPointConicalGradientLayout>();
139 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400140 const SkPoint& focalParamsValue = _outer.focalParams;
141 if (focalParamsPrev != focalParamsValue) {
142 focalParamsPrev = focalParamsValue;
143 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400144 }
145 }
146 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400147 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
148 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400149};
150GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
151 return new GrGLSLTwoPointConicalGradientLayout();
152}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500153void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400154 GrProcessorKeyBuilder* b) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400155 b->add32((int32_t)type);
156 b->add32((int32_t)isRadiusIncreasing);
157 b->add32((int32_t)isFocalOnCircle);
158 b->add32((int32_t)isWellBehaved);
159 b->add32((int32_t)isSwapped);
160 b->add32((int32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400161}
162bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
163 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
164 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400165 if (gradientMatrix != that.gradientMatrix) return false;
166 if (type != that.type) return false;
167 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
168 if (isFocalOnCircle != that.isFocalOnCircle) return false;
169 if (isWellBehaved != that.isWellBehaved) return false;
170 if (isSwapped != that.isSwapped) return false;
171 if (isNativelyFocal != that.isNativelyFocal) return false;
172 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400173 return true;
174}
175GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
176 const GrTwoPointConicalGradientLayout& src)
177 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400178 , fCoordTransform0(src.fCoordTransform0)
179 , gradientMatrix(src.gradientMatrix)
180 , type(src.type)
181 , isRadiusIncreasing(src.isRadiusIncreasing)
182 , isFocalOnCircle(src.isFocalOnCircle)
183 , isWellBehaved(src.isWellBehaved)
184 , isSwapped(src.isSwapped)
185 , isNativelyFocal(src.isNativelyFocal)
186 , focalParams(src.focalParams) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400187 this->addCoordTransform(&fCoordTransform0);
188}
189std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
190 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
191}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400192GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
193#if GR_TEST_UTILS
194std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
195 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500196 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400197 SkScalar offset = scale / 32.0f;
198
Mike Kleind6ab77a2019-03-21 08:18:24 -0500199 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400200 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500201 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400202 d->fRandom->nextRangeScalar(0.0f, scale)};
203 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
204 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400205
206 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
207 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
208 // We won't treat isWellDefined and isRadiusIncreasing specially because they
209 // should have high probability to be turned on and off as we're getting random
210 // radii and centers.
211
212 int mask = d->fRandom->nextU();
213 int type = mask & kTestTypeMask;
214 if (type == static_cast<int>(Type::kRadial)) {
215 center2 = center1;
216 // Make sure that the radii are different
217 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400218 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400219 }
220 } else if (type == static_cast<int>(Type::kStrip)) {
Brian Osman788b9162020-02-07 10:36:46 -0500221 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400222 radius2 = radius1;
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 } else { // kFocal_Type
228 // Make sure that the centers are different
229 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400230 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400231 }
232
233 if (kTestNativelyFocalBit & mask) {
234 radius1 = 0;
235 }
236 if (kTestFocalOnCircleBit & mask) {
237 radius2 = radius1 + SkPoint::Distance(center1, center2);
238 }
239 if (kTestSwappedBit & mask) {
240 std::swap(radius1, radius2);
241 radius2 = 0;
242 }
243
244 // Make sure that the radii are different
245 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400246 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400247 }
248 }
249
250 if (SkScalarNearlyZero(radius1 - radius2) &&
251 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400252 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400253 }
254
255 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500256 auto shader = params.fUseColors4f
257 ? SkGradientShader::MakeTwoPointConical(
258 center1, radius1, center2, radius2, params.fColors4f,
259 params.fColorSpace, params.fStops, params.fColorCount,
260 params.fTileMode)
261 : SkGradientShader::MakeTwoPointConical(
262 center1, radius1, center2, radius2, params.fColors,
263 params.fStops, params.fColorCount, params.fTileMode);
264 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400265 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
266
Brian Salomon4dea72a2019-12-18 10:43:10 -0500267 SkASSERT_RELEASE(fp);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400268 return fp;
269}
270#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400271
272// .fp files do not let you reference outside enum definitions, so we have to explicitly map
273// between the two compatible enum defs
274GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
275 switch (type) {
276 case SkTwoPointConicalGradient::Type::kRadial:
277 return GrTwoPointConicalGradientLayout::Type::kRadial;
278 case SkTwoPointConicalGradient::Type::kStrip:
279 return GrTwoPointConicalGradientLayout::Type::kStrip;
280 case SkTwoPointConicalGradient::Type::kFocal:
281 return GrTwoPointConicalGradientLayout::Type::kFocal;
282 }
283 SkDEBUGFAIL("Should not be reachable");
284 return GrTwoPointConicalGradientLayout::Type::kRadial;
285}
286
287std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500288 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400289 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
290
291 // The focalData struct is only valid if isFocal is true
292 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500293 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400294
295 // Calculate optimization switches from gradient specification
296 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500297 bool isWellBehaved = isFocal && focalData.isWellBehaved();
298 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400299 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
300
301 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
302 // However, all types start with the total inverse local matrix calculated from the shader
303 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500304 bool isRadiusIncreasing;
305 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400306 SkMatrix matrix;
307
308 // Initialize the base matrix
Florin Malita52f02912020-03-09 16:33:17 -0400309 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400310 return nullptr;
311 }
312
313 if (isFocal) {
314 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
315
316 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
317
318 matrix.postConcat(grad.getGradientMatrix());
319 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500320 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400321 isRadiusIncreasing = dr >= 0;
322
323 SkScalar r0 = grad.getStartRadius() / dr;
324 focalParams.set(r0, r0 * r0);
325
326 // GPU radial matrix is different from the original matrix, since we map the diff radius
327 // to have |dr| = 1, so manually compute the final gradient matrix here.
328
329 // Map center to (0, 0)
330 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
331
332 // scale |diffRadius| to 1
333 matrix.postScale(1 / dr, 1 / dr);
334 } else { // kStrip
335 isRadiusIncreasing = false; // kStrip doesn't use this flag
336
337 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
338 focalParams.set(r0, r0 * r0);
339
340 matrix.postConcat(grad.getGradientMatrix());
341 }
342
Mike Kleind6ab77a2019-03-21 08:18:24 -0500343 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
344 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
345 isNativelyFocal, focalParams));
Michael Ludwig8f685082018-09-12 15:23:01 -0400346}