1 /*
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
8 #include "GrGaussianConvolutionFragmentProcessor.h"
9
10 #include "GrTexture.h"
11 #include "GrTextureProxy.h"
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLProgramDataManager.h"
15 #include "glsl/GrGLSLUniformHandler.h"
16
17 // For brevity
18 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
19 using Direction = GrGaussianConvolutionFragmentProcessor::Direction;
20
21 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor {
22 public:
23 void emitCode(EmitArgs&) override;
24
25 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
26
27 protected:
28 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
29
30 private:
31 UniformHandle fKernelUni;
32 UniformHandle fImageIncrementUni;
33 UniformHandle fBoundsUni;
34
35 typedef GrGLSLFragmentProcessor INHERITED;
36 };
37
emitCode(EmitArgs & args)38 void GrGLConvolutionEffect::emitCode(EmitArgs& args) {
39 const GrGaussianConvolutionFragmentProcessor& ce =
40 args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
41
42 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
43 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
44 "ImageIncrement");
45 if (ce.useBounds()) {
46 fBoundsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
47 "Bounds");
48 }
49
50 int width = ce.width();
51
52 int arrayCount = (width + 3) / 4;
53 SkASSERT(4 * arrayCount >= width);
54
55 fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, kHalf4_GrSLType,
56 "Kernel", arrayCount);
57
58 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
59 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
60
61 fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
62
63 const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni);
64 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
65
66 fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc);
67 fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
68
69 // Manually unroll loop because some drivers don't; yields 20-30% speedup.
70 const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
71 for (int i = 0; i < width; i++) {
72 SkString index;
73 SkString kernelIndex;
74 index.appendS32(i / 4);
75 kernel.appendArrayAccess(index.c_str(), &kernelIndex);
76 kernelIndex.append(kVecSuffix[i & 0x3]);
77
78 fragBuilder->codeAppend("coordSampled = coord;");
79 if (ce.useBounds()) {
80 // We used to compute a bool indicating whether we're in bounds or not, cast it to a
81 // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
82 // to have a bug that caused corruption.
83 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni);
84 const char* component = ce.direction() == Direction::kY ? "y" : "x";
85
86 switch (ce.mode()) {
87 case GrTextureDomain::kClamp_Mode: {
88 fragBuilder->codeAppendf("coordSampled.%s = clamp(coord.%s, %s.x, %s.y);\n",
89 component, component, bounds, bounds);
90 break;
91 }
92 case GrTextureDomain::kRepeat_Mode: {
93 fragBuilder->codeAppendf("coordSampled.%s = "
94 "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n",
95 component, component, bounds, bounds, bounds, bounds);
96 break;
97 }
98 case GrTextureDomain::kDecal_Mode: {
99 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
100 component, bounds, component, bounds);
101 break;
102 }
103 default: {
104 SK_ABORT("Unsupported operation.");
105 }
106 }
107 }
108 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
109 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled");
110 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
111 if (GrTextureDomain::kDecal_Mode == ce.mode()) {
112 fragBuilder->codeAppend("}");
113 }
114 fragBuilder->codeAppendf("coord += %s;\n", imgInc);
115 }
116 fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
117 }
118
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)119 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
120 const GrFragmentProcessor& processor) {
121 const GrGaussianConvolutionFragmentProcessor& conv =
122 processor.cast<GrGaussianConvolutionFragmentProcessor>();
123 GrSurfaceProxy* proxy = conv.textureSampler(0).proxy();
124 GrTexture& texture = *proxy->peekTexture();
125
126 float imageIncrement[2] = {0};
127 float ySign = proxy->origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
128 switch (conv.direction()) {
129 case Direction::kX:
130 imageIncrement[0] = 1.0f / texture.width();
131 break;
132 case Direction::kY:
133 imageIncrement[1] = ySign / texture.height();
134 break;
135 default:
136 SK_ABORT("Unknown filter direction.");
137 }
138 pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
139 if (conv.useBounds()) {
140 float bounds[2] = {0};
141 bounds[0] = conv.bounds()[0];
142 bounds[1] = conv.bounds()[1];
143 if (GrTextureDomain::kClamp_Mode == conv.mode()) {
144 bounds[0] += SK_ScalarHalf;
145 bounds[1] -= SK_ScalarHalf;
146 }
147 if (Direction::kX == conv.direction()) {
148 SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width()));
149 bounds[0] *= inv;
150 bounds[1] *= inv;
151 } else {
152 SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height()));
153 if (proxy->origin() != kTopLeft_GrSurfaceOrigin) {
154 float tmp = bounds[0];
155 bounds[0] = 1.0f - (inv * bounds[1]);
156 bounds[1] = 1.0f - (inv * tmp);
157 } else {
158 bounds[0] *= inv;
159 bounds[1] *= inv;
160 }
161 }
162
163 SkASSERT(bounds[0] <= bounds[1]);
164 pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
165 }
166 int width = conv.width();
167
168 int arrayCount = (width + 3) / 4;
169 SkASSERT(4 * arrayCount >= width);
170 pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
171 }
172
GenKey(const GrProcessor & processor,const GrShaderCaps &,GrProcessorKeyBuilder * b)173 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
174 GrProcessorKeyBuilder* b) {
175 const GrGaussianConvolutionFragmentProcessor& conv =
176 processor.cast<GrGaussianConvolutionFragmentProcessor>();
177 uint32_t key = conv.radius();
178 key <<= 3;
179 key |= Direction::kY == conv.direction() ? 0x4 : 0x0;
180 key |= static_cast<uint32_t>(conv.mode());
181 b->add32(key);
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
fill_in_1D_gaussian_kernel(float * kernel,int width,float gaussianSigma,int radius)185 static void fill_in_1D_gaussian_kernel(float* kernel, int width, float gaussianSigma, int radius) {
186 const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;
187 if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) {
188 for (int i = 0; i < width; ++i) {
189 kernel[i] = 0.0f;
190 }
191 return;
192 }
193
194 const float denom = 1.0f / twoSigmaSqrd;
195
196 float sum = 0.0f;
197 for (int i = 0; i < width; ++i) {
198 float x = static_cast<float>(i - radius);
199 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
200 // is dropped here, since we renormalize the kernel below.
201 kernel[i] = sk_float_exp(-x * x * denom);
202 sum += kernel[i];
203 }
204 // Normalize the kernel
205 float scale = 1.0f / sum;
206 for (int i = 0; i < width; ++i) {
207 kernel[i] *= scale;
208 }
209 }
210
GrGaussianConvolutionFragmentProcessor(sk_sp<GrTextureProxy> proxy,Direction direction,int radius,float gaussianSigma,GrTextureDomain::Mode mode,int bounds[2])211 GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
212 sk_sp<GrTextureProxy> proxy,
213 Direction direction,
214 int radius,
215 float gaussianSigma,
216 GrTextureDomain::Mode mode,
217 int bounds[2])
218 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
219 ModulateForSamplerOptFlags(proxy->config(),
220 mode == GrTextureDomain::kDecal_Mode))
221 , fCoordTransform(proxy.get())
222 , fTextureSampler(std::move(proxy))
223 , fRadius(radius)
224 , fDirection(direction)
225 , fMode(mode) {
226 // Make sure the sampler's ctor uses the clamp wrap mode
227 SkASSERT(fTextureSampler.samplerState().wrapModeX() == GrSamplerState::WrapMode::kClamp &&
228 fTextureSampler.samplerState().wrapModeY() == GrSamplerState::WrapMode::kClamp);
229 this->addCoordTransform(&fCoordTransform);
230 this->setTextureSamplerCnt(1);
231 SkASSERT(radius <= kMaxKernelRadius);
232
233 fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
234
235 memcpy(fBounds, bounds, sizeof(fBounds));
236 }
237
GrGaussianConvolutionFragmentProcessor(const GrGaussianConvolutionFragmentProcessor & that)238 GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
239 const GrGaussianConvolutionFragmentProcessor& that)
240 : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
241 , fCoordTransform(that.fCoordTransform)
242 , fTextureSampler(that.fTextureSampler)
243 , fRadius(that.fRadius)
244 , fDirection(that.fDirection)
245 , fMode(that.fMode) {
246 this->addCoordTransform(&fCoordTransform);
247 this->setTextureSamplerCnt(1);
248 memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
249 memcpy(fBounds, that.fBounds, sizeof(fBounds));
250 }
251
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const252 void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
253 GrProcessorKeyBuilder* b) const {
254 GrGLConvolutionEffect::GenKey(*this, caps, b);
255 }
256
onCreateGLSLInstance() const257 GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const {
258 return new GrGLConvolutionEffect;
259 }
260
onIsEqual(const GrFragmentProcessor & sBase) const261 bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
262 const GrGaussianConvolutionFragmentProcessor& s =
263 sBase.cast<GrGaussianConvolutionFragmentProcessor>();
264 return (this->radius() == s.radius() && this->direction() == s.direction() &&
265 this->mode() == s.mode() &&
266 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
267 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
268 }
269
270 ///////////////////////////////////////////////////////////////////////////////
271
272 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor);
273
274 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)275 std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate(
276 GrProcessorTestData* d) {
277 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
278 : GrProcessorUnitTest::kAlphaTextureIdx;
279 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
280
281 int bounds[2];
282 int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1);
283
284 Direction dir;
285 if (d->fRandom->nextBool()) {
286 dir = Direction::kX;
287 bounds[0] = d->fRandom->nextRangeU(0, proxy->width()-2);
288 bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->width()-1);
289 } else {
290 dir = Direction::kY;
291 bounds[0] = d->fRandom->nextRangeU(0, proxy->height()-2);
292 bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->height()-1);
293 }
294
295 int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
296 float sigma = radius / 3.f;
297
298 return GrGaussianConvolutionFragmentProcessor::Make(
299 d->textureProxy(texIdx),
300 dir, radius, sigma, static_cast<GrTextureDomain::Mode>(modeIdx), bounds);
301 }
302 #endif
303