1 /*
2 * Copyright 2019 Google LLC
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 GrColorMatrixFragmentProcessor.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrColorMatrixFragmentProcessor.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 GrGLSLColorMatrixFragmentProcessor : public GrGLSLFragmentProcessor {
20 public:
GrGLSLColorMatrixFragmentProcessor()21 GrGLSLColorMatrixFragmentProcessor() {}
emitCode(EmitArgs & args)22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrColorMatrixFragmentProcessor& _outer =
25 args.fFp.cast<GrColorMatrixFragmentProcessor>();
26 (void)_outer;
27 auto m = _outer.m;
28 (void)m;
29 auto v = _outer.v;
30 (void)v;
31 auto unpremulInput = _outer.unpremulInput;
32 (void)unpremulInput;
33 auto clampRGBOutput = _outer.clampRGBOutput;
34 (void)clampRGBOutput;
35 auto premulOutput = _outer.premulOutput;
36 (void)premulOutput;
37 mVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4x4_GrSLType, "m");
38 vVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "v");
39 fragBuilder->codeAppendf(
40 "half4 inputColor = %s;\n@if (%s) {\n half nonZeroAlpha = max(inputColor.w, "
41 "9.9999997473787516e-05);\n inputColor = half4(inputColor.xyz / nonZeroAlpha, "
42 "nonZeroAlpha);\n}\n%s = %s * inputColor + %s;\n@if (%s) {\n %s = clamp(%s, "
43 "0.0, 1.0);\n} else {\n %s.w = clamp(%s.w, 0.0, 1.0);\n}\n@if (%s) {\n "
44 "%s.xyz *= %s.w;\n}\n",
45 args.fInputColor, (_outer.unpremulInput ? "true" : "false"), args.fOutputColor,
46 args.fUniformHandler->getUniformCStr(mVar),
47 args.fUniformHandler->getUniformCStr(vVar),
48 (_outer.clampRGBOutput ? "true" : "false"), args.fOutputColor, args.fOutputColor,
49 args.fOutputColor, args.fOutputColor, (_outer.premulOutput ? "true" : "false"),
50 args.fOutputColor, args.fOutputColor);
51 }
52
53 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)54 void onSetData(const GrGLSLProgramDataManager& pdman,
55 const GrFragmentProcessor& _proc) override {
56 const GrColorMatrixFragmentProcessor& _outer = _proc.cast<GrColorMatrixFragmentProcessor>();
57 {
58 const SkMatrix44& mValue = _outer.m;
59 if (mPrev != (mValue)) {
60 mPrev = mValue;
61 pdman.setSkMatrix44(mVar, mValue);
62 }
63 const SkVector4& vValue = _outer.v;
64 if (vPrev != (vValue)) {
65 vPrev = vValue;
66 pdman.set4fv(vVar, 1, vValue.fData);
67 }
68 }
69 }
70 SkMatrix44 mPrev = SkMatrix44(SkMatrix44::kNaN_Constructor);
71 SkVector4 vPrev = SkVector4(SK_MScalarNaN, SK_MScalarNaN, SK_MScalarNaN, SK_MScalarNaN);
72 UniformHandle mVar;
73 UniformHandle vVar;
74 };
onCreateGLSLInstance() const75 GrGLSLFragmentProcessor* GrColorMatrixFragmentProcessor::onCreateGLSLInstance() const {
76 return new GrGLSLColorMatrixFragmentProcessor();
77 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const78 void GrColorMatrixFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
79 GrProcessorKeyBuilder* b) const {
80 b->add32((int32_t)unpremulInput);
81 b->add32((int32_t)clampRGBOutput);
82 b->add32((int32_t)premulOutput);
83 }
onIsEqual(const GrFragmentProcessor & other) const84 bool GrColorMatrixFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
85 const GrColorMatrixFragmentProcessor& that = other.cast<GrColorMatrixFragmentProcessor>();
86 (void)that;
87 if (m != that.m) return false;
88 if (v != that.v) return false;
89 if (unpremulInput != that.unpremulInput) return false;
90 if (clampRGBOutput != that.clampRGBOutput) return false;
91 if (premulOutput != that.premulOutput) return false;
92 return true;
93 }
GrColorMatrixFragmentProcessor(const GrColorMatrixFragmentProcessor & src)94 GrColorMatrixFragmentProcessor::GrColorMatrixFragmentProcessor(
95 const GrColorMatrixFragmentProcessor& src)
96 : INHERITED(kGrColorMatrixFragmentProcessor_ClassID, src.optimizationFlags())
97 , m(src.m)
98 , v(src.v)
99 , unpremulInput(src.unpremulInput)
100 , clampRGBOutput(src.clampRGBOutput)
101 , premulOutput(src.premulOutput) {}
clone() const102 std::unique_ptr<GrFragmentProcessor> GrColorMatrixFragmentProcessor::clone() const {
103 return std::unique_ptr<GrFragmentProcessor>(new GrColorMatrixFragmentProcessor(*this));
104 }
105 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrColorMatrixFragmentProcessor);
106 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)107 std::unique_ptr<GrFragmentProcessor> GrColorMatrixFragmentProcessor::TestCreate(
108 GrProcessorTestData* d) {
109 float m[20];
110 for (int i = 0; i < 20; ++i) {
111 m[i] = d->fRandom->nextRangeScalar(-10.f, 10.f);
112 }
113 bool unpremul = d->fRandom->nextBool();
114 bool clampRGB = d->fRandom->nextBool();
115 bool premul = d->fRandom->nextBool();
116 return Make(m, unpremul, clampRGB, premul);
117 }
118 #endif
119