tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 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 | #ifndef GrConvolutionEffect_DEFINED |
| 9 | #define GrConvolutionEffect_DEFINED |
| 10 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 11 | #include "Gr1DKernelEffect.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 12 | #include "GrInvariantOutput.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 14 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 15 | * A convolution effect. The kernel is specified as an array of 2 * half-width |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 16 | * + 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 | */ |
| 19 | class GrConvolutionEffect : public Gr1DKernelEffect { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 20 | |
| 21 | public: |
| 22 | |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 23 | /// Convolve with an arbitrary user-specified kernel |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 24 | static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager, |
| 25 | GrTexture* tex, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 26 | Direction dir, |
| 27 | int halfWidth, |
| 28 | const float* kernel, |
| 29 | bool useBounds, |
| 30 | float bounds[2]) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame^] | 31 | return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, kernel, useBounds, |
| 32 | bounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 33 | } |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 35 | /// Convolve with a Gaussian kernel |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 36 | static GrFragmentProcessor* CreateGaussian(GrProcessorDataManager* procDataManager, |
| 37 | GrTexture* tex, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 38 | Direction dir, |
| 39 | int halfWidth, |
| 40 | float gaussianSigma, |
| 41 | bool useBounds, |
| 42 | float bounds[2]) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame^] | 43 | return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, gaussianSigma, |
| 44 | useBounds, bounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 45 | } |
| 46 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 47 | virtual ~GrConvolutionEffect(); |
| 48 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 49 | const float* kernel() const { return fKernel; } |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 50 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 51 | const float* bounds() const { return fBounds; } |
| 52 | bool useBounds() const { return fUseBounds; } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | const char* name() const override { return "Convolution"; } |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 55 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 56 | 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.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 66 | |
| 67 | protected: |
| 68 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 69 | float fKernel[kMaxKernelWidth]; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 70 | bool fUseBounds; |
| 71 | float fBounds[2]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 72 | |
| 73 | private: |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 74 | GrConvolutionEffect(GrProcessorDataManager*, |
| 75 | GrTexture*, Direction, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 76 | int halfWidth, |
| 77 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 78 | bool useBounds, |
| 79 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 80 | |
| 81 | /// Convolve with a Gaussian kernel |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 82 | GrConvolutionEffect(GrProcessorDataManager*, |
| 83 | GrTexture*, Direction, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 84 | int halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 85 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 86 | bool useBounds, |
| 87 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 88 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 89 | GrGLFragmentProcessor* onCreateGLInstance() const override; |
| 90 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 91 | void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
| 92 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 96 | // If the texture was opaque we could know that the output color if we knew the sum of the |
| 97 | // kernel values. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 98 | inout->mulByUnknownFourComponents(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 99 | } |
| 100 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 101 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 103 | typedef Gr1DKernelEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | #endif |