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 | } |
Brian Salomon | 7caa137 | 2019-12-09 10:40:56 -0500 | [diff] [blame] | 92 | // Deferring implementing kMirrorRepeat until we use DomainEffects as |
| 93 | // child processors. Fallback to Repeat. |
| 94 | case GrTextureDomain::kMirrorRepeat_Mode: |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 95 | case GrTextureDomain::kRepeat_Mode: { |
| 96 | fragBuilder->codeAppendf("coordSampled.%s = " |
| 97 | "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n", |
| 98 | component, component, bounds, bounds, bounds, bounds); |
| 99 | break; |
| 100 | } |
| 101 | case GrTextureDomain::kDecal_Mode: { |
| 102 | fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {", |
| 103 | component, bounds, component, bounds); |
| 104 | break; |
| 105 | } |
| 106 | default: { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 107 | SK_ABORT("Unsupported operation."); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 110 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 111 | fragBuilder->codeAppendf("%s += ", args.fOutputColor); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 112 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 113 | fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 114 | if (GrTextureDomain::kDecal_Mode == ce.mode()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 115 | fragBuilder->codeAppend("}"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 116 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 117 | fragBuilder->codeAppendf("coord += %s;\n", imgInc); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 118 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 119 | fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 120 | } |
| 121 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 122 | void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 123 | const GrFragmentProcessor& processor) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 124 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 125 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
Robert Phillips | c5c0247 | 2019-12-12 13:24:36 +0000 | [diff] [blame^] | 126 | GrSurfaceProxy* proxy = conv.textureSampler(0).proxy(); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 127 | GrTexture& texture = *proxy->peekTexture(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 128 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 129 | float imageIncrement[2] = {0}; |
Robert Phillips | c5c0247 | 2019-12-12 13:24:36 +0000 | [diff] [blame^] | 130 | float ySign = proxy->origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 131 | switch (conv.direction()) { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 132 | case Direction::kX: |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 133 | imageIncrement[0] = 1.0f / texture.width(); |
| 134 | break; |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 135 | case Direction::kY: |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 136 | imageIncrement[1] = ySign / texture.height(); |
| 137 | break; |
| 138 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 139 | SK_ABORT("Unknown filter direction."); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 140 | } |
| 141 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 142 | if (conv.useBounds()) { |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 143 | float bounds[2] = {0}; |
| 144 | bounds[0] = conv.bounds()[0]; |
| 145 | bounds[1] = conv.bounds()[1]; |
| 146 | if (GrTextureDomain::kClamp_Mode == conv.mode()) { |
| 147 | bounds[0] += SK_ScalarHalf; |
| 148 | bounds[1] -= SK_ScalarHalf; |
| 149 | } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 150 | if (Direction::kX == conv.direction()) { |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 151 | SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width())); |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 152 | bounds[0] *= inv; |
| 153 | bounds[1] *= inv; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 154 | } else { |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 155 | SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height())); |
Robert Phillips | c5c0247 | 2019-12-12 13:24:36 +0000 | [diff] [blame^] | 156 | if (proxy->origin() != kTopLeft_GrSurfaceOrigin) { |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 157 | float tmp = bounds[0]; |
| 158 | bounds[0] = 1.0f - (inv * bounds[1]); |
| 159 | bounds[1] = 1.0f - (inv * tmp); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 160 | } else { |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 161 | bounds[0] *= inv; |
| 162 | bounds[1] *= inv; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 163 | } |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 164 | } |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 165 | |
| 166 | SkASSERT(bounds[0] <= bounds[1]); |
| 167 | pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 168 | } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 169 | int width = conv.width(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 170 | |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 171 | int arrayCount = (width + 3) / 4; |
| 172 | SkASSERT(4 * arrayCount >= width); |
| 173 | pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 176 | void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | GrProcessorKeyBuilder* b) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 178 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 179 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 180 | uint32_t key = conv.radius(); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 181 | key <<= 3; |
Brian Osman | ef79fa8 | 2019-07-16 13:34:14 -0400 | [diff] [blame] | 182 | if (conv.useBounds()) { |
| 183 | key |= Direction::kY == conv.direction() ? 0x4 : 0x0; |
| 184 | } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 185 | key |= static_cast<uint32_t>(conv.mode()); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 186 | b->add32(key); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 187 | } |
| 188 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 189 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 190 | 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] | 191 | const float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma; |
Greg Daniel | 3aecc30 | 2018-04-03 13:38:01 -0400 | [diff] [blame] | 192 | if (SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)) { |
| 193 | for (int i = 0; i < width; ++i) { |
| 194 | kernel[i] = 0.0f; |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | const float denom = 1.0f / twoSigmaSqrd; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 200 | |
| 201 | float sum = 0.0f; |
| 202 | for (int i = 0; i < width; ++i) { |
| 203 | float x = static_cast<float>(i - radius); |
| 204 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 205 | // is dropped here, since we renormalize the kernel below. |
| 206 | kernel[i] = sk_float_exp(-x * x * denom); |
| 207 | sum += kernel[i]; |
| 208 | } |
| 209 | // Normalize the kernel |
| 210 | float scale = 1.0f / sum; |
| 211 | for (int i = 0; i < width; ++i) { |
| 212 | kernel[i] *= scale; |
| 213 | } |
| 214 | } |
| 215 | |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 216 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 217 | sk_sp<GrSurfaceProxy> proxy, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 218 | SkAlphaType alphaType, |
| 219 | Direction direction, |
| 220 | int radius, |
| 221 | float gaussianSigma, |
| 222 | GrTextureDomain::Mode mode, |
| 223 | int bounds[2]) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 224 | : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 225 | ModulateForSamplerOptFlags(alphaType, mode == GrTextureDomain::kDecal_Mode)) |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 226 | , fCoordTransform(proxy.get()) |
| 227 | , fTextureSampler(std::move(proxy)) |
| 228 | , fRadius(radius) |
| 229 | , fDirection(direction) |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 230 | , fMode(mode) { |
Michael Ludwig | 257a03d | 2018-12-13 14:07:07 -0500 | [diff] [blame] | 231 | // Make sure the sampler's ctor uses the clamp wrap mode |
| 232 | SkASSERT(fTextureSampler.samplerState().wrapModeX() == GrSamplerState::WrapMode::kClamp && |
| 233 | fTextureSampler.samplerState().wrapModeY() == GrSamplerState::WrapMode::kClamp); |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 234 | this->addCoordTransform(&fCoordTransform); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 235 | this->setTextureSamplerCnt(1); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 236 | SkASSERT(radius <= kMaxKernelRadius); |
| 237 | |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 238 | fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius()); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 239 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 240 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 243 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
| 244 | const GrGaussianConvolutionFragmentProcessor& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 245 | : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags()) |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 246 | , fCoordTransform(that.fCoordTransform) |
| 247 | , fTextureSampler(that.fTextureSampler) |
| 248 | , fRadius(that.fRadius) |
| 249 | , fDirection(that.fDirection) |
| 250 | , fMode(that.fMode) { |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 251 | this->addCoordTransform(&fCoordTransform); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 252 | this->setTextureSamplerCnt(1); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 253 | memcpy(fKernel, that.fKernel, that.width() * sizeof(float)); |
| 254 | memcpy(fBounds, that.fBounds, sizeof(fBounds)); |
| 255 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 256 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 257 | void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 258 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 259 | GrGLConvolutionEffect::GenKey(*this, caps, b); |
| 260 | } |
| 261 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 262 | GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 263 | return new GrGLConvolutionEffect; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 266 | bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 267 | const GrGaussianConvolutionFragmentProcessor& s = |
| 268 | sBase.cast<GrGaussianConvolutionFragmentProcessor>(); |
| 269 | return (this->radius() == s.radius() && this->direction() == s.direction() && |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 270 | this->mode() == s.mode() && |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 271 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 272 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 273 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 274 | |
| 275 | /////////////////////////////////////////////////////////////////////////////// |
| 276 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 277 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 278 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 279 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 280 | std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate( |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 281 | GrProcessorTestData* d) { |
| 282 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 283 | : GrProcessorUnitTest::kAlphaTextureIdx; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 284 | sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 285 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 286 | int bounds[2]; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 287 | int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 288 | |
| 289 | Direction dir; |
| 290 | if (d->fRandom->nextBool()) { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 291 | dir = Direction::kX; |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 292 | bounds[0] = d->fRandom->nextRangeU(0, proxy->width()-2); |
| 293 | bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->width()-1); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 294 | } else { |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 295 | dir = Direction::kY; |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 296 | bounds[0] = d->fRandom->nextRangeU(0, proxy->height()-2); |
| 297 | bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, proxy->height()-1); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 298 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 299 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 300 | int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 301 | float sigma = radius / 3.f; |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 302 | |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 303 | auto alphaType = static_cast<SkAlphaType>( |
| 304 | d->fRandom->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType)); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 305 | return GrGaussianConvolutionFragmentProcessor::Make( |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 306 | std::move(proxy), alphaType, dir, radius, sigma, |
| 307 | static_cast<GrTextureDomain::Mode>(modeIdx), bounds); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 308 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 309 | #endif |