Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [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 GrGaussianConvolutionFragmentProcessor_DEFINED |
| 9 | #define GrGaussianConvolutionFragmentProcessor_DEFINED |
| 10 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
| 12 | #include "GrFragmentProcessor.h" |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 13 | #include "GrTextureDomain.h" |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * A 1D Gaussian convolution effect. The kernel is computed as an array of 2 * half-width weights. |
| 17 | * Each texel is multiplied by it's weight and summed to determine the filtered color. The output |
| 18 | * color is set to a modulation of the filtered and input colors. |
| 19 | */ |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 20 | class GrGaussianConvolutionFragmentProcessor : public GrFragmentProcessor { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 21 | public: |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 22 | enum class Direction { kX, kY }; |
| 23 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 24 | /// Convolve with a Gaussian kernel |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 25 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 26 | Direction dir, |
| 27 | int halfWidth, |
| 28 | float gaussianSigma, |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 29 | GrTextureDomain::Mode mode, |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 30 | int* bounds) { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 31 | return sk_sp<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor( |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 32 | std::move(proxy), dir, halfWidth, gaussianSigma, mode, bounds)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 35 | const float* kernel() const { return fKernel; } |
| 36 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 37 | const int* bounds() const { return fBounds; } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 38 | bool useBounds() const { return fMode != GrTextureDomain::kIgnore_Mode; } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 39 | int radius() const { return fRadius; } |
| 40 | int width() const { return 2 * fRadius + 1; } |
| 41 | Direction direction() const { return fDirection; } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 42 | |
| 43 | GrTextureDomain::Mode mode() const { return fMode; } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 44 | |
| 45 | const char* name() const override { return "GaussianConvolution"; } |
| 46 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 47 | sk_sp<GrFragmentProcessor> clone() const override { |
| 48 | return sk_sp<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(*this)); |
| 49 | } |
| 50 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 51 | // This was decided based on the min allowed value for the max texture |
| 52 | // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
| 53 | // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
| 54 | // would exceed a 32 wide kernel. |
| 55 | static const int kMaxKernelRadius = 12; |
| 56 | // With a C++11 we could have a constexpr version of WidthFromRadius() |
| 57 | // and not have to duplicate this calculation. |
| 58 | static const int kMaxKernelWidth = 2 * kMaxKernelRadius + 1; |
| 59 | |
| 60 | private: |
| 61 | /// Convolve with a Gaussian kernel |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 62 | GrGaussianConvolutionFragmentProcessor(sk_sp<GrTextureProxy>, Direction, |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 63 | int halfWidth, float gaussianSigma, |
| 64 | GrTextureDomain::Mode mode, int bounds[2]); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 65 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 66 | explicit GrGaussianConvolutionFragmentProcessor(const GrGaussianConvolutionFragmentProcessor&); |
| 67 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 68 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 69 | |
| 70 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 71 | |
| 72 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 73 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 74 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 75 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 76 | GrCoordTransform fCoordTransform; |
| 77 | TextureSampler fTextureSampler; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 78 | // TODO: Inline the kernel constants into the generated shader code. This may involve pulling |
| 79 | // some of the logic from SkGpuBlurUtils into this class related to radius/sigma calculations. |
| 80 | float fKernel[kMaxKernelWidth]; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 81 | int fBounds[2]; |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 82 | int fRadius; |
| 83 | Direction fDirection; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 84 | GrTextureDomain::Mode fMode; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 85 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 86 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #endif |