blob: e075aecffb902e185259e83f30b32934436eaf57 [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 Salomon08cb4bf2020-05-07 15:34:15 -040024class GrGaussianConvolutionFragmentProcessor::Impl : public GrGLSLFragmentProcessor {
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
John Stiles7571f9e2020-09-02 22:42:33 -040039 using INHERITED = GrGLSLFragmentProcessor;
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 Nicholasb14e6b92021-04-08 16:56:05 -040064 Var increment(kUniform_Modifier, kHalf2_Type, "Increment");
Ethan Nicholas48850572021-02-25 14:45:38 -050065 fIncrementUni = VarUniformHandle(increment);
robertphillipsbf536af2016-02-04 06:11:53 -080066
Albert Chaulk52b9e812021-04-12 17:23:25 -040067 int width = SkGpuBlurUtils::LinearKernelWidth(ce.fRadius);
robertphillipsbf536af2016-02-04 06:11:53 -080068
Brian Salomon3036def2021-04-21 19:30:57 -040069 LoopType loopType = loop_type(*args.fShaderCaps);
70
71 int arrayCount;
72 if (loopType == LoopType::kVariableLength) {
73 // Size the kernel uniform for the maximum width.
74 arrayCount = (SkGpuBlurUtils::LinearKernelWidth(kMaxKernelRadius) + 3) / 4;
75 } else {
76 arrayCount = (width + 3) / 4;
77 SkASSERT(4 * arrayCount >= width);
78 }
jvanverth78d6eb02016-03-02 13:21:16 -080079
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040080 Var kernel(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Kernel");
Ethan Nicholas48850572021-02-25 14:45:38 -050081 fKernelUni = VarUniformHandle(kernel);
ericrk7a787b42015-07-21 14:06:16 -070082
ericrk7a787b42015-07-21 14:06:16 -070083
Albert Chaulk52b9e812021-04-12 17:23:25 -040084 Var offsets(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Offsets");
85 fOffsetsUni = VarUniformHandle(offsets);
86
Brian Salomon3036def2021-04-21 19:30:57 -040087 Var color(kHalf4_Type, "color", Half4(0));
88 Declare(color);
89
Albert Chaulk52b9e812021-04-12 17:23:25 -040090 Var coord(kFloat2_Type, "coord", sk_SampleCoord());
91 Declare(coord);
92
Brian Salomon3036def2021-04-21 19:30:57 -040093 switch (loopType) {
94 case LoopType::kUnrolled:
95 for (int i = 0; i < width; i++) {
96 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
97 kernel[i / 4][i & 0x3];
98 }
99 break;
100 case LoopType::kFixedLength: {
101 Var i(kInt_Type, "i", 0);
102 For(Declare(i), i < width, i++,
103 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
104 kernel[i / 4][i & 0x3]);
105 break;
106 }
107 case LoopType::kVariableLength: {
108 Var kernelWidth(kUniform_Modifier, kInt_Type, "kernelWidth");
109 fKernelWidthUni = VarUniformHandle(kernelWidth);
110 Var i(kInt_Type, "i", 0);
111 For(Declare(i), i < kernelWidth, i++,
112 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
113 kernel[i / 4][i & 0x3]);
114 break;
Brian Salomon80a1f942021-04-21 10:56:41 -0400115 }
Albert Chaulk52b9e812021-04-12 17:23:25 -0400116 }
Brian Salomon5d627f32021-04-21 09:26:24 -0400117
Ethan Nicholas48850572021-02-25 14:45:38 -0500118 Return(color);
119 EndFragmentProcessor();
ericrk7a787b42015-07-21 14:06:16 -0700120}
121
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400122void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
123 const GrFragmentProcessor& processor) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400124 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -0800125
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400126 float increment[2] = {};
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400127 increment[static_cast<int>(conv.fDirection)] = 1;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400128 pdman.set2fv(fIncrementUni, 1, increment);
Robert Phillipseaded9d2018-05-01 15:17:50 -0400129
Albert Chaulk52b9e812021-04-12 17:23:25 -0400130 int width = SkGpuBlurUtils::LinearKernelWidth(conv.fRadius);
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400131 int arrayCount = (width + 3)/4;
132 SkDEBUGCODE(size_t arraySize = 4*arrayCount;)
133 SkASSERT(arraySize >= static_cast<size_t>(width));
134 SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel));
135 pdman.set4fv(fKernelUni, arrayCount, conv.fKernel);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400136 pdman.set4fv(fOffsetsUni, arrayCount, conv.fOffsets);
Brian Salomon3036def2021-04-21 19:30:57 -0400137 if (fKernelWidthUni.isValid()) {
138 pdman.set1i(fKernelWidthUni, width);
139 }
ericrk7a787b42015-07-21 14:06:16 -0700140}
141
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400142void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor,
Brian Salomon3036def2021-04-21 19:30:57 -0400143 const GrShaderCaps& shaderCaps,
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400144 GrProcessorKeyBuilder* b) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400145 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
Brian Salomon3036def2021-04-21 19:30:57 -0400146 if (loop_type(shaderCaps) != LoopType::kVariableLength) {
147 b->add32(conv.fRadius);
148 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000149}
150
bsalomon@google.comb505a122012-05-31 18:40:36 +0000151///////////////////////////////////////////////////////////////////////////////
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400152
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400153std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
Greg Daniel5c082492020-01-29 15:06:49 -0500154 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500155 SkAlphaType alphaType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400156 Direction dir,
157 int halfWidth,
158 float gaussianSigma,
159 GrSamplerState::WrapMode wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400160 const SkIRect& subset,
161 const SkIRect* pixelDomain,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400162 const GrCaps& caps) {
163 std::unique_ptr<GrFragmentProcessor> child;
Albert Chaulk52b9e812021-04-12 17:23:25 -0400164 bool is_zero_sigma = SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma);
165 // We should sample as nearest if there will be no shader to preserve existing behaviour, but
166 // the linear blur requires a linear sample.
167 GrSamplerState::Filter filter = is_zero_sigma ?
168 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Albert Chaulk52b9e812021-04-12 17:23:25 -0400169 GrSamplerState sampler(wm, filter);
170 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400171 halfWidth = 0;
172 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400173 if (pixelDomain) {
174 // Inset because we expect to be invoked at pixel centers.
175 SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400176 switch (dir) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400177 case Direction::kX: domain.outset(halfWidth, 0); break;
178 case Direction::kY: domain.outset(0, halfWidth); break;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400179 }
180 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400181 SkRect::Make(subset), domain, caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400182 } else {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400183 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
184 SkRect::Make(subset), caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400185 }
Michael Ludwige2674642020-10-21 13:01:34 -0400186
Albert Chaulk52b9e812021-04-12 17:23:25 -0400187 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400188 return child;
189 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400190 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
191 std::move(child), dir, halfWidth, gaussianSigma));
192}
193
194GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
195 std::unique_ptr<GrFragmentProcessor> child,
Brian Salomonfc118442019-11-22 19:09:27 -0500196 Direction direction,
197 int radius,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400198 float gaussianSigma)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400199 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400200 ProcessorOptimizationFlags(child.get()))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400201 , fRadius(radius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400202 , fDirection(direction) {
Brian Osman1298bc42020-06-30 13:39:35 -0400203 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Brian Salomon68626432020-04-15 12:56:13 +0000204 SkASSERT(radius <= kMaxKernelRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400205 SkGpuBlurUtils::Compute1DLinearGaussianKernel(fKernel, fOffsets, gaussianSigma, fRadius);
Michael Ludwigfbe28592020-06-26 16:02:15 -0400206 this->setUsesSampleCoordsDirectly();
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000207}
208
Brian Salomon3f6f9652017-07-28 07:34:05 -0400209GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
210 const GrGaussianConvolutionFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400211 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400212 , fRadius(that.fRadius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400213 , fDirection(that.fDirection) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400214 this->cloneAndRegisterAllChildProcessors(that);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400215 memcpy(fKernel, that.fKernel, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
216 memcpy(fOffsets, that.fOffsets, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
Michael Ludwigfbe28592020-06-26 16:02:15 -0400217 this->setUsesSampleCoordsDirectly();
Brian Salomon3f6f9652017-07-28 07:34:05 -0400218}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000219
Brian Salomonaee504b2017-01-24 12:29:36 -0500220void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
221 GrProcessorKeyBuilder* b) const {
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400222 Impl::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800223}
224
Brian Salomon18ab2032021-02-23 10:07:05 -0500225std::unique_ptr<GrGLSLFragmentProcessor>
226GrGaussianConvolutionFragmentProcessor::onMakeProgramImpl() const {
227 return std::make_unique<Impl>();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000228}
229
Brian Salomonaee504b2017-01-24 12:29:36 -0500230bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400231 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
Albert Chaulk52b9e812021-04-12 17:23:25 -0400232 return fRadius == that.fRadius && fDirection == that.fDirection &&
233 std::equal(fKernel, fKernel + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fKernel) &&
234 std::equal(fOffsets, fOffsets + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fOffsets);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000235}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000236
237///////////////////////////////////////////////////////////////////////////////
238
Brian Salomonaee504b2017-01-24 12:29:36 -0500239GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000240
Hal Canary6f6961e2017-01-31 13:50:44 -0500241#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400242std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500243 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500244 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500245
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400246 Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX;
247 SkIRect subset{
248 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
249 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
250 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
251 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
252 };
253 subset.sort();
Brian Salomon68626432020-04-15 12:56:13 +0000254
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400255 auto wm = static_cast<GrSamplerState::WrapMode>(
256 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
Robert Phillips08c5ec72017-01-30 12:26:47 -0500257 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500258 float sigma = radius / 3.f;
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400259 SkIRect temp;
260 SkIRect* domain = nullptr;
261 if (d->fRandom->nextBool()) {
262 temp = {
263 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
264 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
265 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
266 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
267 };
268 temp.sort();
269 domain = &temp;
270 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500271
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400272 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400273 subset, domain, *d->caps());
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000274}
Hal Canary6f6961e2017-01-31 13:50:44 -0500275#endif