Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrMagnifierEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 11 | #include "GrMagnifierEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 19 | class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor { |
| 20 | public: |
| 21 | GrGLSLMagnifierEffect() {} |
| 22 | void emitCode(EmitArgs& args) override { |
| 23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 24 | const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 25 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 26 | auto bounds = _outer.bounds; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 27 | (void)bounds; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 28 | auto srcRect = _outer.srcRect; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 29 | (void)srcRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 30 | auto xInvZoom = _outer.xInvZoom; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 31 | (void)xInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 32 | auto yInvZoom = _outer.yInvZoom; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 33 | (void)yInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 34 | auto xInvInset = _outer.xInvInset; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 35 | (void)xInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 36 | auto yInvInset = _outer.yInvInset; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 37 | (void)yInvInset; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 38 | boundsUniformVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 39 | kFloat4_GrSLType, "boundsUniform"); |
| 40 | xInvZoomVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 41 | kFloat_GrSLType, "xInvZoom"); |
| 42 | yInvZoomVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 43 | kFloat_GrSLType, "yInvZoom"); |
| 44 | xInvInsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 45 | kFloat_GrSLType, "xInvInset"); |
| 46 | yInvInsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 47 | kFloat_GrSLType, "yInvInset"); |
| 48 | offsetVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 49 | kHalf2_GrSLType, "offset"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 50 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 51 | R"SkSL(float2 coord = %s; |
| 52 | float2 zoom_coord = float2(%s) + coord * float2(%s, %s); |
| 53 | float2 delta = (coord - %s.xy) * %s.zw; |
| 54 | delta = min(delta, float2(half2(1.0, 1.0)) - delta); |
| 55 | delta *= float2(%s, %s); |
| 56 | float weight = 0.0; |
| 57 | if (delta.x < 2.0 && delta.y < 2.0) { |
| 58 | delta = float2(half2(2.0, 2.0)) - delta; |
| 59 | float dist = length(delta); |
| 60 | dist = max(2.0 - dist, 0.0); |
| 61 | weight = min(dist * dist, 1.0); |
| 62 | } else { |
| 63 | float2 delta_squared = delta * delta; |
| 64 | weight = min(min(delta_squared.x, delta_squared.y), 1.0); |
| 65 | })SkSL", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 66 | args.fSampleCoord, args.fUniformHandler->getUniformCStr(offsetVar), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 67 | args.fUniformHandler->getUniformCStr(xInvZoomVar), |
| 68 | args.fUniformHandler->getUniformCStr(yInvZoomVar), |
| 69 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
| 70 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
| 71 | args.fUniformHandler->getUniformCStr(xInvInsetVar), |
| 72 | args.fUniformHandler->getUniformCStr(yInvInsetVar)); |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame^] | 73 | SkString _coords1077("mix(coord, zoom_coord, weight)"); |
| 74 | SkString _sample1077; |
| 75 | _sample1077 = this->invokeChild(_outer.src_index, args, _coords1077.c_str()); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 76 | fragBuilder->codeAppendf( |
| 77 | R"SkSL( |
| 78 | %s = %s; |
| 79 | )SkSL", |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame^] | 80 | args.fOutputColor, _sample1077.c_str()); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
| 84 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 85 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 86 | const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); |
| 87 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 88 | pdman.set1f(xInvZoomVar, (_outer.xInvZoom)); |
| 89 | pdman.set1f(yInvZoomVar, (_outer.yInvZoom)); |
| 90 | pdman.set1f(xInvInsetVar, (_outer.xInvInset)); |
| 91 | pdman.set1f(yInvInsetVar, (_outer.yInvInset)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 92 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 93 | auto bounds = _outer.bounds; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 94 | (void)bounds; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 95 | UniformHandle& boundsUniform = boundsUniformVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 96 | (void)boundsUniform; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 97 | auto srcRect = _outer.srcRect; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 98 | (void)srcRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 99 | UniformHandle& xInvZoom = xInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 100 | (void)xInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 101 | UniformHandle& yInvZoom = yInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 102 | (void)yInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 103 | UniformHandle& xInvInset = xInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 104 | (void)xInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 105 | UniformHandle& yInvInset = yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 106 | (void)yInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 107 | UniformHandle& offset = offsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 108 | (void)offset; |
| 109 | |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 110 | pdman.set2f(offset, srcRect.x(), srcRect.y()); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 111 | pdman.set4f(boundsUniform, bounds.x(), bounds.y(), 1.f / bounds.width(), |
| 112 | 1.f / bounds.height()); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 113 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 114 | UniformHandle boundsUniformVar; |
| 115 | UniformHandle offsetVar; |
| 116 | UniformHandle xInvZoomVar; |
| 117 | UniformHandle yInvZoomVar; |
| 118 | UniformHandle xInvInsetVar; |
| 119 | UniformHandle yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 120 | }; |
| 121 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
| 122 | return new GrGLSLMagnifierEffect(); |
| 123 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 124 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 125 | GrProcessorKeyBuilder* b) const {} |
| 126 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 127 | const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); |
| 128 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 129 | if (bounds != that.bounds) return false; |
| 130 | if (srcRect != that.srcRect) return false; |
| 131 | if (xInvZoom != that.xInvZoom) return false; |
| 132 | if (yInvZoom != that.yInvZoom) return false; |
| 133 | if (xInvInset != that.xInvInset) return false; |
| 134 | if (yInvInset != that.yInvInset) return false; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) |
| 138 | : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 139 | , bounds(src.bounds) |
| 140 | , srcRect(src.srcRect) |
| 141 | , xInvZoom(src.xInvZoom) |
| 142 | , yInvZoom(src.yInvZoom) |
| 143 | , xInvInset(src.xInvInset) |
| 144 | , yInvInset(src.yInvInset) { |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 145 | { src_index = this->cloneAndRegisterChildProcessor(src.childProcessor(src.src_index)); } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 146 | this->setUsesSampleCoordsDirectly(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 147 | } |
| 148 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { |
| 149 | return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this)); |
| 150 | } |
| 151 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
| 152 | #if GR_TEST_UTILS |
| 153 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 154 | const int kMaxWidth = 200; |
| 155 | const int kMaxHeight = 200; |
| 156 | const SkScalar kMaxInset = 20.0f; |
| 157 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 158 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 159 | SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 160 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 161 | SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); |
| 162 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 163 | |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 164 | auto src = GrProcessorUnitTest::MakeChildFP(d); |
| 165 | auto effect = GrMagnifierEffect::Make(std::move(src), |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 166 | bounds, |
| 167 | srcRect, |
| 168 | srcRect.width() / bounds.width(), |
| 169 | srcRect.height() / bounds.height(), |
| 170 | bounds.width() / inset, |
| 171 | bounds.height() / inset); |
| 172 | SkASSERT(effect); |
| 173 | return effect; |
| 174 | } |
| 175 | #endif |