• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrDualIntervalGradientColorizer.fp; do not modify.
10  **************************************************************************************************/
11 #include "GrDualIntervalGradientColorizer.h"
12 
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"
19 class GrGLSLDualIntervalGradientColorizer : public GrGLSLFragmentProcessor {
20 public:
GrGLSLDualIntervalGradientColorizer()21     GrGLSLDualIntervalGradientColorizer() {}
emitCode(EmitArgs & args)22     void emitCode(EmitArgs& args) override {
23         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24         const GrDualIntervalGradientColorizer& _outer =
25                 args.fFp.cast<GrDualIntervalGradientColorizer>();
26         (void)_outer;
27         auto scale01 = _outer.scale01;
28         (void)scale01;
29         auto bias01 = _outer.bias01;
30         (void)bias01;
31         auto scale23 = _outer.scale23;
32         (void)scale23;
33         auto bias23 = _outer.bias23;
34         (void)bias23;
35         auto threshold = _outer.threshold;
36         (void)threshold;
37         scale01Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
38                                                       "scale01");
39         bias01Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
40                                                      "bias01");
41         scale23Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
42                                                       "scale23");
43         bias23Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
44                                                      "bias23");
45         thresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
46                                                         "threshold");
47         fragBuilder->codeAppendf(
48                 "half t = %s.x;\nfloat4 scale, bias;\nif (t < %s) {\n    scale = %s;\n    bias = "
49                 "%s;\n} else {\n    scale = %s;\n    bias = %s;\n}\n%s = half4(float(t) * scale + "
50                 "bias);\n",
51                 args.fInputColor, args.fUniformHandler->getUniformCStr(thresholdVar),
52                 args.fUniformHandler->getUniformCStr(scale01Var),
53                 args.fUniformHandler->getUniformCStr(bias01Var),
54                 args.fUniformHandler->getUniformCStr(scale23Var),
55                 args.fUniformHandler->getUniformCStr(bias23Var), args.fOutputColor);
56     }
57 
58 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)59     void onSetData(const GrGLSLProgramDataManager& pdman,
60                    const GrFragmentProcessor& _proc) override {
61         const GrDualIntervalGradientColorizer& _outer =
62                 _proc.cast<GrDualIntervalGradientColorizer>();
63         {
64             const SkPMColor4f& scale01Value = _outer.scale01;
65             if (scale01Prev != scale01Value) {
66                 scale01Prev = scale01Value;
67                 pdman.set4fv(scale01Var, 1, scale01Value.vec());
68             }
69             const SkPMColor4f& bias01Value = _outer.bias01;
70             if (bias01Prev != bias01Value) {
71                 bias01Prev = bias01Value;
72                 pdman.set4fv(bias01Var, 1, bias01Value.vec());
73             }
74             const SkPMColor4f& scale23Value = _outer.scale23;
75             if (scale23Prev != scale23Value) {
76                 scale23Prev = scale23Value;
77                 pdman.set4fv(scale23Var, 1, scale23Value.vec());
78             }
79             const SkPMColor4f& bias23Value = _outer.bias23;
80             if (bias23Prev != bias23Value) {
81                 bias23Prev = bias23Value;
82                 pdman.set4fv(bias23Var, 1, bias23Value.vec());
83             }
84             float thresholdValue = _outer.threshold;
85             if (thresholdPrev != thresholdValue) {
86                 thresholdPrev = thresholdValue;
87                 pdman.set1f(thresholdVar, thresholdValue);
88             }
89         }
90     }
91     SkPMColor4f scale01Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
92     SkPMColor4f bias01Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
93     SkPMColor4f scale23Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
94     SkPMColor4f bias23Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
95     float thresholdPrev = SK_FloatNaN;
96     UniformHandle scale01Var;
97     UniformHandle bias01Var;
98     UniformHandle scale23Var;
99     UniformHandle bias23Var;
100     UniformHandle thresholdVar;
101 };
onCreateGLSLInstance() const102 GrGLSLFragmentProcessor* GrDualIntervalGradientColorizer::onCreateGLSLInstance() const {
103     return new GrGLSLDualIntervalGradientColorizer();
104 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const105 void GrDualIntervalGradientColorizer::onGetGLSLProcessorKey(const GrShaderCaps& caps,
106                                                             GrProcessorKeyBuilder* b) const {}
onIsEqual(const GrFragmentProcessor & other) const107 bool GrDualIntervalGradientColorizer::onIsEqual(const GrFragmentProcessor& other) const {
108     const GrDualIntervalGradientColorizer& that = other.cast<GrDualIntervalGradientColorizer>();
109     (void)that;
110     if (scale01 != that.scale01) return false;
111     if (bias01 != that.bias01) return false;
112     if (scale23 != that.scale23) return false;
113     if (bias23 != that.bias23) return false;
114     if (threshold != that.threshold) return false;
115     return true;
116 }
GrDualIntervalGradientColorizer(const GrDualIntervalGradientColorizer & src)117 GrDualIntervalGradientColorizer::GrDualIntervalGradientColorizer(
118         const GrDualIntervalGradientColorizer& src)
119         : INHERITED(kGrDualIntervalGradientColorizer_ClassID, src.optimizationFlags())
120         , scale01(src.scale01)
121         , bias01(src.bias01)
122         , scale23(src.scale23)
123         , bias23(src.bias23)
124         , threshold(src.threshold) {}
clone() const125 std::unique_ptr<GrFragmentProcessor> GrDualIntervalGradientColorizer::clone() const {
126     return std::unique_ptr<GrFragmentProcessor>(new GrDualIntervalGradientColorizer(*this));
127 }
128 
Make(const SkPMColor4f & c0,const SkPMColor4f & c1,const SkPMColor4f & c2,const SkPMColor4f & c3,float threshold)129 std::unique_ptr<GrFragmentProcessor> GrDualIntervalGradientColorizer::Make(const SkPMColor4f& c0,
130                                                                            const SkPMColor4f& c1,
131                                                                            const SkPMColor4f& c2,
132                                                                            const SkPMColor4f& c3,
133                                                                            float threshold) {
134     // Derive scale and biases from the 4 colors and threshold
135     auto vc0 = Sk4f::Load(c0.vec());
136     auto vc1 = Sk4f::Load(c1.vec());
137     auto scale01 = (vc1 - vc0) / threshold;
138     // bias01 = c0
139 
140     auto vc2 = Sk4f::Load(c2.vec());
141     auto vc3 = Sk4f::Load(c3.vec());
142     auto scale23 = (vc3 - vc2) / (1 - threshold);
143     auto bias23 = vc2 - threshold * scale23;
144 
145     return std::unique_ptr<GrFragmentProcessor>(new GrDualIntervalGradientColorizer(
146             {scale01[0], scale01[1], scale01[2], scale01[3]}, c0,
147             {scale23[0], scale23[1], scale23[2], scale23[3]},
148             {bias23[0], bias23[1], bias23[2], bias23[3]}, threshold));
149 }
150