| 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/GrGLSLProgramBuilder.h" | |
| 12 #include "glsl/GrGLSLProgramDataManager.h" | 11 #include "glsl/GrGLSLProgramDataManager.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&); | 19 GrGLConvolutionEffect(const GrProcessor&); |
| 20 | 20 |
| 21 virtual void emitCode(EmitArgs&) override; | 21 virtual void emitCode(EmitArgs&) override; |
| 22 | 22 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { | 43 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrProcessor& processor) { |
| 44 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); | 44 const GrConvolutionEffect& c = processor.cast<GrConvolutionEffect>(); |
| 45 fRadius = c.radius(); | 45 fRadius = c.radius(); |
| 46 fUseBounds = c.useBounds(); | 46 fUseBounds = c.useBounds(); |
| 47 fDirection = c.direction(); | 47 fDirection = c.direction(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void GrGLConvolutionEffect::emitCode(EmitArgs& args) { | 50 void GrGLConvolutionEffect::emitCode(EmitArgs& args) { |
| 51 fImageIncrementUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragme
nt_Visibility, | 51 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 52 kVec2f_GrSLType, kDefault_GrSLPreci
sion, | 52 fImageIncrementUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragm
ent_Visibility, |
| 53 "ImageIncrement"); | 53 kVec2f_GrSLType, kDefault_Gr
SLPrecision, |
| 54 "ImageIncrement"); |
| 54 if (this->useBounds()) { | 55 if (this->useBounds()) { |
| 55 fBoundsUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_V
isibility, | 56 fBoundsUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_
Visibility, |
| 56 kVec2f_GrSLType, kDefault_GrSLPrecision
, | 57 kVec2f_GrSLType, kDefault_GrSLPr
ecision, |
| 57 "Bounds"); | 58 "Bounds"); |
| 58 } | 59 } |
| 59 fKernelUni = args.fBuilder->addUniformArray(GrGLSLProgramBuilder::kFragment_
Visibility, | 60 fKernelUni = uniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment
_Visibility, |
| 60 kFloat_GrSLType, kDefault_GrSLPrecisio
n, | 61 kFloat_GrSLType, kDefault_GrSLP
recision, |
| 61 "Kernel", this->width()); | 62 "Kernel", this->width()); |
| 62 | 63 |
| 63 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 64 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 64 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); | 65 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 65 | 66 |
| 66 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor); | 67 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor); |
| 67 | 68 |
| 68 int width = this->width(); | 69 int width = this->width(); |
| 69 const GrGLSLShaderVar& kernel = args.fBuilder->getUniformVariable(fKernelUni
); | 70 const GrGLSLShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUn
i); |
| 70 const char* imgInc = args.fBuilder->getUniformCStr(fImageIncrementUni); | 71 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
| 71 | 72 |
| 72 fragBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_st
r(), fRadius, imgInc); | 73 fragBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_st
r(), fRadius, imgInc); |
| 73 | 74 |
| 74 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 75 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| 75 for (int i = 0; i < width; i++) { | 76 for (int i = 0; i < width; i++) { |
| 76 SkString index; | 77 SkString index; |
| 77 SkString kernelIndex; | 78 SkString kernelIndex; |
| 78 index.appendS32(i); | 79 index.appendS32(i); |
| 79 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 80 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| 80 | 81 |
| 81 if (this->useBounds()) { | 82 if (this->useBounds()) { |
| 82 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a | 83 // We used to compute a bool indicating whether we're in bounds or n
ot, cast it to a |
| 83 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems | 84 // float, and then mul weight*texture_sample by the float. However,
the Adreno 430 seems |
| 84 // to have a bug that caused corruption. | 85 // to have a bug that caused corruption. |
| 85 const char* bounds = args.fBuilder->getUniformCStr(fBoundsUni); | 86 const char* bounds = uniformHandler->getUniformCStr(fBoundsUni); |
| 86 const char* component = this->direction() == Gr1DKernelEffect::kY_Di
rection ? "y" : "x"; | 87 const char* component = this->direction() == Gr1DKernelEffect::kY_Di
rection ? "y" : "x"; |
| 87 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y)
{", | 88 fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y)
{", |
| 88 component, bounds, component, bounds); | 89 component, bounds, component, bounds); |
| 89 } | 90 } |
| 90 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor); | 91 fragBuilder->codeAppendf("\t\t%s += ", args.fOutputColor); |
| 91 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); | 92 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
| 92 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); | 93 fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 93 if (this->useBounds()) { | 94 if (this->useBounds()) { |
| 94 fragBuilder->codeAppend("}"); | 95 fragBuilder->codeAppend("}"); |
| 95 } | 96 } |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 } | 232 } |
| 232 | 233 |
| 233 bool useBounds = d->fRandom->nextBool(); | 234 bool useBounds = d->fRandom->nextBool(); |
| 234 return GrConvolutionEffect::Create(d->fTextures[texIdx], | 235 return GrConvolutionEffect::Create(d->fTextures[texIdx], |
| 235 dir, | 236 dir, |
| 236 radius, | 237 radius, |
| 237 kernel, | 238 kernel, |
| 238 useBounds, | 239 useBounds, |
| 239 bounds); | 240 bounds); |
| 240 } | 241 } |
| OLD | NEW |