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