blob: b70c07c86b4eb40c8c98d84a8afc20914c700c57 [file] [log] [blame]
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -05003 *
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 Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrMagnifierEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050011#include "GrMagnifierEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012
13#include "include/gpu/GrTexture.h"
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 Nicholas2d5f9b32017-12-13 14:36:14 -050019class GrGLSLMagnifierEffect : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLMagnifierEffect() {}
22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050024 const GrMagnifierEffect& _outer = args.fFp.cast<GrMagnifierEffect>();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050025 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040026 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050027 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040028 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050029 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040030 auto xInvZoom = _outer.xInvZoom;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050031 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040032 auto yInvZoom = _outer.yInvZoom;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050033 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040034 auto xInvInset = _outer.xInvInset;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050035 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040036 auto yInvInset = _outer.yInvInset;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050037 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040038 boundsUniformVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050039 kFragment_GrShaderFlag, kFloat4_GrSLType, "boundsUniform");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040040 xInvZoomVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050041 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvZoom");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040042 yInvZoomVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050043 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvZoom");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040044 xInvInsetVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050045 kFragment_GrShaderFlag, kFloat_GrSLType, "xInvInset");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040046 yInvInsetVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050047 kFragment_GrShaderFlag, kFloat_GrSLType, "yInvInset");
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040048 offsetVar =
Ethan Nicholas858fecc2019-03-07 13:19:18 -050049 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "offset");
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050050 SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
51 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050052 "float2 coord = %s;\nfloat2 zoom_coord = float2(%s) + coord * float2(%s, "
53 "%s);\nfloat2 delta = (coord - %s.xy) * %s.zw;\ndelta = min(delta, "
54 "float2(half2(1.0, 1.0)) - delta);\ndelta *= float2(%s, %s);\nfloat weight = "
55 "0.0;\nif (delta.x < 2.0 && delta.y < 2.0) {\n delta = float2(half2(2.0, 2.0)) "
56 "- delta;\n float dist = length(delta);\n dist = max(2.0 - dist, 0.0);\n "
57 "weight = min(dist * dist, 1.0);\n} else {\n float2 delta_squared = delta * "
58 "delta;\n weight = min(min(delta_squared.x, delta_square",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050059 sk_TransformedCoords2D_0.c_str(),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040060 args.fUniformHandler->getUniformCStr(offsetVar),
61 args.fUniformHandler->getUniformCStr(xInvZoomVar),
62 args.fUniformHandler->getUniformCStr(yInvZoomVar),
63 args.fUniformHandler->getUniformCStr(boundsUniformVar),
64 args.fUniformHandler->getUniformCStr(boundsUniformVar),
65 args.fUniformHandler->getUniformCStr(xInvInsetVar),
66 args.fUniformHandler->getUniformCStr(yInvInsetVar));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050067 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050068 "d.y), 1.0);\n}\n%s = texture(%s, mix(coord, zoom_coord, weight)).%s;\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050069 args.fOutputColor,
Stephen Whited523a062019-06-19 13:12:46 -040070 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050071 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
72 }
73
74private:
75 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050076 const GrFragmentProcessor& _proc) override {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050077 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
78 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040079 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
80 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
81 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
82 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050083 }
84 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -050085 GrTexture& src = *srcProxy.peekTexture();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050086 (void)src;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040087 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050088 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040089 UniformHandle& boundsUniform = boundsUniformVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050090 (void)boundsUniform;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040091 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050092 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040093 UniformHandle& xInvZoom = xInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050094 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040095 UniformHandle& yInvZoom = yInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050096 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040097 UniformHandle& xInvInset = xInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050098 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040099 UniformHandle& yInvInset = yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500100 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400101 UniformHandle& offset = offsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500102 (void)offset;
103
104 SkScalar invW = 1.0f / src.width();
105 SkScalar invH = 1.0f / src.height();
106
107 {
108 SkScalar y = srcRect.y() * invH;
109 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
110 y = 1.0f - (srcRect.height() / bounds.height()) - y;
111 }
112
113 pdman.set2f(offset, srcRect.x() * invW, y);
114 }
115
116 {
117 SkScalar y = bounds.y() * invH;
118 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
119 y = 1.0f - bounds.height() * invH;
120 }
121
122 pdman.set4f(boundsUniform,
123 bounds.x() * invW,
124 y,
125 SkIntToScalar(src.width()) / bounds.width(),
126 SkIntToScalar(src.height()) / bounds.height());
127 }
128 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400129 UniformHandle boundsUniformVar;
130 UniformHandle offsetVar;
131 UniformHandle xInvZoomVar;
132 UniformHandle yInvZoomVar;
133 UniformHandle xInvInsetVar;
134 UniformHandle yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500135};
136GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
137 return new GrGLSLMagnifierEffect();
138}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500139void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500140 GrProcessorKeyBuilder* b) const {}
141bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
142 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
143 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400144 if (src != that.src) return false;
145 if (bounds != that.bounds) return false;
146 if (srcRect != that.srcRect) return false;
147 if (xInvZoom != that.xInvZoom) return false;
148 if (yInvZoom != that.yInvZoom) return false;
149 if (xInvInset != that.xInvInset) return false;
150 if (yInvInset != that.yInvInset) return false;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500151 return true;
152}
153GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
154 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400155 , srcCoordTransform(src.srcCoordTransform)
156 , src(src.src)
157 , bounds(src.bounds)
158 , srcRect(src.srcRect)
159 , xInvZoom(src.xInvZoom)
160 , yInvZoom(src.yInvZoom)
161 , xInvInset(src.xInvInset)
162 , yInvInset(src.yInvInset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400163 this->setTextureSamplerCnt(1);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400164 this->addCoordTransform(&srcCoordTransform);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500165}
166std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
167 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
168}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400169const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400170 return IthTextureSampler(index, src);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400171}
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500172GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
173#if GR_TEST_UTILS
174std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500175 sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
176 const int kMaxWidth = 200;
177 const int kMaxHeight = 200;
178 const SkScalar kMaxInset = 20.0f;
179 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
180 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
181 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500182
Mike Kleind6ab77a2019-03-21 08:18:24 -0500183 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
184 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500185
186 auto effect = GrMagnifierEffect::Make(std::move(proxy),
187 bounds,
188 srcRect,
189 srcRect.width() / bounds.width(),
190 srcRect.height() / bounds.height(),
191 bounds.width() / inset,
192 bounds.height() / inset);
193 SkASSERT(effect);
194 return effect;
195}
196#endif