blob: 722423ac3c211934a47fbc7cde68edbda9b95973 [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]),
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050072 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
73 }
74
75private:
76 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050077 const GrFragmentProcessor& _proc) override {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050078 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
79 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040080 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
81 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
82 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
83 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050084 }
85 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -050086 GrTexture& src = *srcProxy.peekTexture();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050087 (void)src;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040088 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050089 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040090 UniformHandle& boundsUniform = boundsUniformVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050091 (void)boundsUniform;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040092 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050093 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040094 UniformHandle& xInvZoom = xInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050095 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040096 UniformHandle& yInvZoom = yInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050097 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040098 UniformHandle& xInvInset = xInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050099 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400100 UniformHandle& yInvInset = yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500101 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400102 UniformHandle& offset = offsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500103 (void)offset;
104
105 SkScalar invW = 1.0f / src.width();
106 SkScalar invH = 1.0f / src.height();
107
108 {
109 SkScalar y = srcRect.y() * invH;
110 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
111 y = 1.0f - (srcRect.height() / bounds.height()) - y;
112 }
113
114 pdman.set2f(offset, srcRect.x() * invW, y);
115 }
116
117 {
118 SkScalar y = bounds.y() * invH;
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400119 SkScalar hSign = 1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500120 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400121 y = 1.0f - bounds.y() * invH;
122 hSign = -1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500123 }
124
125 pdman.set4f(boundsUniform,
126 bounds.x() * invW,
127 y,
128 SkIntToScalar(src.width()) / bounds.width(),
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400129 hSign * SkIntToScalar(src.height()) / bounds.height());
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500130 }
131 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400132 UniformHandle boundsUniformVar;
133 UniformHandle offsetVar;
134 UniformHandle xInvZoomVar;
135 UniformHandle yInvZoomVar;
136 UniformHandle xInvInsetVar;
137 UniformHandle yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500138};
139GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
140 return new GrGLSLMagnifierEffect();
141}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500142void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500143 GrProcessorKeyBuilder* b) const {}
144bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
145 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
146 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400147 if (src != that.src) return false;
148 if (bounds != that.bounds) return false;
149 if (srcRect != that.srcRect) return false;
150 if (xInvZoom != that.xInvZoom) return false;
151 if (yInvZoom != that.yInvZoom) return false;
152 if (xInvInset != that.xInvInset) return false;
153 if (yInvInset != that.yInvInset) return false;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500154 return true;
155}
156GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
157 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400158 , srcCoordTransform(src.srcCoordTransform)
159 , src(src.src)
160 , bounds(src.bounds)
161 , srcRect(src.srcRect)
162 , xInvZoom(src.xInvZoom)
163 , yInvZoom(src.yInvZoom)
164 , xInvInset(src.xInvInset)
165 , yInvInset(src.yInvInset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400166 this->setTextureSamplerCnt(1);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400167 this->addCoordTransform(&srcCoordTransform);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500168}
169std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
170 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
171}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400172const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400173 return IthTextureSampler(index, src);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400174}
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500175GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
176#if GR_TEST_UTILS
177std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500178 sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
179 const int kMaxWidth = 200;
180 const int kMaxHeight = 200;
181 const SkScalar kMaxInset = 20.0f;
182 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
183 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
184 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500185
Mike Kleind6ab77a2019-03-21 08:18:24 -0500186 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
187 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500188
189 auto effect = GrMagnifierEffect::Make(std::move(proxy),
190 bounds,
191 srcRect,
192 srcRect.width() / bounds.width(),
193 srcRect.height() / bounds.height(),
194 bounds.width() / inset,
195 bounds.height() / inset);
196 SkASSERT(effect);
197 return effect;
198}
199#endif