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