blob: 2b32d79953f6ab6090aaf2e77b031b08437c7f57 [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
Greg Daniel456f9b52020-03-05 19:14:18 +000010#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrTextureProxy.h"
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040012#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
16#include "src/gpu/glsl/GrGLSLUniformHandler.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000017
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000018// For brevity
Brian Salomonb133ffe2017-07-27 11:53:21 -040019using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
20using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
bsalomon@google.com032b2212012-07-16 13:36:18 +000021
egdaniel64c47282015-11-13 06:54:19 -080022class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000023public:
robertphillips9cdb9922016-02-03 12:25:40 -080024 void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070025
Brian Salomon94efbf52016-11-29 13:43:05 -050026 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000027
wangyixb1daa862015-08-18 11:29:31 -070028protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040029 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070030
ericrk0f386122015-07-21 13:15:47 -070031private:
Brian Salomonaee504b2017-01-24 12:29:36 -050032 UniformHandle fKernelUni;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040033 UniformHandle fIncrementUni;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000034
egdaniel64c47282015-11-13 06:54:19 -080035 typedef GrGLSLFragmentProcessor INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000036};
37
wangyix7c157a92015-07-22 15:08:53 -070038void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
Brian Salomonaee504b2017-01-24 12:29:36 -050039 const GrGaussianConvolutionFragmentProcessor& ce =
40 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080041
egdaniel7ea439b2015-12-03 09:20:44 -080042 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040043
44 const char* inc;
45 fIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
46 "Increment", &inc);
robertphillipsbf536af2016-02-04 06:11:53 -080047
Brian Salomonb133ffe2017-07-27 11:53:21 -040048 int width = ce.width();
robertphillipsbf536af2016-02-04 06:11:53 -080049
jvanverth78d6eb02016-03-02 13:21:16 -080050 int arrayCount = (width + 3) / 4;
51 SkASSERT(4 * arrayCount >= width);
52
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040053 const char* kernel;
Ethan Nicholas16464c32020-04-06 13:53:05 -040054 fKernelUni = uniformHandler->addUniformArray(&ce, kFragment_GrShaderFlag, kHalf4_GrSLType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040055 "Kernel", arrayCount, &kernel);
ericrk7a787b42015-07-21 14:06:16 -070056
cdalton85285412016-02-18 12:37:07 -080057 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040058 auto coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
59 ce.sampleMatrix());
ericrk7a787b42015-07-21 14:06:16 -070060
Ethan Nicholasf7b88202017-09-18 14:10:39 -040061 fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070062
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040063 fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), inc);
Ethan Nicholas8aa45692017-09-20 11:24:15 -040064 fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
ericrk7a787b42015-07-21 14:06:16 -070065
66 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040067 static constexpr const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
ericrk7a787b42015-07-21 14:06:16 -070068 for (int i = 0; i < width; i++) {
ericrk7a787b42015-07-21 14:06:16 -070069 SkString kernelIndex;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040070 kernelIndex.printf("%s[%d]", kernel, i/4);
jvanverth78d6eb02016-03-02 13:21:16 -080071 kernelIndex.append(kVecSuffix[i & 0x3]);
ericrk7a787b42015-07-21 14:06:16 -070072
wutao039a7c72017-06-30 10:44:45 -070073 fragBuilder->codeAppend("coordSampled = coord;");
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040074 auto sample = this->invokeChild(0, args, "coordSampled");
75 fragBuilder->codeAppendf("%s += %s", args.fOutputColor, sample.c_str());
76 fragBuilder->codeAppendf(" * %s;", kernelIndex.c_str());
77 fragBuilder->codeAppendf("coord += %s;", inc);
ericrk7a787b42015-07-21 14:06:16 -070078 }
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040079 fragBuilder->codeAppendf("%s *= %s;", args.fOutputColor, args.fInputColor);
ericrk7a787b42015-07-21 14:06:16 -070080}
81
egdaniel018fb622015-10-28 07:26:40 -070082void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -040083 const GrFragmentProcessor& processor) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040084 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
robertphillipsbf536af2016-02-04 06:11:53 -080085
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040086 float increment[2] = {};
87 increment[static_cast<int>(conv.direction())] = 1;
88 pdman.set2fv(fIncrementUni, 1, increment);
Robert Phillipseaded9d2018-05-01 15:17:50 -040089
Brian Salomonb133ffe2017-07-27 11:53:21 -040090 int width = conv.width();
jvanverth78d6eb02016-03-02 13:21:16 -080091 int arrayCount = (width + 3) / 4;
92 SkASSERT(4 * arrayCount >= width);
93 pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
ericrk7a787b42015-07-21 14:06:16 -070094}
95
Brian Salomon94efbf52016-11-29 13:43:05 -050096void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070097 GrProcessorKeyBuilder* b) {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -040098 const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
99 b->add32(conv.radius());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000100}
101
bsalomon@google.comb505a122012-05-31 18:40:36 +0000102///////////////////////////////////////////////////////////////////////////////
Robert Phillips369e8b72017-08-01 16:13:04 -0400103static void fill_in_1D_gaussian_kernel(float* kernel, int width, float gaussianSigma, int radius) {
Greg Daniel4eda8d92018-04-03 14:03:15 -0400104 const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
Greg Daniel3aecc302018-04-03 13:38:01 -0400105 if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) {
106 for (int i = 0; i < width; ++i) {
107 kernel[i] = 0.0f;
108 }
109 return;
110 }
111
112 const float denom = 1.0f / twoSigmaSqrd;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500113
114 float sum = 0.0f;
115 for (int i = 0; i < width; ++i) {
116 float x = static_cast<float>(i - radius);
117 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
118 // is dropped here, since we renormalize the kernel below.
119 kernel[i] = sk_float_exp(-x * x * denom);
120 sum += kernel[i];
121 }
122 // Normalize the kernel
123 float scale = 1.0f / sum;
124 for (int i = 0; i < width; ++i) {
125 kernel[i] *= scale;
126 }
127}
128
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400129std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
Greg Daniel5c082492020-01-29 15:06:49 -0500130 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500131 SkAlphaType alphaType,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400132 Direction dir,
133 int halfWidth,
134 float gaussianSigma,
135 GrSamplerState::WrapMode wm,
Brian Salomon88d04cb2020-05-06 20:32:25 +0000136 const int bounds[2],
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400137 const GrCaps& caps) {
138 std::unique_ptr<GrFragmentProcessor> child;
Brian Salomon88d04cb2020-05-06 20:32:25 +0000139 GrSamplerState sampler;
140 switch (dir) {
141 case Direction::kX: sampler.setWrapModeX(wm); break;
142 case Direction::kY: sampler.setWrapModeY(wm); break;
143 }
144 if (bounds) {
145 SkASSERT(bounds[0] < bounds[1]);
146 SkRect subset;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400147 switch (dir) {
Brian Salomon88d04cb2020-05-06 20:32:25 +0000148 case Direction::kX:
149 subset = SkRect::MakeLTRB(bounds[0], 0, bounds[1], view.height());
150 break;
151 case Direction::kY:
152 subset = SkRect::MakeLTRB(0, bounds[0], view.width(), bounds[1]);
153 break;
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400154 }
155 child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
Brian Salomon88d04cb2020-05-06 20:32:25 +0000156 subset, caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400157 } else {
Brian Salomon88d04cb2020-05-06 20:32:25 +0000158 child = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps);
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400159 }
160 return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
161 std::move(child), dir, halfWidth, gaussianSigma));
162}
163
164GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
165 std::unique_ptr<GrFragmentProcessor> child,
Brian Salomonfc118442019-11-22 19:09:27 -0500166 Direction direction,
167 int radius,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400168 float gaussianSigma)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400169 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400170 ProcessorOptimizationFlags(child.get()))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400171 , fRadius(radius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400172 , fDirection(direction) {
173 child->setSampledWithExplicitCoords();
174 this->registerChildProcessor(std::move(child));
Brian Salomon68626432020-04-15 12:56:13 +0000175 SkASSERT(radius <= kMaxKernelRadius);
Brian Salomon68626432020-04-15 12:56:13 +0000176 fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400177 this->addCoordTransform(&fCoordTransform);
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000178}
179
Brian Salomon3f6f9652017-07-28 07:34:05 -0400180GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
181 const GrGaussianConvolutionFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400182 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
Brian Salomon3f6f9652017-07-28 07:34:05 -0400183 , fRadius(that.fRadius)
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400184 , fDirection(that.fDirection) {
185 auto child = that.childProcessor(0).clone();
186 child->setSampledWithExplicitCoords();
187 this->registerChildProcessor(std::move(child));
Brian Salomon68626432020-04-15 12:56:13 +0000188 memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400189 this->addCoordTransform(&fCoordTransform);
Brian Salomon3f6f9652017-07-28 07:34:05 -0400190}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000191
Brian Salomonaee504b2017-01-24 12:29:36 -0500192void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
193 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800194 GrGLConvolutionEffect::GenKey(*this, caps, b);
195}
196
Brian Salomonaee504b2017-01-24 12:29:36 -0500197GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800198 return new GrGLConvolutionEffect;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000199}
200
Brian Salomonaee504b2017-01-24 12:29:36 -0500201bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400202 const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
203 return this->radius() == that.radius() && this->direction() == that.direction() &&
204 std::equal(fKernel, fKernel + this->width(), that.fKernel);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000205}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000206
207///////////////////////////////////////////////////////////////////////////////
208
Brian Salomonaee504b2017-01-24 12:29:36 -0500209GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000210
Hal Canary6f6961e2017-01-31 13:50:44 -0500211#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400212std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500213 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500214 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500215
Brian Salomon88d04cb2020-05-06 20:32:25 +0000216 Direction dir;
217 int bounds[2];
218 do {
219 if (d->fRandom->nextBool()) {
220 dir = Direction::kX;
221 bounds[0] = d->fRandom->nextRangeU(0, view.width() - 1);
222 bounds[1] = d->fRandom->nextRangeU(0, view.width() - 1);
223 } else {
224 dir = Direction::kY;
225 bounds[0] = d->fRandom->nextRangeU(0, view.height() - 1);
226 bounds[1] = d->fRandom->nextRangeU(0, view.height() - 1);
227 }
228 } while (bounds[0] == bounds[1]);
229 std::sort(bounds, bounds + 2);
Brian Salomon68626432020-04-15 12:56:13 +0000230
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400231 auto wm = static_cast<GrSamplerState::WrapMode>(
232 d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
Robert Phillips08c5ec72017-01-30 12:26:47 -0500233 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500234 float sigma = radius / 3.f;
Robert Phillips08c5ec72017-01-30 12:26:47 -0500235
Brian Salomon5ed3c2f2020-04-13 16:51:39 -0400236 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
Brian Salomon88d04cb2020-05-06 20:32:25 +0000237 bounds, *d->caps());
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000238}
Hal Canary6f6961e2017-01-31 13:50:44 -0500239#endif