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