blob: 5a29834fad980662b5e2940c23e6ec8af62714b7 [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 Nicholasd4efe682019-08-29 16:10:13 -040050 SkString sk_TransformedCoords2D_0 =
51 fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050052 fragBuilder->codeAppendf(
Ethan Nicholase1f55022019-02-05 17:17:40 -050053 "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 Salomonbf5c0c02019-11-11 14:55:28 -050060 sk_TransformedCoords2D_0.c_str(),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040061 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));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050068 fragBuilder->codeAppendf(
Ethan Nicholas13863662019-07-29 13:05:15 -040069 "d.y), 1.0);\n}\n%s = sample(%s, mix(coord, zoom_coord, weight)).%s;\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050070 args.fOutputColor,
Stephen Whited523a062019-06-19 13:12:46 -040071 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Greg Daniel369ee6b2019-12-02 15:30:02 -050072 fragBuilder->getProgramBuilder()
73 ->samplerSwizzle(args.fTexSamplers[0])
74 .asString()
75 .c_str());
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050076 }
77
78private:
79 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050080 const GrFragmentProcessor& _proc) override {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050081 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
82 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040083 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
84 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
85 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
86 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050087 }
88 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -050089 GrTexture& src = *srcProxy.peekTexture();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050090 (void)src;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040091 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050092 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040093 UniformHandle& boundsUniform = boundsUniformVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050094 (void)boundsUniform;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040095 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050096 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040097 UniformHandle& xInvZoom = xInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050098 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040099 UniformHandle& yInvZoom = yInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500100 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400101 UniformHandle& xInvInset = xInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500102 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400103 UniformHandle& yInvInset = yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500104 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400105 UniformHandle& offset = offsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500106 (void)offset;
107
108 SkScalar invW = 1.0f / src.width();
109 SkScalar invH = 1.0f / src.height();
110
111 {
112 SkScalar y = srcRect.y() * invH;
113 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
114 y = 1.0f - (srcRect.height() / bounds.height()) - y;
115 }
116
117 pdman.set2f(offset, srcRect.x() * invW, y);
118 }
119
120 {
121 SkScalar y = bounds.y() * invH;
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400122 SkScalar hSign = 1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500123 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400124 y = 1.0f - bounds.y() * invH;
125 hSign = -1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500126 }
127
128 pdman.set4f(boundsUniform,
129 bounds.x() * invW,
130 y,
131 SkIntToScalar(src.width()) / bounds.width(),
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400132 hSign * SkIntToScalar(src.height()) / bounds.height());
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500133 }
134 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400135 UniformHandle boundsUniformVar;
136 UniformHandle offsetVar;
137 UniformHandle xInvZoomVar;
138 UniformHandle yInvZoomVar;
139 UniformHandle xInvInsetVar;
140 UniformHandle yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500141};
142GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
143 return new GrGLSLMagnifierEffect();
144}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500145void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500146 GrProcessorKeyBuilder* b) const {}
147bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
148 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
149 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400150 if (src != that.src) return false;
151 if (bounds != that.bounds) return false;
152 if (srcRect != that.srcRect) return false;
153 if (xInvZoom != that.xInvZoom) return false;
154 if (yInvZoom != that.yInvZoom) return false;
155 if (xInvInset != that.xInvInset) return false;
156 if (yInvInset != that.yInvInset) return false;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500157 return true;
158}
159GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
160 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400161 , srcCoordTransform(src.srcCoordTransform)
162 , src(src.src)
163 , bounds(src.bounds)
164 , srcRect(src.srcRect)
165 , xInvZoom(src.xInvZoom)
166 , yInvZoom(src.yInvZoom)
167 , xInvInset(src.xInvInset)
168 , yInvInset(src.yInvInset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400169 this->setTextureSamplerCnt(1);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400170 this->addCoordTransform(&srcCoordTransform);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500171}
172std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
173 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
174}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400175const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400176 return IthTextureSampler(index, src);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400177}
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500178GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
179#if GR_TEST_UTILS
180std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500181 sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
182 const int kMaxWidth = 200;
183 const int kMaxHeight = 200;
184 const SkScalar kMaxInset = 20.0f;
185 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
186 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
187 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500188
Mike Kleind6ab77a2019-03-21 08:18:24 -0500189 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
190 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500191
192 auto effect = GrMagnifierEffect::Make(std::move(proxy),
193 bounds,
194 srcRect,
195 srcRect.width() / bounds.width(),
196 srcRect.height() / bounds.height(),
197 bounds.width() / inset,
198 bounds.height() / inset);
199 SkASSERT(effect);
200 return effect;
201}
202#endif