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