OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 #include "GrMatrixConvolutionEffect.h" | 7 #include "GrMatrixConvolutionEffect.h" |
8 #include "glsl/GrGLSLFragmentProcessor.h" | 8 #include "glsl/GrGLSLFragmentProcessor.h" |
9 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 9 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
10 #include "glsl/GrGLSLProgramDataManager.h" | 10 #include "glsl/GrGLSLProgramDataManager.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 typedef GrGLSLFragmentProcessor INHERITED; | 32 typedef GrGLSLFragmentProcessor INHERITED; |
33 }; | 33 }; |
34 | 34 |
35 void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { | 35 void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { |
36 const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffe
ct>(); | 36 const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffe
ct>(); |
37 const GrTextureDomain& domain = mce.domain(); | 37 const GrTextureDomain& domain = mce.domain(); |
38 | 38 |
39 int kWidth = mce.kernelSize().width(); | 39 int kWidth = mce.kernelSize().width(); |
40 int kHeight = mce.kernelSize().height(); | 40 int kHeight = mce.kernelSize().height(); |
41 | 41 |
| 42 int arrayCount = (kWidth + 3) / 4; |
| 43 SkASSERT(4 * arrayCount >= kWidth); |
| 44 |
42 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 45 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
43 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 46 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
44 kVec2f_GrSLType, kDefault_Gr
SLPrecision, | 47 kVec2f_GrSLType, kDefault_Gr
SLPrecision, |
45 "ImageIncrement"); | 48 "ImageIncrement"); |
46 fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, | 49 fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, |
47 kFloat_GrSLType, kDefault_GrSLP
recision, | 50 kVec4f_GrSLType, kDefault_GrSLP
recision, |
48 "Kernel", | 51 "Kernel", |
49 kWidth * kHeight); | 52 arrayCount); |
50 fKernelOffsetUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 53 fKernelOffsetUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
51 kVec2f_GrSLType, kDefault_GrSL
Precision, | 54 kVec2f_GrSLType, kDefault_GrSL
Precision, |
52 "KernelOffset"); | 55 "KernelOffset"); |
53 fGainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 56 fGainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
54 kFloat_GrSLType, kDefault_GrSLPrecisio
n, "Gain"); | 57 kFloat_GrSLType, kDefault_GrSLPrecisio
n, "Gain"); |
55 fBiasUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 58 fBiasUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
56 kFloat_GrSLType, kDefault_GrSLPrecisio
n, "Bias"); | 59 kFloat_GrSLType, kDefault_GrSLPrecisio
n, "Bias"); |
57 | 60 |
58 const char* kernelOffset = uniformHandler->getUniformCStr(fKernelOffsetUni); | 61 const char* kernelOffset = uniformHandler->getUniformCStr(fKernelOffsetUni); |
59 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); | 62 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
60 const char* kernel = uniformHandler->getUniformCStr(fKernelUni); | 63 const char* kernel = uniformHandler->getUniformCStr(fKernelUni); |
61 const char* gain = uniformHandler->getUniformCStr(fGainUni); | 64 const char* gain = uniformHandler->getUniformCStr(fGainUni); |
62 const char* bias = uniformHandler->getUniformCStr(fBiasUni); | 65 const char* bias = uniformHandler->getUniformCStr(fBiasUni); |
63 | 66 |
64 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 67 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
65 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); | 68 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
66 fragBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);"); | 69 fragBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);"); |
67 fragBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), ker
nelOffset, imgInc); | 70 fragBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), ker
nelOffset, imgInc); |
68 fragBuilder->codeAppend("vec4 c;"); | 71 fragBuilder->codeAppend("vec4 c;"); |
69 | 72 |
| 73 const char* kVecSuffix[4] = { ".x", ".y", ".z", ".w" }; |
70 for (int y = 0; y < kHeight; y++) { | 74 for (int y = 0; y < kHeight; y++) { |
71 for (int x = 0; x < kWidth; x++) { | 75 for (int x = 0; x < kWidth; x++) { |
72 GrGLSLShaderBuilder::ShaderBlock block(fragBuilder); | 76 GrGLSLShaderBuilder::ShaderBlock block(fragBuilder); |
73 fragBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, k
Width, x); | 77 int offset = y*kWidth + x; |
| 78 |
| 79 fragBuilder->codeAppendf("float k = %s[%d]%s;", kernel, offset / 4, |
| 80 kVecSuffix[offset & 0x3]); |
74 SkString coord; | 81 SkString coord; |
75 coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); | 82 coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); |
76 fDomain.sampleTexture(fragBuilder, | 83 fDomain.sampleTexture(fragBuilder, |
77 uniformHandler, | 84 uniformHandler, |
78 args.fGLSLCaps, | 85 args.fGLSLCaps, |
79 domain, | 86 domain, |
80 "c", | 87 "c", |
81 coord, | 88 coord, |
82 args.fSamplers[0]); | 89 args.fSamplers[0]); |
83 if (!mce.convolveAlpha()) { | 90 if (!mce.convolveAlpha()) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 const GrProcessor& processor) { | 130 const GrProcessor& processor) { |
124 const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEf
fect>(); | 131 const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEf
fect>(); |
125 GrTexture& texture = *conv.texture(0); | 132 GrTexture& texture = *conv.texture(0); |
126 | 133 |
127 float imageIncrement[2]; | 134 float imageIncrement[2]; |
128 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 135 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
129 imageIncrement[0] = 1.0f / texture.width(); | 136 imageIncrement[0] = 1.0f / texture.width(); |
130 imageIncrement[1] = ySign / texture.height(); | 137 imageIncrement[1] = ySign / texture.height(); |
131 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 138 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
132 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); | 139 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
133 pdman.set1fv(fKernelUni, conv.kernelSize().width() * conv.kernelSize().heigh
t(), conv.kernel()); | 140 int kernelCount = conv.kernelSize().width() * conv.kernelSize().height(); |
| 141 int arrayCount = (kernelCount + 3) / 4; |
| 142 SkASSERT(4 * arrayCount >= kernelCount); |
| 143 pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); |
134 pdman.set1f(fGainUni, conv.gain()); | 144 pdman.set1f(fGainUni, conv.gain()); |
135 pdman.set1f(fBiasUni, conv.bias()); | 145 pdman.set1f(fBiasUni, conv.bias()); |
136 fDomain.setData(pdman, conv.domain(), texture.origin()); | 146 fDomain.setData(pdman, conv.domain(), texture.origin()); |
137 } | 147 } |
138 | 148 |
139 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, | 149 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
140 const SkIRect& bounds, | 150 const SkIRect& bounds, |
141 const SkISize& kernelSize, | 151 const SkISize& kernelSize, |
142 const SkScalar* kernel, | 152 const SkScalar* kernel, |
143 SkScalar gain, | 153 SkScalar gain, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 return GrMatrixConvolutionEffect::Create(d->fTextures[texIdx], | 258 return GrMatrixConvolutionEffect::Create(d->fTextures[texIdx], |
249 bounds, | 259 bounds, |
250 kernelSize, | 260 kernelSize, |
251 kernel.get(), | 261 kernel.get(), |
252 gain, | 262 gain, |
253 bias, | 263 bias, |
254 kernelOffset, | 264 kernelOffset, |
255 tileMode, | 265 tileMode, |
256 convolveAlpha); | 266 convolveAlpha); |
257 } | 267 } |
OLD | NEW |