blob: 804a42c6d82cf9eb06de5b03c90c10e50282a0f8 [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
13#include "include/gpu/GrTexture.h"
14#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 Nicholasbcd51e82019-04-09 10:40:41 -040043 focalParamsVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
44 "focalParams");
Ethan Nicholasd4efe682019-08-29 16:10:13 -040045 SkString sk_TransformedCoords2D_0 =
46 fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
Michael Ludwig8f685082018-09-12 15:23:01 -040047 fragBuilder->codeAppendf(
Michael Ludwigbf6bf392018-09-19 16:46:07 -040048 "float2 p = %s;\nfloat t = -1.0;\nhalf v = 1.0;\n@switch (%d) {\n case 1:\n "
Ethan Nicholase1f55022019-02-05 17:17:40 -050049 " {\n half r0_2 = %s.y;\n t = float(r0_2) - p.y * p.y;\n "
50 " if (t >= 0.0) {\n t = p.x + sqrt(t);\n } else "
51 "{\n v = -1.0;\n }\n }\n break;\n case "
52 "0:\n {\n half r0 = %s.x;\n @if (%s) {\n "
53 " t = length(p) - float(r0);\n } else {\n t = "
54 "-length(p) - float(r0);\n ",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040055 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
56 : "_coords",
57 (int)_outer.type, args.fUniformHandler->getUniformCStr(focalParamsVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040058 args.fUniformHandler->getUniformCStr(focalParamsVar),
59 (_outer.isRadiusIncreasing ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040060 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050061 " }\n }\n break;\n case 2:\n {\n half invR1 "
62 "= %s.x;\n half fx = %s.y;\n float x_t = -1.0;\n "
63 "@if (%s) {\n x_t = dot(p, p) / p.x;\n } else if (%s) "
64 "{\n x_t = length(p) - p.x * float(invR1);\n } else {\n "
65 " float temp = p.x * p.x - p.y * p.y;\n if (temp >= "
66 "0.0) {\n @if (%s || !%s) {\n x_t = "
67 "-sqrt(temp) - p.x * float(invR1)",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040068 args.fUniformHandler->getUniformCStr(focalParamsVar),
69 args.fUniformHandler->getUniformCStr(focalParamsVar),
70 (_outer.isFocalOnCircle ? "true" : "false"),
71 (_outer.isWellBehaved ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
72 (_outer.isRadiusIncreasing ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040073 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050074 ";\n } else {\n x_t = sqrt(temp) - p.x * "
75 "float(invR1);\n }\n }\n }\n "
76 " @if (!%s) {\n if (x_t <= 0.0) {\n v = -1.0;\n "
77 " }\n }\n @if (%s) {\n @if (%s) "
78 "{\n t = x_t;\n } else {\n t "
79 "= x_t + float(fx);\n }\n } else {\n @if "
80 "(%s) {\n ",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040081 (_outer.isWellBehaved ? "true" : "false"),
82 (_outer.isRadiusIncreasing ? "true" : "false"),
83 (_outer.isNativelyFocal ? "true" : "false"),
84 (_outer.isNativelyFocal ? "true" : "false"));
Michael Ludwig8f685082018-09-12 15:23:01 -040085 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050086 " t = -x_t;\n } else {\n t = -x_t + "
Michael Ludwigbf6bf392018-09-19 16:46:07 -040087 "float(fx);\n }\n }\n @if (%s) {\n "
88 " t = 1.0 - t;\n }\n }\n break;\n}\n%s = "
89 "half4(half(t), v, 0.0, 0.0);\n",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040090 (_outer.isSwapped ? "true" : "false"), args.fOutputColor);
Michael Ludwig8f685082018-09-12 15:23:01 -040091 }
92
93private:
94 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050095 const GrFragmentProcessor& _proc) override {
Michael Ludwig8f685082018-09-12 15:23:01 -040096 const GrTwoPointConicalGradientLayout& _outer =
97 _proc.cast<GrTwoPointConicalGradientLayout>();
98 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040099 const SkPoint& focalParamsValue = _outer.focalParams;
100 if (focalParamsPrev != focalParamsValue) {
101 focalParamsPrev = focalParamsValue;
102 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
Michael Ludwig8f685082018-09-12 15:23:01 -0400103 }
104 }
105 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400106 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
107 UniformHandle focalParamsVar;
Michael Ludwig8f685082018-09-12 15:23:01 -0400108};
109GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
110 return new GrGLSLTwoPointConicalGradientLayout();
111}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500112void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Michael Ludwig8f685082018-09-12 15:23:01 -0400113 GrProcessorKeyBuilder* b) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400114 b->add32((int32_t)type);
115 b->add32((int32_t)isRadiusIncreasing);
116 b->add32((int32_t)isFocalOnCircle);
117 b->add32((int32_t)isWellBehaved);
118 b->add32((int32_t)isSwapped);
119 b->add32((int32_t)isNativelyFocal);
Michael Ludwig8f685082018-09-12 15:23:01 -0400120}
121bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
122 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
123 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400124 if (gradientMatrix != that.gradientMatrix) return false;
125 if (type != that.type) return false;
126 if (isRadiusIncreasing != that.isRadiusIncreasing) return false;
127 if (isFocalOnCircle != that.isFocalOnCircle) return false;
128 if (isWellBehaved != that.isWellBehaved) return false;
129 if (isSwapped != that.isSwapped) return false;
130 if (isNativelyFocal != that.isNativelyFocal) return false;
131 if (focalParams != that.focalParams) return false;
Michael Ludwig8f685082018-09-12 15:23:01 -0400132 return true;
133}
134GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
135 const GrTwoPointConicalGradientLayout& src)
136 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400137 , fCoordTransform0(src.fCoordTransform0)
138 , gradientMatrix(src.gradientMatrix)
139 , type(src.type)
140 , isRadiusIncreasing(src.isRadiusIncreasing)
141 , isFocalOnCircle(src.isFocalOnCircle)
142 , isWellBehaved(src.isWellBehaved)
143 , isSwapped(src.isSwapped)
144 , isNativelyFocal(src.isNativelyFocal)
145 , focalParams(src.focalParams) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400146 this->addCoordTransform(&fCoordTransform0);
147}
148std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
149 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
150}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400151GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
152#if GR_TEST_UTILS
153std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
154 GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500155 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
Michael Ludwig083bc152018-10-01 17:15:15 -0400156 SkScalar offset = scale / 32.0f;
157
Mike Kleind6ab77a2019-03-21 08:18:24 -0500158 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400159 d->fRandom->nextRangeScalar(0.0f, scale)};
Mike Kleind6ab77a2019-03-21 08:18:24 -0500160 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
Michael Ludwig083bc152018-10-01 17:15:15 -0400161 d->fRandom->nextRangeScalar(0.0f, scale)};
162 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
163 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400164
165 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
166 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
167 // We won't treat isWellDefined and isRadiusIncreasing specially because they
168 // should have high probability to be turned on and off as we're getting random
169 // radii and centers.
170
171 int mask = d->fRandom->nextU();
172 int type = mask & kTestTypeMask;
173 if (type == static_cast<int>(Type::kRadial)) {
174 center2 = center1;
175 // Make sure that the radii are different
176 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400177 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400178 }
179 } else if (type == static_cast<int>(Type::kStrip)) {
180 radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
181 radius2 = radius1;
182 // Make sure that the centers are different
183 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400184 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400185 }
186 } else { // kFocal_Type
187 // Make sure that the centers are different
188 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400189 center2.fX += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400190 }
191
192 if (kTestNativelyFocalBit & mask) {
193 radius1 = 0;
194 }
195 if (kTestFocalOnCircleBit & mask) {
196 radius2 = radius1 + SkPoint::Distance(center1, center2);
197 }
198 if (kTestSwappedBit & mask) {
199 std::swap(radius1, radius2);
200 radius2 = 0;
201 }
202
203 // Make sure that the radii are different
204 if (SkScalarNearlyZero(radius1 - radius2)) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400205 radius2 += offset;
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400206 }
207 }
208
209 if (SkScalarNearlyZero(radius1 - radius2) &&
210 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
Michael Ludwig083bc152018-10-01 17:15:15 -0400211 radius2 += offset; // make sure that we're not degenerated
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400212 }
213
214 GrGradientShader::RandomParams params(d->fRandom);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500215 auto shader = params.fUseColors4f
216 ? SkGradientShader::MakeTwoPointConical(
217 center1, radius1, center2, radius2, params.fColors4f,
218 params.fColorSpace, params.fStops, params.fColorCount,
219 params.fTileMode)
220 : SkGradientShader::MakeTwoPointConical(
221 center1, radius1, center2, radius2, params.fColors,
222 params.fStops, params.fColorCount, params.fTileMode);
223 GrTest::TestAsFPArgs asFPArgs(d);
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400224 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
225
226 GrAlwaysAssert(fp);
227 return fp;
228}
229#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400230
231// .fp files do not let you reference outside enum definitions, so we have to explicitly map
232// between the two compatible enum defs
233GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
234 switch (type) {
235 case SkTwoPointConicalGradient::Type::kRadial:
236 return GrTwoPointConicalGradientLayout::Type::kRadial;
237 case SkTwoPointConicalGradient::Type::kStrip:
238 return GrTwoPointConicalGradientLayout::Type::kStrip;
239 case SkTwoPointConicalGradient::Type::kFocal:
240 return GrTwoPointConicalGradientLayout::Type::kFocal;
241 }
242 SkDEBUGFAIL("Should not be reachable");
243 return GrTwoPointConicalGradientLayout::Type::kRadial;
244}
245
246std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
Mike Kleind6ab77a2019-03-21 08:18:24 -0500247 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
Michael Ludwig8f685082018-09-12 15:23:01 -0400248 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
249
250 // The focalData struct is only valid if isFocal is true
251 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500252 bool isFocal = grType == Type::kFocal;
Michael Ludwig8f685082018-09-12 15:23:01 -0400253
254 // Calculate optimization switches from gradient specification
255 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500256 bool isWellBehaved = isFocal && focalData.isWellBehaved();
257 bool isSwapped = isFocal && focalData.isSwapped();
Michael Ludwig8f685082018-09-12 15:23:01 -0400258 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
259
260 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
261 // However, all types start with the total inverse local matrix calculated from the shader
262 // and args
Mike Kleind6ab77a2019-03-21 08:18:24 -0500263 bool isRadiusIncreasing;
264 SkPoint focalParams; // really just a 2D tuple
Michael Ludwig8f685082018-09-12 15:23:01 -0400265 SkMatrix matrix;
266
267 // Initialize the base matrix
268 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
269 return nullptr;
270 }
271
272 if (isFocal) {
273 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
274
275 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
276
277 matrix.postConcat(grad.getGradientMatrix());
278 } else if (grType == Type::kRadial) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500279 SkScalar dr = grad.getDiffRadius();
Michael Ludwig8f685082018-09-12 15:23:01 -0400280 isRadiusIncreasing = dr >= 0;
281
282 SkScalar r0 = grad.getStartRadius() / dr;
283 focalParams.set(r0, r0 * r0);
284
285 // GPU radial matrix is different from the original matrix, since we map the diff radius
286 // to have |dr| = 1, so manually compute the final gradient matrix here.
287
288 // Map center to (0, 0)
289 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
290
291 // scale |diffRadius| to 1
292 matrix.postScale(1 / dr, 1 / dr);
293 } else { // kStrip
294 isRadiusIncreasing = false; // kStrip doesn't use this flag
295
296 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
297 focalParams.set(r0, r0 * r0);
298
299 matrix.postConcat(grad.getGradientMatrix());
300 }
301
Mike Kleind6ab77a2019-03-21 08:18:24 -0500302 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
303 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
304 isNativelyFocal, focalParams));
Michael Ludwig8f685082018-09-12 15:23:01 -0400305}