blob: 1dfd3046cea0e93bf5cc81536d698ffee5319bdf [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;
Brian Salomon2c596592019-08-13 20:05:04 -040030 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");
Ethan Nicholas82399462017-10-16 12:35:44 -040036 }
Brian Salomon2c596592019-08-13 20:05:04 -040037 if (!highp) {
38 rectHVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
39 "rectH");
Ethan Nicholas82399462017-10-16 12:35:44 -040040 }
Brian Salomon2c596592019-08-13 20:05:04 -040041 sigmaVar =
42 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "sigma");
Ethan Nicholas82399462017-10-16 12:35:44 -040043 fragBuilder->codeAppendf(
Brian Salomon2c596592019-08-13 20:05:04 -040044 "/* 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)");
Ethan Nicholas82399462017-10-16 12:35:44 -040056 fragBuilder->codeAppendf(
Brian Salomon2c596592019-08-13 20:05:04 -040057 " 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)");
Ethan Nicholas82399462017-10-16 12:35:44 -040067 fragBuilder->codeAppendf(
Brian Salomon2c596592019-08-13 20:05:04 -040068 " 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);
Ethan Nicholas82399462017-10-16 12:35:44 -040078 }
79
80private:
81 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050082 const GrFragmentProcessor& _proc) override {
Ethan Nicholas82399462017-10-16 12:35:44 -040083 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
Brian Salomon2c596592019-08-13 20:05:04 -040084 { pdman.set1f(sigmaVar, (_outer.sigma)); }
85 auto rect = _outer.rect;
Ethan Nicholas82399462017-10-16 12:35:44 -040086 (void)rect;
Brian Salomon2c596592019-08-13 20:05:04 -040087 UniformHandle& rectF = rectFVar;
88 (void)rectF;
89 UniformHandle& rectH = rectHVar;
90 (void)rectH;
91 UniformHandle& sigma = sigmaVar;
Ethan Nicholas82399462017-10-16 12:35:44 -040092 (void)sigma;
Ethan Nicholas82399462017-10-16 12:35:44 -040093
Brian Salomon2c596592019-08-13 20:05:04 -040094 float r[]{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
95 pdman.set4fv(highp ? rectF : rectH, 1, r);
Ethan Nicholas82399462017-10-16 12:35:44 -040096 }
Brian Salomon2c596592019-08-13 20:05:04 -040097 bool highp = false;
98 UniformHandle rectFVar;
99 UniformHandle rectHVar;
100 UniformHandle sigmaVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400101};
102GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
103 return new GrGLSLRectBlurEffect();
104}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500105void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholascab767f2019-07-01 13:32:07 -0400106 GrProcessorKeyBuilder* b) const {
Brian Salomon2c596592019-08-13 20:05:04 -0400107 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);
Ethan Nicholascab767f2019-07-01 13:32:07 -0400111}
Ethan Nicholas82399462017-10-16 12:35:44 -0400112bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
113 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
114 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400115 if (rect != that.rect) return false;
116 if (sigma != that.sigma) return false;
Ethan Nicholas82399462017-10-16 12:35:44 -0400117 return true;
118}
119GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
120 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400121 , rect(src.rect)
Brian Salomon2c596592019-08-13 20:05:04 -0400122 , sigma(src.sigma) {}
Ethan Nicholas82399462017-10-16 12:35:44 -0400123std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
124 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this));
125}
126GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
127#if GR_TEST_UTILS
128std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500129 float sigma = data->fRandom->nextRangeF(3, 8);
130 float width = data->fRandom->nextRangeF(200, 300);
Ethan Nicholas82399462017-10-16 12:35:44 -0400131 float height = data->fRandom->nextRangeF(200, 300);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500132 return GrRectBlurEffect::Make(data->proxyProvider(), *data->caps()->shaderCaps(),
133 SkRect::MakeWH(width, height), sigma);
Ethan Nicholas82399462017-10-16 12:35:44 -0400134}
135#endif