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 GrDitherEffect.fp; do not modify.
10 **************************************************************************************************/
11#include "GrDitherEffect.h"
12#if SK_SUPPORT_GPU
13#include "glsl/GrGLSLFragmentProcessor.h"
14#include "glsl/GrGLSLFragmentShaderBuilder.h"
15#include "glsl/GrGLSLProgramBuilder.h"
16#include "GrTexture.h"
17#include "SkSLCPP.h"
18#include "SkSLUtil.h"
19class GrGLSLDitherEffect : public GrGLSLFragmentProcessor {
20public:
21    GrGLSLDitherEffect() {}
22    void emitCode(EmitArgs& args) override {
23        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24        const GrDitherEffect& _outer = args.fFp.cast<GrDitherEffect>();
25        (void)_outer;
26        auto rangeType = _outer.rangeType();
27        (void)rangeType;
28        fragBuilder->codeAppendf(
29                "half value;\nhalf range;\n@switch (%d) {\n    case 0:\n        range = "
30                "0.0039215686274509803;\n        break;\n    case 1:\n        range = "
31                "0.015873015873015872;\n        break;\n    default:\n        range = "
32                "0.066666666666666666;\n        break;\n}\n@if (sk_Caps.integerSupport) {\n    "
33                "uint x = uint(sk_FragCoord.x);\n    uint y = uint(sk_FragCoord.y);\n    uint m = "
34                "(((((y & 1) << 5 | (x & 1) << 4) | (y & 2) << 2) | (x & 2) << 1) | (y & 4) >> 1) "
35                "| (x & 4) >> 2;\n    value = float(float(half(m)) / 64.0) - 0.4",
36                _outer.rangeType());
37        fragBuilder->codeAppendf(
38                "921875;\n} else {\n    half4 modValues = half4(mod(sk_FragCoord.xyxy, "
39                "float4(half4(2.0, 2.0, 4.0, 4.0))));\n    half4 stepValues = "
40                "half4(step(float4(modValues), float4(half4(1.0, 1.0, 2.0, 2.0))));\n    value = "
41                "float(dot(stepValues, half4(0.5, 0.25, 0.125, 0.0625))) - 0.46875;\n}\n%s = "
42                "half4(clamp(float3(%s.xyz + value * range), 0.0, float(%s.w)), %s.w);\n",
43                args.fOutputColor, args.fInputColor ? args.fInputColor : "half4(1)",
44                args.fInputColor ? args.fInputColor : "half4(1)",
45                args.fInputColor ? args.fInputColor : "half4(1)");
46    }
47
48private:
49    void onSetData(const GrGLSLProgramDataManager& pdman,
50                   const GrFragmentProcessor& _proc) override {}
51};
52GrGLSLFragmentProcessor* GrDitherEffect::onCreateGLSLInstance() const {
53    return new GrGLSLDitherEffect();
54}
55void GrDitherEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
56                                           GrProcessorKeyBuilder* b) const {
57    b->add32((int32_t)fRangeType);
58}
59bool GrDitherEffect::onIsEqual(const GrFragmentProcessor& other) const {
60    const GrDitherEffect& that = other.cast<GrDitherEffect>();
61    (void)that;
62    if (fRangeType != that.fRangeType) return false;
63    return true;
64}
65GrDitherEffect::GrDitherEffect(const GrDitherEffect& src)
66        : INHERITED(kGrDitherEffect_ClassID, src.optimizationFlags()), fRangeType(src.fRangeType) {}
67std::unique_ptr<GrFragmentProcessor> GrDitherEffect::clone() const {
68    return std::unique_ptr<GrFragmentProcessor>(new GrDitherEffect(*this));
69}
70GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDitherEffect);
71#if GR_TEST_UTILS
72std::unique_ptr<GrFragmentProcessor> GrDitherEffect::TestCreate(GrProcessorTestData* testData) {
73    float range = testData->fRandom->nextRangeF(0.001f, 0.05f);
74    return std::unique_ptr<GrFragmentProcessor>(new GrDitherEffect(range));
75}
76#endif
77#endif
78