| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 const char* vertexCoords, | 58 const char* vertexCoords, |
| 59 const char* outputColor, | 59 const char* outputColor, |
| 60 const char* inputColor, | 60 const char* inputColor, |
| 61 const TextureSamplerArray& samplers) { | 61 const TextureSamplerArray& samplers) { |
| 62 const char* coords; | 62 const char* coords; |
| 63 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); | 63 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
| 64 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 64 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, |
| 65 kVec2f_GrSLType, "ImageIncrement"); | 65 kVec2f_GrSLType, "ImageIncrement"); |
| 66 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, | 66 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderTyp
e, |
| 67 kFloat_GrSLType, "Kernel", this->width
()); | 67 kFloat_GrSLType, "Kernel", this->width
()); |
| 68 SkString* code = &builder->fFSCode; | |
| 69 | 68 |
| 70 code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 69 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| 71 | 70 |
| 72 int width = this ->width(); | 71 int width = this ->width(); |
| 73 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); | 72 const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 74 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 73 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 75 | 74 |
| 76 code->appendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc)
; | 75 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius
, imgInc); |
| 77 | 76 |
| 78 // Manually unroll loop because some drivers don't; yields 20-30% speedup. | 77 // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| 79 for (int i = 0; i < width; i++) { | 78 for (int i = 0; i < width; i++) { |
| 80 SkString index; | 79 SkString index; |
| 81 SkString kernelIndex; | 80 SkString kernelIndex; |
| 82 index.appendS32(i); | 81 index.appendS32(i); |
| 83 kernel.appendArrayAccess(index.c_str(), &kernelIndex); | 82 kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| 84 code->appendf("\t\t%s += ", outputColor); | 83 builder->fsCodeAppendf("\t\t%s += ", outputColor); |
| 85 builder->appendTextureLookup(&builder->fFSCode, samplers[0], "coord"); | 84 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sa
mplers[0], "coord"); |
| 86 code->appendf(" * %s;\n", kernelIndex.c_str()); | 85 builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 87 code->appendf("\t\tcoord += %s;\n", imgInc); | 86 builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc); |
| 88 } | 87 } |
| 89 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor); | 88 SkString modulate; |
| 89 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 90 builder->fsCodeAppend(modulate.c_str()); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffe
ctStage& stage) { | 93 void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, const GrEffe
ctStage& stage) { |
| 93 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(st
age); | 94 const GrConvolutionEffect& conv = GetEffectFromStage<GrConvolutionEffect>(st
age); |
| 94 GrTexture& texture = *conv.texture(0); | 95 GrTexture& texture = *conv.texture(0); |
| 95 // the code we generated was for a specific kernel radius | 96 // the code we generated was for a specific kernel radius |
| 96 GrAssert(conv.radius() == fRadius); | 97 GrAssert(conv.radius() == fRadius); |
| 97 float imageIncrement[2] = { 0 }; | 98 float imageIncrement[2] = { 0 }; |
| 98 switch (conv.direction()) { | 99 switch (conv.direction()) { |
| 99 case Gr1DKernelEffect::kX_Direction: | 100 case Gr1DKernelEffect::kX_Direction: |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 GrEffectUnitTest::kAlphaTextureIdx; | 186 GrEffectUnitTest::kAlphaTextureIdx; |
| 186 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; | 187 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 187 int radius = random->nextRangeU(1, kMaxKernelRadius); | 188 int radius = random->nextRangeU(1, kMaxKernelRadius); |
| 188 float kernel[kMaxKernelRadius]; | 189 float kernel[kMaxKernelRadius]; |
| 189 for (int i = 0; i < kMaxKernelRadius; ++i) { | 190 for (int i = 0; i < kMaxKernelRadius; ++i) { |
| 190 kernel[i] = random->nextSScalar1(); | 191 kernel[i] = random->nextSScalar1(); |
| 191 } | 192 } |
| 192 | 193 |
| 193 return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel); | 194 return GrConvolutionEffect::Create(textures[texIdx], dir, radius,kernel); |
| 194 } | 195 } |
| OLD | NEW |