1 /*
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 GrUnrolledBinaryGradientColorizer.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrUnrolledBinaryGradientColorizer.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"
18 class GrGLSLUnrolledBinaryGradientColorizer : public GrGLSLFragmentProcessor {
19 public:
GrGLSLUnrolledBinaryGradientColorizer()20 GrGLSLUnrolledBinaryGradientColorizer() {}
emitCode(EmitArgs & args)21 void emitCode(EmitArgs& args) override {
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23 const GrUnrolledBinaryGradientColorizer& _outer =
24 args.fFp.cast<GrUnrolledBinaryGradientColorizer>();
25 (void)_outer;
26 auto intervalCount = _outer.intervalCount();
27 (void)intervalCount;
28 auto scale0_1 = _outer.scale0_1();
29 (void)scale0_1;
30 auto scale2_3 = _outer.scale2_3();
31 (void)scale2_3;
32 auto scale4_5 = _outer.scale4_5();
33 (void)scale4_5;
34 auto scale6_7 = _outer.scale6_7();
35 (void)scale6_7;
36 auto scale8_9 = _outer.scale8_9();
37 (void)scale8_9;
38 auto scale10_11 = _outer.scale10_11();
39 (void)scale10_11;
40 auto scale12_13 = _outer.scale12_13();
41 (void)scale12_13;
42 auto scale14_15 = _outer.scale14_15();
43 (void)scale14_15;
44 auto bias0_1 = _outer.bias0_1();
45 (void)bias0_1;
46 auto bias2_3 = _outer.bias2_3();
47 (void)bias2_3;
48 auto bias4_5 = _outer.bias4_5();
49 (void)bias4_5;
50 auto bias6_7 = _outer.bias6_7();
51 (void)bias6_7;
52 auto bias8_9 = _outer.bias8_9();
53 (void)bias8_9;
54 auto bias10_11 = _outer.bias10_11();
55 (void)bias10_11;
56 auto bias12_13 = _outer.bias12_13();
57 (void)bias12_13;
58 auto bias14_15 = _outer.bias14_15();
59 (void)bias14_15;
60 auto thresholds1_7 = _outer.thresholds1_7();
61 (void)thresholds1_7;
62 auto thresholds9_13 = _outer.thresholds9_13();
63 (void)thresholds9_13;
64 fScale0_1Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
65 kDefault_GrSLPrecision, "scale0_1");
66 if (intervalCount > 1) {
67 fScale2_3Var = args.fUniformHandler->addUniform(
68 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale2_3");
69 }
70 if (intervalCount > 2) {
71 fScale4_5Var = args.fUniformHandler->addUniform(
72 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale4_5");
73 }
74 if (intervalCount > 3) {
75 fScale6_7Var = args.fUniformHandler->addUniform(
76 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale6_7");
77 }
78 if (intervalCount > 4) {
79 fScale8_9Var = args.fUniformHandler->addUniform(
80 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale8_9");
81 }
82 if (intervalCount > 5) {
83 fScale10_11Var = args.fUniformHandler->addUniform(
84 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale10_11");
85 }
86 if (intervalCount > 6) {
87 fScale12_13Var = args.fUniformHandler->addUniform(
88 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale12_13");
89 }
90 if (intervalCount > 7) {
91 fScale14_15Var = args.fUniformHandler->addUniform(
92 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "scale14_15");
93 }
94 fBias0_1Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
95 kDefault_GrSLPrecision, "bias0_1");
96 if (intervalCount > 1) {
97 fBias2_3Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
98 kDefault_GrSLPrecision, "bias2_3");
99 }
100 if (intervalCount > 2) {
101 fBias4_5Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
102 kDefault_GrSLPrecision, "bias4_5");
103 }
104 if (intervalCount > 3) {
105 fBias6_7Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
106 kDefault_GrSLPrecision, "bias6_7");
107 }
108 if (intervalCount > 4) {
109 fBias8_9Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
110 kDefault_GrSLPrecision, "bias8_9");
111 }
112 if (intervalCount > 5) {
113 fBias10_11Var = args.fUniformHandler->addUniform(
114 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "bias10_11");
115 }
116 if (intervalCount > 6) {
117 fBias12_13Var = args.fUniformHandler->addUniform(
118 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "bias12_13");
119 }
120 if (intervalCount > 7) {
121 fBias14_15Var = args.fUniformHandler->addUniform(
122 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "bias14_15");
123 }
124 fThresholds1_7Var = args.fUniformHandler->addUniform(
125 kFragment_GrShaderFlag, kHalf4_GrSLType, kDefault_GrSLPrecision, "thresholds1_7");
126 fThresholds9_13Var = args.fUniformHandler->addUniform(
127 kFragment_GrShaderFlag, kHalf4_GrSLType, kDefault_GrSLPrecision, "thresholds9_13");
128 fragBuilder->codeAppendf(
129 "half t = %s.x;\nfloat4 scale, bias;\nif (%d <= 4 || t < %s.w) {\n if (%d <= 2 "
130 "|| t < %s.y) {\n if (%d <= 1 || t < %s.x) {\n scale = %s;\n "
131 " bias = %s;\n } else {\n scale = %s;\n bias = "
132 "%s;\n }\n } else {\n if (%d <= 3 || t < %s.z) {\n "
133 "scale = %s;\n bias = %s;\n } else {\n scale = %s;\n "
134 " bias = %s;\n }\n }\n} else {\n if (%d <= 6 || t < %s.y) "
135 "{\n if (%d <= 5 || t <",
136 args.fInputColor, _outer.intervalCount(),
137 args.fUniformHandler->getUniformCStr(fThresholds1_7Var), _outer.intervalCount(),
138 args.fUniformHandler->getUniformCStr(fThresholds1_7Var), _outer.intervalCount(),
139 args.fUniformHandler->getUniformCStr(fThresholds1_7Var),
140 args.fUniformHandler->getUniformCStr(fScale0_1Var),
141 args.fUniformHandler->getUniformCStr(fBias0_1Var),
142 fScale2_3Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale2_3Var)
143 : "float4(0)",
144 fBias2_3Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias2_3Var)
145 : "float4(0)",
146 _outer.intervalCount(), args.fUniformHandler->getUniformCStr(fThresholds1_7Var),
147 fScale4_5Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale4_5Var)
148 : "float4(0)",
149 fBias4_5Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias4_5Var)
150 : "float4(0)",
151 fScale6_7Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale6_7Var)
152 : "float4(0)",
153 fBias6_7Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias6_7Var)
154 : "float4(0)",
155 _outer.intervalCount(), args.fUniformHandler->getUniformCStr(fThresholds9_13Var),
156 _outer.intervalCount());
157 fragBuilder->codeAppendf(
158 " %s.x) {\n scale = %s;\n bias = %s;\n } else {\n "
159 " scale = %s;\n bias = %s;\n }\n } else {\n if "
160 "(%d <= 7 || t < %s.z) {\n scale = %s;\n bias = %s;\n "
161 "} else {\n scale = %s;\n bias = %s;\n }\n "
162 "}\n}\n%s = half4(float(t) * scale + bias);\n",
163 args.fUniformHandler->getUniformCStr(fThresholds9_13Var),
164 fScale8_9Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale8_9Var)
165 : "float4(0)",
166 fBias8_9Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias8_9Var)
167 : "float4(0)",
168 fScale10_11Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale10_11Var)
169 : "float4(0)",
170 fBias10_11Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias10_11Var)
171 : "float4(0)",
172 _outer.intervalCount(), args.fUniformHandler->getUniformCStr(fThresholds9_13Var),
173 fScale12_13Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale12_13Var)
174 : "float4(0)",
175 fBias12_13Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias12_13Var)
176 : "float4(0)",
177 fScale14_15Var.isValid() ? args.fUniformHandler->getUniformCStr(fScale14_15Var)
178 : "float4(0)",
179 fBias14_15Var.isValid() ? args.fUniformHandler->getUniformCStr(fBias14_15Var)
180 : "float4(0)",
181 args.fOutputColor);
182 }
183
184 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)185 void onSetData(const GrGLSLProgramDataManager& pdman,
186 const GrFragmentProcessor& _proc) override {
187 const GrUnrolledBinaryGradientColorizer& _outer =
188 _proc.cast<GrUnrolledBinaryGradientColorizer>();
189 {
190 pdman.set4fv(fScale0_1Var, 1, (_outer.scale0_1()).vec());
191 if (fScale2_3Var.isValid()) {
192 pdman.set4fv(fScale2_3Var, 1, (_outer.scale2_3()).vec());
193 }
194 if (fScale4_5Var.isValid()) {
195 pdman.set4fv(fScale4_5Var, 1, (_outer.scale4_5()).vec());
196 }
197 if (fScale6_7Var.isValid()) {
198 pdman.set4fv(fScale6_7Var, 1, (_outer.scale6_7()).vec());
199 }
200 if (fScale8_9Var.isValid()) {
201 pdman.set4fv(fScale8_9Var, 1, (_outer.scale8_9()).vec());
202 }
203 if (fScale10_11Var.isValid()) {
204 pdman.set4fv(fScale10_11Var, 1, (_outer.scale10_11()).vec());
205 }
206 if (fScale12_13Var.isValid()) {
207 pdman.set4fv(fScale12_13Var, 1, (_outer.scale12_13()).vec());
208 }
209 if (fScale14_15Var.isValid()) {
210 pdman.set4fv(fScale14_15Var, 1, (_outer.scale14_15()).vec());
211 }
212 pdman.set4fv(fBias0_1Var, 1, (_outer.bias0_1()).vec());
213 if (fBias2_3Var.isValid()) {
214 pdman.set4fv(fBias2_3Var, 1, (_outer.bias2_3()).vec());
215 }
216 if (fBias4_5Var.isValid()) {
217 pdman.set4fv(fBias4_5Var, 1, (_outer.bias4_5()).vec());
218 }
219 if (fBias6_7Var.isValid()) {
220 pdman.set4fv(fBias6_7Var, 1, (_outer.bias6_7()).vec());
221 }
222 if (fBias8_9Var.isValid()) {
223 pdman.set4fv(fBias8_9Var, 1, (_outer.bias8_9()).vec());
224 }
225 if (fBias10_11Var.isValid()) {
226 pdman.set4fv(fBias10_11Var, 1, (_outer.bias10_11()).vec());
227 }
228 if (fBias12_13Var.isValid()) {
229 pdman.set4fv(fBias12_13Var, 1, (_outer.bias12_13()).vec());
230 }
231 if (fBias14_15Var.isValid()) {
232 pdman.set4fv(fBias14_15Var, 1, (_outer.bias14_15()).vec());
233 }
234 pdman.set4fv(fThresholds1_7Var, 1,
235 reinterpret_cast<const float*>(&(_outer.thresholds1_7())));
236 pdman.set4fv(fThresholds9_13Var, 1,
237 reinterpret_cast<const float*>(&(_outer.thresholds9_13())));
238 }
239 }
240 UniformHandle fScale0_1Var;
241 UniformHandle fScale2_3Var;
242 UniformHandle fScale4_5Var;
243 UniformHandle fScale6_7Var;
244 UniformHandle fScale8_9Var;
245 UniformHandle fScale10_11Var;
246 UniformHandle fScale12_13Var;
247 UniformHandle fScale14_15Var;
248 UniformHandle fBias0_1Var;
249 UniformHandle fBias2_3Var;
250 UniformHandle fBias4_5Var;
251 UniformHandle fBias6_7Var;
252 UniformHandle fBias8_9Var;
253 UniformHandle fBias10_11Var;
254 UniformHandle fBias12_13Var;
255 UniformHandle fBias14_15Var;
256 UniformHandle fThresholds1_7Var;
257 UniformHandle fThresholds9_13Var;
258 };
onCreateGLSLInstance() const259 GrGLSLFragmentProcessor* GrUnrolledBinaryGradientColorizer::onCreateGLSLInstance() const {
260 return new GrGLSLUnrolledBinaryGradientColorizer();
261 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const262 void GrUnrolledBinaryGradientColorizer::onGetGLSLProcessorKey(const GrShaderCaps& caps,
263 GrProcessorKeyBuilder* b) const {
264 b->add32((int32_t)fIntervalCount);
265 }
onIsEqual(const GrFragmentProcessor & other) const266 bool GrUnrolledBinaryGradientColorizer::onIsEqual(const GrFragmentProcessor& other) const {
267 const GrUnrolledBinaryGradientColorizer& that = other.cast<GrUnrolledBinaryGradientColorizer>();
268 (void)that;
269 if (fIntervalCount != that.fIntervalCount) return false;
270 if (fScale0_1 != that.fScale0_1) return false;
271 if (fScale2_3 != that.fScale2_3) return false;
272 if (fScale4_5 != that.fScale4_5) return false;
273 if (fScale6_7 != that.fScale6_7) return false;
274 if (fScale8_9 != that.fScale8_9) return false;
275 if (fScale10_11 != that.fScale10_11) return false;
276 if (fScale12_13 != that.fScale12_13) return false;
277 if (fScale14_15 != that.fScale14_15) return false;
278 if (fBias0_1 != that.fBias0_1) return false;
279 if (fBias2_3 != that.fBias2_3) return false;
280 if (fBias4_5 != that.fBias4_5) return false;
281 if (fBias6_7 != that.fBias6_7) return false;
282 if (fBias8_9 != that.fBias8_9) return false;
283 if (fBias10_11 != that.fBias10_11) return false;
284 if (fBias12_13 != that.fBias12_13) return false;
285 if (fBias14_15 != that.fBias14_15) return false;
286 if (fThresholds1_7 != that.fThresholds1_7) return false;
287 if (fThresholds9_13 != that.fThresholds9_13) return false;
288 return true;
289 }
GrUnrolledBinaryGradientColorizer(const GrUnrolledBinaryGradientColorizer & src)290 GrUnrolledBinaryGradientColorizer::GrUnrolledBinaryGradientColorizer(
291 const GrUnrolledBinaryGradientColorizer& src)
292 : INHERITED(kGrUnrolledBinaryGradientColorizer_ClassID, src.optimizationFlags())
293 , fIntervalCount(src.fIntervalCount)
294 , fScale0_1(src.fScale0_1)
295 , fScale2_3(src.fScale2_3)
296 , fScale4_5(src.fScale4_5)
297 , fScale6_7(src.fScale6_7)
298 , fScale8_9(src.fScale8_9)
299 , fScale10_11(src.fScale10_11)
300 , fScale12_13(src.fScale12_13)
301 , fScale14_15(src.fScale14_15)
302 , fBias0_1(src.fBias0_1)
303 , fBias2_3(src.fBias2_3)
304 , fBias4_5(src.fBias4_5)
305 , fBias6_7(src.fBias6_7)
306 , fBias8_9(src.fBias8_9)
307 , fBias10_11(src.fBias10_11)
308 , fBias12_13(src.fBias12_13)
309 , fBias14_15(src.fBias14_15)
310 , fThresholds1_7(src.fThresholds1_7)
311 , fThresholds9_13(src.fThresholds9_13) {}
clone() const312 std::unique_ptr<GrFragmentProcessor> GrUnrolledBinaryGradientColorizer::clone() const {
313 return std::unique_ptr<GrFragmentProcessor>(new GrUnrolledBinaryGradientColorizer(*this));
314 }
315
316 static const int kMaxIntervals = 8;
Make(const SkPMColor4f * colors,const SkScalar * positions,int count)317 std::unique_ptr<GrFragmentProcessor> GrUnrolledBinaryGradientColorizer::Make(
318 const SkPMColor4f* colors, const SkScalar* positions, int count) {
319 // Depending on how the positions resolve into hard stops or regular stops, the number of
320 // intervals specified by the number of colors/positions can change. For instance, a plain
321 // 3 color gradient is two intervals, but a 4 color gradient with a hard stop is also
322 // two intervals. At the most extreme end, an 8 interval gradient made entirely of hard
323 // stops has 16 colors.
324
325 if (count > kMaxColorCount) {
326 // Definitely cannot represent this gradient configuration
327 return nullptr;
328 }
329
330 // The raster implementation also uses scales and biases, but since they must be calculated
331 // after the dst color space is applied, it limits our ability to cache their values.
332 SkPMColor4f scales[kMaxIntervals];
333 SkPMColor4f biases[kMaxIntervals];
334 SkScalar thresholds[kMaxIntervals];
335
336 int intervalCount = 0;
337
338 for (int i = 0; i < count - 1; i++) {
339 if (intervalCount >= kMaxIntervals) {
340 // Already reached kMaxIntervals, and haven't run out of color stops so this
341 // gradient cannot be represented by this shader.
342 return nullptr;
343 }
344
345 SkScalar t0 = positions[i];
346 SkScalar t1 = positions[i + 1];
347 SkScalar dt = t1 - t0;
348 // If the interval is empty, skip to the next interval. This will automatically create
349 // distinct hard stop intervals as needed. It also protects against malformed gradients
350 // that have repeated hard stops at the very beginning that are effectively unreachable.
351 if (SkScalarNearlyZero(dt)) {
352 continue;
353 }
354
355 auto c0 = Sk4f::Load(colors[i].vec());
356 auto c1 = Sk4f::Load(colors[i + 1].vec());
357
358 auto scale = (c1 - c0) / dt;
359 auto bias = c0 - t0 * scale;
360
361 scale.store(scales + intervalCount);
362 bias.store(biases + intervalCount);
363 thresholds[intervalCount] = t1;
364 intervalCount++;
365 }
366
367 // For isEqual to make sense, set the unused values to something consistent
368 for (int i = intervalCount; i < kMaxIntervals; i++) {
369 scales[i] = SK_PMColor4fTRANSPARENT;
370 biases[i] = SK_PMColor4fTRANSPARENT;
371 thresholds[i] = 0.0;
372 }
373
374 return std::unique_ptr<GrFragmentProcessor>(new GrUnrolledBinaryGradientColorizer(
375 intervalCount, scales[0], scales[1], scales[2], scales[3], scales[4], scales[5],
376 scales[6], scales[7], biases[0], biases[1], biases[2], biases[3], biases[4], biases[5],
377 biases[6], biases[7],
378 SkRect::MakeLTRB(thresholds[0], thresholds[1], thresholds[2], thresholds[3]),
379 SkRect::MakeLTRB(thresholds[4], thresholds[5], thresholds[6], 0.0)));
380 }
381