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" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "glsl/GrGLSLProgramBuilder.h" |
| 15 | #include "GrTexture.h" |
| 16 | #include "SkSLCPP.h" |
| 17 | #include "SkSLUtil.h" |
| 18 | class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor { |
| 19 | public: |
| 20 | GrGLSLMagnifierEffect() {} |
| 21 | void emitCode(EmitArgs& args) override { |
| 22 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 23 | const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>(); |
| 24 | (void)_outer; |
| 25 | auto bounds = _outer.bounds(); |
| 26 | (void)bounds; |
| 27 | auto srcRect = _outer.srcRect(); |
| 28 | (void)srcRect; |
| 29 | auto xInvZoom = _outer.xInvZoom(); |
| 30 | (void)xInvZoom; |
| 31 | auto yInvZoom = _outer.yInvZoom(); |
| 32 | (void)yInvZoom; |
| 33 | auto xInvInset = _outer.xInvInset(); |
| 34 | (void)xInvInset; |
| 35 | auto yInvInset = _outer.yInvInset(); |
| 36 | (void)yInvInset; |
| 37 | fBoundsUniformVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame^] | 38 | kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 39 | fXInvZoomVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame^] | 40 | kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 41 | fYInvZoomVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame^] | 42 | kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 43 | fXInvInsetVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame^] | 44 | kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 45 | fYInvInsetVar = args.fUniformHandler->addUniform( |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame^] | 46 | kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset"); |
| 47 | fOffsetVar = |
| 48 | args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "offset"); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 49 | SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 50 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 51 | "float2 coord = %s;\nfloat2 zoom_coord = float2(%s) + coord * float2(%s, " |
| 52 | "%s);\nfloat2 delta = (coord - %s.xy) * %s.zw;\ndelta = min(delta, " |
| 53 | "float2(half2(1.0, 1.0)) - delta);\ndelta *= float2(%s, %s);\nfloat weight = " |
| 54 | "0.0;\nif (delta.x < 2.0 && delta.y < 2.0) {\n delta = float2(half2(2.0, 2.0)) " |
| 55 | "- delta;\n float dist = length(delta);\n dist = max(2.0 - dist, 0.0);\n " |
| 56 | "weight = min(dist * dist, 1.0);\n} else {\n float2 delta_squared = delta * " |
| 57 | "delta;\n weight = min(min(delta_squared.x, delta_square", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 58 | sk_TransformedCoords2D_0.c_str(), |
| 59 | args.fUniformHandler->getUniformCStr(fOffsetVar), |
| 60 | args.fUniformHandler->getUniformCStr(fXInvZoomVar), |
| 61 | args.fUniformHandler->getUniformCStr(fYInvZoomVar), |
| 62 | args.fUniformHandler->getUniformCStr(fBoundsUniformVar), |
| 63 | args.fUniformHandler->getUniformCStr(fBoundsUniformVar), |
| 64 | args.fUniformHandler->getUniformCStr(fXInvInsetVar), |
| 65 | args.fUniformHandler->getUniformCStr(fYInvInsetVar)); |
| 66 | fragBuilder->codeAppendf( |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 67 | "d.y), 1.0);\n}\n%s = texture(%s, mix(coord, zoom_coord, weight)).%s;\n", |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 68 | args.fOutputColor, |
| 69 | fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), |
| 70 | fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str()); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 75 | const GrFragmentProcessor& _proc) override { |
| 76 | const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>(); |
| 77 | { |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 78 | pdman.set1f(fXInvZoomVar, (_outer.xInvZoom())); |
| 79 | pdman.set1f(fYInvZoomVar, (_outer.yInvZoom())); |
| 80 | pdman.set1f(fXInvInsetVar, (_outer.xInvInset())); |
| 81 | pdman.set1f(fYInvInsetVar, (_outer.yInvInset())); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 82 | } |
| 83 | GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy(); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 84 | GrTexture& src = *srcProxy.peekTexture(); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 85 | (void)src; |
| 86 | auto bounds = _outer.bounds(); |
| 87 | (void)bounds; |
| 88 | UniformHandle& boundsUniform = fBoundsUniformVar; |
| 89 | (void)boundsUniform; |
| 90 | auto srcRect = _outer.srcRect(); |
| 91 | (void)srcRect; |
| 92 | UniformHandle& xInvZoom = fXInvZoomVar; |
| 93 | (void)xInvZoom; |
| 94 | UniformHandle& yInvZoom = fYInvZoomVar; |
| 95 | (void)yInvZoom; |
| 96 | UniformHandle& xInvInset = fXInvInsetVar; |
| 97 | (void)xInvInset; |
| 98 | UniformHandle& yInvInset = fYInvInsetVar; |
| 99 | (void)yInvInset; |
| 100 | UniformHandle& offset = fOffsetVar; |
| 101 | (void)offset; |
| 102 | |
| 103 | SkScalar invW = 1.0f / src.width(); |
| 104 | SkScalar invH = 1.0f / src.height(); |
| 105 | |
| 106 | { |
| 107 | SkScalar y = srcRect.y() * invH; |
| 108 | if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) { |
| 109 | y = 1.0f - (srcRect.height() / bounds.height()) - y; |
| 110 | } |
| 111 | |
| 112 | pdman.set2f(offset, srcRect.x() * invW, y); |
| 113 | } |
| 114 | |
| 115 | { |
| 116 | SkScalar y = bounds.y() * invH; |
| 117 | if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) { |
| 118 | y = 1.0f - bounds.height() * invH; |
| 119 | } |
| 120 | |
| 121 | pdman.set4f(boundsUniform, |
| 122 | bounds.x() * invW, |
| 123 | y, |
| 124 | SkIntToScalar(src.width()) / bounds.width(), |
| 125 | SkIntToScalar(src.height()) / bounds.height()); |
| 126 | } |
| 127 | } |
| 128 | UniformHandle fBoundsUniformVar; |
| 129 | UniformHandle fOffsetVar; |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 130 | UniformHandle fXInvZoomVar; |
| 131 | UniformHandle fYInvZoomVar; |
| 132 | UniformHandle fXInvInsetVar; |
| 133 | UniformHandle fYInvInsetVar; |
| 134 | }; |
| 135 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
| 136 | return new GrGLSLMagnifierEffect(); |
| 137 | } |
| 138 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 139 | GrProcessorKeyBuilder* b) const {} |
| 140 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 141 | const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>(); |
| 142 | (void)that; |
| 143 | if (fSrc != that.fSrc) return false; |
| 144 | if (fBounds != that.fBounds) return false; |
| 145 | if (fSrcRect != that.fSrcRect) return false; |
| 146 | if (fXInvZoom != that.fXInvZoom) return false; |
| 147 | if (fYInvZoom != that.fYInvZoom) return false; |
| 148 | if (fXInvInset != that.fXInvInset) return false; |
| 149 | if (fYInvInset != that.fYInvInset) return false; |
| 150 | return true; |
| 151 | } |
| 152 | GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src) |
| 153 | : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags()) |
| 154 | , fSrc(src.fSrc) |
| 155 | , fBounds(src.fBounds) |
| 156 | , fSrcRect(src.fSrcRect) |
| 157 | , fXInvZoom(src.fXInvZoom) |
| 158 | , fYInvZoom(src.fYInvZoom) |
| 159 | , fXInvInset(src.fXInvInset) |
| 160 | , fYInvInset(src.fYInvInset) |
| 161 | , fSrcCoordTransform(src.fSrcCoordTransform) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 162 | this->setTextureSamplerCnt(1); |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 163 | this->addCoordTransform(&fSrcCoordTransform); |
| 164 | } |
| 165 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const { |
| 166 | return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this)); |
| 167 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 168 | const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const { |
| 169 | return IthTextureSampler(index, fSrc); |
| 170 | } |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 171 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
| 172 | #if GR_TEST_UTILS |
| 173 | std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
| 174 | sk_sp<GrTextureProxy> proxy = d->textureProxy(0); |
| 175 | const int kMaxWidth = 200; |
| 176 | const int kMaxHeight = 200; |
| 177 | const SkScalar kMaxInset = 20.0f; |
| 178 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 179 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 180 | SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset); |
| 181 | |
| 182 | SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)); |
| 183 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 184 | |
| 185 | auto effect = GrMagnifierEffect::Make(std::move(proxy), |
| 186 | bounds, |
| 187 | srcRect, |
| 188 | srcRect.width() / bounds.width(), |
| 189 | srcRect.height() / bounds.height(), |
| 190 | bounds.width() / inset, |
| 191 | bounds.height() / inset); |
| 192 | SkASSERT(effect); |
| 193 | return effect; |
| 194 | } |
| 195 | #endif |