blob: 118efe8383706382abb89a768d9924826ad6a3f0 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
13#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
15#include "src/gpu/glsl/GrGLSLUniformHandler.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000016
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000017// For brevity
Brian Salomonb133ffe2017-07-27 11:53:21 -040018using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
19using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
bsalomon@google.com032b2212012-07-16 13:36:18 +000020
egdaniel64c47282015-11-13 06:54:19 -080021class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000022public:
robertphillips9cdb9922016-02-03 12:25:40 -080023 void emitCode(EmitArgs&) override;
ericrk7a787b42015-07-21 14:06:16 -070024
Brian Salomon94efbf52016-11-29 13:43:05 -050025 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000026
wangyixb1daa862015-08-18 11:29:31 -070027protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040028 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -070029
ericrk0f386122015-07-21 13:15:47 -070030private:
Brian Salomonaee504b2017-01-24 12:29:36 -050031 UniformHandle fKernelUni;
Brian Salomon68626432020-04-15 12:56:13 +000032 UniformHandle fImageIncrementUni;
33 UniformHandle fBoundsUni;
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 Salomon68626432020-04-15 12:56:13 +000043 fImageIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
44 "ImageIncrement");
45 if (ce.useBounds()) {
46 fBoundsUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
47 "Bounds");
48 }
robertphillipsbf536af2016-02-04 06:11:53 -080049
Brian Salomonb133ffe2017-07-27 11:53:21 -040050 int width = ce.width();
robertphillipsbf536af2016-02-04 06:11:53 -080051
jvanverth78d6eb02016-03-02 13:21:16 -080052 int arrayCount = (width + 3) / 4;
53 SkASSERT(4 * arrayCount >= width);
54
Ethan Nicholas16464c32020-04-06 13:53:05 -040055 fKernelUni = uniformHandler->addUniformArray(&ce, kFragment_GrShaderFlag, kHalf4_GrSLType,
Brian Salomon68626432020-04-15 12:56:13 +000056 "Kernel", arrayCount);
ericrk7a787b42015-07-21 14:06:16 -070057
cdalton85285412016-02-18 12:37:07 -080058 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Brian Salomon68626432020-04-15 12:56:13 +000059 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
60 ce.sampleMatrix());
ericrk7a787b42015-07-21 14:06:16 -070061
Ethan Nicholasf7b88202017-09-18 14:10:39 -040062 fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
ericrk7a787b42015-07-21 14:06:16 -070063
Brian Salomon68626432020-04-15 12:56:13 +000064 const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni);
65 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
66
67 fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc);
Ethan Nicholas8aa45692017-09-20 11:24:15 -040068 fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
ericrk7a787b42015-07-21 14:06:16 -070069
70 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
Brian Salomon68626432020-04-15 12:56:13 +000071 const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
ericrk7a787b42015-07-21 14:06:16 -070072 for (int i = 0; i < width; i++) {
Brian Salomon68626432020-04-15 12:56:13 +000073 SkString index;
ericrk7a787b42015-07-21 14:06:16 -070074 SkString kernelIndex;
Brian Salomon68626432020-04-15 12:56:13 +000075 index.appendS32(i / 4);
76 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
jvanverth78d6eb02016-03-02 13:21:16 -080077 kernelIndex.append(kVecSuffix[i & 0x3]);
ericrk7a787b42015-07-21 14:06:16 -070078
wutao039a7c72017-06-30 10:44:45 -070079 fragBuilder->codeAppend("coordSampled = coord;");
Brian Salomon68626432020-04-15 12:56:13 +000080 if (ce.useBounds()) {
81 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
82 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
83 // to have a bug that caused corruption.
84 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni);
85 const char* component = ce.direction() == Direction::kY ? "y" : "x";
86
87 switch (ce.mode()) {
88 case GrTextureDomain::kClamp_Mode: {
89 fragBuilder->codeAppendf("coordSampled.%s = clamp(coord.%s, %s.x, %s.y);\n",
90 component, component, bounds, bounds);
91 break;
92 }
93 // Deferring implementing kMirrorRepeat until we use DomainEffects as
94 // child processors. Fallback to Repeat.
95 case GrTextureDomain::kMirrorRepeat_Mode:
96 case GrTextureDomain::kRepeat_Mode: {
97 fragBuilder->codeAppendf("coordSampled.%s = "
98 "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n",
99 component, component, bounds, bounds, bounds, bounds);
100 break;
101 }
102 case GrTextureDomain::kDecal_Mode: {
103 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
104 component, bounds, component, bounds);
105 break;
106 }
107 default: {
108 SK_ABORT("Unsupported operation.");
109 }
110 }
111 }
112 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
113 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled");
114 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
115 if (GrTextureDomain::kDecal_Mode == ce.mode()) {
116 fragBuilder->codeAppend("}");
117 }
118 fragBuilder->codeAppendf("coord += %s;\n", imgInc);
ericrk7a787b42015-07-21 14:06:16 -0700119 }
Brian Salomon68626432020-04-15 12:56:13 +0000120 fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
ericrk7a787b42015-07-21 14:06:16 -0700121}
122
egdaniel018fb622015-10-28 07:26:40 -0700123void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400124 const GrFragmentProcessor& processor) {
Brian Salomon68626432020-04-15 12:56:13 +0000125 const GrGaussianConvolutionFragmentProcessor& conv =
126 processor.cast<GrGaussianConvolutionFragmentProcessor>();
127 const auto& view = conv.textureSampler(0).view();
128 GrSurfaceProxy* proxy = view.proxy();
129 GrTexture& texture = *proxy->peekTexture();
robertphillipsbf536af2016-02-04 06:11:53 -0800130
Brian Salomon68626432020-04-15 12:56:13 +0000131 float imageIncrement[2] = {0};
132 float ySign = view.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
133 switch (conv.direction()) {
134 case Direction::kX:
135 imageIncrement[0] = 1.0f / texture.width();
136 break;
137 case Direction::kY:
138 imageIncrement[1] = ySign / texture.height();
139 break;
140 default:
141 SK_ABORT("Unknown filter direction.");
142 }
143 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
144 if (conv.useBounds()) {
145 float bounds[2] = {0};
146 bounds[0] = conv.bounds()[0];
147 bounds[1] = conv.bounds()[1];
148 if (GrTextureDomain::kClamp_Mode == conv.mode()) {
149 bounds[0] += SK_ScalarHalf;
150 bounds[1] -= SK_ScalarHalf;
151 }
152 if (Direction::kX == conv.direction()) {
153 SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width()));
154 bounds[0] *= inv;
155 bounds[1] *= inv;
156 } else {
157 SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height()));
158 if (view.origin() != kTopLeft_GrSurfaceOrigin) {
159 float tmp = bounds[0];
160 bounds[0] = 1.0f - (inv * bounds[1]);
161 bounds[1] = 1.0f - (inv * tmp);
162 } else {
163 bounds[0] *= inv;
164 bounds[1] *= inv;
165 }
166 }
Robert Phillipseaded9d2018-05-01 15:17:50 -0400167
Brian Salomon68626432020-04-15 12:56:13 +0000168 SkASSERT(bounds[0] <= bounds[1]);
169 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
170 }
Brian Salomonb133ffe2017-07-27 11:53:21 -0400171 int width = conv.width();
Brian Salomon68626432020-04-15 12:56:13 +0000172
jvanverth78d6eb02016-03-02 13:21:16 -0800173 int arrayCount = (width + 3) / 4;
174 SkASSERT(4 * arrayCount >= width);
175 pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
ericrk7a787b42015-07-21 14:06:16 -0700176}
177
Brian Salomon94efbf52016-11-29 13:43:05 -0500178void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700179 GrProcessorKeyBuilder* b) {
Brian Salomon68626432020-04-15 12:56:13 +0000180 const GrGaussianConvolutionFragmentProcessor& conv =
181 processor.cast<GrGaussianConvolutionFragmentProcessor>();
182 uint32_t key = conv.radius();
183 key <<= 3;
184 if (conv.useBounds()) {
185 key |= Direction::kY == conv.direction() ? 0x4 : 0x0;
186 }
187 key |= static_cast<uint32_t>(conv.mode());
188 b->add32(key);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000189}
190
bsalomon@google.comb505a122012-05-31 18:40:36 +0000191///////////////////////////////////////////////////////////////////////////////
Robert Phillips369e8b72017-08-01 16:13:04 -0400192static void fill_in_1D_gaussian_kernel(float* kernel, int width, float gaussianSigma, int radius) {
Greg Daniel4eda8d92018-04-03 14:03:15 -0400193 const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
Greg Daniel3aecc302018-04-03 13:38:01 -0400194 if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) {
195 for (int i = 0; i < width; ++i) {
196 kernel[i] = 0.0f;
197 }
198 return;
199 }
200
201 const float denom = 1.0f / twoSigmaSqrd;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500202
203 float sum = 0.0f;
204 for (int i = 0; i < width; ++i) {
205 float x = static_cast<float>(i - radius);
206 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
207 // is dropped here, since we renormalize the kernel below.
208 kernel[i] = sk_float_exp(-x * x * denom);
209 sum += kernel[i];
210 }
211 // Normalize the kernel
212 float scale = 1.0f / sum;
213 for (int i = 0; i < width; ++i) {
214 kernel[i] *= scale;
215 }
216}
217
Brian Salomon68626432020-04-15 12:56:13 +0000218GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
Greg Daniel5c082492020-01-29 15:06:49 -0500219 GrSurfaceProxyView view,
Brian Salomonfc118442019-11-22 19:09:27 -0500220 SkAlphaType alphaType,
221 Direction direction,
222 int radius,
Brian Salomon68626432020-04-15 12:56:13 +0000223 float gaussianSigma,
224 GrTextureDomain::Mode mode,
225 int bounds[2])
Ethan Nicholasabff9562017-10-09 10:54:08 -0400226 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
Brian Salomon68626432020-04-15 12:56:13 +0000227 ModulateForSamplerOptFlags(alphaType, mode == GrTextureDomain::kDecal_Mode))
228 , fCoordTransform(view.proxy(), view.origin())
229 , fTextureSampler(std::move(view))
Brian Salomonb133ffe2017-07-27 11:53:21 -0400230 , fRadius(radius)
Brian Salomon68626432020-04-15 12:56:13 +0000231 , fDirection(direction)
232 , fMode(mode) {
Brian Salomoneb48024f2020-04-13 16:51:39 -0400233 this->addCoordTransform(&fCoordTransform);
Brian Salomon68626432020-04-15 12:56:13 +0000234 this->setTextureSamplerCnt(1);
235 SkASSERT(radius <= kMaxKernelRadius);
236
237 fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
238 // SkGpuBlurUtils is not as aggressive as it once was about avoiding domains. So we check
239 // here if we can omit the domain. TODO: remove this when this effect uses a child to
240 // sample the texture.
241 auto samplerProxy = fTextureSampler.proxy();
242 if (!samplerProxy->isFullyLazy()) {
243 int wh = (fDirection == Direction::kX) ? samplerProxy->backingStoreDimensions().width()
244 : samplerProxy->backingStoreDimensions().height();
245 if (bounds[0] == 0 && bounds[1] == wh) {
246 bool useSampler = false;
247 GrSamplerState::WrapMode samplerMode = GrSamplerState::WrapMode::kClamp;
248 switch (fMode) {
249 case GrTextureDomain::kClamp_Mode:
250 case GrTextureDomain::kIgnore_Mode:
251 useSampler = true;
252 break;
253 case GrTextureDomain::kRepeat_Mode:
254 useSampler = true;
255 samplerMode = GrSamplerState::WrapMode::kRepeat;
256 break;
257 case GrTextureDomain::kMirrorRepeat_Mode:
258 useSampler = true;
259 samplerMode = GrSamplerState::WrapMode::kMirrorRepeat;
260 break;
261 case GrTextureDomain::kDecal_Mode:
262 // Not sure if we support this in HW without having GrCaps here.
263 // Just wait until we replace this with GrTextureEffect.
264 break;
265 }
266 if (useSampler) {
267 fMode = GrTextureDomain::kIgnore_Mode;
268 if (fDirection == Direction::kX) {
269 fTextureSampler.samplerState().setWrapModeX(samplerMode);
270 } else {
271 fTextureSampler.samplerState().setWrapModeY(samplerMode);
272 }
273 }
274 }
275 }
276 memcpy(fBounds, bounds, sizeof(fBounds));
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000277}
278
Brian Salomon3f6f9652017-07-28 07:34:05 -0400279GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
280 const GrGaussianConvolutionFragmentProcessor& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400281 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
Brian Salomon68626432020-04-15 12:56:13 +0000282 , fCoordTransform(that.fCoordTransform)
283 , fTextureSampler(that.fTextureSampler)
Brian Salomon3f6f9652017-07-28 07:34:05 -0400284 , fRadius(that.fRadius)
Brian Salomon68626432020-04-15 12:56:13 +0000285 , fDirection(that.fDirection)
286 , fMode(that.fMode) {
Brian Salomoneb48024f2020-04-13 16:51:39 -0400287 this->addCoordTransform(&fCoordTransform);
Brian Salomon68626432020-04-15 12:56:13 +0000288 this->setTextureSamplerCnt(1);
289 memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
290 memcpy(fBounds, that.fBounds, sizeof(fBounds));
Brian Salomon3f6f9652017-07-28 07:34:05 -0400291}
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000292
Brian Salomonaee504b2017-01-24 12:29:36 -0500293void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
294 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800295 GrGLConvolutionEffect::GenKey(*this, caps, b);
296}
297
Brian Salomonaee504b2017-01-24 12:29:36 -0500298GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800299 return new GrGLConvolutionEffect;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000300}
301
Brian Salomonaee504b2017-01-24 12:29:36 -0500302bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
Brian Salomon68626432020-04-15 12:56:13 +0000303 const GrGaussianConvolutionFragmentProcessor& s =
304 sBase.cast<GrGaussianConvolutionFragmentProcessor>();
305 return (this->radius() == s.radius() && this->direction() == s.direction() &&
306 this->mode() == s.mode() &&
307 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
308 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000309}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000310
311///////////////////////////////////////////////////////////////////////////////
312
Brian Salomonaee504b2017-01-24 12:29:36 -0500313GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000314
Hal Canary6f6961e2017-01-31 13:50:44 -0500315#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400316std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
Brian Salomonaee504b2017-01-24 12:29:36 -0500317 GrProcessorTestData* d) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500318 auto [view, ct, at] = d->randomView();
Brian Salomonaee504b2017-01-24 12:29:36 -0500319
Brian Salomoneb48024f2020-04-13 16:51:39 -0400320 int bounds[2];
Brian Salomon68626432020-04-15 12:56:13 +0000321 int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000322
Brian Salomon68626432020-04-15 12:56:13 +0000323 Direction dir;
324 if (d->fRandom->nextBool()) {
325 dir = Direction::kX;
326 bounds[0] = d->fRandom->nextRangeU(0, view.width()-2);
327 bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, view.width()-1);
328 } else {
329 dir = Direction::kY;
330 bounds[0] = d->fRandom->nextRangeU(0, view.height()-2);
331 bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, view.height()-1);
332 }
333
Robert Phillips08c5ec72017-01-30 12:26:47 -0500334 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
Brian Salomonaee504b2017-01-24 12:29:36 -0500335 float sigma = radius / 3.f;
Robert Phillips08c5ec72017-01-30 12:26:47 -0500336
Brian Salomon68626432020-04-15 12:56:13 +0000337 return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma,
338 static_cast<GrTextureDomain::Mode>(modeIdx),
339 bounds);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000340}
Hal Canary6f6961e2017-01-31 13:50:44 -0500341#endif