John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 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 | |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 13 | #include "src/core/SkUtils.h" |
John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 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"); |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 31 | SkString _sample302 = this->invokeChild(0, args); |
John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 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) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 73 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 74 | } |
| 75 | std::unique_ptr<GrFragmentProcessor> GrDitherEffect::clone() const { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 76 | return std::make_unique<GrDitherEffect>(*this); |
John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 77 | } |
| 78 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDitherEffect); |
| 79 | #if GR_TEST_UTILS |
| 80 | std::unique_ptr<GrFragmentProcessor> GrDitherEffect::TestCreate(GrProcessorTestData* d) { |
Brian Osman | c262a12 | 2020-08-06 16:34:34 -0400 | [diff] [blame] | 81 | float range = 1.0f - d->fRandom->nextRangeF(0.0f, 1.0f); |
John Stiles | 53ee0fc | 2020-07-09 15:29:33 -0400 | [diff] [blame] | 82 | return GrDitherEffect::Make(GrProcessorUnitTest::MakeChildFP(d), range); |
| 83 | } |
| 84 | #endif |