blob: 2cf36af5373adfcd16449df5249c445ce8b3de9b [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
8#ifndef GrConvolutionEffect_DEFINED
9#define GrConvolutionEffect_DEFINED
10
bsalomon@google.comb505a122012-05-31 18:40:36 +000011#include "Gr1DKernelEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013
bsalomon@google.comb505a122012-05-31 18:40:36 +000014/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000015 * A convolution effect. The kernel is specified as an array of 2 * half-width
bsalomon@google.comb505a122012-05-31 18:40:36 +000016 * + 1 weights. Each texel is multiplied by it's weight and summed to determine
17 * the output color. The output color is modulated by the input color.
18 */
19class GrConvolutionEffect : public Gr1DKernelEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000020
21public:
22
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000023 /// Convolve with an arbitrary user-specified kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070024 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager,
25 GrTexture* tex,
joshualittb0a8a372014-09-23 09:50:21 -070026 Direction dir,
27 int halfWidth,
28 const float* kernel,
29 bool useBounds,
30 float bounds[2]) {
halcanary385fe4d2015-08-26 13:07:48 -070031 return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, kernel, useBounds,
32 bounds);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000033 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000034
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000035 /// Convolve with a Gaussian kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070036 static GrFragmentProcessor* CreateGaussian(GrProcessorDataManager* procDataManager,
37 GrTexture* tex,
joshualittb0a8a372014-09-23 09:50:21 -070038 Direction dir,
39 int halfWidth,
40 float gaussianSigma,
41 bool useBounds,
42 float bounds[2]) {
halcanary385fe4d2015-08-26 13:07:48 -070043 return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, gaussianSigma,
44 useBounds, bounds);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000045 }
46
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000047 virtual ~GrConvolutionEffect();
48
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000049 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000050
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000051 const float* bounds() const { return fBounds; }
52 bool useBounds() const { return fUseBounds; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000053
mtklein36352bf2015-03-25 18:17:31 -070054 const char* name() const override { return "Convolution"; }
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000055
bsalomon@google.comb505a122012-05-31 18:40:36 +000056 enum {
57 // This was decided based on the min allowed value for the max texture
58 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
59 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
60 // would exceed a 32 wide kernel.
61 kMaxKernelRadius = 12,
62 // With a C++11 we could have a constexpr version of WidthFromRadius()
63 // and not have to duplicate this calculation.
64 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
65 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000066
67protected:
68
bsalomon@google.comb505a122012-05-31 18:40:36 +000069 float fKernel[kMaxKernelWidth];
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000070 bool fUseBounds;
71 float fBounds[2];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000072
73private:
joshualitt5f10b5c2015-07-09 10:24:35 -070074 GrConvolutionEffect(GrProcessorDataManager*,
75 GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000076 int halfWidth,
77 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000078 bool useBounds,
79 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000080
81 /// Convolve with a Gaussian kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070082 GrConvolutionEffect(GrProcessorDataManager*,
83 GrTexture*, Direction,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000084 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000085 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000086 bool useBounds,
87 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000088
wangyixb1daa862015-08-18 11:29:31 -070089 GrGLFragmentProcessor* onCreateGLInstance() const override;
90
wangyix4b3050b2015-08-04 07:59:37 -070091 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
92
mtklein36352bf2015-03-25 18:17:31 -070093 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000094
mtklein36352bf2015-03-25 18:17:31 -070095 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070096 // If the texture was opaque we could know that the output color if we knew the sum of the
97 // kernel values.
joshualitt56995b52014-12-11 15:44:02 -080098 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -070099 }
100
joshualittb0a8a372014-09-23 09:50:21 -0700101 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000102
bsalomon@google.comb505a122012-05-31 18:40:36 +0000103 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000104};
105
106#endif