1/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8/**************************************************************************************************
9 *** This file was autogenerated from GrRectBlurEffect.fp; do not modify.
10 **************************************************************************************************/
11#include "GrRectBlurEffect.h"
12
13#include "src/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"
19class GrGLSLRectBlurEffect : public GrGLSLFragmentProcessor {
20public:
21 GrGLSLRectBlurEffect() {}
22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrRectBlurEffect& _outer = args.fFp.cast<GrRectBlurEffect>();
25 (void)_outer;
26 auto rect = _outer.rect;
27 (void)rect;
28 auto invSixSigma = _outer.invSixSigma;
29 (void)invSixSigma;
30 auto isFast = _outer.isFast;
31 (void)isFast;
32 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) {
36 rectFVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
37 kFloat4_GrSLType, "rectF");
38 }
39 if (!highp) {
40 rectHVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
41 kHalf4_GrSLType, "rectH");
42 }
43 invSixSigmaVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
44 kHalf_GrSLType, "invSixSigma");
45 fragBuilder->codeAppendf(
46 "/* key */ bool highp = %s;\nhalf xCoverage, yCoverage;\n@if (%s) {\n half x, "
47 "y;\n @if (highp) {\n x = max(half(%s.x - sk_FragCoord.x), "
48 "half(sk_FragCoord.x - %s.z));\n y = max(half(%s.y - sk_FragCoord.y), "
49 "half(sk_FragCoord.y - %s.w));\n } else {\n x = max(half(float(%s.x) - "
50 "sk_FragCoord.x), half(sk_FragCoord.x - float(%s.z)));\n y = "
51 "max(half(float(%s.y) - sk_FragCoord.y), half(sk_FragCoord.y - float(%s.w)));\n "
52 "}\n xCoverage = sample(%s, float2(half2(x * %s, 0.5))).",
53 (highp ? "true" : "false"), (_outer.isFast ? "true" : "false"),
54 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
55 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
56 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
57 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
58 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
59 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
60 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
61 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
62 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
63 args.fUniformHandler->getUniformCStr(invSixSigmaVar));
64 fragBuilder->codeAppendf(
65 "%s.w;\n yCoverage = sample(%s, float2(half2(y * %s, 0.5))).%s.w;\n %s = (%s "
66 "* xCoverage) * yCoverage;\n} else {\n half l, r, t, b;\n @if (highp) {\n "
67 " l = half(sk_FragCoord.x - %s.x);\n r = half(%s.z - sk_FragCoord.x);\n "
68 " t = half(sk_FragCoord.y - %s.y);\n b = half(%s.w - "
69 "sk_FragCoord.y);\n } else {\n l = half(sk_FragCoord.x - float(%s.x));\n "
70 " r = half(float(%s.z) - sk_FragCoord.x);\n t = half(sk_FragCoord.y - "
71 "float(%s.y));\n b = half(float(",
72 fragBuilder->getProgramBuilder()
73 ->samplerSwizzle(args.fTexSamplers[0])
74 .asString()
75 .c_str(),
76 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
77 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
78 fragBuilder->getProgramBuilder()
79 ->samplerSwizzle(args.fTexSamplers[0])
80 .asString()
81 .c_str(),
82 args.fOutputColor, args.fInputColor,
83 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
84 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
85 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
86 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
87 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
88 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
89 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
90 fragBuilder->codeAppendf(
91 "%s.w) - sk_FragCoord.y);\n }\n half il = 1.0 + l * %s;\n half ir = 1.0 + "
92 "r * %s;\n half it = 1.0 + t * %s;\n half ib = 1.0 + b * %s;\n xCoverage "
93 "= (1.0 - sample(%s, float2(half2(il, 0.5))).%s.w) - sample(%s, float2(half2(ir, "
94 "0.5))).%s.w;\n yCoverage = (1.0 - sample(%s, float2(half2(it, 0.5))).%s.w) - "
95 "sample(%s, float2(half2(ib, 0.5))).%s.w;\n}\n%s = (%s * xCoverage) * yCoverage;\n",
96 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
97 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
98 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
99 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
100 args.fUniformHandler->getUniformCStr(invSixSigmaVar),
101 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
102 fragBuilder->getProgramBuilder()
103 ->samplerSwizzle(args.fTexSamplers[0])
104 .asString()
105 .c_str(),
106 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
107 fragBuilder->getProgramBuilder()
108 ->samplerSwizzle(args.fTexSamplers[0])
109 .asString()
110 .c_str(),
111 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
112 fragBuilder->getProgramBuilder()
113 ->samplerSwizzle(args.fTexSamplers[0])
114 .asString()
115 .c_str(),
116 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
117 fragBuilder->getProgramBuilder()
118 ->samplerSwizzle(args.fTexSamplers[0])
119 .asString()
120 .c_str(),
121 args.fOutputColor, args.fInputColor);
122 }
123
124private:
125 void onSetData(const GrGLSLProgramDataManager& pdman,
126 const GrFragmentProcessor& _proc) override {
127 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
128 { pdman.set1f(invSixSigmaVar, (_outer.invSixSigma)); }
129 auto rect = _outer.rect;
130 (void)rect;
131 UniformHandle& rectF = rectFVar;
132 (void)rectF;
133 UniformHandle& rectH = rectHVar;
134 (void)rectH;
135 const GrSurfaceProxyView& integralView = _outer.textureSampler(0).view();
136 GrTexture& integral = *integralView.proxy()->peekTexture();
137 (void)integral;
138 UniformHandle& invSixSigma = invSixSigmaVar;
139 (void)invSixSigma;
140 auto isFast = _outer.isFast;
141 (void)isFast;
142
143 float r[]{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
144 pdman.set4fv(highp ? rectF : rectH, 1, r);
145 }
146 bool highp = false;
147 UniformHandle rectFVar;
148 UniformHandle rectHVar;
149 UniformHandle invSixSigmaVar;
150};
151GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
152 return new GrGLSLRectBlurEffect();
153}
154void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
155 GrProcessorKeyBuilder* b) const {
156 bool highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
157 abs(rect.right()) > 16000.0) ||
158 abs(rect.bottom()) > 16000.0;
159 b->add32((int32_t)highp);
160 b->add32((int32_t)isFast);
161}
162bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
163 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
164 (void)that;
165 if (rect != that.rect) return false;
166 if (integral != that.integral) return false;
167 if (invSixSigma != that.invSixSigma) return false;
168 if (isFast != that.isFast) return false;
169 return true;
170}
171GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
172 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
173 , rect(src.rect)
174 , integral(src.integral)
175 , invSixSigma(src.invSixSigma)
176 , isFast(src.isFast) {
177 this->setTextureSamplerCnt(1);
178}
179std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
180 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this));
181}
182const GrFragmentProcessor::TextureSampler& GrRectBlurEffect::onTextureSampler(int index) const {
183 return IthTextureSampler(index, integral);
184}
185GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
186#if GR_TEST_UTILS
187std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
188 float sigma = data->fRandom->nextRangeF(3, 8);
189 float width = data->fRandom->nextRangeF(200, 300);
190 float height = data->fRandom->nextRangeF(200, 300);
191 return GrRectBlurEffect::Make(data->context(), *data->caps()->shaderCaps(),
192 SkRect::MakeWH(width, height), sigma);
193}
194#endif
195