joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "GrMatrixConvolutionEffect.h" |
| 8 | #include "gl/GrGLShaderBuilder.h" |
| 9 | #include "gl/GrGLEffect.h" |
| 10 | #include "gl/GrGLSL.h" |
| 11 | #include "gl/GrGLTexture.h" |
| 12 | #include "GrTBackendEffectFactory.h" |
| 13 | |
| 14 | class GrGLMatrixConvolutionEffect : public GrGLEffect { |
| 15 | public: |
| 16 | GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 17 | const GrDrawEffect& effect); |
| 18 | virtual void emitCode(GrGLShaderBuilder*, |
| 19 | const GrDrawEffect&, |
| 20 | const GrEffectKey&, |
| 21 | const char* outputColor, |
| 22 | const char* inputColor, |
| 23 | const TransformedCoordsArray&, |
| 24 | const TextureSamplerArray&) SK_OVERRIDE; |
| 25 | |
| 26 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
| 27 | |
| 28 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 29 | |
| 30 | private: |
| 31 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 32 | typedef GrMatrixConvolutionEffect::TileMode TileMode; |
| 33 | SkISize fKernelSize; |
| 34 | TileMode fTileMode; |
| 35 | bool fConvolveAlpha; |
| 36 | |
| 37 | UniformHandle fBoundsUni; |
| 38 | UniformHandle fKernelUni; |
| 39 | UniformHandle fImageIncrementUni; |
| 40 | UniformHandle fKernelOffsetUni; |
| 41 | UniformHandle fGainUni; |
| 42 | UniformHandle fBiasUni; |
| 43 | |
| 44 | typedef GrGLEffect INHERITED; |
| 45 | }; |
| 46 | |
| 47 | GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 48 | const GrDrawEffect& drawEffect) |
| 49 | : INHERITED(factory) { |
| 50 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 51 | fKernelSize = m.kernelSize(); |
| 52 | fTileMode = m.tileMode(); |
| 53 | fConvolveAlpha = m.convolveAlpha(); |
| 54 | } |
| 55 | |
| 56 | static void appendTextureLookup(GrGLShaderBuilder* builder, |
| 57 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 58 | const char* coord, |
| 59 | const char* bounds, |
| 60 | GrMatrixConvolutionEffect::TileMode tileMode) { |
| 61 | SkString clampedCoord; |
| 62 | switch (tileMode) { |
| 63 | case GrMatrixConvolutionEffect::kClamp_TileMode: |
| 64 | clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds); |
| 65 | coord = clampedCoord.c_str(); |
| 66 | break; |
| 67 | case GrMatrixConvolutionEffect::kRepeat_TileMode: |
| 68 | clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds); |
| 69 | coord = clampedCoord.c_str(); |
| 70 | break; |
| 71 | case GrMatrixConvolutionEffect::kClampToBlack_TileMode: |
| 72 | builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0, 0) : ", coord, bounds, bounds, coord); |
| 73 | break; |
| 74 | } |
| 75 | builder->fsAppendTextureLookup(sampler, coord); |
| 76 | } |
| 77 | |
| 78 | void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
| 79 | const GrDrawEffect&, |
| 80 | const GrEffectKey& key, |
| 81 | const char* outputColor, |
| 82 | const char* inputColor, |
| 83 | const TransformedCoordsArray& coords, |
| 84 | const TextureSamplerArray& samplers) { |
| 85 | sk_ignore_unused_variable(inputColor); |
| 86 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 87 | fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 88 | kVec4f_GrSLType, "Bounds"); |
| 89 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 90 | kVec2f_GrSLType, "ImageIncrement"); |
| 91 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
| 92 | kFloat_GrSLType, |
| 93 | "Kernel", |
| 94 | fKernelSize.width() * fKernelSize.height()); |
| 95 | fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 96 | kVec2f_GrSLType, "KernelOffset"); |
| 97 | fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 98 | kFloat_GrSLType, "Gain"); |
| 99 | fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 100 | kFloat_GrSLType, "Bias"); |
| 101 | |
| 102 | const char* bounds = builder->getUniformCStr(fBoundsUni); |
| 103 | const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); |
| 104 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 105 | const char* kernel = builder->getUniformCStr(fKernelUni); |
| 106 | const char* gain = builder->getUniformCStr(fGainUni); |
| 107 | const char* bias = builder->getUniformCStr(fBiasUni); |
| 108 | int kWidth = fKernelSize.width(); |
| 109 | int kHeight = fKernelSize.height(); |
| 110 | |
| 111 | builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); |
| 112 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc); |
| 113 | builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); |
| 114 | builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); |
| 115 | builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth); |
| 116 | builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", imgInc); |
| 117 | builder->fsCodeAppend("\t\t\t\tvec4 c = "); |
| 118 | appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); |
| 119 | builder->fsCodeAppend(";\n"); |
| 120 | if (!fConvolveAlpha) { |
| 121 | builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); |
| 122 | } |
| 123 | builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); |
| 124 | builder->fsCodeAppend("\t\t\t}\n"); |
| 125 | builder->fsCodeAppend("\t\t}\n"); |
| 126 | if (fConvolveAlpha) { |
| 127 | builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, bias); |
| 128 | builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", |
| 129 | outputColor, outputColor, outputColor); |
| 130 | } else { |
| 131 | builder->fsCodeAppend("\t\tvec4 c = "); |
| 132 | appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTileMode); |
| 133 | builder->fsCodeAppend(";\n"); |
| 134 | builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); |
| 135 | builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias); |
| 136 | builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | namespace { |
| 141 | |
| 142 | int encodeXY(int x, int y) { |
| 143 | SkASSERT(x >= 1 && y >= 1 && x * y <= 32); |
| 144 | if (y < x) |
| 145 | return 0x40 | encodeXY(y, x); |
| 146 | else |
| 147 | return (0x40 >> x) | (y - x); |
| 148 | } |
| 149 | |
| 150 | }; |
| 151 | |
| 152 | void GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, |
| 153 | const GrGLCaps&, GrEffectKeyBuilder* b) { |
| 154 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 155 | uint32_t key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); |
| 156 | key |= m.tileMode() << 7; |
| 157 | key |= m.convolveAlpha() ? 1 << 9 : 0; |
| 158 | b->add32(key); |
| 159 | } |
| 160 | |
| 161 | void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, |
| 162 | const GrDrawEffect& drawEffect) { |
| 163 | const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 164 | GrTexture& texture = *conv.texture(0); |
| 165 | // the code we generated was for a specific kernel size |
| 166 | SkASSERT(conv.kernelSize() == fKernelSize); |
| 167 | SkASSERT(conv.tileMode() == fTileMode); |
| 168 | float imageIncrement[2]; |
| 169 | float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 170 | imageIncrement[0] = 1.0f / texture.width(); |
| 171 | imageIncrement[1] = ySign / texture.height(); |
| 172 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 173 | uman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
| 174 | uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel()); |
| 175 | uman.set1f(fGainUni, conv.gain()); |
| 176 | uman.set1f(fBiasUni, conv.bias()); |
| 177 | const SkIRect& bounds = conv.bounds(); |
| 178 | float left = (float) bounds.left() / texture.width(); |
| 179 | float top = (float) bounds.top() / texture.height(); |
| 180 | float right = (float) bounds.right() / texture.width(); |
| 181 | float bottom = (float) bounds.bottom() / texture.height(); |
| 182 | if (texture.origin() == kBottomLeft_GrSurfaceOrigin) { |
| 183 | uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top); |
| 184 | } else { |
| 185 | uman.set4f(fBoundsUni, left, top, right, bottom); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
| 190 | const SkIRect& bounds, |
| 191 | const SkISize& kernelSize, |
| 192 | const SkScalar* kernel, |
| 193 | SkScalar gain, |
| 194 | SkScalar bias, |
| 195 | const SkIPoint& kernelOffset, |
| 196 | TileMode tileMode, |
| 197 | bool convolveAlpha) |
| 198 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), |
| 199 | fBounds(bounds), |
| 200 | fKernelSize(kernelSize), |
| 201 | fGain(SkScalarToFloat(gain)), |
| 202 | fBias(SkScalarToFloat(bias) / 255.0f), |
| 203 | fTileMode(tileMode), |
| 204 | fConvolveAlpha(convolveAlpha) { |
| 205 | fKernel = new float[kernelSize.width() * kernelSize.height()]; |
| 206 | for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
| 207 | fKernel[i] = SkScalarToFloat(kernel[i]); |
| 208 | } |
| 209 | fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
| 210 | fKernelOffset[1] = static_cast<float>(kernelOffset.y()); |
| 211 | this->setWillNotUseInputColor(); |
| 212 | } |
| 213 | |
| 214 | GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
| 215 | delete[] fKernel; |
| 216 | } |
| 217 | |
| 218 | const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
| 219 | return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
| 220 | } |
| 221 | |
| 222 | bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
| 223 | const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(sBase); |
| 224 | return this->texture(0) == s.texture(0) && |
| 225 | fKernelSize == s.kernelSize() && |
| 226 | !memcmp(fKernel, s.kernel(), |
| 227 | fKernelSize.width() * fKernelSize.height() * sizeof(float)) && |
| 228 | fGain == s.gain() && |
| 229 | fBias == s.bias() && |
| 230 | fKernelOffset == s.kernelOffset() && |
| 231 | fTileMode == s.tileMode() && |
| 232 | fConvolveAlpha == s.convolveAlpha(); |
| 233 | } |
| 234 | |
| 235 | GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); |
| 236 | |
| 237 | GrEffect* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
| 238 | GrContext* context, |
| 239 | const GrDrawTargetCaps&, |
| 240 | GrTexture* textures[]) { |
| 241 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 242 | GrEffectUnitTest::kAlphaTextureIdx; |
| 243 | int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
| 244 | int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
| 245 | SkISize kernelSize = SkISize::Make(width, height); |
| 246 | SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
| 247 | for (int i = 0; i < width * height; i++) { |
| 248 | kernel.get()[i] = random->nextSScalar1(); |
| 249 | } |
| 250 | SkScalar gain = random->nextSScalar1(); |
| 251 | SkScalar bias = random->nextSScalar1(); |
| 252 | SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), |
| 253 | random->nextRangeU(0, kernelSize.height())); |
| 254 | SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), |
| 255 | random->nextRangeU(0, textures[texIdx]->height()), |
| 256 | random->nextRangeU(0, textures[texIdx]->width()), |
| 257 | random->nextRangeU(0, textures[texIdx]->height())); |
| 258 | TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); |
| 259 | bool convolveAlpha = random->nextBool(); |
| 260 | return GrMatrixConvolutionEffect::Create(textures[texIdx], |
| 261 | bounds, |
| 262 | kernelSize, |
| 263 | kernel.get(), |
| 264 | gain, |
| 265 | bias, |
| 266 | kernelOffset, |
| 267 | tileMode, |
| 268 | convolveAlpha); |
| 269 | } |