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