1 | /* |
2 | * Copyright 2020 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 | |
13 | #include "src/core/SkUtils.h" |
14 | #include "src/gpu/GrTexture.h" |
15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
18 | #include "src/sksl/SkSLCPP.h" |
19 | #include "src/sksl/SkSLUtil.h" |
20 | class GrGLSLDitherEffect : public GrGLSLFragmentProcessor { |
21 | public: |
22 | GrGLSLDitherEffect() {} |
23 | void emitCode(EmitArgs& args) override { |
24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
25 | const GrDitherEffect& _outer = args.fFp.cast<GrDitherEffect>(); |
26 | (void)_outer; |
27 | auto range = _outer.range; |
28 | (void)range; |
29 | rangeVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, kHalf_GrSLType, |
30 | "range" ); |
31 | SkString _sample302 = this->invokeChild(0, args); |
32 | fragBuilder->codeAppendf( |
33 | R"SkSL(half4 color = %s; |
34 | half value; |
35 | @if (sk_Caps.integerSupport) { |
36 | uint x = uint(sk_FragCoord.x); |
37 | uint y = uint(sk_FragCoord.y) ^ x; |
38 | uint m = (((((y & 1) << 5 | (x & 1) << 4) | (y & 2) << 2) | (x & 2) << 1) | (y & 4) >> 1) | (x & 4) >> 2; |
39 | value = half(m) / 64.0 - 0.4921875; |
40 | } else { |
41 | half4 bits = mod(half4(sk_FragCoord.yxyx), half4(2.0, 2.0, 4.0, 4.0)); |
42 | bits.zw = step(2.0, bits.zw); |
43 | bits.xz = abs(bits.xz - bits.yw); |
44 | value = dot(bits, half4(0.5, 0.25, 0.125, 0.0625)) - 0.46875; |
45 | } |
46 | %s = half4(clamp(color.xyz + value * %s, 0.0, color.w), color.w); |
47 | )SkSL" , |
48 | _sample302.c_str(), args.fOutputColor, |
49 | args.fUniformHandler->getUniformCStr(rangeVar)); |
50 | } |
51 | |
52 | private: |
53 | void onSetData(const GrGLSLProgramDataManager& pdman, |
54 | const GrFragmentProcessor& _proc) override { |
55 | const GrDitherEffect& _outer = _proc.cast<GrDitherEffect>(); |
56 | { pdman.set1f(rangeVar, (_outer.range)); } |
57 | } |
58 | UniformHandle rangeVar; |
59 | }; |
60 | GrGLSLFragmentProcessor* GrDitherEffect::onCreateGLSLInstance() const { |
61 | return new GrGLSLDitherEffect(); |
62 | } |
63 | void GrDitherEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
64 | GrProcessorKeyBuilder* b) const {} |
65 | bool GrDitherEffect::onIsEqual(const GrFragmentProcessor& other) const { |
66 | const GrDitherEffect& that = other.cast<GrDitherEffect>(); |
67 | (void)that; |
68 | if (range != that.range) return false; |
69 | return true; |
70 | } |
71 | GrDitherEffect::GrDitherEffect(const GrDitherEffect& src) |
72 | : INHERITED(kGrDitherEffect_ClassID, src.optimizationFlags()), range(src.range) { |
73 | this->cloneAndRegisterAllChildProcessors(src); |
74 | } |
75 | std::unique_ptr<GrFragmentProcessor> GrDitherEffect::clone() const { |
76 | return std::make_unique<GrDitherEffect>(*this); |
77 | } |
78 | #if GR_TEST_UTILS |
79 | SkString GrDitherEffect::onDumpInfo() const { return SkStringPrintf("(range=%f)" , range); } |
80 | #endif |
81 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDitherEffect); |
82 | #if GR_TEST_UTILS |
83 | std::unique_ptr<GrFragmentProcessor> GrDitherEffect::TestCreate(GrProcessorTestData* d) { |
84 | float range = 1.0f - d->fRandom->nextRangeF(0.0f, 1.0f); |
85 | return GrDitherEffect::Make(GrProcessorUnitTest::MakeChildFP(d), range); |
86 | } |
87 | #endif |
88 | |