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 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 8 | #include "GrGaussianConvolutionFragmentProcessor.h" |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 9 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 10 | #include "GrTexture.h" |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 11 | #include "GrTextureProxy.h" |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 12 | #include "../private/GrGLSL.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLUniformHandler.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 18 | // For brevity |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 19 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 20 | using Direction = GrGaussianConvolutionFragmentProcessor::Direction; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 21 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 22 | class GrGLConvolutionEffect : public GrGLSLFragmentProcessor { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 23 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 24 | void emitCode(EmitArgs&) override; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 25 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 26 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 27 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 28 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 29 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 30 | |
ericrk | 0f38612 | 2015-07-21 13:15:47 -0700 | [diff] [blame] | 31 | private: |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 32 | UniformHandle fKernelUni; |
| 33 | UniformHandle fImageIncrementUni; |
| 34 | UniformHandle fBoundsUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 35 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 36 | typedef GrGLSLFragmentProcessor INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 39 | void GrGLConvolutionEffect::emitCode(EmitArgs& args) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 40 | const GrGaussianConvolutionFragmentProcessor& ce = |
| 41 | args.fFp.cast<GrGaussianConvolutionFragmentProcessor>(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 42 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 43 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 44 | fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kVec2f_GrSLType, |
| 45 | kDefault_GrSLPrecision, "ImageIncrement"); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 46 | if (ce.useBounds()) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 47 | fBoundsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kVec2f_GrSLType, |
| 48 | kDefault_GrSLPrecision, "Bounds"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 49 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 50 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 51 | int width = ce.width(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 52 | |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 53 | int arrayCount = (width + 3) / 4; |
| 54 | SkASSERT(4 * arrayCount >= width); |
| 55 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 56 | fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, kVec4f_GrSLType, |
| 57 | kDefault_GrSLPrecision, "Kernel", arrayCount); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 58 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 59 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 60 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 61 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 62 | fragBuilder->codeAppendf("%s = vec4(0, 0, 0, 0);", args.fOutputColor); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 63 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 64 | const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 65 | const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 66 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 67 | fragBuilder->codeAppendf("vec2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 68 | fragBuilder->codeAppend("vec2 coordSampled = vec2(0, 0);"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 69 | |
| 70 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 71 | const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"}; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 72 | for (int i = 0; i < width; i++) { |
| 73 | SkString index; |
| 74 | SkString kernelIndex; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 75 | index.appendS32(i / 4); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 76 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 77 | kernelIndex.append(kVecSuffix[i & 0x3]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 78 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 79 | fragBuilder->codeAppend("coordSampled = coord;"); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 80 | if (ce.useBounds()) { |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 81 | // We used to compute a bool indicating whether we're in bounds or not, cast it to a |
| 82 | // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems |
| 83 | // to have a bug that caused corruption. |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 84 | const char* bounds = uniformHandler->getUniformCStr(fBoundsUni); |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 85 | const char* component = ce.direction() == Direction::kY ? "y" : "x"; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 86 | |
| 87 | switch (ce.mode()) { |
| 88 | case GrTextureDomain::kClamp_Mode: { |
| 89 | fragBuilder->codeAppendf("coordSampled.%s = clamp(coord.%s, %s.x, %s.y);\n", |
| 90 | component, component, bounds, bounds); |
| 91 | break; |
| 92 | } |
| 93 | case GrTextureDomain::kRepeat_Mode: { |
| 94 | fragBuilder->codeAppendf("coordSampled.%s = " |
| 95 | "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n", |
| 96 | component, component, bounds, bounds, bounds, bounds); |
| 97 | break; |
| 98 | } |
| 99 | case GrTextureDomain::kDecal_Mode: { |
| 100 | fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {", |
| 101 | component, bounds, component, bounds); |
| 102 | break; |
| 103 | } |
| 104 | default: { |
| 105 | SkFAIL("Unsupported operation."); |
| 106 | } |
| 107 | } |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 108 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 109 | fragBuilder->codeAppendf("%s += ", args.fOutputColor); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 110 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 111 | fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 112 | if (GrTextureDomain::kDecal_Mode == ce.mode()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 113 | fragBuilder->codeAppend("}"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 114 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 115 | fragBuilder->codeAppendf("coord += %s;\n", imgInc); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 116 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 117 | fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 118 | } |
| 119 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 120 | void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 121 | const GrFragmentProcessor& processor) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 122 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 123 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 124 | GrSurfaceProxy* proxy = conv.textureSampler(0).proxy(); |
| 125 | GrTexture& texture = *proxy->priv().peekTexture(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 126 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 127 | float imageIncrement[2] = {0}; |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 128 | float ySign = proxy->origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 129 | switch (conv.direction()) { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 130 | case Direction::kX: |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 131 | imageIncrement[0] = 1.0f / texture.width(); |
| 132 | break; |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 133 | case Direction::kY: |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 134 | imageIncrement[1] = ySign / texture.height(); |
| 135 | break; |
| 136 | default: |
| 137 | SkFAIL("Unknown filter direction."); |
| 138 | } |
| 139 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 140 | if (conv.useBounds()) { |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 141 | float bounds[2] = {0}; |
| 142 | bounds[0] = conv.bounds()[0]; |
| 143 | bounds[1] = conv.bounds()[1]; |
| 144 | if (GrTextureDomain::kClamp_Mode == conv.mode()) { |
| 145 | bounds[0] += SK_ScalarHalf; |
| 146 | bounds[1] -= SK_ScalarHalf; |
| 147 | } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 148 | if (Direction::kX == conv.direction()) { |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 149 | SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width())); |
| 150 | pdman.set2f(fBoundsUni, inv * bounds[0], inv * bounds[1]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 151 | } else { |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 152 | SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height())); |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 153 | if (proxy->origin() != kTopLeft_GrSurfaceOrigin) { |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 154 | pdman.set2f(fBoundsUni, 1.0f - (inv * bounds[1]), 1.0f - (inv * bounds[0])); |
| 155 | } else { |
| 156 | pdman.set2f(fBoundsUni, inv * bounds[1], inv * bounds[0]); |
| 157 | } |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 160 | int width = conv.width(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 161 | |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 162 | int arrayCount = (width + 3) / 4; |
| 163 | SkASSERT(4 * arrayCount >= width); |
| 164 | pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 167 | void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | GrProcessorKeyBuilder* b) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 169 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 170 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 171 | uint32_t key = conv.radius(); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 172 | key <<= 3; |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 173 | key |= Direction::kY == conv.direction() ? 0x4 : 0x0; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 174 | key |= static_cast<uint32_t>(conv.mode()); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 175 | b->add32(key); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 178 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 179 | static void fill_in_1D_guassian_kernel(float* kernel, int width, float gaussianSigma, int radius) { |
| 180 | const float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 181 | |
| 182 | float sum = 0.0f; |
| 183 | for (int i = 0; i < width; ++i) { |
| 184 | float x = static_cast<float>(i - radius); |
| 185 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 186 | // is dropped here, since we renormalize the kernel below. |
| 187 | kernel[i] = sk_float_exp(-x * x * denom); |
| 188 | sum += kernel[i]; |
| 189 | } |
| 190 | // Normalize the kernel |
| 191 | float scale = 1.0f / sum; |
| 192 | for (int i = 0; i < width; ++i) { |
| 193 | kernel[i] *= scale; |
| 194 | } |
| 195 | } |
| 196 | |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 197 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 198 | sk_sp<GrTextureProxy> proxy, |
| 199 | Direction direction, |
| 200 | int radius, |
| 201 | float gaussianSigma, |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 202 | GrTextureDomain::Mode mode, |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 203 | int bounds[2]) |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 204 | : INHERITED(ModulateByConfigOptimizationFlags(proxy->config())) |
| 205 | , fCoordTransform(proxy.get()) |
| 206 | , fTextureSampler(std::move(proxy)) |
| 207 | , fRadius(radius) |
| 208 | , fDirection(direction) |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 209 | , fMode(mode) { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 210 | this->initClassID<GrGaussianConvolutionFragmentProcessor>(); |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 211 | this->addCoordTransform(&fCoordTransform); |
| 212 | this->addTextureSampler(&fTextureSampler); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 213 | SkASSERT(radius <= kMaxKernelRadius); |
| 214 | |
| 215 | fill_in_1D_guassian_kernel(fKernel, this->width(), gaussianSigma, this->radius()); |
| 216 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 217 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame^] | 220 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
| 221 | const GrGaussianConvolutionFragmentProcessor& that) |
| 222 | : INHERITED(that.optimizationFlags()) |
| 223 | , fCoordTransform(that.fCoordTransform) |
| 224 | , fTextureSampler(that.fTextureSampler) |
| 225 | , fRadius(that.fRadius) |
| 226 | , fDirection(that.fDirection) |
| 227 | , fMode(that.fMode) { |
| 228 | this->initClassID<GrGaussianConvolutionFragmentProcessor>(); |
| 229 | this->addCoordTransform(&fCoordTransform); |
| 230 | this->addTextureSampler(&fTextureSampler); |
| 231 | memcpy(fKernel, that.fKernel, that.width() * sizeof(float)); |
| 232 | memcpy(fBounds, that.fBounds, sizeof(fBounds)); |
| 233 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 234 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 235 | void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 236 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 237 | GrGLConvolutionEffect::GenKey(*this, caps, b); |
| 238 | } |
| 239 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 240 | GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 241 | return new GrGLConvolutionEffect; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 244 | bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 245 | const GrGaussianConvolutionFragmentProcessor& s = |
| 246 | sBase.cast<GrGaussianConvolutionFragmentProcessor>(); |
| 247 | return (this->radius() == s.radius() && this->direction() == s.direction() && |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 248 | this->mode() == s.mode() && |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 249 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 250 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 251 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 252 | |
| 253 | /////////////////////////////////////////////////////////////////////////////// |
| 254 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 255 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 256 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 257 | #if GR_TEST_UTILS |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 258 | sk_sp<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate( |
| 259 | GrProcessorTestData* d) { |
| 260 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 261 | : GrProcessorUnitTest::kAlphaTextureIdx; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 262 | sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 263 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 264 | int bounds[2]; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 265 | int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 266 | |
| 267 | Direction dir; |
| 268 | if (d->fRandom->nextBool()) { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 269 | dir = Direction::kX; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 270 | bounds[0] = d->fRandom->nextRangeU(0, proxy->width()-1); |
| 271 | bounds[1] = d->fRandom->nextRangeU(bounds[0], proxy->width()-1); |
| 272 | } else { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 273 | dir = Direction::kY; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 274 | bounds[0] = d->fRandom->nextRangeU(0, proxy->height()-1); |
| 275 | bounds[1] = d->fRandom->nextRangeU(bounds[0], proxy->height()-1); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 276 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 277 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 278 | int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 279 | float sigma = radius / 3.f; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 280 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 281 | return GrGaussianConvolutionFragmentProcessor::Make( |
| 282 | d->textureProxy(texIdx), |
| 283 | dir, radius, sigma, static_cast<GrTextureDomain::Mode>(modeIdx), bounds); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 284 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 285 | #endif |