| 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 "glsl/GrGLSLFragmentProcessor.h" | 9 #include "glsl/GrGLSLFragmentProcessor.h" |
| 10 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 10 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 11 #include "glsl/GrGLSLProgramDataManager.h" | 11 #include "glsl/GrGLSLProgramDataManager.h" |
| 12 #include "glsl/GrGLSLUniformHandler.h" | 12 #include "glsl/GrGLSLUniformHandler.h" |
| 13 | 13 |
| 14 // For brevity | 14 // For brevity |
| 15 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; | 15 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
| 16 | 16 |
| 17 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor { | 17 class GrGLConvolutionEffect : public GrGLSLFragmentProcessor { |
| 18 public: | 18 public: |
| 19 GrGLConvolutionEffect(const GrProcessor&); | |
| 20 | |
| 21 void emitCode(EmitArgs&) override; | 19 void emitCode(EmitArgs&) override; |
| 22 | 20 |
| 23 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); | 21 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
| 24 | 22 |
| 25 protected: | 23 protected: |
| 26 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) ov
erride; | 24 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&) ov
erride; |
| 27 | 25 |
| 28 private: | 26 private: |
| 29 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } | |
| 30 bool useBounds() const { return fUseBounds; } | |
| 31 Gr1DKernelEffect::Direction direction() const { return fDirection; } | |
| 32 | |
| 33 int fRadius; | |
| 34 bool fUseBounds; | |
| 35 Gr1DKernelEffect::Direction fDirection; | |
| 36 UniformHandle fKernelUni; | 27 UniformHandle fKernelUni; |
| 37 UniformHandle fImageIncrementUni; | 28 UniformHandle fImageIncrementUni; |
| 38 UniformHandle fBoundsUni; | 29 UniformHandle fBoundsUni; |
| 39 | 30 |
| 40 typedef GrGLSLFragmentProcessor INHERITED; | 31 typedef GrGLSLFragmentProcessor INHERITED; |
| 41 }; | 32 }; |
| 42 | 33 |
| 43 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { | 34 void GrGLConvolutionEffect::emitCode(EmitArgs& args) { |
| 44 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); | 35 const GrConvolutionEffect& ce = args.fFp.cast<GrConvolutionEffect>(); |
| 45 fRadius = c.radius(); | |
| 46 fUseBounds = c.useBounds(); | |
| 47 fDirection = c.direction(); | |
| 48 } | |
| 49 | 36 |
| 50 void GrGLConvolutionEffect::emitCode(EmitArgs& args) { | |
| 51 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 37 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 52 fImageIncrementUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragm
ent_Visibility, | 38 fImageIncrementUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragm
ent_Visibility, |
| 53 kVec2f_GrSLType, kDefault_Gr
SLPrecision, | 39 kVec2f_GrSLType, kDefault_Gr
SLPrecision, |
| 54 "ImageIncrement"); | 40 "ImageIncrement"); |
| 55 if (this->useBounds()) { | 41 if (ce.useBounds()) { |
| 56 fBoundsUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_
Visibility, | 42 fBoundsUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_
Visibility, |
| 57 kVec2f_GrSLType, kDefault_GrSLPr
ecision, | 43 kVec2f_GrSLType, kDefault_GrSLPr
ecision, |
| 58 "Bounds"); | 44 "Bounds"); |
| 59 } | 45 } |
| 46 |
| 47 int width = Gr1DKernelEffect::WidthFromRadius(ce.radius()); |
| 48 |
| 60 fKernelUni = uniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment
_Visibility, | 49 fKernelUni = uniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment
_Visibility, |
| 61 kFloat_GrSLType, kDefault_GrSLP
recision, | 50 kFloat_GrSLType, kDefault_GrSLP
recision, |
| 62 "Kernel", this->width()); | 51 "Kernel", width); |
| 63 | 52 |
| 64 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 53 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 65 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); | 54 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 66 | 55 |
| 67 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor); | 56 fragBuilder->codeAppendf("%s = vec4(0, 0, 0, 0);", args.fOutputColor); |
| 68 | 57 |
| 69 int width = this->width(); | |
| 70 const GrGLSLShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUn
i); | 58 const GrGLSLShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUn
i); |
| 71 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); | 59 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
| 72 | 60 |
| 73 fragBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_st
r(), fRadius, imgInc); | 61 fragBuilder->codeAppendf("vec2 coord = %s - %d.0 * %s;", coords2D.c_str(), c
e.radius(), imgInc); |
| 74 | 62 |
| 75 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 63 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| 76 for (int i = 0; i < width; i++) { | 64 for (int i = 0; i < width; i++) { |
| 77 SkString index; | 65 SkString index; |
| 78 SkString kernelIndex; | 66 SkString kernelIndex; |
| 79 index.appendS32(i); | 67 index.appendS32(i); |
| 80 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 68 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| 81 | 69 |
| 82 if (this->useBounds()) { | 70 if (ce.useBounds()) { |
| 83 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a | 71 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a |
| 84 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems | 72 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems |
| 85 // to have a bug that caused corruption. | 73 // to have a bug that caused corruption. |
| 86 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni); | 74 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni); |
| 87 const char* component = this->direction() == Gr1DKernelEffect::kY_Di
rection ? "y" : "x"; | 75 const char* component = ce.direction() == Gr1DKernelEffect::kY_Direc
tion ? "y" : "x"; |
| 88 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y)
{", | 76 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y)
{", |
| 89 component, bounds, component, bounds); | 77 component, bounds, component, bounds); |
| 90 } | 78 } |
| 91 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor); | 79 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor); |
| 92 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); | 80 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
| 93 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); | 81 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 94 if (this->useBounds()) { | 82 if (ce.useBounds()) { |
| 95 fragBuilder->codeAppend("}"); | 83 fragBuilder->codeAppend("}"); |
| 96 } | 84 } |
| 97 fragBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); | 85 fragBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); |
| 98 } | 86 } |
| 99 | 87 |
| 100 SkString modulate; | 88 SkString modulate; |
| 101 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); | 89 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
| 102 fragBuilder->codeAppend(modulate.c_str()); | 90 fragBuilder->codeAppend(modulate.c_str()); |
| 103 } | 91 } |
| 104 | 92 |
| 105 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 93 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 106 const GrProcessor& processor) { | 94 const GrProcessor& processor) { |
| 107 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); | 95 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); |
| 108 GrTexture& texture = *conv.texture(0); | 96 GrTexture& texture = *conv.texture(0); |
| 109 // the code we generated was for a specific kernel radius | 97 |
| 110 SkASSERT(conv.radius() == fRadius); | |
| 111 float imageIncrement[2] = { 0 }; | 98 float imageIncrement[2] = { 0 }; |
| 112 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 99 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 113 switch (conv.direction()) { | 100 switch (conv.direction()) { |
| 114 case Gr1DKernelEffect::kX_Direction: | 101 case Gr1DKernelEffect::kX_Direction: |
| 115 imageIncrement[0] = 1.0f / texture.width(); | 102 imageIncrement[0] = 1.0f / texture.width(); |
| 116 break; | 103 break; |
| 117 case Gr1DKernelEffect::kY_Direction: | 104 case Gr1DKernelEffect::kY_Direction: |
| 118 imageIncrement[1] = ySign / texture.height(); | 105 imageIncrement[1] = ySign / texture.height(); |
| 119 break; | 106 break; |
| 120 default: | 107 default: |
| 121 SkFAIL("Unknown filter direction."); | 108 SkFAIL("Unknown filter direction."); |
| 122 } | 109 } |
| 123 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 110 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 124 if (conv.useBounds()) { | 111 if (conv.useBounds()) { |
| 125 const float* bounds = conv.bounds(); | 112 const float* bounds = conv.bounds(); |
| 126 if (Gr1DKernelEffect::kY_Direction == conv.direction() && | 113 if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
| 127 texture.origin() != kTopLeft_GrSurfaceOrigin) { | 114 texture.origin() != kTopLeft_GrSurfaceOrigin) { |
| 128 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); | 115 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
| 129 } else { | 116 } else { |
| 130 pdman.set2f(fBoundsUni, bounds[0], bounds[1]); | 117 pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
| 131 } | 118 } |
| 132 } | 119 } |
| 133 pdman.set1fv(fKernelUni, this->width(), conv.kernel()); | 120 int width = Gr1DKernelEffect::WidthFromRadius(conv.radius()); |
| 121 |
| 122 pdman.set1fv(fKernelUni, width, conv.kernel()); |
| 134 } | 123 } |
| 135 | 124 |
| 136 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCap
s&, | 125 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrGLSLCap
s&, |
| 137 GrProcessorKeyBuilder* b) { | 126 GrProcessorKeyBuilder* b) { |
| 138 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); | 127 const GrConvolutionEffect& conv = processor.cast<GrConvolutionEffect>(); |
| 139 uint32_t key = conv.radius(); | 128 uint32_t key = conv.radius(); |
| 140 key <<= 2; | 129 key <<= 2; |
| 141 if (conv.useBounds()) { | 130 if (conv.useBounds()) { |
| 142 key |= 0x2; | 131 key |= 0x2; |
| 143 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; | 132 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 183 |
| 195 GrConvolutionEffect::~GrConvolutionEffect() { | 184 GrConvolutionEffect::~GrConvolutionEffect() { |
| 196 } | 185 } |
| 197 | 186 |
| 198 void GrConvolutionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 187 void GrConvolutionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 199 GrProcessorKeyBuilder* b) const
{ | 188 GrProcessorKeyBuilder* b) const
{ |
| 200 GrGLConvolutionEffect::GenKey(*this, caps, b); | 189 GrGLConvolutionEffect::GenKey(*this, caps, b); |
| 201 } | 190 } |
| 202 | 191 |
| 203 GrGLSLFragmentProcessor* GrConvolutionEffect::onCreateGLSLInstance() const { | 192 GrGLSLFragmentProcessor* GrConvolutionEffect::onCreateGLSLInstance() const { |
| 204 return new GrGLConvolutionEffect(*this); | 193 return new GrGLConvolutionEffect; |
| 205 } | 194 } |
| 206 | 195 |
| 207 bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { | 196 bool GrConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 208 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>(); | 197 const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>(); |
| 209 return (this->radius() == s.radius() && | 198 return (this->radius() == s.radius() && |
| 210 this->direction() == s.direction() && | 199 this->direction() == s.direction() && |
| 211 this->useBounds() == s.useBounds() && | 200 this->useBounds() == s.useBounds() && |
| 212 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && | 201 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
| 213 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); | 202 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
| 214 } | 203 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 232 } | 221 } |
| 233 | 222 |
| 234 bool useBounds = d->fRandom->nextBool(); | 223 bool useBounds = d->fRandom->nextBool(); |
| 235 return GrConvolutionEffect::Create(d->fTextures[texIdx], | 224 return GrConvolutionEffect::Create(d->fTextures[texIdx], |
| 236 dir, | 225 dir, |
| 237 radius, | 226 radius, |
| 238 kernel, | 227 kernel, |
| 239 useBounds, | 228 useBounds, |
| 240 bounds); | 229 bounds); |
| 241 } | 230 } |
| OLD | NEW |