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