blob: 4f7a2eaefbe4dcb36161eca97359bc7b8cd8af3e [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",
Ethan Nicholasd4efe682019-08-29 16:10:13 -040060 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
61 : "_coords",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040062 args.fUniformHandler->getUniformCStr(offsetVar),
63 args.fUniformHandler->getUniformCStr(xInvZoomVar),
64 args.fUniformHandler->getUniformCStr(yInvZoomVar),
65 args.fUniformHandler->getUniformCStr(boundsUniformVar),
66 args.fUniformHandler->getUniformCStr(boundsUniformVar),
67 args.fUniformHandler->getUniformCStr(xInvInsetVar),
68 args.fUniformHandler->getUniformCStr(yInvInsetVar));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050069 fragBuilder->codeAppendf(
Ethan Nicholas13863662019-07-29 13:05:15 -040070 "d.y), 1.0);\n}\n%s = sample(%s, mix(coord, zoom_coord, weight)).%s;\n",
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050071 args.fOutputColor,
Stephen Whited523a062019-06-19 13:12:46 -040072 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050073 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
74 }
75
76private:
77 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050078 const GrFragmentProcessor& _proc) override {
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050079 const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
80 {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040081 pdman.set1f(xInvZoomVar, (_outer.xInvZoom));
82 pdman.set1f(yInvZoomVar, (_outer.yInvZoom));
83 pdman.set1f(xInvInsetVar, (_outer.xInvInset));
84 pdman.set1f(yInvInsetVar, (_outer.yInvInset));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050085 }
86 GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -050087 GrTexture& src = *srcProxy.peekTexture();
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050088 (void)src;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040089 auto bounds = _outer.bounds;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050090 (void)bounds;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040091 UniformHandle& boundsUniform = boundsUniformVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050092 (void)boundsUniform;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040093 auto srcRect = _outer.srcRect;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050094 (void)srcRect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040095 UniformHandle& xInvZoom = xInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050096 (void)xInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040097 UniformHandle& yInvZoom = yInvZoomVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -050098 (void)yInvZoom;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040099 UniformHandle& xInvInset = xInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500100 (void)xInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400101 UniformHandle& yInvInset = yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500102 (void)yInvInset;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400103 UniformHandle& offset = offsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500104 (void)offset;
105
106 SkScalar invW = 1.0f / src.width();
107 SkScalar invH = 1.0f / src.height();
108
109 {
110 SkScalar y = srcRect.y() * invH;
111 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
112 y = 1.0f - (srcRect.height() / bounds.height()) - y;
113 }
114
115 pdman.set2f(offset, srcRect.x() * invW, y);
116 }
117
118 {
119 SkScalar y = bounds.y() * invH;
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400120 SkScalar hSign = 1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500121 if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400122 y = 1.0f - bounds.y() * invH;
123 hSign = -1.f;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500124 }
125
126 pdman.set4f(boundsUniform,
127 bounds.x() * invW,
128 y,
129 SkIntToScalar(src.width()) / bounds.width(),
Michael Ludwig9c853fa2019-08-28 14:21:08 -0400130 hSign * SkIntToScalar(src.height()) / bounds.height());
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500131 }
132 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400133 UniformHandle boundsUniformVar;
134 UniformHandle offsetVar;
135 UniformHandle xInvZoomVar;
136 UniformHandle yInvZoomVar;
137 UniformHandle xInvInsetVar;
138 UniformHandle yInvInsetVar;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500139};
140GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
141 return new GrGLSLMagnifierEffect();
142}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500143void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500144 GrProcessorKeyBuilder* b) const {}
145bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& other) const {
146 const GrMagnifierEffect& that = other.cast<GrMagnifierEffect>();
147 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400148 if (src != that.src) return false;
149 if (bounds != that.bounds) return false;
150 if (srcRect != that.srcRect) return false;
151 if (xInvZoom != that.xInvZoom) return false;
152 if (yInvZoom != that.yInvZoom) return false;
153 if (xInvInset != that.xInvInset) return false;
154 if (yInvInset != that.yInvInset) return false;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500155 return true;
156}
157GrMagnifierEffect::GrMagnifierEffect(const GrMagnifierEffect& src)
158 : INHERITED(kGrMagnifierEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400159 , srcCoordTransform(src.srcCoordTransform)
160 , src(src.src)
161 , bounds(src.bounds)
162 , srcRect(src.srcRect)
163 , xInvZoom(src.xInvZoom)
164 , yInvZoom(src.yInvZoom)
165 , xInvInset(src.xInvInset)
166 , yInvInset(src.yInvInset) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400167 this->setTextureSamplerCnt(1);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400168 this->addCoordTransform(&srcCoordTransform);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500169}
170std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::clone() const {
171 return std::unique_ptr<GrFragmentProcessor>(new GrMagnifierEffect(*this));
172}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400173const GrFragmentProcessor::TextureSampler& GrMagnifierEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400174 return IthTextureSampler(index, src);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400175}
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500176GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
177#if GR_TEST_UTILS
178std::unique_ptr<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500179 sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
180 const int kMaxWidth = 200;
181 const int kMaxHeight = 200;
182 const SkScalar kMaxInset = 20.0f;
183 uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
184 uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
185 SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500186
Mike Kleind6ab77a2019-03-21 08:18:24 -0500187 SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
188 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500189
190 auto effect = GrMagnifierEffect::Make(std::move(proxy),
191 bounds,
192 srcRect,
193 srcRect.width() / bounds.width(),
194 srcRect.height() / bounds.height(),
195 bounds.width() / inset,
196 bounds.height() / inset);
197 SkASSERT(effect);
198 return effect;
199}
200#endif