| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrConvolutionEffect.h" | 8 #include "GrConvolutionEffect.h" |
| 9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
| 10 #include "gl/GrGLEffectMatrix.h" | 10 #include "gl/GrGLEffectMatrix.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 const char* outputColor, | 26 const char* outputColor, |
| 27 const char* inputColor, | 27 const char* inputColor, |
| 28 const TextureSamplerArray&) SK_OVERRIDE; | 28 const TextureSamplerArray&) SK_OVERRIDE; |
| 29 | 29 |
| 30 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK
_OVERRIDE; | 30 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK
_OVERRIDE; |
| 31 | 31 |
| 32 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 32 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } | 35 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
| 36 bool useCropRect() const { return fUseCropRect; } |
| 36 | 37 |
| 37 int fRadius; | 38 int fRadius; |
| 39 bool fUseCropRect; |
| 38 UniformHandle fKernelUni; | 40 UniformHandle fKernelUni; |
| 39 UniformHandle fImageIncrementUni; | 41 UniformHandle fImageIncrementUni; |
| 42 UniformHandle fCropRectUni; |
| 40 GrGLEffectMatrix fEffectMatrix; | 43 GrGLEffectMatrix fEffectMatrix; |
| 41 | 44 |
| 42 typedef GrGLEffect INHERITED; | 45 typedef GrGLEffect INHERITED; |
| 43 }; | 46 }; |
| 44 | 47 |
| 45 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, | 48 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, |
| 46 const GrDrawEffect& drawEffect) | 49 const GrDrawEffect& drawEffect) |
| 47 : INHERITED(factory) | 50 : INHERITED(factory) |
| 48 , fKernelUni(kInvalidUniformHandle) | 51 , fKernelUni(kInvalidUniformHandle) |
| 49 , fImageIncrementUni(kInvalidUniformHandle) | 52 , fImageIncrementUni(kInvalidUniformHandle) |
| 53 , fCropRectUni(kInvalidUniformHandle) |
| 50 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) { | 54 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) { |
| 51 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); | 55 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); |
| 52 fRadius = c.radius(); | 56 fRadius = c.radius(); |
| 57 fUseCropRect = c.useCropRect(); |
| 53 } | 58 } |
| 54 | 59 |
| 55 void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, | 60 void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
| 56 const GrDrawEffect&, | 61 const GrDrawEffect&, |
| 57 EffectKey key, | 62 EffectKey key, |
| 58 const char* outputColor, | 63 const char* outputColor, |
| 59 const char* inputColor, | 64 const char* inputColor, |
| 60 const TextureSamplerArray& samplers) { | 65 const TextureSamplerArray& samplers) { |
| 61 const char* coords; | 66 const char* coords; |
| 62 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | 67 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
| 63 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 68 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, |
| 64 kVec2f_GrSLType, "ImageIncrement"); | 69 kVec2f_GrSLType, "ImageIncrement"); |
| 70 if (this->useCropRect()) { |
| 71 fCropRectUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderTy
pe, |
| 72 kVec4f_GrSLType, "CropRect"); |
| 73 } |
| 65 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, | 74 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, |
| 66 kFloat_GrSLType, "Kernel", this->width
()); | 75 kFloat_GrSLType, "Kernel", this->width
()); |
| 67 | 76 |
| 68 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 77 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| 69 | 78 |
| 70 int width = this ->width(); | 79 int width = this->width(); |
| 71 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); | 80 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 72 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 81 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 73 | 82 |
| 74 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius
, imgInc); | 83 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius
, imgInc); |
| 75 | 84 |
| 76 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 85 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| 77 for (int i = 0; i < width; i++) { | 86 for (int i = 0; i < width; i++) { |
| 78 SkString index; | 87 SkString index; |
| 79 SkString kernelIndex; | 88 SkString kernelIndex; |
| 80 index.appendS32(i); | 89 index.appendS32(i); |
| 81 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 90 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| 82 builder->fsCodeAppendf("\t\t%s += ", outputColor); | 91 builder->fsCodeAppendf("\t\t%s += ", outputColor); |
| 83 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sa
mplers[0], "coord"); | 92 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sa
mplers[0], "coord"); |
| 93 if (this->useCropRect()) { |
| 94 const char* cropRect = builder->getUniformCStr(fCropRectUni); |
| 95 builder->fsCodeAppendf(" * float(coord.x >= %s.x && coord.x <= %s.y
&& coord.y >= %s.z && coord.y <= %s.w)", |
| 96 cropRect, cropRect, cropRect, cropRect); |
| 97 } |
| 84 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str()); | 98 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 85 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc); | 99 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc); |
| 86 } | 100 } |
| 101 |
| 87 SkString modulate; | 102 SkString modulate; |
| 88 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 103 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 89 builder->fsCodeAppend(modulate.c_str()); | 104 builder->fsCodeAppend(modulate.c_str()); |
| 90 } | 105 } |
| 91 | 106 |
| 92 void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, | 107 void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, |
| 93 const GrDrawEffect& drawEffect) { | 108 const GrDrawEffect& drawEffect) { |
| 94 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); | 109 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); |
| 95 GrTexture& texture = *conv.texture(0); | 110 GrTexture& texture = *conv.texture(0); |
| 96 // the code we generated was for a specific kernel radius | 111 // the code we generated was for a specific kernel radius |
| 97 GrAssert(conv.radius() == fRadius); | 112 GrAssert(conv.radius() == fRadius); |
| 98 float imageIncrement[2] = { 0 }; | 113 float imageIncrement[2] = { 0 }; |
| 114 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 99 switch (conv.direction()) { | 115 switch (conv.direction()) { |
| 100 case Gr1DKernelEffect::kX_Direction: | 116 case Gr1DKernelEffect::kX_Direction: |
| 101 imageIncrement[0] = 1.0f / texture.width(); | 117 imageIncrement[0] = 1.0f / texture.width(); |
| 102 break; | 118 break; |
| 103 case Gr1DKernelEffect::kY_Direction: | 119 case Gr1DKernelEffect::kY_Direction: |
| 104 imageIncrement[1] = 1.0f / texture.height(); | 120 imageIncrement[1] = ySign / texture.height(); |
| 105 break; | 121 break; |
| 106 default: | 122 default: |
| 107 GrCrash("Unknown filter direction."); | 123 GrCrash("Unknown filter direction."); |
| 108 } | 124 } |
| 109 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 125 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 126 if (conv.useCropRect()) { |
| 127 float c[4]; |
| 128 memcpy(c, conv.cropRect(), sizeof(c)); |
| 129 if (texture.origin() != kTopLeft_GrSurfaceOrigin) { |
| 130 float tmp = 1.0f - c[2]; |
| 131 c[2] = 1.0f - c[3]; |
| 132 c[3] = tmp; |
| 133 } |
| 134 uman.set4fv(fCropRectUni, 0, 1, c); |
| 135 } |
| 110 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); | 136 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); |
| 111 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0)); | 137 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0)); |
| 112 } | 138 } |
| 113 | 139 |
| 114 GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffe
ct, | 140 GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffe
ct, |
| 115 const GrGLCaps&) { | 141 const GrGLCaps&) { |
| 116 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); | 142 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); |
| 117 EffectKey key = conv.radius(); | 143 EffectKey key = conv.radius() << 1; |
| 144 key |= conv.useCropRect() ? 0x1 : 0x0; |
| 118 key <<= GrGLEffectMatrix::kKeyBits; | 145 key <<= GrGLEffectMatrix::kKeyBits; |
| 119 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(), | 146 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(), |
| 120 drawEffect, | 147 drawEffect, |
| 121 conv.coordsType(), | 148 conv.coordsType(), |
| 122 conv.texture(0)); | 149 conv.texture(0)); |
| 123 return key | matrixKey; | 150 return key | matrixKey; |
| 124 } | 151 } |
| 125 | 152 |
| 126 /////////////////////////////////////////////////////////////////////////////// | 153 /////////////////////////////////////////////////////////////////////////////// |
| 127 | 154 |
| 128 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, | 155 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 129 Direction direction, | 156 Direction direction, |
| 130 int radius, | 157 int radius, |
| 131 const float* kernel) | 158 const float* kernel, |
| 132 : Gr1DKernelEffect(texture, direction, radius) { | 159 bool useCropRect, |
| 160 float cropRect[4]) |
| 161 : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) { |
| 133 GrAssert(radius <= kMaxKernelRadius); | 162 GrAssert(radius <= kMaxKernelRadius); |
| 134 GrAssert(NULL != kernel); | 163 GrAssert(NULL != kernel); |
| 135 int width = this->width(); | 164 int width = this->width(); |
| 136 for (int i = 0; i < width; i++) { | 165 for (int i = 0; i < width; i++) { |
| 137 fKernel[i] = kernel[i]; | 166 fKernel[i] = kernel[i]; |
| 138 } | 167 } |
| 168 memcpy(fCropRect, cropRect, sizeof(fCropRect)); |
| 139 } | 169 } |
| 140 | 170 |
| 141 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, | 171 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 142 Direction direction, | 172 Direction direction, |
| 143 int radius, | 173 int radius, |
| 144 float gaussianSigma) | 174 float gaussianSigma, |
| 145 : Gr1DKernelEffect(texture, direction, radius) { | 175 bool useCropRect, |
| 176 float cropRect[4]) |
| 177 : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) { |
| 146 GrAssert(radius <= kMaxKernelRadius); | 178 GrAssert(radius <= kMaxKernelRadius); |
| 147 int width = this->width(); | 179 int width = this->width(); |
| 148 | 180 |
| 149 float sum = 0.0f; | 181 float sum = 0.0f; |
| 150 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); | 182 float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 151 for (int i = 0; i < width; ++i) { | 183 for (int i = 0; i < width; ++i) { |
| 152 float x = static_cast<float>(i - this->radius()); | 184 float x = static_cast<float>(i - this->radius()); |
| 153 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian | 185 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 154 // is dropped here, since we renormalize the kernel below. | 186 // is dropped here, since we renormalize the kernel below. |
| 155 fKernel[i] = sk_float_exp(- x * x * denom); | 187 fKernel[i] = sk_float_exp(- x * x * denom); |
| 156 sum += fKernel[i]; | 188 sum += fKernel[i]; |
| 157 } | 189 } |
| 158 // Normalize the kernel | 190 // Normalize the kernel |
| 159 float scale = 1.0f / sum; | 191 float scale = 1.0f / sum; |
| 160 for (int i = 0; i < width; ++i) { | 192 for (int i = 0; i < width; ++i) { |
| 161 fKernel[i] *= scale; | 193 fKernel[i] *= scale; |
| 162 } | 194 } |
| 195 memcpy(fCropRect, cropRect, sizeof(fCropRect)); |
| 163 } | 196 } |
| 164 | 197 |
| 165 GrConvolutionEffect::~GrConvolutionEffect() { | 198 GrConvolutionEffect::~GrConvolutionEffect() { |
| 166 } | 199 } |
| 167 | 200 |
| 168 const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { | 201 const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
| 169 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); | 202 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
| 170 } | 203 } |
| 171 | 204 |
| 172 bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 205 bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
| 173 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); | 206 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); |
| 174 return (this->texture(0) == s.texture(0) && | 207 return (this->texture(0) == s.texture(0) && |
| 175 this->radius() == s.radius() && | 208 this->radius() == s.radius() && |
| 176 this->direction() == s.direction() && | 209 this->direction() == s.direction() && |
| 210 0 == memcmp(fCropRect, s.fCropRect, sizeof(fCropRect)) && |
| 177 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); | 211 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
| 178 } | 212 } |
| 179 | 213 |
| 180 /////////////////////////////////////////////////////////////////////////////// | 214 /////////////////////////////////////////////////////////////////////////////// |
| 181 | 215 |
| 182 GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); | 216 GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); |
| 183 | 217 |
| 184 GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random, | 218 GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random, |
| 185 GrContext*, | 219 GrContext*, |
| 186 const GrDrawTargetCaps&, | 220 const GrDrawTargetCaps&, |
| 187 GrTexture* textures[]) { | 221 GrTexture* textures[]) { |
| 188 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 222 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 189 GrEffectUnitTest::kAlphaTextureIdx; | 223 GrEffectUnitTest::kAlphaTextureIdx; |
| 190 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; | 224 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 191 int radius = random->nextRangeU(1, kMaxKernelRadius); | 225 int radius = random->nextRangeU(1, kMaxKernelRadius); |
| 192 float kernel[kMaxKernelRadius]; | 226 float kernel[kMaxKernelRadius]; |
| 227 float cropRect[4]; |
| 193 for (int i = 0; i < kMaxKernelRadius; ++i) { | 228 for (int i = 0; i < kMaxKernelRadius; ++i) { |
| 194 kernel[i] = random->nextSScalar1(); | 229 kernel[i] = random->nextSScalar1(); |
| 195 } | 230 } |
| 231 for (int i = 0; i < 4; ++i) { |
| 232 cropRect[i] = random->nextF(); |
| 233 } |
| 196 | 234 |
| 197 return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel); | 235 bool useCropRect = random->nextBool(); |
| 236 return GrConvolutionEffect::Create(textures[texIdx], |
| 237 dir, |
| 238 radius, |
| 239 kernel, |
| 240 useCropRect, |
| 241 cropRect); |
| 198 } | 242 } |
| OLD | NEW |