blob: 9763f3fdf4df26d6be30919c650f956e9dffcfa8 [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
John Stiles45f5b032020-07-27 17:31:29 -040013#include "src/core/SkUtils.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
18#include "src/sksl/SkSLCPP.h"
19#include "src/sksl/SkSLUtil.h"
Ethan Nicholas82399462017-10-16 12:35:44 -040020class GrGLSLRectBlurEffect : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLRectBlurEffect() {}
23 void emitCode(EmitArgs& args) override {
24 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050025 const GrRectBlurEffect& _outer = args.fFp.cast<GrRectBlurEffect>();
Ethan Nicholas82399462017-10-16 12:35:44 -040026 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040027 auto rect = _outer.rect;
Ethan Nicholas82399462017-10-16 12:35:44 -040028 (void)rect;
Brian Salomon96c8aeb2020-09-23 11:37:13 -040029 auto applyInvVM = _outer.applyInvVM;
30 (void)applyInvVM;
31 auto invVM = _outer.invVM;
32 (void)invVM;
Brian Salomonb2d5d402019-09-10 10:11:52 -040033 auto isFast = _outer.isFast;
34 (void)isFast;
Brian Salomon2c596592019-08-13 20:05:04 -040035 highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
36 abs(rect.right()) > 16000.0) ||
37 abs(rect.bottom()) > 16000.0;
38 if (highp) {
Ethan Nicholas16464c32020-04-06 13:53:05 -040039 rectFVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
40 kFloat4_GrSLType, "rectF");
Ethan Nicholas82399462017-10-16 12:35:44 -040041 }
Brian Salomon2c596592019-08-13 20:05:04 -040042 if (!highp) {
Ethan Nicholas16464c32020-04-06 13:53:05 -040043 rectHVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
44 kHalf4_GrSLType, "rectH");
Ethan Nicholas82399462017-10-16 12:35:44 -040045 }
Brian Salomon96c8aeb2020-09-23 11:37:13 -040046 if (applyInvVM) {
47 invVMVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
48 kFloat3x3_GrSLType, "invVM");
49 }
Ethan Nicholas82399462017-10-16 12:35:44 -040050 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040051 R"SkSL(/* key */ bool highp = %s;
Brian Osmanc0213602020-10-06 14:43:32 -040052half xCoverage;
53half yCoverage;
54
Brian Salomon96c8aeb2020-09-23 11:37:13 -040055float2 pos = sk_FragCoord.xy;
56@if (%s) {
57 pos = (%s * float3(pos, 1.0)).xy;
58}
John Stiles50819422020-06-18 13:00:38 -040059@if (%s) {
60 half2 xy;
61 @if (highp) {
Brian Salomon96c8aeb2020-09-23 11:37:13 -040062 xy = max(half2(%s.xy - pos), half2(pos - %s.zw));
John Stiles50819422020-06-18 13:00:38 -040063 } else {
Brian Salomon96c8aeb2020-09-23 11:37:13 -040064 xy = max(half2(float2(%s.xy) - pos), half2(pos - float2(%s.zw)));
John Stiles50819422020-06-18 13:00:38 -040065 })SkSL",
Brian Salomon96c8aeb2020-09-23 11:37:13 -040066 (highp ? "true" : "false"), (_outer.applyInvVM ? "true" : "false"),
67 invVMVar.isValid() ? args.fUniformHandler->getUniformCStr(invVMVar) : "float3x3(1)",
68 (_outer.isFast ? "true" : "false"),
Brian Salomonb2d5d402019-09-10 10:11:52 -040069 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
70 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
Brian Salomon2c596592019-08-13 20:05:04 -040071 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
Brian Salomon86b2f392020-06-16 16:01:07 -040072 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
Robert Phillipsa6919c62020-09-30 11:20:13 -040073 SkString _coords8175("float2(half2(xy.x, 0.5))");
74 SkString _sample8175 = this->invokeChild(1, args, _coords8175.c_str());
John Stiles50819422020-06-18 13:00:38 -040075 fragBuilder->codeAppendf(
76 R"SkSL(
77 xCoverage = %s.w;)SkSL",
Robert Phillipsa6919c62020-09-30 11:20:13 -040078 _sample8175.c_str());
79 SkString _coords8233("float2(half2(xy.y, 0.5))");
80 SkString _sample8233 = this->invokeChild(1, args, _coords8233.c_str());
Ethan Nicholas82399462017-10-16 12:35:44 -040081 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040082 R"SkSL(
83 yCoverage = %s.w;
84} else {
85 half4 rect;
86 @if (highp) {
Brian Salomon96c8aeb2020-09-23 11:37:13 -040087 rect.xy = half2(%s.xy - pos);
88 rect.zw = half2(pos - %s.zw);
John Stiles50819422020-06-18 13:00:38 -040089 } else {
Brian Salomon96c8aeb2020-09-23 11:37:13 -040090 rect.xy = half2(float2(%s.xy) - pos);
91 rect.zw = half2(pos - float2(%s.zw));
John Stiles50819422020-06-18 13:00:38 -040092 })SkSL",
Robert Phillipsa6919c62020-09-30 11:20:13 -040093 _sample8233.c_str(),
John Stilese7b4d732020-06-10 12:46:40 -040094 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
95 rectFVar.isValid() ? args.fUniformHandler->getUniformCStr(rectFVar) : "float4(0)",
96 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)",
Brian Salomon86b2f392020-06-16 16:01:07 -040097 rectHVar.isValid() ? args.fUniformHandler->getUniformCStr(rectHVar) : "half4(0)");
Robert Phillipsa6919c62020-09-30 11:20:13 -040098 SkString _coords9552("float2(half2(rect.x, 0.5))");
99 SkString _sample9552 = this->invokeChild(1, args, _coords9552.c_str());
100 SkString _coords9615("float2(half2(rect.z, 0.5))");
101 SkString _sample9615 = this->invokeChild(1, args, _coords9615.c_str());
John Stiles50819422020-06-18 13:00:38 -0400102 fragBuilder->codeAppendf(
103 R"SkSL(
104 xCoverage = (1.0 - %s.w) - %s.w;)SkSL",
Robert Phillipsa6919c62020-09-30 11:20:13 -0400105 _sample9552.c_str(), _sample9615.c_str());
106 SkString _coords9679("float2(half2(rect.y, 0.5))");
107 SkString _sample9679 = this->invokeChild(1, args, _coords9679.c_str());
108 SkString _coords9742("float2(half2(rect.w, 0.5))");
109 SkString _sample9742 = this->invokeChild(1, args, _coords9742.c_str());
John Stiles50819422020-06-18 13:00:38 -0400110 fragBuilder->codeAppendf(
111 R"SkSL(
112 yCoverage = (1.0 - %s.w) - %s.w;
113})SkSL",
Robert Phillipsa6919c62020-09-30 11:20:13 -0400114 _sample9679.c_str(), _sample9742.c_str());
115 SkString _sample9811 = this->invokeChild(0, args);
Brian Salomonb2d5d402019-09-10 10:11:52 -0400116 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -0400117 R"SkSL(
118half4 inputColor = %s;
119%s = (inputColor * xCoverage) * yCoverage;
120)SkSL",
Robert Phillipsa6919c62020-09-30 11:20:13 -0400121 _sample9811.c_str(), args.fOutputColor);
Ethan Nicholas82399462017-10-16 12:35:44 -0400122 }
123
124private:
125 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -0500126 const GrFragmentProcessor& _proc) override {
Ethan Nicholas82399462017-10-16 12:35:44 -0400127 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400128 {
129 if (invVMVar.isValid()) {
130 static_assert(1 == 1);
131 pdman.setSkMatrix(invVMVar, (_outer.invVM));
132 }
133 }
Brian Salomon2c596592019-08-13 20:05:04 -0400134 auto rect = _outer.rect;
Ethan Nicholas82399462017-10-16 12:35:44 -0400135 (void)rect;
Brian Salomon2c596592019-08-13 20:05:04 -0400136 UniformHandle& rectF = rectFVar;
137 (void)rectF;
138 UniformHandle& rectH = rectHVar;
139 (void)rectH;
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400140 auto applyInvVM = _outer.applyInvVM;
141 (void)applyInvVM;
142 UniformHandle& invVM = invVMVar;
143 (void)invVM;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400144 auto isFast = _outer.isFast;
145 (void)isFast;
Ethan Nicholas82399462017-10-16 12:35:44 -0400146
Brian Salomon2c596592019-08-13 20:05:04 -0400147 float r[]{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
148 pdman.set4fv(highp ? rectF : rectH, 1, r);
Ethan Nicholas82399462017-10-16 12:35:44 -0400149 }
Brian Salomon2c596592019-08-13 20:05:04 -0400150 bool highp = false;
151 UniformHandle rectFVar;
152 UniformHandle rectHVar;
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400153 UniformHandle invVMVar;
Ethan Nicholas82399462017-10-16 12:35:44 -0400154};
155GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
156 return new GrGLSLRectBlurEffect();
157}
Mike Kleind6ab77a2019-03-21 08:18:24 -0500158void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholascab767f2019-07-01 13:32:07 -0400159 GrProcessorKeyBuilder* b) const {
Brian Salomon2c596592019-08-13 20:05:04 -0400160 bool highp = ((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
161 abs(rect.right()) > 16000.0) ||
162 abs(rect.bottom()) > 16000.0;
John Stiles45f5b032020-07-27 17:31:29 -0400163 b->add32((uint32_t)highp);
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400164 b->add32((uint32_t)applyInvVM);
John Stiles45f5b032020-07-27 17:31:29 -0400165 b->add32((uint32_t)isFast);
Ethan Nicholascab767f2019-07-01 13:32:07 -0400166}
Ethan Nicholas82399462017-10-16 12:35:44 -0400167bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
168 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
169 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400170 if (rect != that.rect) return false;
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400171 if (applyInvVM != that.applyInvVM) return false;
172 if (invVM != that.invVM) return false;
Brian Salomonb2d5d402019-09-10 10:11:52 -0400173 if (isFast != that.isFast) return false;
Ethan Nicholas82399462017-10-16 12:35:44 -0400174 return true;
175}
John Stiles735a5a72020-08-26 10:21:10 -0400176bool GrRectBlurEffect::usesExplicitReturn() const { return false; }
Ethan Nicholas82399462017-10-16 12:35:44 -0400177GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src)
178 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400179 , rect(src.rect)
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400180 , applyInvVM(src.applyInvVM)
181 , invVM(src.invVM)
Brian Salomonb2d5d402019-09-10 10:11:52 -0400182 , isFast(src.isFast) {
Brian Osman12c5d292020-07-13 16:11:35 -0400183 this->cloneAndRegisterAllChildProcessors(src);
Brian Salomone7366842019-09-04 11:20:45 -0400184}
Ethan Nicholas82399462017-10-16 12:35:44 -0400185std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400186 return std::make_unique<GrRectBlurEffect>(*this);
Ethan Nicholas82399462017-10-16 12:35:44 -0400187}
John Stiles8d9bf642020-08-12 15:07:45 -0400188#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400189SkString GrRectBlurEffect::onDumpInfo() const {
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400190 return SkStringPrintf(
191 "(rect=float4(%f, %f, %f, %f), applyInvVM=%s, invVM=float3x3(%f, %f, %f, %f, %f, %f, "
192 "%f, %f, %f), isFast=%s)",
193 rect.left(), rect.top(), rect.right(), rect.bottom(), (applyInvVM ? "true" : "false"),
194 invVM.rc(0, 0), invVM.rc(1, 0), invVM.rc(2, 0), invVM.rc(0, 1), invVM.rc(1, 1),
195 invVM.rc(2, 1), invVM.rc(0, 2), invVM.rc(1, 2), invVM.rc(2, 2),
196 (isFast ? "true" : "false"));
John Stiles47b4e222020-08-12 09:56:50 -0400197}
198#endif
Ethan Nicholas82399462017-10-16 12:35:44 -0400199GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect);
200#if GR_TEST_UTILS
201std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500202 float sigma = data->fRandom->nextRangeF(3, 8);
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400203 int x = data->fRandom->nextRangeF(1, 200);
204 int y = data->fRandom->nextRangeF(1, 200);
Mike Kleind6ab77a2019-03-21 08:18:24 -0500205 float width = data->fRandom->nextRangeF(200, 300);
Ethan Nicholas82399462017-10-16 12:35:44 -0400206 float height = data->fRandom->nextRangeF(200, 300);
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400207 SkMatrix vm = GrTest::TestMatrixPreservesRightAngles(data->fRandom);
208 auto rect = SkRect::MakeXYWH(x, y, width, height);
John Stiles6609cb62020-07-17 14:52:12 -0400209 return GrRectBlurEffect::Make(data->inputFP(), data->context(), *data->caps()->shaderCaps(),
Brian Salomon96c8aeb2020-09-23 11:37:13 -0400210 rect, vm, sigma);
Ethan Nicholas82399462017-10-16 12:35:44 -0400211}
212#endif