blob: 9e21fa18b9f105ef7d2edfa784fc270ddefda2ee [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#if !defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
36 UniformHandle fOffsetsUni;
37#endif
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040038 UniformHandle fIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000039
John Stiles7571f9e2020-09-02 22:42:33 -040040 using INHERITED = GrGLSLFragmentProcessor;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000041};
42
Brian Salomon08cb4bf2020-05-07 15:34:15 -040043void GrGaussianConvolutionFragmentProcessor::Impl::emitCode(EmitArgs& args) {
Brian Salomonaee504b2017-01-24 12:29:36 -050044 const GrGaussianConvolutionFragmentProcessor& ce =
45 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080046
Ethan Nicholas48850572021-02-25 14:45:38 -050047 using namespace SkSL::dsl;
48 StartFragmentProcessor(this, &args);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040049 Var increment(kUniform_Modifier, kHalf2_Type, "Increment");
Ethan Nicholas48850572021-02-25 14:45:38 -050050 fIncrementUni = VarUniformHandle(increment);
robertphillipsbf536af2016-02-04 06:11:53 -080051
Albert Chaulk52b9e812021-04-12 17:23:25 -040052#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Michael Ludwige2674642020-10-21 13:01:34 -040053 int width = SkGpuBlurUtils::KernelWidth(ce.fRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -040054#else
55 int width = SkGpuBlurUtils::LinearKernelWidth(ce.fRadius);
56#endif
robertphillipsbf536af2016-02-04 06:11:53 -080057
jvanverth78d6eb02016-03-02 13:21:16 -080058 int arrayCount = (width + 3) / 4;
59 SkASSERT(4 * arrayCount >= width);
60
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040061 Var kernel(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Kernel");
Ethan Nicholas48850572021-02-25 14:45:38 -050062 fKernelUni = VarUniformHandle(kernel);
ericrk7a787b42015-07-21 14:06:16 -070063
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040064 Var color(kHalf4_Type, "color", Half4(0));
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050065 Declare(color);
ericrk7a787b42015-07-21 14:06:16 -070066
Albert Chaulk52b9e812021-04-12 17:23:25 -040067#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040068 Var coord(kFloat2_Type, "coord", sk_SampleCoord() - ce.fRadius * increment);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050069 Declare(coord);
ericrk7a787b42015-07-21 14:06:16 -070070
71 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
72 for (int i = 0; i < width; i++) {
John Stiles1c506432020-12-14 16:14:45 -050073 if (i != 0) {
Ethan Nicholas48850572021-02-25 14:45:38 -050074 coord += increment;
John Stiles1c506432020-12-14 16:14:45 -050075 }
Ethan Nicholas48850572021-02-25 14:45:38 -050076 color += SampleChild(/*index=*/0, coord) * kernel[i / 4][i & 0x3];
ericrk7a787b42015-07-21 14:06:16 -070077 }
Albert Chaulk52b9e812021-04-12 17:23:25 -040078#else
79 Var offsets(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Offsets");
80 fOffsetsUni = VarUniformHandle(offsets);
81
82 Var coord(kFloat2_Type, "coord", sk_SampleCoord());
83 Declare(coord);
84
85 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
86 for (int i = 0; i < width; i++) {
87 color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) *
88 kernel[i / 4][i & 0x3];
89 }
90#endif
Ethan Nicholas48850572021-02-25 14:45:38 -050091 Return(color);
92 EndFragmentProcessor();
ericrk7a787b42015-07-21 14:06:16 -070093}
94
Brian Salomon08cb4bf2020-05-07 15:34:15 -040095void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
96 const GrFragmentProcessor& processor) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040097 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080098
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040099 float increment[2] = {};
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400100 increment[static_cast<int>(conv.fDirection)] = 1;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400101 pdman.set2fv(fIncrementUni, 1, increment);
Robert Phillipseaded9d2018-05-01 15:17:50 -0400102
Albert Chaulk52b9e812021-04-12 17:23:25 -0400103#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Michael Ludwige2674642020-10-21 13:01:34 -0400104 int width = SkGpuBlurUtils::KernelWidth(conv.fRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400105#else
106 int width = SkGpuBlurUtils::LinearKernelWidth(conv.fRadius);
107#endif
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400108 int arrayCount = (width + 3)/4;
109 SkDEBUGCODE(size_t arraySize = 4*arrayCount;)
110 SkASSERT(arraySize >= static_cast<size_t>(width));
111 SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel));
112 pdman.set4fv(fKernelUni, arrayCount, conv.fKernel);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400113#if !defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
114 pdman.set4fv(fOffsetsUni, arrayCount, conv.fOffsets);
115#endif
ericrk7a787b42015-07-21 14:06:16 -0700116}
117
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400118void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor,
119 const GrShaderCaps&,
120 GrProcessorKeyBuilder* b) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400121 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400122 b->add32(conv.fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000123}
124
bsalomon@google.comb505a122012-05-31 18:40:36 +0000125///////////////////////////////////////////////////////////////////////////////
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400126
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400127std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
Greg Daniel5c082492020-01-29 15:06:49 -0500128 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500129 SkAlphaType alphaType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400130 Direction dir,
131 int halfWidth,
132 float gaussianSigma,
133 GrSamplerState::WrapMode wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400134 const SkIRect& subset,
135 const SkIRect* pixelDomain,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400136 const GrCaps& caps) {
137 std::unique_ptr<GrFragmentProcessor> child;
Albert Chaulk52b9e812021-04-12 17:23:25 -0400138 bool is_zero_sigma = SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma);
139 // We should sample as nearest if there will be no shader to preserve existing behaviour, but
140 // the linear blur requires a linear sample.
141 GrSamplerState::Filter filter = is_zero_sigma ?
142 GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
143#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
144 filter = GrSamplerState::Filter::kNearest;
145#endif
146
147 GrSamplerState sampler(wm, filter);
148 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400149 halfWidth = 0;
150 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400151 if (pixelDomain) {
152 // Inset because we expect to be invoked at pixel centers.
153 SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400154 switch (dir) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400155 case Direction::kX: domain.outset(halfWidth, 0); break;
156 case Direction::kY: domain.outset(0, halfWidth); break;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400157 }
158 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400159 SkRect::Make(subset), domain, caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400160 } else {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400161 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
162 SkRect::Make(subset), caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400163 }
Michael Ludwige2674642020-10-21 13:01:34 -0400164
Albert Chaulk52b9e812021-04-12 17:23:25 -0400165 if (is_zero_sigma) {
Michael Ludwige2674642020-10-21 13:01:34 -0400166 return child;
167 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400168 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
169 std::move(child), dir, halfWidth, gaussianSigma));
170}
171
172GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
173 std::unique_ptr<GrFragmentProcessor> child,
Brian Salomonfc118442019-11-22 19:09:27 -0500174 Direction direction,
175 int radius,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400176 float gaussianSigma)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400177 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400178 ProcessorOptimizationFlags(child.get()))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400179 , fRadius(radius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400180 , fDirection(direction) {
Brian Osman1298bc42020-06-30 13:39:35 -0400181 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Brian Salomon68626432020-04-15 12:56:13 +0000182 SkASSERT(radius <= kMaxKernelRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400183#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Michael Ludwige2674642020-10-21 13:01:34 -0400184 SkGpuBlurUtils::Compute1DGaussianKernel(fKernel, gaussianSigma, fRadius);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400185#else
186 SkGpuBlurUtils::Compute1DLinearGaussianKernel(fKernel, fOffsets, gaussianSigma, fRadius);
187#endif
Michael Ludwigfbe28592020-06-26 16:02:15 -0400188 this->setUsesSampleCoordsDirectly();
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000189}
190
Brian Salomon3f6f9652017-07-28 07:34:05 -0400191GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
192 const GrGaussianConvolutionFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400193 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400194 , fRadius(that.fRadius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400195 , fDirection(that.fDirection) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400196 this->cloneAndRegisterAllChildProcessors(that);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400197#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Michael Ludwige2674642020-10-21 13:01:34 -0400198 memcpy(fKernel, that.fKernel, SkGpuBlurUtils::KernelWidth(fRadius) * sizeof(float));
Albert Chaulk52b9e812021-04-12 17:23:25 -0400199#else
200 memcpy(fKernel, that.fKernel, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
201 memcpy(fOffsets, that.fOffsets, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float));
202#endif
Michael Ludwigfbe28592020-06-26 16:02:15 -0400203 this->setUsesSampleCoordsDirectly();
Brian Salomon3f6f9652017-07-28 07:34:05 -0400204}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000205
Brian Salomonaee504b2017-01-24 12:29:36 -0500206void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
207 GrProcessorKeyBuilder* b) const {
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400208 Impl::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800209}
210
Brian Salomon18ab2032021-02-23 10:07:05 -0500211std::unique_ptr<GrGLSLFragmentProcessor>
212GrGaussianConvolutionFragmentProcessor::onMakeProgramImpl() const {
213 return std::make_unique<Impl>();
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000214}
215
Brian Salomonaee504b2017-01-24 12:29:36 -0500216bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400217 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
Albert Chaulk52b9e812021-04-12 17:23:25 -0400218#if defined(SK_DISABLE_BILINEAR_BLUR_OPTIMIZATION)
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400219 return fRadius == that.fRadius && fDirection == that.fDirection &&
Michael Ludwige2674642020-10-21 13:01:34 -0400220 std::equal(fKernel, fKernel + SkGpuBlurUtils::KernelWidth(fRadius), that.fKernel);
Albert Chaulk52b9e812021-04-12 17:23:25 -0400221#else
222 return fRadius == that.fRadius && fDirection == that.fDirection &&
223 std::equal(fKernel, fKernel + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fKernel) &&
224 std::equal(fOffsets, fOffsets + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fOffsets);
225#endif
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000226}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000227
228///////////////////////////////////////////////////////////////////////////////
229
Brian Salomonaee504b2017-01-24 12:29:36 -0500230GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000231
Hal Canary6f6961e2017-01-31 13:50:44 -0500232#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400233std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500234 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500235 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500236
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400237 Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX;
238 SkIRect subset{
239 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
240 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
241 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
242 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
243 };
244 subset.sort();
Brian Salomon68626432020-04-15 12:56:13 +0000245
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400246 auto wm = static_cast<GrSamplerState::WrapMode>(
247 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
Robert Phillips08c5ec72017-01-30 12:26:47 -0500248 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500249 float sigma = radius / 3.f;
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400250 SkIRect temp;
251 SkIRect* domain = nullptr;
252 if (d->fRandom->nextBool()) {
253 temp = {
254 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
255 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
256 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
257 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
258 };
259 temp.sort();
260 domain = &temp;
261 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500262
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400263 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400264 subset, domain, *d->caps());
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000265}
Hal Canary6f6961e2017-01-31 13:50:44 -0500266#endif