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 "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"
19 class GrGLSLRectBlurEffect : public GrGLSLFragmentProcessor {
20 public:
GrGLSLRectBlurEffect()21 GrGLSLRectBlurEffect() {}
emitCode(EmitArgs & args)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 sigma = _outer.sigma;
29 (void)sigma;
30 highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
31 abs(rect.right()) > 16000.0) ||
32 abs(rect.bottom()) > 16000.0;
33 if (highp) {
34 rectFVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
35 "rectF");
36 }
37 if (!highp) {
38 rectHVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
39 "rectH");
40 }
41 sigmaVar =
42 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "sigma");
43 fragBuilder->codeAppendf(
44 "/* key */ bool highp = %s;\nhalf invr = 1.0 / (2.0 * %s);\nhalf x;\n@if (highp) "
45 "{\n float lDiff = %s.x - sk_FragCoord.x;\n float rDiff = sk_FragCoord.x - "
46 "%s.z;\n x = half(max(lDiff, rDiff) * float(invr));\n} else {\n half lDiff = "
47 "half(float(%s.x) - sk_FragCoord.x);\n half rDiff = half(sk_FragCoord.x - "
48 "float(%s.z));\n x = max(lDiff, rDiff) * invr;\n}\nhalf xCoverage;\nif (x > "
49 "1.5) {\n xCoverage = 0.0;\n} else if (x < -1.5) {\n xCoverage = 1.0;\n} "
50 "else {\n half x2 = x * x;\n half",
51 (highp ? "true" : "false"), args.fUniformHandler->getUniformCStr(sigmaVar),
52 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
53 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
54 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
55 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
56 fragBuilder->codeAppendf(
57 " x3 = x2 * x;\n if (x > 0.5) {\n xCoverage = 0.5625 - ((x3 / 6.0 - (3.0 "
58 "* x2) * 0.25) + 1.125 * x);\n } else if (x > -0.5) {\n xCoverage = 0.5 "
59 "- (0.75 * x - x3 / 3.0);\n } else {\n xCoverage = 0.4375 + ((-x3 / 6.0 "
60 "- (3.0 * x2) * 0.25) - 1.125 * x);\n }\n}\nhalf y;\n@if (highp) {\n float "
61 "tDiff = %s.y - sk_FragCoord.y;\n float bDiff = sk_FragCoord.y - %s.w;\n y = "
62 "half(max(tDiff, bDiff) * float(invr));\n} else {\n half tDiff = "
63 "half(float(%s.y) - sk_FragCoord.y);\n ",
64 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
65 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
66 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
67 fragBuilder->codeAppendf(
68 " half bDiff = half(sk_FragCoord.y - float(%s.w));\n y = max(tDiff, bDiff) * "
69 "invr;\n}\nhalf yCoverage;\nif (y > 1.5) {\n yCoverage = 0.0;\n} else if (y < "
70 "-1.5) {\n yCoverage = 1.0;\n} else {\n half y2 = y * y;\n half y3 = y2 * "
71 "y;\n if (y > 0.5) {\n yCoverage = 0.5625 - ((y3 / 6.0 - (3.0 * y2) * "
72 "0.25) + 1.125 * y);\n } else if (y > -0.5) {\n yCoverage = 0.5 - (0.75 "
73 "* y - y3 / 3.0);\n } else {\n yCoverage = 0.4375 + ((-y3 / 6.0 - (3.0 * "
74 "y2) * 0.25) - 1.125 * y);\n ",
75 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
76 fragBuilder->codeAppendf(" }\n}\n%s = (%s * xCoverage) * yCoverage;\n", args.fOutputColor,
77 args.fInputColor);
78 }
79
80 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)81 void onSetData(const GrGLSLProgramDataManager& pdman,
82 const GrFragmentProcessor& _proc) override {
83 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
84 { pdman.set1f(sigmaVar, (_outer.sigma)); }
85 auto rect = _outer.rect;
86 (void)rect;
87 UniformHandle& rectF = rectFVar;
88 (void)rectF;
89 UniformHandle& rectH = rectHVar;
90 (void)rectH;
91 UniformHandle& sigma = sigmaVar;
92 (void)sigma;
93
94 float r[]{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
95 pdman.set4fv(highp ? rectF : rectH, 1, r);
96 }
97 bool highp = false;
98 UniformHandle rectFVar;
99 UniformHandle rectHVar;
100 UniformHandle sigmaVar;
101 };
onCreateGLSLInstance() const102 GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
103 return new GrGLSLRectBlurEffect();
104 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const105 void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
106 GrProcessorKeyBuilder* b) const {
107 bool highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
108 abs(rect.right()) > 16000.0) ||
109 abs(rect.bottom()) > 16000.0;
110 b->add32((int32_t)highp);
111 }
onIsEqual(const GrFragmentProcessor & other) const112 bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
113 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
114 (void)that;
115 if (rect != that.rect) return false;
116 if (sigma != that.sigma) return false;
117 return true;
118 }
GrRectBlurEffect(const GrRectBlurEffect & src)119 GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
120 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
121 , rect(src.rect)
122 , sigma(src.sigma) {}
clone() const123 std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
124 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this));
125 }
126 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
127 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * data)128 std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
129 float sigma = data->fRandom->nextRangeF(3, 8);
130 float width = data->fRandom->nextRangeF(200, 300);
131 float height = data->fRandom->nextRangeF(200, 300);
132 return GrRectBlurEffect::Make(data->proxyProvider(), *data->caps()->shaderCaps(),
133 SkRect::MakeWH(width, height), sigma);
134 }
135 #endif
136