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/GrGLFragmentProcessor.h" | 9 #include "gl/GrGLFragmentProcessor.h" |
10 #include "gl/GrGLTexture.h" | 10 #include "gl/GrGLTexture.h" |
11 #include "gl/builders/GrGLProgramBuilder.h" | 11 #include "gl/builders/GrGLProgramBuilder.h" |
12 | 12 |
13 // For brevity | 13 // For brevity |
14 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 14 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
15 | 15 |
16 class GrGLConvolutionEffect : public GrGLFragmentProcessor { | 16 class GrGLConvolutionEffect : public GrGLFragmentProcessor { |
17 public: | 17 public: |
18 GrGLConvolutionEffect(const GrProcessor&); | 18 GrGLConvolutionEffect(const GrProcessor&); |
19 | 19 |
20 virtual void emitCode(GrGLFPBuilder*, | 20 virtual void emitCode(EmitArgs&) override; |
21 const GrFragmentProcessor&, | |
22 const char* outputColor, | |
23 const char* inputColor, | |
24 const TransformedCoordsArray&, | |
25 const TextureSamplerArray&) override; | |
26 | 21 |
27 void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) overri
de; | 22 void setData(const GrGLProgramDataManager& pdman, const GrProcessor&) overri
de; |
28 | 23 |
29 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); | 24 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
30 | 25 |
31 private: | 26 private: |
32 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } | 27 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
33 bool useBounds() const { return fUseBounds; } | 28 bool useBounds() const { return fUseBounds; } |
34 Gr1DKernelEffect::Direction direction() const { return fDirection; } | 29 Gr1DKernelEffect::Direction direction() const { return fDirection; } |
35 | 30 |
36 int fRadius; | 31 int fRadius; |
37 bool fUseBounds; | 32 bool fUseBounds; |
38 Gr1DKernelEffect::Direction fDirection; | 33 Gr1DKernelEffect::Direction fDirection; |
39 UniformHandle fKernelUni; | 34 UniformHandle fKernelUni; |
40 UniformHandle fImageIncrementUni; | 35 UniformHandle fImageIncrementUni; |
41 UniformHandle fBoundsUni; | 36 UniformHandle fBoundsUni; |
42 | 37 |
43 typedef GrGLFragmentProcessor INHERITED; | 38 typedef GrGLFragmentProcessor INHERITED; |
44 }; | 39 }; |
45 | 40 |
46 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { | 41 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { |
47 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); | 42 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); |
48 fRadius = c.radius(); | 43 fRadius = c.radius(); |
49 fUseBounds = c.useBounds(); | 44 fUseBounds = c.useBounds(); |
50 fDirection = c.direction(); | 45 fDirection = c.direction(); |
51 } | 46 } |
52 | 47 |
53 void GrGLConvolutionEffect::emitCode(GrGLFPBuilder* builder, | 48 void GrGLConvolutionEffect::emitCode(EmitArgs& args) { |
54 const GrFragmentProcessor&, | 49 fImageIncrementUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, |
55 const char* outputColor, | |
56 const char* inputColor, | |
57 const TransformedCoordsArray& coords, | |
58 const TextureSamplerArray& samplers) { | |
59 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | |
60 kVec2f_GrSLType, kDefault_GrSLPreci
sion, | 50 kVec2f_GrSLType, kDefault_GrSLPreci
sion, |
61 "ImageIncrement"); | 51 "ImageIncrement"); |
62 if (this->useBounds()) { | 52 if (this->useBounds()) { |
63 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, | 53 fBoundsUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, |
64 kVec2f_GrSLType, kDefault_GrSLPrecision
, | 54 kVec2f_GrSLType, kDefault_GrSLPrecision
, |
65 "Bounds"); | 55 "Bounds"); |
66 } | 56 } |
67 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, | 57 fKernelUni = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Vi
sibility, |
68 kFloat_GrSLType, kDefault_GrSLPrecisio
n, | 58 kFloat_GrSLType, kDefault_GrSLPrecisio
n, |
69 "Kernel", this->width()); | 59 "Kernel", this->width()); |
70 | 60 |
71 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 61 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
72 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); | 62 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
73 | 63 |
74 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 64 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor); |
75 | 65 |
76 int width = this->width(); | 66 int width = this->width(); |
77 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); | 67 const GrGLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni); |
78 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 68 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni); |
79 | 69 |
80 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); | 70 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); |
81 | 71 |
82 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 72 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
83 for (int i = 0; i < width; i++) { | 73 for (int i = 0; i < width; i++) { |
84 SkString index; | 74 SkString index; |
85 SkString kernelIndex; | 75 SkString kernelIndex; |
86 index.appendS32(i); | 76 index.appendS32(i); |
87 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 77 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
88 | 78 |
89 if (this->useBounds()) { | 79 if (this->useBounds()) { |
90 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a | 80 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a |
91 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems | 81 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems |
92 // to have a bug that caused corruption. | 82 // to have a bug that caused corruption. |
93 const char* bounds = builder->getUniformCStr(fBoundsUni); | 83 const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni); |
94 const char* component = this->direction() == Gr1DKernelEffect::kY_Di
rection ? "y" : "x"; | 84 const char* component = this->direction() == Gr1DKernelEffect::kY_Di
rection ? "y" : "x"; |
95 fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {"
, | 85 fsBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {"
, |
96 component, bounds, component, bounds); | 86 component, bounds, component, bounds); |
97 } | 87 } |
98 fsBuilder->codeAppendf("\t\t%s += ", outputColor); | 88 fsBuilder->codeAppendf("\t\t%s += ", args.fOutputColor); |
99 fsBuilder->appendTextureLookup(samplers[0], "coord"); | 89 fsBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
100 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); | 90 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
101 if (this->useBounds()) { | 91 if (this->useBounds()) { |
102 fsBuilder->codeAppend("}"); | 92 fsBuilder->codeAppend("}"); |
103 } | 93 } |
104 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); | 94 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); |
105 } | 95 } |
106 | 96 |
107 SkString modulate; | 97 SkString modulate; |
108 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); | 98 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
109 fsBuilder->codeAppend(modulate.c_str()); | 99 fsBuilder->codeAppend(modulate.c_str()); |
110 } | 100 } |
111 | 101 |
112 void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, | 102 void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
113 const GrProcessor& processor) { | 103 const GrProcessor& processor) { |
114 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); | 104 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); |
115 GrTexture& texture = *conv.texture(0); | 105 GrTexture& texture = *conv.texture(0); |
116 // the code we generated was for a specific kernel radius | 106 // the code we generated was for a specific kernel radius |
117 SkASSERT(conv.radius() == fRadius); | 107 SkASSERT(conv.radius() == fRadius); |
118 float imageIncrement[2] = { 0 }; | 108 float imageIncrement[2] = { 0 }; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 232 |
243 bool useBounds = d->fRandom->nextBool(); | 233 bool useBounds = d->fRandom->nextBool(); |
244 return GrConvolutionEffect::Create(d->fProcDataManager, | 234 return GrConvolutionEffect::Create(d->fProcDataManager, |
245 d->fTextures[texIdx], | 235 d->fTextures[texIdx], |
246 dir, | 236 dir, |
247 radius, | 237 radius, |
248 kernel, | 238 kernel, |
249 useBounds, | 239 useBounds, |
250 bounds); | 240 bounds); |
251 } | 241 } |
OLD | NEW |