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 | |
| 8 | #include "GrConvolutionEffect.h" |
| 9 | #include "gl/GrGLProgramStage.h" |
| 10 | #include "gl/GrGLSL.h" |
| 11 | #include "gl/GrGLTexture.h" |
| 12 | #include "GrProgramStageFactory.h" |
| 13 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 14 | // For brevity |
| 15 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 16 | static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 17 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 18 | class GrGLConvolutionEffect : public GrGLProgramStage { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 19 | public: |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 20 | GrGLConvolutionEffect(const GrProgramStageFactory& factory, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 21 | const GrCustomStage& stage); |
| 22 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 23 | virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 24 | virtual void emitVS(GrGLShaderBuilder* builder, |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 25 | const char* vertexCoords) SK_OVERRIDE {}; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 26 | virtual void emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 27 | const char* outputColor, |
| 28 | const char* inputColor, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame^] | 29 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 31 | virtual void setData(const GrGLUniformManager& uman, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 32 | const GrCustomStage&, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 33 | const GrRenderTarget*, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 34 | int stageNum) SK_OVERRIDE; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 35 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 36 | static inline StageKey GenKey(const GrCustomStage&, const GrGLCaps&); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 37 | |
| 38 | private: |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 39 | int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
| 40 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 41 | int fRadius; |
| 42 | UniformHandle fKernelUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 43 | UniformHandle fImageIncrementUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 44 | |
| 45 | typedef GrGLProgramStage INHERITED; |
| 46 | }; |
| 47 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 48 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProgramStageFactory& factory, |
| 49 | const GrCustomStage& stage) |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 50 | : GrGLProgramStage(factory) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 51 | , fKernelUni(kInvalidUniformHandle) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 52 | , fImageIncrementUni(kInvalidUniformHandle) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 53 | const GrConvolutionEffect& c = |
| 54 | static_cast<const GrConvolutionEffect&>(stage); |
| 55 | fRadius = c.radius(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 56 | } |
| 57 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 58 | void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) { |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 59 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 60 | kVec2f_GrSLType, "ImageIncrement"); |
| 61 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 62 | kFloat_GrSLType, "Kernel", this->width()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 65 | void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 66 | const char* outputColor, |
| 67 | const char* inputColor, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame^] | 68 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 69 | SkString* code = &builder->fFSCode; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 70 | |
tomhudson@google.com | db3a154 | 2012-06-13 18:12:57 +0000 | [diff] [blame] | 71 | code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | fc0d23a | 2012-06-15 21:50:15 +0000 | [diff] [blame] | 73 | int width = this ->width(); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 74 | const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 75 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | 5c33b71 | 2012-08-03 18:22:53 +0000 | [diff] [blame] | 77 | code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n", |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 78 | builder->defaultTexCoordsName(), fRadius, imgInc); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 79 | |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 80 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
bsalomon@google.com | fc0d23a | 2012-06-15 21:50:15 +0000 | [diff] [blame] | 81 | for (int i = 0; i < width; i++) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 82 | SkString index; |
| 83 | SkString kernelIndex; |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 84 | index.appendS32(i); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 85 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
tomhudson@google.com | db3a154 | 2012-06-13 18:12:57 +0000 | [diff] [blame] | 86 | code->appendf("\t\t%s += ", outputColor); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame^] | 87 | builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord"); |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 88 | code->appendf(" * %s;\n", kernelIndex.c_str()); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 89 | code->appendf("\t\tcoord += %s;\n", imgInc); |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 90 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 91 | GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 94 | void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 95 | const GrCustomStage& data, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 96 | const GrRenderTarget*, |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 97 | int stageNum) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 98 | const GrConvolutionEffect& conv = |
| 99 | static_cast<const GrConvolutionEffect&>(data); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 100 | GrTexture& texture = *data.texture(0); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 101 | // the code we generated was for a specific kernel radius |
| 102 | GrAssert(conv.radius() == fRadius); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 103 | float imageIncrement[2] = { 0 }; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 104 | switch (conv.direction()) { |
| 105 | case Gr1DKernelEffect::kX_Direction: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 106 | imageIncrement[0] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 107 | break; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 108 | case Gr1DKernelEffect::kY_Direction: |
| 109 | imageIncrement[1] = 1.0f / texture.height(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 110 | break; |
| 111 | default: |
| 112 | GrCrash("Unknown filter direction."); |
| 113 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 114 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 115 | uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 118 | GrGLProgramStage::StageKey GrGLConvolutionEffect::GenKey(const GrCustomStage& s, |
| 119 | const GrGLCaps& caps) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 120 | return static_cast<const GrConvolutionEffect&>(s).radius(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 123 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 124 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 125 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 126 | Direction direction, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 127 | int radius, |
| 128 | const float* kernel) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 129 | : Gr1DKernelEffect(texture, direction, radius) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 130 | GrAssert(radius <= kMaxKernelRadius); |
| 131 | int width = this->width(); |
| 132 | if (NULL != kernel) { |
| 133 | for (int i = 0; i < width; i++) { |
| 134 | fKernel[i] = kernel[i]; |
| 135 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 139 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 140 | Direction direction, |
| 141 | int radius, |
| 142 | float gaussianSigma) |
| 143 | : Gr1DKernelEffect(texture, direction, radius) { |
| 144 | GrAssert(radius <= kMaxKernelRadius); |
| 145 | int width = this->width(); |
| 146 | |
| 147 | float sum = 0.0f; |
| 148 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 149 | for (int i = 0; i < width; ++i) { |
| 150 | float x = static_cast<float>(i - this->radius()); |
| 151 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 152 | // is dropped here, since we renormalize the kernel below. |
| 153 | fKernel[i] = sk_float_exp(- x * x * denom); |
| 154 | sum += fKernel[i]; |
| 155 | } |
| 156 | // Normalize the kernel |
| 157 | float scale = 1.0f / sum; |
| 158 | for (int i = 0; i < width; ++i) { |
| 159 | fKernel[i] *= scale; |
| 160 | } |
| 161 | } |
| 162 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 163 | GrConvolutionEffect::~GrConvolutionEffect() { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 166 | const GrProgramStageFactory& GrConvolutionEffect::getFactory() const { |
| 167 | return GrTProgramStageFactory<GrConvolutionEffect>::getInstance(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 168 | } |
| 169 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 170 | bool GrConvolutionEffect::isEqual(const GrCustomStage& sBase) const { |
| 171 | const GrConvolutionEffect& s = |
| 172 | static_cast<const GrConvolutionEffect&>(sBase); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 173 | return (INHERITED::isEqual(sBase) && |
| 174 | this->radius() == s.radius() && |
| 175 | this->direction() == s.direction() && |
| 176 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 177 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 178 | |
| 179 | /////////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | GR_DEFINE_CUSTOM_STAGE_TEST(GrConvolutionEffect); |
| 182 | |
| 183 | GrCustomStage* GrConvolutionEffect::TestCreate(SkRandom* random, |
| 184 | GrContext* context, |
| 185 | GrTexture* textures[]) { |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 186 | int texIdx = random->nextBool() ? GrCustomStageUnitTest::kSkiaPMTextureIdx : |
| 187 | GrCustomStageUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 188 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 189 | int radius = random->nextRangeU(1, kMaxKernelRadius); |
| 190 | float kernel[kMaxKernelRadius]; |
| 191 | for (int i = 0; i < kMaxKernelRadius; ++i) { |
| 192 | kernel[i] = random->nextSScalar1(); |
| 193 | } |
| 194 | |
| 195 | return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel)); |
| 196 | } |
| 197 | |