blob: 8f1573360dc0ce0c8da45a18884d4f7046024871 [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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000019// For brevity
Brian Salomonb133ffe2017-07-27 11:53:21 -040020using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
21using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
bsalomon@google.com032b2212012-07-16 13:36:18 +000022
Brian Salomon08cb4bf2020-05-07 15:34:15 -040023class GrGaussianConvolutionFragmentProcessor::Impl : public GrGLSLFragmentProcessor {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000024public:
robertphillips9cdb9922016-02-03 12:25:40 -080025 void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070026
Brian Salomon94efbf52016-11-29 13:43:05 -050027 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000028
wangyixb1daa862015-08-18 11:29:31 -070029protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040030 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070031
ericrk0f386122015-07-21 13:15:47 -070032private:
Brian Salomonaee504b2017-01-24 12:29:36 -050033 UniformHandle fKernelUni;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040034 UniformHandle fIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000035
John Stiles7571f9e2020-09-02 22:42:33 -040036 using INHERITED = GrGLSLFragmentProcessor;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000037};
38
Brian Salomon08cb4bf2020-05-07 15:34:15 -040039void GrGaussianConvolutionFragmentProcessor::Impl::emitCode(EmitArgs& args) {
Brian Salomonaee504b2017-01-24 12:29:36 -050040 const GrGaussianConvolutionFragmentProcessor& ce =
41 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080042
egdaniel7ea439b2015-12-03 09:20:44 -080043 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040044
45 const char* inc;
46 fIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
47 "Increment", &inc);
robertphillipsbf536af2016-02-04 06:11:53 -080048
Michael Ludwige2674642020-10-21 13:01:34 -040049 int width = SkGpuBlurUtils::KernelWidth(ce.fRadius);
robertphillipsbf536af2016-02-04 06:11:53 -080050
jvanverth78d6eb02016-03-02 13:21:16 -080051 int arrayCount = (width + 3) / 4;
52 SkASSERT(4 * arrayCount >= width);
53
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040054 const char* kernel;
Ethan Nicholas16464c32020-04-06 13:53:05 -040055 fKernelUni = uniformHandler->addUniformArray(&ce, kFragment_GrShaderFlag, kHalf4_GrSLType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040056 "Kernel", arrayCount, &kernel);
ericrk7a787b42015-07-21 14:06:16 -070057
cdalton85285412016-02-18 12:37:07 -080058 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
ericrk7a787b42015-07-21 14:06:16 -070059
Ethan Nicholasf7b88202017-09-18 14:10:39 -040060 fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070061
Michael Ludwige88320b2020-06-24 09:04:56 -040062 fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", args.fSampleCoord, ce.fRadius, inc);
Ethan Nicholas8aa45692017-09-20 11:24:15 -040063 fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
ericrk7a787b42015-07-21 14:06:16 -070064
65 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040066 static constexpr const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
ericrk7a787b42015-07-21 14:06:16 -070067 for (int i = 0; i < width; i++) {
ericrk7a787b42015-07-21 14:06:16 -070068 SkString kernelIndex;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040069 kernelIndex.printf("%s[%d]", kernel, i/4);
jvanverth78d6eb02016-03-02 13:21:16 -080070 kernelIndex.append(kVecSuffix[i & 0x3]);
ericrk7a787b42015-07-21 14:06:16 -070071
wutao039a7c72017-06-30 10:44:45 -070072 fragBuilder->codeAppend("coordSampled = coord;");
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040073 auto sample = this->invokeChild(0, args, "coordSampled");
74 fragBuilder->codeAppendf("%s += %s", args.fOutputColor, sample.c_str());
75 fragBuilder->codeAppendf(" * %s;", kernelIndex.c_str());
76 fragBuilder->codeAppendf("coord += %s;", inc);
ericrk7a787b42015-07-21 14:06:16 -070077 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040078 fragBuilder->codeAppendf("%s *= %s;", args.fOutputColor, args.fInputColor);
ericrk7a787b42015-07-21 14:06:16 -070079}
80
Brian Salomon08cb4bf2020-05-07 15:34:15 -040081void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
82 const GrFragmentProcessor& processor) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040083 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080084
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040085 float increment[2] = {};
Brian Salomon08cb4bf2020-05-07 15:34:15 -040086 increment[static_cast<int>(conv.fDirection)] = 1;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040087 pdman.set2fv(fIncrementUni, 1, increment);
Robert Phillipseaded9d2018-05-01 15:17:50 -040088
Michael Ludwige2674642020-10-21 13:01:34 -040089 int width = SkGpuBlurUtils::KernelWidth(conv.fRadius);
Brian Salomon08cb4bf2020-05-07 15:34:15 -040090 int arrayCount = (width + 3)/4;
91 SkDEBUGCODE(size_t arraySize = 4*arrayCount;)
92 SkASSERT(arraySize >= static_cast<size_t>(width));
93 SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel));
94 pdman.set4fv(fKernelUni, arrayCount, conv.fKernel);
ericrk7a787b42015-07-21 14:06:16 -070095}
96
Brian Salomon08cb4bf2020-05-07 15:34:15 -040097void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor,
98 const GrShaderCaps&,
99 GrProcessorKeyBuilder* b) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400100 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400101 b->add32(conv.fRadius);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000102}
103
bsalomon@google.comb505a122012-05-31 18:40:36 +0000104///////////////////////////////////////////////////////////////////////////////
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400105
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400106std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
Greg Daniel5c082492020-01-29 15:06:49 -0500107 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500108 SkAlphaType alphaType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400109 Direction dir,
110 int halfWidth,
111 float gaussianSigma,
112 GrSamplerState::WrapMode wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400113 const SkIRect& subset,
114 const SkIRect* pixelDomain,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400115 const GrCaps& caps) {
116 std::unique_ptr<GrFragmentProcessor> child;
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400117 GrSamplerState sampler(wm, GrSamplerState::Filter::kNearest);
Michael Ludwige2674642020-10-21 13:01:34 -0400118 if (SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma)) {
119 halfWidth = 0;
120 }
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400121 if (pixelDomain) {
122 // Inset because we expect to be invoked at pixel centers.
123 SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400124 switch (dir) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400125 case Direction::kX: domain.outset(halfWidth, 0); break;
126 case Direction::kY: domain.outset(0, halfWidth); break;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400127 }
128 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400129 SkRect::Make(subset), domain, caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400130 } else {
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400131 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
132 SkRect::Make(subset), caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400133 }
Michael Ludwige2674642020-10-21 13:01:34 -0400134
135 if (SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma)) {
136 return child;
137 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400138 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
139 std::move(child), dir, halfWidth, gaussianSigma));
140}
141
142GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
143 std::unique_ptr<GrFragmentProcessor> child,
Brian Salomonfc118442019-11-22 19:09:27 -0500144 Direction direction,
145 int radius,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400146 float gaussianSigma)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400147 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400148 ProcessorOptimizationFlags(child.get()))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400149 , fRadius(radius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400150 , fDirection(direction) {
Brian Osman1298bc42020-06-30 13:39:35 -0400151 this->registerChild(std::move(child), SkSL::SampleUsage::Explicit());
Brian Salomon68626432020-04-15 12:56:13 +0000152 SkASSERT(radius <= kMaxKernelRadius);
Michael Ludwige2674642020-10-21 13:01:34 -0400153 SkGpuBlurUtils::Compute1DGaussianKernel(fKernel, gaussianSigma, fRadius);
Michael Ludwigfbe28592020-06-26 16:02:15 -0400154 this->setUsesSampleCoordsDirectly();
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000155}
156
Brian Salomon3f6f9652017-07-28 07:34:05 -0400157GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
158 const GrGaussianConvolutionFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400159 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400160 , fRadius(that.fRadius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400161 , fDirection(that.fDirection) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400162 this->cloneAndRegisterAllChildProcessors(that);
Michael Ludwige2674642020-10-21 13:01:34 -0400163 memcpy(fKernel, that.fKernel, SkGpuBlurUtils::KernelWidth(fRadius) * sizeof(float));
Michael Ludwigfbe28592020-06-26 16:02:15 -0400164 this->setUsesSampleCoordsDirectly();
Brian Salomon3f6f9652017-07-28 07:34:05 -0400165}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000166
Brian Salomonaee504b2017-01-24 12:29:36 -0500167void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
168 GrProcessorKeyBuilder* b) const {
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400169 Impl::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800170}
171
Brian Salomonaee504b2017-01-24 12:29:36 -0500172GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400173 return new Impl;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000174}
175
Brian Salomonaee504b2017-01-24 12:29:36 -0500176bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400177 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
Brian Salomon08cb4bf2020-05-07 15:34:15 -0400178 return fRadius == that.fRadius && fDirection == that.fDirection &&
Michael Ludwige2674642020-10-21 13:01:34 -0400179 std::equal(fKernel, fKernel + SkGpuBlurUtils::KernelWidth(fRadius), that.fKernel);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000180}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000181
182///////////////////////////////////////////////////////////////////////////////
183
Brian Salomonaee504b2017-01-24 12:29:36 -0500184GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000185
Hal Canary6f6961e2017-01-31 13:50:44 -0500186#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400187std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500188 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500189 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500190
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400191 Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX;
192 SkIRect subset{
193 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
194 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
195 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
196 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
197 };
198 subset.sort();
Brian Salomon68626432020-04-15 12:56:13 +0000199
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400200 auto wm = static_cast<GrSamplerState::WrapMode>(
201 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
Robert Phillips08c5ec72017-01-30 12:26:47 -0500202 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500203 float sigma = radius / 3.f;
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400204 SkIRect temp;
205 SkIRect* domain = nullptr;
206 if (d->fRandom->nextBool()) {
207 temp = {
208 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
209 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
210 static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)),
211 static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)),
212 };
213 temp.sort();
214 domain = &temp;
215 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500216
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400217 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400218 subset, domain, *d->caps());
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000219}
Hal Canary6f6961e2017-01-31 13:50:44 -0500220#endif