blob: cd67661336a7735b3f4b7104eab45567ae37580e [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}
149
150// .fp files do not let you reference outside enum definitions, so we have to explicitly map
151// between the two compatible enum defs
152GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
153 switch (type) {
154 case SkTwoPointConicalGradient::Type::kRadial:
155 return GrTwoPointConicalGradientLayout::Type::kRadial;
156 case SkTwoPointConicalGradient::Type::kStrip:
157 return GrTwoPointConicalGradientLayout::Type::kStrip;
158 case SkTwoPointConicalGradient::Type::kFocal:
159 return GrTwoPointConicalGradientLayout::Type::kFocal;
160 }
161 SkDEBUGFAIL("Should not be reachable");
162 return GrTwoPointConicalGradientLayout::Type::kRadial;
163}
164
165std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
166 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
167 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
168
169 // The focalData struct is only valid if isFocal is true
170 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
171 bool isFocal = grType == Type::kFocal;
172
173 // Calculate optimization switches from gradient specification
174 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
175 bool isWellBehaved = isFocal && focalData.isWellBehaved();
176 bool isSwapped = isFocal && focalData.isSwapped();
177 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
178
179 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
180 // However, all types start with the total inverse local matrix calculated from the shader
181 // and args
182 bool isRadiusIncreasing;
183 SkPoint focalParams; // really just a 2D tuple
184 SkMatrix matrix;
185
186 // Initialize the base matrix
187 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
188 return nullptr;
189 }
190
191 if (isFocal) {
192 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
193
194 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
195
196 matrix.postConcat(grad.getGradientMatrix());
197 } else if (grType == Type::kRadial) {
198 SkScalar dr = grad.getDiffRadius();
199 isRadiusIncreasing = dr >= 0;
200
201 SkScalar r0 = grad.getStartRadius() / dr;
202 focalParams.set(r0, r0 * r0);
203
204 // GPU radial matrix is different from the original matrix, since we map the diff radius
205 // to have |dr| = 1, so manually compute the final gradient matrix here.
206
207 // Map center to (0, 0)
208 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
209
210 // scale |diffRadius| to 1
211 matrix.postScale(1 / dr, 1 / dr);
212 } else { // kStrip
213 isRadiusIncreasing = false; // kStrip doesn't use this flag
214
215 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
216 focalParams.set(r0, r0 * r0);
217
218 matrix.postConcat(grad.getGradientMatrix());
219 }
220
221 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
222 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
223 isNativelyFocal, focalParams));
224}