blob: 4ad19798d341cd785f40f8d80364d0ac7b15da0b [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
Greg Daniel456f9b52020-03-05 19:14:18 +000013#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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;
Brian Salomonb2d5d402019-09-10 10:11:52 -040028 auto invSixSigma = _outer.invSixSigma;
29 (void)invSixSigma;
30 auto isFast = _outer.isFast;
31 (void)isFast;
Brian Salomon2c596592019-08-13 20:05:04 -040032 highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
33 abs(rect.right()) > 16000.0) ||
34 abs(rect.bottom()) > 16000.0;
35 if (highp) {
Ethan Nicholas16464c32020-04-06 13:53:05 -040036 rectFVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
37 kFloat4_GrSLType, "rectF");
Ethan Nicholas82399462017-10-16 12:35:44 -040038 }
Brian Salomon2c596592019-08-13 20:05:04 -040039 if (!highp) {
Ethan Nicholas16464c32020-04-06 13:53:05 -040040 rectHVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
41 kHalf4_GrSLType, "rectH");
Ethan Nicholas82399462017-10-16 12:35:44 -040042 }
Ethan Nicholas16464c32020-04-06 13:53:05 -040043 invSixSigmaVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
44 kHalf_GrSLType, "invSixSigma");
Ethan Nicholas82399462017-10-16 12:35:44 -040045 fragBuilder->codeAppendf(
John Stilese7b4d732020-06-10 12:46:40 -040046 "/* key */ bool highp = %s;\nhalf xCoverage, yCoverage;\n@if (%s) {\n half2 "
47 "xy;\n @if (highp) {\n xy = max(half2(%s.xy - sk_FragCoord.xy), "
48 "half2(sk_FragCoord.xy - %s.zw));\n } else {\n xy = "
49 "max(half2(float2(%s.xy) - sk_FragCoord.xy), half2(sk_FragCoord.xy - "
50 "float2(%s.zw)));\n }\n xy *= %s;\n xCoverage = sample(%s, "
51 "float2(half2(xy.x, 0.5))).%s.w;\n yCoverage = sample(%s, float2(half2(xy.y, "
52 "0.5))).%s.w;\n} else {\n half4 rect;\n @if (highp) {\n rect.xy = "
53 "half2(sk",
Brian Salomonb2d5d402019-09-10 10:11:52 -040054 (highp ? "true" : "false"), (_outer.isFast ? "true" : "false"),
55 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
56 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
Brian Salomon2c596592019-08-13 20:05:04 -040057 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
Brian Salomon5331e2a2019-08-19 17:18:10 -040058 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
John Stilese7b4d732020-06-10 12:46:40 -040059 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
Brian Salomone7366842019-09-04 11:20:45 -040060 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
John Stilese7b4d732020-06-10 12:46:40 -040061 fragBuilder->getProgramBuilder()
62 ->samplerSwizzle(args.fTexSamplers[0])
63 .asString()
64 .c_str(),
65 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
66 fragBuilder->getProgramBuilder()
67 ->samplerSwizzle(args.fTexSamplers[0])
68 .asString()
69 .c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -040070 fragBuilder->codeAppendf(
John Stilese7b4d732020-06-10 12:46:40 -040071 "_FragCoord.xy - %s.xy);\n rect.zw = half2(%s.zw - sk_FragCoord.xy);\n } "
72 "else {\n rect.xy = half2(sk_FragCoord.xy - float2(%s.xy));\n "
73 "rect.zw = half2(float2(%s.zw) - sk_FragCoord.xy);\n }\n half4 irect = rect "
74 "* %s + half4(1.0);\n xCoverage = (1.0 - sample(%s, float2(half2(irect.x, "
75 "0.5))).%s.w) - sample(%s, float2(half2(irect.z, 0.5))).%s.w;\n yCoverage = "
76 "(1.0 - sample(%s, float2(half2(irect.y, 0.5))).%s.w) - sample(%s, "
77 "float2(half2(irect.w, 0.5))).%s.w;\n}",
78 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
79 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
80 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
81 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
82 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
83 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Greg Daniel369ee6b2019-12-02 15:30:02 -050084 fragBuilder->getProgramBuilder()
85 ->samplerSwizzle(args.fTexSamplers[0])
86 .asString()
87 .c_str(),
Brian Salomonb2d5d402019-09-10 10:11:52 -040088 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
Greg Daniel369ee6b2019-12-02 15:30:02 -050089 fragBuilder->getProgramBuilder()
90 ->samplerSwizzle(args.fTexSamplers[0])
91 .asString()
92 .c_str(),
John Stilese7b4d732020-06-10 12:46:40 -040093 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
94 fragBuilder->getProgramBuilder()
95 ->samplerSwizzle(args.fTexSamplers[0])
96 .asString()
97 .c_str(),
98 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
99 fragBuilder->getProgramBuilder()
100 ->samplerSwizzle(args.fTexSamplers[0])
101 .asString()
102 .c_str());
103 SkString _input8899 = SkStringPrintf("%s", args.fInputColor);
104 SkString _sample8899;
105 if (_outer.inputFP_index >= 0) {
106 _sample8899 = this->invokeChild(_outer.inputFP_index, _input8899.c_str(), args);
107 } else {
108 _sample8899 = _input8899;
109 }
Brian Salomonb2d5d402019-09-10 10:11:52 -0400110 fragBuilder->codeAppendf(
John Stilese7b4d732020-06-10 12:46:40 -0400111 "\nhalf4 inputColor = %s;\n%s = (inputColor * xCoverage) * yCoverage;\n",
112 _sample8899.c_str(), args.fOutputColor);
Ethan Nicholas82399462017-10-16 12:35:44 -0400113 }
114
115private:
116 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500117 const GrFragmentProcessor& _proc) override {
Ethan Nicholas82399462017-10-16 12:35:44 -0400118 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
Brian Salomonb2d5d402019-09-10 10:11:52 -0400119 { pdman.set1f(invSixSigmaVar, (_outer.invSixSigma)); }
Brian Salomon2c596592019-08-13 20:05:04 -0400120 auto rect = _outer.rect;
Ethan Nicholas82399462017-10-16 12:35:44 -0400121 (void)rect;
Brian Salomon2c596592019-08-13 20:05:04 -0400122 UniformHandle& rectF = rectFVar;
123 (void)rectF;
124 UniformHandle& rectH = rectHVar;
125 (void)rectH;
Robert Phillipsbd99c0c2019-12-12 13:26:58 +0000126 const GrSurfaceProxyView& integralView = _outer.textureSampler(0).view();
127 GrTexture& integral = *integralView.proxy()->peekTexture();
Brian Salomonb2d5d402019-09-10 10:11:52 -0400128 (void)integral;
129 UniformHandle& invSixSigma = invSixSigmaVar;
130 (void)invSixSigma;
131 auto isFast = _outer.isFast;
132 (void)isFast;
Ethan Nicholas82399462017-10-16 12:35:44 -0400133
Brian Salomon2c596592019-08-13 20:05:04 -0400134 float r[]{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
135 pdman.set4fv(highp ? rectF : rectH, 1, r);
Ethan Nicholas82399462017-10-16 12:35:44 -0400136 }
Brian Salomon2c596592019-08-13 20:05:04 -0400137 bool highp = false;
138 UniformHandle rectFVar;
139 UniformHandle rectHVar;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400140 UniformHandle invSixSigmaVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400141};
142GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
143 return new GrGLSLRectBlurEffect();
144}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500145void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholascab767f2019-07-01 13:32:07 -0400146 GrProcessorKeyBuilder* b) const {
Brian Salomon2c596592019-08-13 20:05:04 -0400147 bool highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
148 abs(rect.right()) > 16000.0) ||
149 abs(rect.bottom()) > 16000.0;
150 b->add32((int32_t)highp);
Brian Salomonb2d5d402019-09-10 10:11:52 -0400151 b->add32((int32_t)isFast);
Ethan Nicholascab767f2019-07-01 13:32:07 -0400152}
Ethan Nicholas82399462017-10-16 12:35:44 -0400153bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
154 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
155 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400156 if (rect != that.rect) return false;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400157 if (integral != that.integral) return false;
158 if (invSixSigma != that.invSixSigma) return false;
159 if (isFast != that.isFast) return false;
Ethan Nicholas82399462017-10-16 12:35:44 -0400160 return true;
161}
162GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
163 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400164 , rect(src.rect)
Brian Salomonb2d5d402019-09-10 10:11:52 -0400165 , integral(src.integral)
166 , invSixSigma(src.invSixSigma)
167 , isFast(src.isFast) {
John Stiles88183902020-06-10 16:40:38 -0400168 if (src.inputFP_index >= 0) {
169 auto inputFP_clone = src.childProcessor(src.inputFP_index).clone();
170 if (src.childProcessor(src.inputFP_index).isSampledWithExplicitCoords()) {
171 inputFP_clone->setSampledWithExplicitCoords();
John Stilese7b4d732020-06-10 12:46:40 -0400172 }
John Stiles88183902020-06-10 16:40:38 -0400173 inputFP_index = this->registerChildProcessor(std::move(inputFP_clone));
John Stilese7b4d732020-06-10 12:46:40 -0400174 }
Brian Salomone7366842019-09-04 11:20:45 -0400175 this->setTextureSamplerCnt(1);
176}
Ethan Nicholas82399462017-10-16 12:35:44 -0400177std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
178 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this));
179}
Brian Salomone7366842019-09-04 11:20:45 -0400180const GrFragmentProcessor::TextureSampler& GrRectBlurEffect::onTextureSampler(int index) const {
Brian Salomonb2d5d402019-09-10 10:11:52 -0400181 return IthTextureSampler(index, integral);
Brian Salomone7366842019-09-04 11:20:45 -0400182}
Ethan Nicholas82399462017-10-16 12:35:44 -0400183GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
184#if GR_TEST_UTILS
185std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500186 float sigma = data->fRandom->nextRangeF(3, 8);
187 float width = data->fRandom->nextRangeF(200, 300);
Ethan Nicholas82399462017-10-16 12:35:44 -0400188 float height = data->fRandom->nextRangeF(200, 300);
John Stilese7b4d732020-06-10 12:46:40 -0400189 return GrRectBlurEffect::Make(/*inputFP=*/nullptr, data->context(), *data->caps()->shaderCaps(),
Mike Kleind6ab77a2019-03-21 08:18:24 -0500190 SkRect::MakeWH(width, height), sigma);
Ethan Nicholas82399462017-10-16 12:35:44 -0400191}
192#endif