blob: 97daa7ee5982fac7c7ab8d9dd30c0c388f780aa5 [file] [log] [blame]
Ethan Nicholas82399462017-10-16 12:35:44 -04001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
Ethan Nicholas82399462017-10-16 12:35:44 -04003 *
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 GrRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
Ethan Nicholas82399462017-10-16 12:35:44 -040011#include "GrRectBlurEffect.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 Nicholas82399462017-10-16 12:35:44 -040019class GrGLSLRectBlurEffect : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLRectBlurEffect() {}
22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050024 const GrRectBlurEffect& _outer = args.fFp.cast<GrRectBlurEffect>();
Ethan Nicholas82399462017-10-16 12:35:44 -040025 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040026 auto rect = _outer.rect;
Ethan Nicholas82399462017-10-16 12:35:44 -040027 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040028 auto sigma = _outer.sigma;
Ethan Nicholas82399462017-10-16 12:35:44 -040029 (void)sigma;
30 highPrecision = ((((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
31 abs(rect.right()) > 16000.0) ||
32 abs(rect.bottom()) > 16000.0) ||
33 abs(rect.right() - rect.left()) > 16000.0) ||
34 abs(rect.bottom() - rect.top()) > 16000.0;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040035 rectVar =
Ethan Nicholas858fecc2019-03-07 13:19:18 -050036 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, "rect");
Ethan Nicholas82399462017-10-16 12:35:44 -040037 if (!highPrecision) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040038 proxyRectHalfVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
39 kHalf4_GrSLType, "proxyRectHalf");
Ethan Nicholas82399462017-10-16 12:35:44 -040040 }
41 if (highPrecision) {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040042 proxyRectFloatVar = args.fUniformHandler->addUniform(
Ethan Nicholas858fecc2019-03-07 13:19:18 -050043 kFragment_GrShaderFlag, kFloat4_GrSLType, "proxyRectFloat");
Ethan Nicholas82399462017-10-16 12:35:44 -040044 }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040045 profileSizeVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
46 "profileSize");
Ethan Nicholas82399462017-10-16 12:35:44 -040047 fragBuilder->codeAppendf(
Ethan Nicholas00543112018-07-31 09:44:36 -040048 "/* key */ bool highPrecision = %s;\n@if (highPrecision) {\n float2 "
49 "translatedPos = sk_FragCoord.xy - %s.xy;\n float width = %s.z - %s.x;\n "
50 "float height = %s.w - %s.y;\n float2 smallDims = float2(width - float(%s), "
Ethan Nicholase1f55022019-02-05 17:17:40 -050051 "height - float(%s));\n float center = float(2.0 * floor(%s / 2.0 + 0.25) - "
52 "1.0);\n float2 wh = smallDims - float2(center, center);\n half hcoord = "
53 "half((abs(translatedPos.x - 0.5 * width) - 0.5 * wh.x) / float(%s));\n half "
Ethan Nicholas13863662019-07-29 13:05:15 -040054 "hlookup = sample(%s, float2(float(hcoord), 0.5)).%s",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040055 (highPrecision ? "true" : "false"), args.fUniformHandler->getUniformCStr(rectVar),
56 args.fUniformHandler->getUniformCStr(rectVar),
57 args.fUniformHandler->getUniformCStr(rectVar),
58 args.fUniformHandler->getUniformCStr(rectVar),
59 args.fUniformHandler->getUniformCStr(rectVar),
60 args.fUniformHandler->getUniformCStr(profileSizeVar),
61 args.fUniformHandler->getUniformCStr(profileSizeVar),
62 args.fUniformHandler->getUniformCStr(profileSizeVar),
63 args.fUniformHandler->getUniformCStr(profileSizeVar),
Ethan Nicholas13863662019-07-29 13:05:15 -040064 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
65 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -040066 fragBuilder->codeAppendf(
Ethan Nicholas13863662019-07-29 13:05:15 -040067 ".w;\n half vcoord = half((abs(translatedPos.y - 0.5 * height) - 0.5 * wh.y) / "
68 "float(%s));\n half vlookup = sample(%s, float2(float(vcoord), 0.5)).%s.w;\n "
69 "%s = (%s * hlookup) * vlookup;\n} else {\n half2 translatedPos = "
Ethan Nicholase1f55022019-02-05 17:17:40 -050070 "half2(sk_FragCoord.xy - %s.xy);\n half width = half(%s.z - %s.x);\n half "
71 "height = half(%s.w - %s.y);\n half2 smallDims = half2(width - %s, height - "
72 "%s);\n half center = 2.0 * floor(%s / 2.0 + 0.25) - 1.0;\n half2 wh = "
Ethan Nicholas13863662019-07-29 13:05:15 -040073 "smallDims - half2(center, center);\n half hco",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040074 args.fUniformHandler->getUniformCStr(profileSizeVar),
Stephen Whited523a062019-06-19 13:12:46 -040075 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Ethan Nicholas82399462017-10-16 12:35:44 -040076 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040077 args.fOutputColor, args.fInputColor, args.fUniformHandler->getUniformCStr(rectVar),
78 args.fUniformHandler->getUniformCStr(rectVar),
79 args.fUniformHandler->getUniformCStr(rectVar),
80 args.fUniformHandler->getUniformCStr(rectVar),
81 args.fUniformHandler->getUniformCStr(rectVar),
82 args.fUniformHandler->getUniformCStr(profileSizeVar),
83 args.fUniformHandler->getUniformCStr(profileSizeVar),
84 args.fUniformHandler->getUniformCStr(profileSizeVar));
Ethan Nicholas82399462017-10-16 12:35:44 -040085 fragBuilder->codeAppendf(
Ethan Nicholas13863662019-07-29 13:05:15 -040086 "ord = (abs(translatedPos.x - 0.5 * width) - 0.5 * wh.x) / %s;\n half hlookup = "
87 "sample(%s, float2(float(hcoord), 0.5)).%s.w;\n half vcoord = "
Ethan Nicholase1f55022019-02-05 17:17:40 -050088 "(abs(translatedPos.y - 0.5 * height) - 0.5 * wh.y) / %s;\n half vlookup = "
Ethan Nicholas13863662019-07-29 13:05:15 -040089 "sample(%s, float2(float(vcoord), 0.5)).%s.w;\n %s = (%s * hlookup) * "
Ethan Nicholas82399462017-10-16 12:35:44 -040090 "vlookup;\n}\n",
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040091 args.fUniformHandler->getUniformCStr(profileSizeVar),
Stephen Whited523a062019-06-19 13:12:46 -040092 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Ethan Nicholas82399462017-10-16 12:35:44 -040093 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040094 args.fUniformHandler->getUniformCStr(profileSizeVar),
Stephen Whited523a062019-06-19 13:12:46 -040095 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Ethan Nicholas82399462017-10-16 12:35:44 -040096 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
Mike Kleind6ab77a2019-03-21 08:18:24 -050097 args.fOutputColor, args.fInputColor);
Ethan Nicholas82399462017-10-16 12:35:44 -040098 }
99
100private:
101 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500102 const GrFragmentProcessor& _proc) override {
Ethan Nicholas82399462017-10-16 12:35:44 -0400103 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400104 { pdman.set4fv(rectVar, 1, reinterpret_cast<const float*>(&(_outer.rect))); }
105 UniformHandle& rect = rectVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400106 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400107 auto sigma = _outer.sigma;
Ethan Nicholas82399462017-10-16 12:35:44 -0400108 (void)sigma;
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500109 GrSurfaceProxy& blurProfileProxy = *_outer.textureSampler(0).proxy();
Mike Kleind6ab77a2019-03-21 08:18:24 -0500110 GrTexture& blurProfile = *blurProfileProxy.peekTexture();
Ethan Nicholas82399462017-10-16 12:35:44 -0400111 (void)blurProfile;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400112 UniformHandle& proxyRectHalf = proxyRectHalfVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400113 (void)proxyRectHalf;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400114 UniformHandle& proxyRectFloat = proxyRectFloatVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400115 (void)proxyRectFloat;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400116 UniformHandle& profileSize = profileSizeVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400117 (void)profileSize;
118
119 pdman.set1f(profileSize, SkScalarCeilToScalar(6 * sigma));
120 }
Mike Kleind6ab77a2019-03-21 08:18:24 -0500121 bool highPrecision = false;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400122 UniformHandle proxyRectHalfVar;
123 UniformHandle proxyRectFloatVar;
124 UniformHandle profileSizeVar;
125 UniformHandle rectVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400126};
127GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
128 return new GrGLSLRectBlurEffect();
129}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500130void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholascab767f2019-07-01 13:32:07 -0400131 GrProcessorKeyBuilder* b) const {
132 bool highPrecision = ((((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
133 abs(rect.right()) > 16000.0) ||
134 abs(rect.bottom()) > 16000.0) ||
135 abs(rect.right() - rect.left()) > 16000.0) ||
136 abs(rect.bottom() - rect.top()) > 16000.0;
137 b->add32((int32_t)highPrecision);
138}
Ethan Nicholas82399462017-10-16 12:35:44 -0400139bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
140 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
141 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400142 if (rect != that.rect) return false;
143 if (sigma != that.sigma) return false;
144 if (blurProfile != that.blurProfile) return false;
Ethan Nicholas82399462017-10-16 12:35:44 -0400145 return true;
146}
147GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
148 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400149 , rect(src.rect)
150 , sigma(src.sigma)
151 , blurProfile(src.blurProfile) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400152 this->setTextureSamplerCnt(1);
Ethan Nicholas82399462017-10-16 12:35:44 -0400153}
154std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
155 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this));
156}
Brian Salomonf7dcd762018-07-30 14:48:15 -0400157const GrFragmentProcessor::TextureSampler& GrRectBlurEffect::onTextureSampler(int index) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400158 return IthTextureSampler(index, blurProfile);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400159}
Ethan Nicholas82399462017-10-16 12:35:44 -0400160GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
161#if GR_TEST_UTILS
162std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500163 float sigma = data->fRandom->nextRangeF(3, 8);
164 float width = data->fRandom->nextRangeF(200, 300);
Ethan Nicholas82399462017-10-16 12:35:44 -0400165 float height = data->fRandom->nextRangeF(200, 300);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500166 return GrRectBlurEffect::Make(data->proxyProvider(), *data->caps()->shaderCaps(),
167 SkRect::MakeWH(width, height), sigma);
Ethan Nicholas82399462017-10-16 12:35:44 -0400168}
169#endif