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" |
| 9 | #include "../private/GrGLSL.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 10 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLUniformHandler.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 15 | // For brevity |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 16 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 17 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | class GrGLConvolutionEffect : public GrGLSLFragmentProcessor { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 19 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 20 | void emitCode(EmitArgs&) override; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 21 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 22 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 23 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 24 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 25 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 26 | |
ericrk | 0f38612 | 2015-07-21 13:15:47 -0700 | [diff] [blame] | 27 | private: |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 28 | UniformHandle fKernelUni; |
| 29 | UniformHandle fImageIncrementUni; |
| 30 | UniformHandle fBoundsUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 31 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 32 | typedef GrGLSLFragmentProcessor INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 35 | void GrGLConvolutionEffect::emitCode(EmitArgs& args) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 36 | const GrGaussianConvolutionFragmentProcessor& ce = |
| 37 | args.fFp.cast<GrGaussianConvolutionFragmentProcessor>(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 38 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 39 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 40 | fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kVec2f_GrSLType, |
| 41 | kDefault_GrSLPrecision, "ImageIncrement"); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 42 | if (ce.useBounds()) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 43 | fBoundsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kVec2f_GrSLType, |
| 44 | kDefault_GrSLPrecision, "Bounds"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 45 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 46 | |
| 47 | int width = Gr1DKernelEffect::WidthFromRadius(ce.radius()); |
| 48 | |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 49 | int arrayCount = (width + 3) / 4; |
| 50 | SkASSERT(4 * arrayCount >= width); |
| 51 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 52 | fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, kVec4f_GrSLType, |
| 53 | kDefault_GrSLPrecision, "Kernel", arrayCount); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 54 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 55 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 56 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 57 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 58 | fragBuilder->codeAppendf("%s = vec4(0, 0, 0, 0);", args.fOutputColor); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 59 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 60 | const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 61 | const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 62 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 63 | fragBuilder->codeAppendf("vec2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 64 | |
| 65 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 66 | const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"}; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 67 | for (int i = 0; i < width; i++) { |
| 68 | SkString index; |
| 69 | SkString kernelIndex; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 70 | index.appendS32(i / 4); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 71 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 72 | kernelIndex.append(kVecSuffix[i & 0x3]); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 73 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 74 | if (ce.useBounds()) { |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 75 | // We used to compute a bool indicating whether we're in bounds or not, cast it to a |
| 76 | // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems |
| 77 | // to have a bug that caused corruption. |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 78 | const char* bounds = uniformHandler->getUniformCStr(fBoundsUni); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 79 | const char* component = ce.direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x"; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 80 | fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {", component, |
| 81 | bounds, component, bounds); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 82 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 83 | fragBuilder->codeAppendf("%s += ", args.fOutputColor); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 84 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 85 | fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 86 | if (ce.useBounds()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 87 | fragBuilder->codeAppend("}"); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 88 | } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 89 | fragBuilder->codeAppendf("coord += %s;\n", imgInc); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 93 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 94 | fragBuilder->codeAppend(modulate.c_str()); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 97 | void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 98 | const GrProcessor& processor) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 99 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 100 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 101 | GrTexture& texture = *conv.textureSampler(0).texture(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 102 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 103 | float imageIncrement[2] = {0}; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 104 | float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 105 | switch (conv.direction()) { |
| 106 | case Gr1DKernelEffect::kX_Direction: |
| 107 | imageIncrement[0] = 1.0f / texture.width(); |
| 108 | break; |
| 109 | case Gr1DKernelEffect::kY_Direction: |
| 110 | imageIncrement[1] = ySign / texture.height(); |
| 111 | break; |
| 112 | default: |
| 113 | SkFAIL("Unknown filter direction."); |
| 114 | } |
| 115 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 116 | if (conv.useBounds()) { |
| 117 | const float* bounds = conv.bounds(); |
| 118 | if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
| 119 | texture.origin() != kTopLeft_GrSurfaceOrigin) { |
| 120 | pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
| 121 | } else { |
| 122 | pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
| 123 | } |
| 124 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 125 | int width = Gr1DKernelEffect::WidthFromRadius(conv.radius()); |
| 126 | |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 127 | int arrayCount = (width + 3) / 4; |
| 128 | SkASSERT(4 * arrayCount >= width); |
| 129 | pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 132 | void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | GrProcessorKeyBuilder* b) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 134 | const GrGaussianConvolutionFragmentProcessor& conv = |
| 135 | processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 136 | uint32_t key = conv.radius(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 137 | key <<= 2; |
| 138 | if (conv.useBounds()) { |
| 139 | key |= 0x2; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 140 | key |= GrGaussianConvolutionFragmentProcessor::kY_Direction == conv.direction() ? 0x1 : 0x0; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 141 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 142 | b->add32(key); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 145 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 146 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 147 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(GrTexture* texture, |
| 148 | Direction direction, |
| 149 | int radius, |
| 150 | float gaussianSigma, |
| 151 | bool useBounds, |
| 152 | float bounds[2]) |
| 153 | : INHERITED(texture, direction, radius), fUseBounds(useBounds) { |
| 154 | this->initClassID<GrGaussianConvolutionFragmentProcessor>(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 155 | SkASSERT(radius <= kMaxKernelRadius); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 156 | int width = this->width(); |
| 157 | |
| 158 | float sum = 0.0f; |
| 159 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 160 | for (int i = 0; i < width; ++i) { |
| 161 | float x = static_cast<float>(i - this->radius()); |
| 162 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 163 | // is dropped here, since we renormalize the kernel below. |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 164 | fKernel[i] = sk_float_exp(-x * x * denom); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 165 | sum += fKernel[i]; |
| 166 | } |
| 167 | // Normalize the kernel |
| 168 | float scale = 1.0f / sum; |
| 169 | for (int i = 0; i < width; ++i) { |
| 170 | fKernel[i] *= scale; |
| 171 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 172 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 175 | GrGaussianConvolutionFragmentProcessor::~GrGaussianConvolutionFragmentProcessor() {} |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 176 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 177 | void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 178 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 179 | GrGLConvolutionEffect::GenKey(*this, caps, b); |
| 180 | } |
| 181 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 182 | GrGLSLFragmentProcessor* GrGaussianConvolutionFragmentProcessor::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 183 | return new GrGLConvolutionEffect; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 186 | bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 187 | const GrGaussianConvolutionFragmentProcessor& s = |
| 188 | sBase.cast<GrGaussianConvolutionFragmentProcessor>(); |
| 189 | return (this->radius() == s.radius() && this->direction() == s.direction() && |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 190 | this->useBounds() == s.useBounds() && |
| 191 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 192 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 193 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 194 | |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 197 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 198 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 199 | sk_sp<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate( |
| 200 | GrProcessorTestData* d) { |
| 201 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 202 | : GrProcessorUnitTest::kAlphaTextureIdx; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 203 | Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
| 204 | int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 205 | |
| 206 | bool useBounds = d->fRandom->nextBool(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 207 | float bounds[2]; |
bungeman@google.com | 4348663 | 2013-08-20 15:20:34 +0000 | [diff] [blame] | 208 | for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 209 | bounds[i] = d->fRandom->nextF(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 210 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 211 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame^] | 212 | float sigma = radius / 3.f; |
| 213 | return GrGaussianConvolutionFragmentProcessor::Make( |
| 214 | d->fTextures[texIdx], dir, radius, sigma, useBounds, bounds); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 215 | } |