OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrConvolutionEffect_DEFINED | 8 #ifndef GrConvolutionEffect_DEFINED |
9 #define GrConvolutionEffect_DEFINED | 9 #define GrConvolutionEffect_DEFINED |
10 | 10 |
11 #include "Gr1DKernelEffect.h" | 11 #include "Gr1DKernelEffect.h" |
12 #include "GrInvariantOutput.h" | 12 #include "GrInvariantOutput.h" |
13 | 13 |
14 /** | 14 /** |
15 * A convolution effect. The kernel is specified as an array of 2 * half-width | 15 * A convolution effect. The kernel is specified as an array of 2 * half-width |
16 * + 1 weights. Each texel is multiplied by it's weight and summed to determine | 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. | 17 * the output color. The output color is modulated by the input color. |
18 */ | 18 */ |
19 class GrConvolutionEffect : public Gr1DKernelEffect { | 19 class GrConvolutionEffect : public Gr1DKernelEffect { |
20 | 20 |
21 public: | 21 public: |
22 | 22 |
23 /// Convolve with an arbitrary user-specified kernel | 23 /// Convolve with an arbitrary user-specified kernel |
24 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager, | 24 static GrFragmentProcessor* Create(GrTexture* tex, |
25 GrTexture* tex, | |
26 Direction dir, | 25 Direction dir, |
27 int halfWidth, | 26 int halfWidth, |
28 const float* kernel, | 27 const float* kernel, |
29 bool useBounds, | 28 bool useBounds, |
30 float bounds[2]) { | 29 float bounds[2]) { |
31 return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, ker
nel, useBounds, | 30 return new GrConvolutionEffect(tex, dir, halfWidth, kernel, useBounds, b
ounds); |
32 bounds); | |
33 } | 31 } |
34 | 32 |
35 /// Convolve with a Gaussian kernel | 33 /// Convolve with a Gaussian kernel |
36 static GrFragmentProcessor* CreateGaussian(GrProcessorDataManager* procDataM
anager, | 34 static GrFragmentProcessor* CreateGaussian(GrTexture* tex, |
37 GrTexture* tex, | |
38 Direction dir, | 35 Direction dir, |
39 int halfWidth, | 36 int halfWidth, |
40 float gaussianSigma, | 37 float gaussianSigma, |
41 bool useBounds, | 38 bool useBounds, |
42 float bounds[2]) { | 39 float bounds[2]) { |
43 return new GrConvolutionEffect(procDataManager, tex, dir, halfWidth, gau
ssianSigma, | 40 return new GrConvolutionEffect(tex, dir, halfWidth, gaussianSigma, useBo
unds, bounds); |
44 useBounds, bounds); | |
45 } | 41 } |
46 | 42 |
47 virtual ~GrConvolutionEffect(); | 43 virtual ~GrConvolutionEffect(); |
48 | 44 |
49 const float* kernel() const { return fKernel; } | 45 const float* kernel() const { return fKernel; } |
50 | 46 |
51 const float* bounds() const { return fBounds; } | 47 const float* bounds() const { return fBounds; } |
52 bool useBounds() const { return fUseBounds; } | 48 bool useBounds() const { return fUseBounds; } |
53 | 49 |
54 const char* name() const override { return "Convolution"; } | 50 const char* name() const override { return "Convolution"; } |
55 | 51 |
56 enum { | 52 enum { |
57 // This was decided based on the min allowed value for the max texture | 53 // 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 | 54 // 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 | 55 // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
60 // would exceed a 32 wide kernel. | 56 // would exceed a 32 wide kernel. |
61 kMaxKernelRadius = 12, | 57 kMaxKernelRadius = 12, |
62 // With a C++11 we could have a constexpr version of WidthFromRadius() | 58 // With a C++11 we could have a constexpr version of WidthFromRadius() |
63 // and not have to duplicate this calculation. | 59 // and not have to duplicate this calculation. |
64 kMaxKernelWidth = 2 * kMaxKernelRadius + 1, | 60 kMaxKernelWidth = 2 * kMaxKernelRadius + 1, |
65 }; | 61 }; |
66 | 62 |
67 protected: | 63 protected: |
68 | 64 |
69 float fKernel[kMaxKernelWidth]; | 65 float fKernel[kMaxKernelWidth]; |
70 bool fUseBounds; | 66 bool fUseBounds; |
71 float fBounds[2]; | 67 float fBounds[2]; |
72 | 68 |
73 private: | 69 private: |
74 GrConvolutionEffect(GrProcessorDataManager*, | 70 GrConvolutionEffect(GrTexture*, Direction, |
75 GrTexture*, Direction, | |
76 int halfWidth, | 71 int halfWidth, |
77 const float* kernel, | 72 const float* kernel, |
78 bool useBounds, | 73 bool useBounds, |
79 float bounds[2]); | 74 float bounds[2]); |
80 | 75 |
81 /// Convolve with a Gaussian kernel | 76 /// Convolve with a Gaussian kernel |
82 GrConvolutionEffect(GrProcessorDataManager*, | 77 GrConvolutionEffect(GrTexture*, Direction, |
83 GrTexture*, Direction, | |
84 int halfWidth, | 78 int halfWidth, |
85 float gaussianSigma, | 79 float gaussianSigma, |
86 bool useBounds, | 80 bool useBounds, |
87 float bounds[2]); | 81 float bounds[2]); |
88 | 82 |
89 GrGLFragmentProcessor* onCreateGLInstance() const override; | 83 GrGLFragmentProcessor* onCreateGLInstance() const override; |
90 | 84 |
91 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const ov
erride; | 85 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const ov
erride; |
92 | 86 |
93 bool onIsEqual(const GrFragmentProcessor&) const override; | 87 bool onIsEqual(const GrFragmentProcessor&) const override; |
94 | 88 |
95 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 89 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
96 // If the texture was opaque we could know that the output color if we k
new the sum of the | 90 // If the texture was opaque we could know that the output color if we k
new the sum of the |
97 // kernel values. | 91 // kernel values. |
98 inout->mulByUnknownFourComponents(); | 92 inout->mulByUnknownFourComponents(); |
99 } | 93 } |
100 | 94 |
101 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 95 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
102 | 96 |
103 typedef Gr1DKernelEffect INHERITED; | 97 typedef Gr1DKernelEffect INHERITED; |
104 }; | 98 }; |
105 | 99 |
106 #endif | 100 #endif |
OLD | NEW |