blob: d03e9eff9a7696cb588d9ee6eda27c5a430e2ebd [file] [log] [blame]
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001/*
2 * Copyright 2012 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrGaussianConvolutionFragmentProcessor.h"
Robert Phillips296b1cc2017-03-15 10:42:12 -04009
Michael Ludwige2674642020-10-21 13:01:34 -040010#include "src/core/SkGpuBlurUtils.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000011#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrTextureProxy.h"
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040013#include "src/gpu/effects/GrTextureEffect.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/GrGLSLProgramDataManager.h"
17#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Ethan Nicholas48850572021-02-25 14:45:38 -050018#include "src/sksl/dsl/priv/DSLFPs.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000020// For brevity
Brian Salomonb133ffe2017-07-27 11:53:21 -040021using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
22using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
bsalomon@google.com032b2212012-07-16 13:36:18 +000023
Brian Salomon3176e862021-08-09 11:23:04 -040024class GrGaussianConvolutionFragmentProcessor::Impl : public GrFragmentProcessor::ProgramImpl {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000025public:
robertphillips9cdb9922016-02-03 12:25:40 -080026 void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070027
Brian Salomon94efbf52016-11-29 13:43:05 -050028 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000029
wangyixb1daa862015-08-18 11:29:31 -070030protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040031 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070032
ericrk0f386122015-07-21 13:15:47 -070033private:
Brian Salomonaee504b2017-01-24 12:29:36 -050034 UniformHandle fKernelUni;
Albert Chaulk52b9e812021-04-12 17:23:25 -040035 UniformHandle fOffsetsUni;
Brian Salomon3036def2021-04-21 19:30:57 -040036 UniformHandle fKernelWidthUni;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040037 UniformHandle fIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000038
Brian Salomon3176e862021-08-09 11:23:04 -040039 using INHERITED = ProgramImpl;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000040};
41
Brian Salomon3036def2021-04-21 19:30:57 -040042enum class LoopType {
43 kUnrolled,
44 kFixedLength,
45 kVariableLength,
46};
47
48static LoopType loop_type(const GrShaderCaps& caps) {
49 // This checks that bitwise integer operations and array indexing by non-consts are allowed.
50 if (caps.generation() < k130_GrGLSLGeneration) {
51 return LoopType::kUnrolled;
52 }
53 // If we're in reduced shader mode and we can have a loop then use a uniform to limit the
54 // number of iterations so we don't need a code variation for each width.
55 return caps.reducedShaderMode() ? LoopType::kVariableLength : LoopType::kFixedLength;
56}
57
Brian Salomon08cb4bf2020-05-07 15:34:15 -040058void GrGaussianConvolutionFragmentProcessor::Impl::emitCode(EmitArgs& args) {
Brian Salomonaee504b2017-01-24 12:29:36 -050059 const GrGaussianConvolutionFragmentProcessor& ce =
60 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080061
Ethan Nicholas48850572021-02-25 14:45:38 -050062 using namespace SkSL::dsl;
63 StartFragmentProcessor(this, &args);
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040064 GlobalVar increment(kUniform_Modifier, kHalf2_Type, "Increment");
65 Declare(increment);
Ethan Nicholas48850572021-02-25 14:45:38 -050066 fIncrementUni = VarUniformHandle(increment);
robertphillipsbf536af2016-02-04 06:11:53 -080067
Albert Chaulk52b9e812021-04-12 17:23:25 -040068 int width = SkGpuBlurUtils::LinearKernelWidth(ce.fRadius);
robertphillipsbf536af2016-02-04 06:11:53 -080069
Brian Salomon3036def2021-04-21 19:30:57 -040070 LoopType loopType = loop_type(*args.fShaderCaps);
71
72 int arrayCount;
73 if (loopType == LoopType::kVariableLength) {
74 // Size the kernel uniform for the maximum width.
75 arrayCount = (SkGpuBlurUtils::LinearKernelWidth(kMaxKernelRadius) + 3) / 4;
76 } else {
77 arrayCount = (width + 3) / 4;
78 SkASSERT(4 * arrayCount >= width);
79 }
jvanverth78d6eb02016-03-02 13:21:16 -080080
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040081 GlobalVar kernel(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Kernel");
82 Declare(kernel);
Ethan Nicholas48850572021-02-25 14:45:38 -050083 fKernelUni = VarUniformHandle(kernel);
ericrk7a787b42015-07-21 14:06:16 -070084
ericrk7a787b42015-07-21 14:06:16 -070085
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040086 GlobalVar offsets(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Offsets");
87 Declare(offsets);
Albert Chaulk52b9e812021-04-12 17:23:25 -040088 fOffsetsUni = VarUniformHandle(offsets);
89
Brian Salomon3036def2021-04-21 19:30:57 -040090 Var color(kHalf4_Type, "color", Half4(0));
91 Declare(color);
92
Albert Chaulk52b9e812021-04-12 17:23:25 -040093 Var coord(kFloat2_Type, "coord", sk_SampleCoord());
94 Declare(coord);
95
Brian Salomon3036def2021-04-21 19:30:57 -040096 switch (loopType) {
97 case LoopType::kUnrolled:
98 for (int i = 0; i < width; i++) {
99 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
100 kernel[i / 4][i & 0x3];
101 }
102 break;
103 case LoopType::kFixedLength: {
104 Var i(kInt_Type, "i", 0);
105 For(Declare(i), i < width, i++,
106 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
107 kernel[i / 4][i & 0x3]);
108 break;
109 }
110 case LoopType::kVariableLength: {
Ethan Nicholasa2d22b22021-07-15 10:35:54 -0400111 GlobalVar kernelWidth(kUniform_Modifier, kInt_Type, "kernelWidth");
112 Declare(kernelWidth);
Brian Salomon3036def2021-04-21 19:30:57 -0400113 fKernelWidthUni = VarUniformHandle(kernelWidth);
114 Var i(kInt_Type, "i", 0);
115 For(Declare(i), i < kernelWidth, i++,
116 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
117 kernel[i / 4][i & 0x3]);
118 break;
Brian Salomon80a1f942021-04-21 10:56:41 -0400119 }
Albert Chaulk52b9e812021-04-12 17:23:25 -0400120 }
Brian Salomon5d627f32021-04-21 09:26:24 -0400121
Ethan Nicholas48850572021-02-25 14:45:38 -0500122 Return(color);
123 EndFragmentProcessor();
ericrk7a787b42015-07-21 14:06:16 -0700124}
125
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400126void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
127 const GrFragmentProcessor& processor) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400128 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -0800129
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400130 float increment[2] = {};
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400131 increment[static_cast<int>(conv.fDirection)] = 1;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400132 pdman.set2fv(fIncrementUni, 1, increment);
Robert Phillipseaded9d2018-05-01 15:17:50 -0400133
Albert Chaulk52b9e812021-04-12 17:23:25 -0400134 int width = SkGpuBlurUtils::LinearKernelWidth(conv.fRadius);
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400135 int arrayCount = (width + 3)/4;
136 SkDEBUGCODE(size_t arraySize = 4*arrayCount;)
137 SkASSERT(arraySize >= static_cast<size_t>(width));
138 SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel));
139 pdman.set4fv(fKernelUni, arrayCount, conv.fKernel);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400140 pdman.set4fv(fOffsetsUni, arrayCount, conv.fOffsets);
Brian Salomon3036def2021-04-21 19:30:57 -0400141 if (fKernelWidthUni.isValid()) {
142 pdman.set1i(fKernelWidthUni, width);
143 }
ericrk7a787b42015-07-21 14:06:16 -0700144}
145
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400146void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor,
Brian Salomon3036def2021-04-21 19:30:57 -0400147 const GrShaderCaps& shaderCaps,
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400148 GrProcessorKeyBuilder* b) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400149 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
Brian Salomon3036def2021-04-21 19:30:57 -0400150 if (loop_type(shaderCaps) != LoopType::kVariableLength) {
151 b->add32(conv.fRadius);
152 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000153}
154
bsalomon@google.comb505a122012-05-31 18:40:36 +0000155///////////////////////////////////////////////////////////////////////////////
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400156
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400157std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
Greg Daniel5c082492020-01-29 15:06:49 -0500158 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500159 SkAlphaType alphaType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400160 Direction dir,
161 int halfWidth,
162 float gaussianSigma,
163 GrSamplerState::WrapMode wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400164 const SkIRect& subset,
165 const SkIRect* pixelDomain,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400166 const GrCaps& caps) {
167 std::unique_ptr<GrFragmentProcessor> child;
Albert Chaulk52b9e812021-04-12 17:23:25 -0400168 bool is_zero_sigma = SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma);
169 // We should sample as nearest if there will be no shader to preserve existing behaviour, but
170 // the linear blur requires a linear sample.
171 GrSamplerState::Filter filter = is_zero_sigma ?
172 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Albert Chaulk52b9e812021-04-12 17:23:25 -0400173 GrSamplerState sampler(wm, filter);
174 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400175 halfWidth = 0;
176 }
Brian Salomon071182e2021-04-22 12:03:59 -0400177 // It's pretty common to blur a subset of an input texture. In reduced shader mode we always
178 // apply the wrap mode in the shader.
179 bool alwaysUseShaderTileMode = caps.reducedShaderMode();
180 if (pixelDomain && !alwaysUseShaderTileMode) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400181 // Inset because we expect to be invoked at pixel centers.
182 SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400183 switch (dir) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400184 case Direction::kX: domain.outset(halfWidth, 0); break;
185 case Direction::kY: domain.outset(0, halfWidth); break;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400186 }
Brian Salomon071182e2021-04-22 12:03:59 -0400187 child = GrTextureEffect::MakeSubset(std::move(view),
188 alphaType,
189 SkMatrix::I(),
190 sampler,
191 SkRect::Make(subset),
192 domain,
193 caps,
194 GrTextureEffect::kDefaultBorder);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400195 } else {
Brian Salomon071182e2021-04-22 12:03:59 -0400196 child = GrTextureEffect::MakeSubset(std::move(view),
197 alphaType,
198 SkMatrix::I(),
199 sampler,
200 SkRect::Make(subset),
201 caps,
202 GrTextureEffect::kDefaultBorder,
203 alwaysUseShaderTileMode);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400204 }
Michael Ludwige2674642020-10-21 13:01:34 -0400205
Albert Chaulk52b9e812021-04-12 17:23:25 -0400206 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400207 return child;
208 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400209 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
210 std::move(child), dir, halfWidth, gaussianSigma));
211}
212
213GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
214 std::unique_ptr<GrFragmentProcessor> child,
Brian Salomonfc118442019-11-22 19:09:27 -0500215 Direction direction,
216 int radius,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400217 float gaussianSigma)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400218 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400219 ProcessorOptimizationFlags(child.get()))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400220 , fRadius(radius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400221 , fDirection(direction) {
Brian Osman1298bc42020-06-30 13:39:35 -0400222 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Brian Salomon68626432020-04-15 12:56:13 +0000223 SkASSERT(radius <= kMaxKernelRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400224 SkGpuBlurUtils::Compute1DLinearGaussianKernel(fKernel, fOffsets, gaussianSigma, fRadius);
Michael Ludwigfbe28592020-06-26 16:02:15 -0400225 this->setUsesSampleCoordsDirectly();
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000226}
227
Brian Salomon3f6f9652017-07-28 07:34:05 -0400228GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
229 const GrGaussianConvolutionFragmentProcessor& that)
John Stiles307f8f52021-08-09 15:36:59 -0400230 : INHERITED(that)
Brian Salomon3f6f9652017-07-28 07:34:05 -0400231 , fRadius(that.fRadius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400232 , fDirection(that.fDirection) {
Albert Chaulk52b9e812021-04-12 17:23:25 -0400233 memcpy(fKernel, that.fKernel, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
234 memcpy(fOffsets, that.fOffsets, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
Brian Salomon3f6f9652017-07-28 07:34:05 -0400235}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000236
Brian Salomon13b28732021-08-06 15:33:58 -0400237void GrGaussianConvolutionFragmentProcessor::onAddToKey(const GrShaderCaps& caps,
238 GrProcessorKeyBuilder* b) const {
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400239 Impl::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800240}
241
Brian Salomon3176e862021-08-09 11:23:04 -0400242std::unique_ptr<GrFragmentProcessor::ProgramImpl>
Brian Salomon18ab2032021-02-23 10:07:05 -0500243GrGaussianConvolutionFragmentProcessor::onMakeProgramImpl() const {
244 return std::make_unique<Impl>();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000245}
246
Brian Salomonaee504b2017-01-24 12:29:36 -0500247bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400248 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
Albert Chaulk52b9e812021-04-12 17:23:25 -0400249 return fRadius == that.fRadius && fDirection == that.fDirection &&
250 std::equal(fKernel, fKernel + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fKernel) &&
251 std::equal(fOffsets, fOffsets + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fOffsets);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000252}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000253
254///////////////////////////////////////////////////////////////////////////////
255
Brian Salomonaee504b2017-01-24 12:29:36 -0500256GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000257
Hal Canary6f6961e2017-01-31 13:50:44 -0500258#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400259std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500260 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500261 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500262
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400263 Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX;
264 SkIRect subset{
265 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
266 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
267 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
268 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
269 };
270 subset.sort();
Brian Salomon68626432020-04-15 12:56:13 +0000271
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400272 auto wm = static_cast<GrSamplerState::WrapMode>(
273 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
Robert Phillips08c5ec72017-01-30 12:26:47 -0500274 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500275 float sigma = radius / 3.f;
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400276 SkIRect temp;
277 SkIRect* domain = nullptr;
278 if (d->fRandom->nextBool()) {
279 temp = {
280 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
281 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
282 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
283 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
284 };
285 temp.sort();
286 domain = &temp;
287 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500288
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400289 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400290 subset, domain, *d->caps());
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000291}
Hal Canary6f6961e2017-01-31 13:50:44 -0500292#endif