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 | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 50 | SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D( |
| 51 | args.fTransformedCoords[0].fVaryingPoint, _outer.sampleMatrix()); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 52 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 53 | R"SkSL(float2 coord = %s; |
| 54 | float2 zoom_coord = float2(%s) + coord * float2(%s, %s); |
| 55 | float2 delta = (coord - %s.xy) * %s.zw; |
| 56 | delta = min(delta, float2(half2(1.0, 1.0)) - delta); |
| 57 | delta *= float2(%s, %s); |
| 58 | float weight = 0.0; |
| 59 | if (delta.x < 2.0 && delta.y < 2.0) { |
| 60 | delta = float2(half2(2.0, 2.0)) - delta; |
| 61 | float dist = length(delta); |
| 62 | dist = max(2.0 - dist, 0.0); |
| 63 | weight = min(dist * dist, 1.0); |
| 64 | } else { |
| 65 | float2 delta_squared = delta * delta; |
| 66 | weight = min(min(delta_squared.x, delta_squared.y), 1.0); |
| 67 | })SkSL", |
| 68 | sk_TransformedCoords2D_0.c_str(), args.fUniformHandler->getUniformCStr(offsetVar), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 69 | args.fUniformHandler->getUniformCStr(xInvZoomVar), |
| 70 | args.fUniformHandler->getUniformCStr(yInvZoomVar), |
| 71 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
| 72 | args.fUniformHandler->getUniformCStr(boundsUniformVar), |
| 73 | args.fUniformHandler->getUniformCStr(xInvInsetVar), |
| 74 | args.fUniformHandler->getUniformCStr(yInvInsetVar)); |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 75 | SkString _sample1112; |
| 76 | SkString _coords1112("mix(coord, zoom_coord, weight)"); |
| 77 | _sample1112 = this->invokeChild(_outer.src_index, args, _coords1112.c_str()); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 78 | fragBuilder->codeAppendf( |
| 79 | R"SkSL( |
| 80 | %s = %s; |
| 81 | )SkSL", |
| 82 | args.fOutputColor, _sample1112.c_str()); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
| 86 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 87 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 88 | const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); |
| 89 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 90 | pdman.set1f(xInvZoomVar, (_outer.xInvZoom)); |
| 91 | pdman.set1f(yInvZoomVar, (_outer.yInvZoom)); |
| 92 | pdman.set1f(xInvInsetVar, (_outer.xInvInset)); |
| 93 | pdman.set1f(yInvInsetVar, (_outer.yInvInset)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 94 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 95 | auto bounds = _outer.bounds; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 96 | (void)bounds; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 97 | UniformHandle& boundsUniform = boundsUniformVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 98 | (void)boundsUniform; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 99 | auto srcRect = _outer.srcRect; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 100 | (void)srcRect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 101 | UniformHandle& xInvZoom = xInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 102 | (void)xInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 103 | UniformHandle& yInvZoom = yInvZoomVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 104 | (void)yInvZoom; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 105 | UniformHandle& xInvInset = xInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 106 | (void)xInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 107 | UniformHandle& yInvInset = yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 108 | (void)yInvInset; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 109 | UniformHandle& offset = offsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 110 | (void)offset; |
| 111 | |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 112 | pdman.set2f(offset, srcRect.x(), srcRect.y()); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 113 | pdman.set4f(boundsUniform, bounds.x(), bounds.y(), 1.f / bounds.width(), |
| 114 | 1.f / bounds.height()); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 115 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 116 | UniformHandle boundsUniformVar; |
| 117 | UniformHandle offsetVar; |
| 118 | UniformHandle xInvZoomVar; |
| 119 | UniformHandle yInvZoomVar; |
| 120 | UniformHandle xInvInsetVar; |
| 121 | UniformHandle yInvInsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 122 | }; |
| 123 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
| 124 | return new GrGLSLMagnifierEffect(); |
| 125 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 126 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 127 | GrProcessorKeyBuilder* b) const {} |
| 128 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 129 | const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); |
| 130 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 131 | if (bounds != that.bounds) return false; |
| 132 | if (srcRect != that.srcRect) return false; |
| 133 | if (xInvZoom != that.xInvZoom) return false; |
| 134 | if (yInvZoom != that.yInvZoom) return false; |
| 135 | if (xInvInset != that.xInvInset) return false; |
| 136 | if (yInvInset != that.yInvInset) return false; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) |
| 140 | : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 141 | , fCoordTransform0(src.fCoordTransform0) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 142 | , bounds(src.bounds) |
| 143 | , srcRect(src.srcRect) |
| 144 | , xInvZoom(src.xInvZoom) |
| 145 | , yInvZoom(src.yInvZoom) |
| 146 | , xInvInset(src.xInvInset) |
| 147 | , yInvInset(src.yInvInset) { |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 148 | { src_index = this->cloneAndRegisterChildProcessor(src.childProcessor(src.src_index)); } |
| 149 | this->addCoordTransform(&fCoordTransform0); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 150 | } |
| 151 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { |
| 152 | return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this)); |
| 153 | } |
| 154 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
| 155 | #if GR_TEST_UTILS |
| 156 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 157 | const int kMaxWidth = 200; |
| 158 | const int kMaxHeight = 200; |
| 159 | const SkScalar kMaxInset = 20.0f; |
| 160 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 161 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 162 | SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 163 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 164 | SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); |
| 165 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 166 | |
Brian Salomon | f5cd604 | 2020-06-16 14:04:25 -0400 | [diff] [blame] | 167 | auto src = GrProcessorUnitTest::MakeChildFP(d); |
| 168 | auto effect = GrMagnifierEffect::Make(std::move(src), |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 169 | bounds, |
| 170 | srcRect, |
| 171 | srcRect.width() / bounds.width(), |
| 172 | srcRect.height() / bounds.height(), |
| 173 | bounds.width() / inset, |
| 174 | bounds.height() / inset); |
| 175 | SkASSERT(effect); |
| 176 | return effect; |
| 177 | } |
| 178 | #endif |