1 /* 2 * Copyright 2014 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #include "GrMatrixConvolutionEffect.h" 8 9 #include "GrTexture.h" 10 #include "GrTextureProxy.h" 11 #include "glsl/GrGLSLFragmentProcessor.h" 12 #include "glsl/GrGLSLFragmentShaderBuilder.h" 13 #include "glsl/GrGLSLProgramDataManager.h" 14 #include "glsl/GrGLSLUniformHandler.h" 15 16 class GrGLMatrixConvolutionEffect : public GrGLSLFragmentProcessor { 17 public: 18 void emitCode(EmitArgs&) override; 19 20 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); 21 22 protected: 23 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; 24 25 private: 26 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; 27 28 UniformHandle fKernelUni; 29 UniformHandle fImageIncrementUni; 30 UniformHandle fKernelOffsetUni; 31 UniformHandle fGainUni; 32 UniformHandle fBiasUni; 33 GrTextureDomain::GLDomain fDomain; 34 35 typedef GrGLSLFragmentProcessor INHERITED; 36 }; 37 38 void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { 39 const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffect>(); 40 const GrTextureDomain& domain = mce.domain(); 41 42 int kWidth = mce.kernelSize().width(); 43 int kHeight = mce.kernelSize().height(); 44 45 int arrayCount = (kWidth * kHeight + 3) / 4; 46 SkASSERT(4 * arrayCount >= kWidth * kHeight); 47 48 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 49 fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, 50 "ImageIncrement"); 51 fKernelUni = uniformHandler->addUniformArray(kFragment_GrShaderFlag, kHalf4_GrSLType, 52 "Kernel", 53 arrayCount); 54 fKernelOffsetUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, 55 "KernelOffset"); 56 fGainUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "Gain"); 57 fBiasUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "Bias"); 58 59 const char* kernelOffset = uniformHandler->getUniformCStr(fKernelOffsetUni); 60 const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); 61 const char* kernel = uniformHandler->getUniformCStr(fKernelUni); 62 const char* gain = uniformHandler->getUniformCStr(fGainUni); 63 const char* bias = uniformHandler->getUniformCStr(fBiasUni); 64 65 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 66 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); 67 fragBuilder->codeAppend("half4 sum = half4(0, 0, 0, 0);"); 68 fragBuilder->codeAppendf("float2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset, imgInc); 69 fragBuilder->codeAppend("half4 c;"); 70 71 const char* kVecSuffix[4] = { ".x", ".y", ".z", ".w" }; 72 for (int y = 0; y < kHeight; y++) { 73 for (int x = 0; x < kWidth; x++) { 74 GrGLSLShaderBuilder::ShaderBlock block(fragBuilder); 75 int offset = y*kWidth + x; 76 77 fragBuilder->codeAppendf("half k = %s[%d]%s;", kernel, offset / 4, 78 kVecSuffix[offset & 0x3]); 79 SkString coord; 80 coord.printf("coord + half2(%d, %d) * %s", x, y, imgInc); 81 fDomain.sampleTexture(fragBuilder, 82 uniformHandler, 83 args.fShaderCaps, 84 domain, 85 "c", 86 coord, 87 args.fTexSamplers[0]); 88 if (!mce.convolveAlpha()) { 89 fragBuilder->codeAppend("c.rgb /= c.a;"); 90 fragBuilder->codeAppend("c.rgb = saturate(c.rgb);"); 91 } 92 fragBuilder->codeAppend("sum += c * k;"); 93 } 94 } 95 if (mce.convolveAlpha()) { 96 fragBuilder->codeAppendf("%s = sum * %s + %s;", args.fOutputColor, gain, bias); 97 fragBuilder->codeAppendf("%s.a = saturate(%s.a);", args.fOutputColor, args.fOutputColor); 98 fragBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);", 99 args.fOutputColor, args.fOutputColor, args.fOutputColor); 100 } else { 101 fDomain.sampleTexture(fragBuilder, 102 uniformHandler, 103 args.fShaderCaps, 104 domain, 105 "c", 106 coords2D, 107 args.fTexSamplers[0]); 108 fragBuilder->codeAppendf("%s.a = c.a;", args.fOutputColor); 109 fragBuilder->codeAppendf("%s.rgb = saturate(sum.rgb * %s + %s);", args.fOutputColor, gain, bias); 110 fragBuilder->codeAppendf("%s.rgb *= %s.a;", args.fOutputColor, args.fOutputColor); 111 } 112 fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor); 113 } 114 115 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, 116 const GrShaderCaps&, GrProcessorKeyBuilder* b) { 117 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>(); 118 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF); 119 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); 120 key |= m.convolveAlpha() ? 1U << 31 : 0; 121 b->add32(key); 122 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); 123 } 124 125 void GrGLMatrixConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, 126 const GrFragmentProcessor& processor) { 127 const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>(); 128 GrTextureProxy* proxy = conv.textureSampler(0).proxy(); 129 GrTexture* texture = proxy->peekTexture(); 130 131 float imageIncrement[2]; 132 float ySign = proxy->origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; 133 imageIncrement[0] = 1.0f / texture->width(); 134 imageIncrement[1] = ySign / texture->height(); 135 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); 136 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); 137 int kernelCount = conv.kernelSize().width() * conv.kernelSize().height(); 138 int arrayCount = (kernelCount + 3) / 4; 139 SkASSERT(4 * arrayCount >= kernelCount); 140 pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); 141 pdman.set1f(fGainUni, conv.gain()); 142 pdman.set1f(fBiasUni, conv.bias()); 143 fDomain.setData(pdman, conv.domain(), proxy, conv.textureSampler(0).samplerState()); 144 } 145 146 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(sk_sp<GrTextureProxy> srcProxy, 147 const SkIRect& srcBounds, 148 const SkISize& kernelSize, 149 const SkScalar* kernel, 150 SkScalar gain, 151 SkScalar bias, 152 const SkIPoint& kernelOffset, 153 GrTextureDomain::Mode tileMode, 154 bool convolveAlpha) 155 // To advertise either the modulation or opaqueness optimizations we'd have to examine the 156 // parameters. 157 : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) 158 , fCoordTransform(srcProxy.get()) 159 , fDomain(srcProxy.get(), GrTextureDomain::MakeTexelDomain(srcBounds, tileMode), 160 tileMode, tileMode) 161 , fTextureSampler(std::move(srcProxy)) 162 , fKernelSize(kernelSize) 163 , fGain(SkScalarToFloat(gain)) 164 , fBias(SkScalarToFloat(bias) / 255.0f) 165 , fConvolveAlpha(convolveAlpha) { 166 this->addCoordTransform(&fCoordTransform); 167 this->setTextureSamplerCnt(1); 168 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { 169 fKernel[i] = SkScalarToFloat(kernel[i]); 170 } 171 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); 172 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); 173 } 174 175 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(const GrMatrixConvolutionEffect& that) 176 : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) 177 , fCoordTransform(that.fCoordTransform) 178 , fDomain(that.fDomain) 179 , fTextureSampler(that.fTextureSampler) 180 , fKernelSize(that.fKernelSize) 181 , fGain(that.fGain) 182 , fBias(that.fBias) 183 , fConvolveAlpha(that.fConvolveAlpha) { 184 this->addCoordTransform(&fCoordTransform); 185 this->setTextureSamplerCnt(1); 186 memcpy(fKernel, that.fKernel, sizeof(float) * fKernelSize.width() * fKernelSize.height()); 187 memcpy(fKernelOffset, that.fKernelOffset, sizeof(fKernelOffset)); 188 } 189 190 std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::clone() const { 191 return std::unique_ptr<GrFragmentProcessor>(new GrMatrixConvolutionEffect(*this)); 192 } 193 194 void GrMatrixConvolutionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 195 GrProcessorKeyBuilder* b) const { 196 GrGLMatrixConvolutionEffect::GenKey(*this, caps, b); 197 } 198 199 GrGLSLFragmentProcessor* GrMatrixConvolutionEffect::onCreateGLSLInstance() const { 200 return new GrGLMatrixConvolutionEffect; 201 } 202 203 bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { 204 const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>(); 205 return fKernelSize == s.kernelSize() && 206 !memcmp(fKernel, s.kernel(), 207 fKernelSize.width() * fKernelSize.height() * sizeof(float)) && 208 fGain == s.gain() && 209 fBias == s.bias() && 210 !memcmp(fKernelOffset, s.kernelOffset(), sizeof(fKernelOffset)) && 211 fConvolveAlpha == s.convolveAlpha() && 212 fDomain == s.domain(); 213 } 214 215 static void fill_in_1D_gaussian_kernel_with_stride(float* kernel, int size, int stride, 216 float twoSigmaSqrd) { 217 SkASSERT(!SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)); 218 219 const float sigmaDenom = 1.0f / twoSigmaSqrd; 220 const int radius = size / 2; 221 222 float sum = 0.0f; 223 for (int i = 0; i < size; ++i) { 224 float term = static_cast<float>(i - radius); 225 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian 226 // is dropped here, since we renormalize the kernel below. 227 kernel[i * stride] = sk_float_exp(-term * term * sigmaDenom); 228 sum += kernel[i * stride]; 229 } 230 // Normalize the kernel 231 float scale = 1.0f / sum; 232 for (int i = 0; i < size; ++i) { 233 kernel[i * stride] *= scale; 234 } 235 } 236 237 static void fill_in_2D_gaussian_kernel(float* kernel, int width, int height, 238 SkScalar sigmaX, SkScalar sigmaY) { 239 SkASSERT(width * height <= MAX_KERNEL_SIZE); 240 const float twoSigmaSqrdX = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)); 241 const float twoSigmaSqrdY = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)); 242 243 // TODO: in all of these degenerate cases we're uploading (and using) a whole lot of zeros. 244 if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) || 245 SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { 246 // In this case the 2D Gaussian degenerates to a 1D Gaussian (in X or Y) or a point 247 SkASSERT(3 == width || 3 == height); 248 memset(kernel, 0, width*height*sizeof(float)); 249 250 if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) && 251 SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { 252 // A point 253 SkASSERT(3 == width && 3 == height); 254 kernel[4] = 1.0f; 255 } else if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero)) { 256 // A 1D Gaussian in Y 257 SkASSERT(3 == width); 258 // Down the middle column of the kernel with a stride of width 259 fill_in_1D_gaussian_kernel_with_stride(&kernel[1], height, width, twoSigmaSqrdY); 260 } else { 261 // A 1D Gaussian in X 262 SkASSERT(SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)); 263 SkASSERT(3 == height); 264 // Down the middle row of the kernel with a stride of 1 265 fill_in_1D_gaussian_kernel_with_stride(&kernel[width], width, 1, twoSigmaSqrdX); 266 } 267 return; 268 } 269 270 const float sigmaXDenom = 1.0f / twoSigmaSqrdX; 271 const float sigmaYDenom = 1.0f / twoSigmaSqrdY; 272 const int xRadius = width / 2; 273 const int yRadius = height / 2; 274 275 float sum = 0.0f; 276 for (int x = 0; x < width; x++) { 277 float xTerm = static_cast<float>(x - xRadius); 278 xTerm = xTerm * xTerm * sigmaXDenom; 279 for (int y = 0; y < height; y++) { 280 float yTerm = static_cast<float>(y - yRadius); 281 float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); 282 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian 283 // is dropped here, since we renormalize the kernel below. 284 kernel[y * width + x] = xyTerm; 285 sum += xyTerm; 286 } 287 } 288 // Normalize the kernel 289 float scale = 1.0f / sum; 290 for (int i = 0; i < width * height; ++i) { 291 kernel[i] *= scale; 292 } 293 } 294 295 // Static function to create a 2D convolution 296 std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::MakeGaussian( 297 sk_sp<GrTextureProxy> srcProxy, 298 const SkIRect& srcBounds, 299 const SkISize& kernelSize, 300 SkScalar gain, 301 SkScalar bias, 302 const SkIPoint& kernelOffset, 303 GrTextureDomain::Mode tileMode, 304 bool convolveAlpha, 305 SkScalar sigmaX, 306 SkScalar sigmaY) { 307 float kernel[MAX_KERNEL_SIZE]; 308 309 fill_in_2D_gaussian_kernel(kernel, kernelSize.width(), kernelSize.height(), sigmaX, sigmaY); 310 311 return std::unique_ptr<GrFragmentProcessor>( 312 new GrMatrixConvolutionEffect(std::move(srcProxy), srcBounds, kernelSize, kernel, 313 gain, bias, kernelOffset, tileMode, convolveAlpha)); 314 } 315 316 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); 317 318 #if GR_TEST_UTILS 319 std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::TestCreate(GrProcessorTestData* d) { 320 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx 321 : GrProcessorUnitTest::kAlphaTextureIdx; 322 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); 323 324 int width = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE); 325 int height = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE / width); 326 SkISize kernelSize = SkISize::Make(width, height); 327 std::unique_ptr<SkScalar[]> kernel(new SkScalar[width * height]); 328 for (int i = 0; i < width * height; i++) { 329 kernel.get()[i] = d->fRandom->nextSScalar1(); 330 } 331 SkScalar gain = d->fRandom->nextSScalar1(); 332 SkScalar bias = d->fRandom->nextSScalar1(); 333 SkIPoint kernelOffset = SkIPoint::Make(d->fRandom->nextRangeU(0, kernelSize.width()), 334 d->fRandom->nextRangeU(0, kernelSize.height())); 335 SkIRect bounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, proxy->width()), 336 d->fRandom->nextRangeU(0, proxy->height()), 337 d->fRandom->nextRangeU(0, proxy->width()), 338 d->fRandom->nextRangeU(0, proxy->height())); 339 GrTextureDomain::Mode tileMode = 340 static_cast<GrTextureDomain::Mode>(d->fRandom->nextRangeU(0, 2)); 341 bool convolveAlpha = d->fRandom->nextBool(); 342 return GrMatrixConvolutionEffect::Make(std::move(proxy), 343 bounds, 344 kernelSize, 345 kernel.get(), 346 gain, 347 bias, 348 kernelOffset, 349 tileMode, 350 convolveAlpha); 351 } 352 #endif 353