blob: 291c867966dd9091bd8fbf3d8f27e43d5d2947ae [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"
12#include "glsl/GrGLSLFragmentProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLProgramBuilder.h"
15#include "GrTexture.h"
16#include "SkSLCPP.h"
17#include "SkSLUtil.h"
18class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor {
19public:
20 GrGLSLTwoPointConicalGradientLayout() {}
21 void emitCode(EmitArgs& args) override {
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23 const GrTwoPointConicalGradientLayout& _outer =
24 args.fFp.cast<GrTwoPointConicalGradientLayout>();
25 (void)_outer;
26 auto gradientMatrix = _outer.gradientMatrix();
27 (void)gradientMatrix;
28 auto type = _outer.type();
29 (void)type;
30 auto isRadiusIncreasing = _outer.isRadiusIncreasing();
31 (void)isRadiusIncreasing;
32 auto isFocalOnCircle = _outer.isFocalOnCircle();
33 (void)isFocalOnCircle;
34 auto isWellBehaved = _outer.isWellBehaved();
35 (void)isWellBehaved;
36 auto isSwapped = _outer.isSwapped();
37 (void)isSwapped;
38 auto isNativelyFocal = _outer.isNativelyFocal();
39 (void)isNativelyFocal;
40 auto focalParams = _outer.focalParams();
41 (void)focalParams;
42 fFocalParamsVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
43 kDefault_GrSLPrecision, "focalParams");
44 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
45 fragBuilder->codeAppendf(
46 "half2 p = half2(%s);\nhalf t = -1.0;\nhalf v = 1.0;\n@switch (%d) {\n case "
47 "1:\n {\n half r0_2 = %s.y;\n t = r0_2 - p.y * p.y;\n "
48 " if (t >= 0.0) {\n t = float(p.x) + sqrt(float(t));\n "
49 " } else {\n v = -1.0;\n }\n }\n "
50 "break;\n case 0:\n {\n half r0 = %s.x;\n @if (%s) "
51 "{\n t = length(p) - r0;\n } else {\n t = "
52 "-length(p) - r0;\n ",
53 sk_TransformedCoords2D_0.c_str(), (int)_outer.type(),
54 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
55 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
56 (_outer.isRadiusIncreasing() ? "true" : "false"));
57 fragBuilder->codeAppendf(
58 " }\n }\n break;\n case 2:\n {\n half invR1 = "
59 "%s.x;\n half fx = %s.y;\n half x_t = -1.0;\n @if "
60 "(%s) {\n x_t = dot(p, p) / p.x;\n } else if (%s) {\n "
61 " x_t = length(p) - p.x * invR1;\n } else {\n "
62 "half temp = p.x * p.x - p.y * p.y;\n if (temp >= 0.0) {\n "
63 " @if (%s || !%s) {\n x_t = "
64 "half(-sqrt(float(temp)) - float(p.x * invR1",
65 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
66 args.fUniformHandler->getUniformCStr(fFocalParamsVar),
67 (_outer.isFocalOnCircle() ? "true" : "false"),
68 (_outer.isWellBehaved() ? "true" : "false"),
69 (_outer.isSwapped() ? "true" : "false"),
70 (_outer.isRadiusIncreasing() ? "true" : "false"));
71 fragBuilder->codeAppendf(
72 "));\n } else {\n x_t = "
73 "half(sqrt(float(temp)) - float(p.x * invR1));\n }\n "
74 " }\n }\n @if (!%s) {\n if (float(x_t) <= "
75 "0.0) {\n v = -1.0;\n }\n }\n "
76 " @if (%s) {\n @if (%s) {\n t = x_t;\n "
77 " } else {\n t = x_t + fx;\n }\n "
78 " } else {\n @if (%s) {",
79 (_outer.isWellBehaved() ? "true" : "false"),
80 (_outer.isRadiusIncreasing() ? "true" : "false"),
81 (_outer.isNativelyFocal() ? "true" : "false"),
82 (_outer.isNativelyFocal() ? "true" : "false"));
83 fragBuilder->codeAppendf(
84 "\n t = -x_t;\n } else {\n t "
85 "= -x_t + fx;\n }\n }\n @if (%s) {\n "
86 " t = 1.0 - t;\n }\n }\n break;\n}\n%s = half4(t, v, "
87 "0.0, 0.0);\n",
88 (_outer.isSwapped() ? "true" : "false"), args.fOutputColor);
89 }
90
91private:
92 void onSetData(const GrGLSLProgramDataManager& pdman,
93 const GrFragmentProcessor& _proc) override {
94 const GrTwoPointConicalGradientLayout& _outer =
95 _proc.cast<GrTwoPointConicalGradientLayout>();
96 {
97 const SkPoint& focalParamsValue = _outer.focalParams();
98 if (fFocalParamsPrev != focalParamsValue) {
99 fFocalParamsPrev = focalParamsValue;
100 pdman.set2f(fFocalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
101 }
102 }
103 }
104 SkPoint fFocalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
105 UniformHandle fFocalParamsVar;
106};
107GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
108 return new GrGLSLTwoPointConicalGradientLayout();
109}
110void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
111 GrProcessorKeyBuilder* b) const {
112 b->add32((int32_t)fType);
113 b->add32((int32_t)fIsRadiusIncreasing);
114 b->add32((int32_t)fIsFocalOnCircle);
115 b->add32((int32_t)fIsWellBehaved);
116 b->add32((int32_t)fIsSwapped);
117 b->add32((int32_t)fIsNativelyFocal);
118}
119bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
120 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
121 (void)that;
122 if (fGradientMatrix != that.fGradientMatrix) return false;
123 if (fType != that.fType) return false;
124 if (fIsRadiusIncreasing != that.fIsRadiusIncreasing) return false;
125 if (fIsFocalOnCircle != that.fIsFocalOnCircle) return false;
126 if (fIsWellBehaved != that.fIsWellBehaved) return false;
127 if (fIsSwapped != that.fIsSwapped) return false;
128 if (fIsNativelyFocal != that.fIsNativelyFocal) return false;
129 if (fFocalParams != that.fFocalParams) return false;
130 return true;
131}
132GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
133 const GrTwoPointConicalGradientLayout& src)
134 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
135 , fGradientMatrix(src.fGradientMatrix)
136 , fType(src.fType)
137 , fIsRadiusIncreasing(src.fIsRadiusIncreasing)
138 , fIsFocalOnCircle(src.fIsFocalOnCircle)
139 , fIsWellBehaved(src.fIsWellBehaved)
140 , fIsSwapped(src.fIsSwapped)
141 , fIsNativelyFocal(src.fIsNativelyFocal)
142 , fFocalParams(src.fFocalParams)
143 , fCoordTransform0(src.fCoordTransform0) {
144 this->addCoordTransform(&fCoordTransform0);
145}
146std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
147 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this));
148}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400149GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
150#if GR_TEST_UTILS
151std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
152 GrProcessorTestData* d) {
153 SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
154 SkPoint center2 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
155 SkScalar radius1 = d->fRandom->nextUScalar1();
156 SkScalar radius2 = d->fRandom->nextUScalar1();
157
158 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
159 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
160 // We won't treat isWellDefined and isRadiusIncreasing specially because they
161 // should have high probability to be turned on and off as we're getting random
162 // radii and centers.
163
164 int mask = d->fRandom->nextU();
165 int type = mask & kTestTypeMask;
166 if (type == static_cast<int>(Type::kRadial)) {
167 center2 = center1;
168 // Make sure that the radii are different
169 if (SkScalarNearlyZero(radius1 - radius2)) {
170 radius2 += .1f;
171 }
172 } else if (type == static_cast<int>(Type::kStrip)) {
173 radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
174 radius2 = radius1;
175 // Make sure that the centers are different
176 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
177 center2.fX += .1f;
178 }
179 } else { // kFocal_Type
180 // Make sure that the centers are different
181 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
182 center2.fX += .1f;
183 }
184
185 if (kTestNativelyFocalBit & mask) {
186 radius1 = 0;
187 }
188 if (kTestFocalOnCircleBit & mask) {
189 radius2 = radius1 + SkPoint::Distance(center1, center2);
190 }
191 if (kTestSwappedBit & mask) {
192 std::swap(radius1, radius2);
193 radius2 = 0;
194 }
195
196 // Make sure that the radii are different
197 if (SkScalarNearlyZero(radius1 - radius2)) {
198 radius2 += .1f;
199 }
200 }
201
202 if (SkScalarNearlyZero(radius1 - radius2) &&
203 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
204 radius2 += .1f; // make sure that we're not degenerated
205 }
206
207 GrGradientShader::RandomParams params(d->fRandom);
208 auto shader = params.fUseColors4f
209 ? SkGradientShader::MakeTwoPointConical(
210 center1, radius1, center2, radius2, params.fColors4f,
211 params.fColorSpace, params.fStops, params.fColorCount,
212 params.fTileMode)
213 : SkGradientShader::MakeTwoPointConical(
214 center1, radius1, center2, radius2, params.fColors,
215 params.fStops, params.fColorCount, params.fTileMode);
216 GrTest::TestAsFPArgs asFPArgs(d);
217 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
218
219 GrAlwaysAssert(fp);
220 return fp;
221}
222#endif
Michael Ludwig8f685082018-09-12 15:23:01 -0400223
224// .fp files do not let you reference outside enum definitions, so we have to explicitly map
225// between the two compatible enum defs
226GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
227 switch (type) {
228 case SkTwoPointConicalGradient::Type::kRadial:
229 return GrTwoPointConicalGradientLayout::Type::kRadial;
230 case SkTwoPointConicalGradient::Type::kStrip:
231 return GrTwoPointConicalGradientLayout::Type::kStrip;
232 case SkTwoPointConicalGradient::Type::kFocal:
233 return GrTwoPointConicalGradientLayout::Type::kFocal;
234 }
235 SkDEBUGFAIL("Should not be reachable");
236 return GrTwoPointConicalGradientLayout::Type::kRadial;
237}
238
239std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
240 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
241 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
242
243 // The focalData struct is only valid if isFocal is true
244 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
245 bool isFocal = grType == Type::kFocal;
246
247 // Calculate optimization switches from gradient specification
248 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
249 bool isWellBehaved = isFocal && focalData.isWellBehaved();
250 bool isSwapped = isFocal && focalData.isSwapped();
251 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
252
253 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
254 // However, all types start with the total inverse local matrix calculated from the shader
255 // and args
256 bool isRadiusIncreasing;
257 SkPoint focalParams; // really just a 2D tuple
258 SkMatrix matrix;
259
260 // Initialize the base matrix
261 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
262 return nullptr;
263 }
264
265 if (isFocal) {
266 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
267
268 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
269
270 matrix.postConcat(grad.getGradientMatrix());
271 } else if (grType == Type::kRadial) {
272 SkScalar dr = grad.getDiffRadius();
273 isRadiusIncreasing = dr >= 0;
274
275 SkScalar r0 = grad.getStartRadius() / dr;
276 focalParams.set(r0, r0 * r0);
277
278 // GPU radial matrix is different from the original matrix, since we map the diff radius
279 // to have |dr| = 1, so manually compute the final gradient matrix here.
280
281 // Map center to (0, 0)
282 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
283
284 // scale |diffRadius| to 1
285 matrix.postScale(1 / dr, 1 / dr);
286 } else { // kStrip
287 isRadiusIncreasing = false; // kStrip doesn't use this flag
288
289 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
290 focalParams.set(r0, r0 * r0);
291
292 matrix.postConcat(grad.getGradientMatrix());
293 }
294
295 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
296 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
297 isNativelyFocal, focalParams));
298}