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" | |
11 #include "gl/GrGLSL.h" | 10 #include "gl/GrGLSL.h" |
12 #include "gl/GrGLTexture.h" | 11 #include "gl/GrGLTexture.h" |
13 #include "GrTBackendEffectFactory.h" | 12 #include "GrTBackendEffectFactory.h" |
14 | 13 |
15 // For brevity | 14 // For brevity |
16 typedef GrGLUniformManager::UniformHandle UniformHandle; | 15 typedef GrGLUniformManager::UniformHandle UniformHandle; |
17 | 16 |
18 class GrGLConvolutionEffect : public GrGLEffect { | 17 class GrGLConvolutionEffect : public GrGLEffect { |
19 public: | 18 public: |
20 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 19 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
21 | 20 |
22 virtual void emitCode(GrGLShaderBuilder*, | 21 virtual void emitCode(GrGLShaderBuilder*, |
23 const GrDrawEffect&, | 22 const GrDrawEffect&, |
24 EffectKey, | 23 EffectKey, |
25 const char* outputColor, | 24 const char* outputColor, |
26 const char* inputColor, | 25 const char* inputColor, |
| 26 const TransformedCoordsArray&, |
27 const TextureSamplerArray&) SK_OVERRIDE; | 27 const TextureSamplerArray&) SK_OVERRIDE; |
28 | 28 |
29 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK
_OVERRIDE; | 29 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK
_OVERRIDE; |
30 | 30 |
31 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 31 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
32 | 32 |
33 private: | 33 private: |
34 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } | 34 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
35 bool useBounds() const { return fUseBounds; } | 35 bool useBounds() const { return fUseBounds; } |
36 Gr1DKernelEffect::Direction direction() const { return fDirection; } | 36 Gr1DKernelEffect::Direction direction() const { return fDirection; } |
37 | 37 |
38 int fRadius; | 38 int fRadius; |
39 bool fUseBounds; | 39 bool fUseBounds; |
40 Gr1DKernelEffect::Direction fDirection; | 40 Gr1DKernelEffect::Direction fDirection; |
41 UniformHandle fKernelUni; | 41 UniformHandle fKernelUni; |
42 UniformHandle fImageIncrementUni; | 42 UniformHandle fImageIncrementUni; |
43 UniformHandle fBoundsUni; | 43 UniformHandle fBoundsUni; |
44 GrGLEffectMatrix fEffectMatrix; | |
45 | 44 |
46 typedef GrGLEffect INHERITED; | 45 typedef GrGLEffect INHERITED; |
47 }; | 46 }; |
48 | 47 |
49 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, | 48 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, |
50 const GrDrawEffect& drawEffect) | 49 const GrDrawEffect& drawEffect) |
51 : INHERITED(factory) | 50 : INHERITED(factory) { |
52 , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) { | |
53 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); | 51 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); |
54 fRadius = c.radius(); | 52 fRadius = c.radius(); |
55 fUseBounds = c.useBounds(); | 53 fUseBounds = c.useBounds(); |
56 fDirection = c.direction(); | 54 fDirection = c.direction(); |
57 } | 55 } |
58 | 56 |
59 void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, | 57 void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
60 const GrDrawEffect&, | 58 const GrDrawEffect&, |
61 EffectKey key, | 59 EffectKey key, |
62 const char* outputColor, | 60 const char* outputColor, |
63 const char* inputColor, | 61 const char* inputColor, |
| 62 const TransformedCoordsArray& coords, |
64 const TextureSamplerArray& samplers) { | 63 const TextureSamplerArray& samplers) { |
65 SkString coords; | 64 SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
66 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | |
67 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | 65 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, |
68 kVec2f_GrSLType, "ImageIncrement"); | 66 kVec2f_GrSLType, "ImageIncrement"); |
69 if (this->useBounds()) { | 67 if (this->useBounds()) { |
70 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility
, | 68 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility
, |
71 kVec2f_GrSLType, "Bounds"); | 69 kVec2f_GrSLType, "Bounds"); |
72 } | 70 } |
73 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit
y, | 71 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit
y, |
74 kFloat_GrSLType, "Kernel", this->width
()); | 72 kFloat_GrSLType, "Kernel", this->width
()); |
75 | 73 |
76 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 74 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
77 | 75 |
78 int width = this->width(); | 76 int width = this->width(); |
79 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); | 77 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
80 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 78 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
81 | 79 |
82 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords.c_str(),
fRadius, imgInc); | 80 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); |
83 | 81 |
84 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 82 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
85 for (int i = 0; i < width; i++) { | 83 for (int i = 0; i < width; i++) { |
86 SkString index; | 84 SkString index; |
87 SkString kernelIndex; | 85 SkString kernelIndex; |
88 index.appendS32(i); | 86 index.appendS32(i); |
89 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 87 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
90 builder->fsCodeAppendf("\t\t%s += ", outputColor); | 88 builder->fsCodeAppendf("\t\t%s += ", outputColor); |
91 builder->fsAppendTextureLookup(samplers[0], "coord"); | 89 builder->fsAppendTextureLookup(samplers[0], "coord"); |
92 if (this->useBounds()) { | 90 if (this->useBounds()) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 if (conv.useBounds()) { | 124 if (conv.useBounds()) { |
127 const float* bounds = conv.bounds(); | 125 const float* bounds = conv.bounds(); |
128 if (Gr1DKernelEffect::kY_Direction == conv.direction() && | 126 if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
129 texture.origin() != kTopLeft_GrSurfaceOrigin) { | 127 texture.origin() != kTopLeft_GrSurfaceOrigin) { |
130 uman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); | 128 uman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
131 } else { | 129 } else { |
132 uman.set2f(fBoundsUni, bounds[0], bounds[1]); | 130 uman.set2f(fBoundsUni, bounds[0], bounds[1]); |
133 } | 131 } |
134 } | 132 } |
135 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); | 133 uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); |
136 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0)); | |
137 } | 134 } |
138 | 135 |
139 GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffe
ct, | 136 GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffe
ct, |
140 const GrGLCaps&) { | 137 const GrGLCaps&) { |
141 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); | 138 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); |
142 EffectKey key = conv.radius(); | 139 EffectKey key = conv.radius(); |
143 key <<= 2; | 140 key <<= 2; |
144 if (conv.useBounds()) { | 141 if (conv.useBounds()) { |
145 key |= 0x2; | 142 key |= 0x2; |
146 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; | 143 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; |
147 } | 144 } |
148 key <<= GrGLEffectMatrix::kKeyBits; | 145 return key; |
149 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(), | |
150 drawEffect, | |
151 conv.coordsType(), | |
152 conv.texture(0)); | |
153 return key | matrixKey; | |
154 } | 146 } |
155 | 147 |
156 /////////////////////////////////////////////////////////////////////////////// | 148 /////////////////////////////////////////////////////////////////////////////// |
157 | 149 |
158 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, | 150 GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
159 Direction direction, | 151 Direction direction, |
160 int radius, | 152 int radius, |
161 const float* kernel, | 153 const float* kernel, |
162 bool useBounds, | 154 bool useBounds, |
163 float bounds[2]) | 155 float bounds[2]) |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } | 229 } |
238 | 230 |
239 bool useBounds = random->nextBool(); | 231 bool useBounds = random->nextBool(); |
240 return GrConvolutionEffect::Create(textures[texIdx], | 232 return GrConvolutionEffect::Create(textures[texIdx], |
241 dir, | 233 dir, |
242 radius, | 234 radius, |
243 kernel, | 235 kernel, |
244 useBounds, | 236 useBounds, |
245 bounds); | 237 bounds); |
246 } | 238 } |
OLD | NEW |