1 /* 2 * Copyright 2018 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 8 /************************************************************************************************** 9 *** This file was autogenerated from GrRectBlurEffect.fp; do not modify. 10 **************************************************************************************************/ 11 #include "GrRectBlurEffect.h" 12 #include "glsl/GrGLSLFragmentProcessor.h" 13 #include "glsl/GrGLSLFragmentShaderBuilder.h" 14 #include "glsl/GrGLSLProgramBuilder.h" 15 #include "GrTexture.h" 16 #include "SkSLCPP.h" 17 #include "SkSLUtil.h" 18 class GrGLSLRectBlurEffect : public GrGLSLFragmentProcessor { 19 public: 20 GrGLSLRectBlurEffect() {} 21 void emitCode(EmitArgs& args) override { 22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 23 const GrRectBlurEffect& _outer = args.fFp.cast<GrRectBlurEffect>(); 24 (void)_outer; 25 auto rect = _outer.rect(); 26 (void)rect; 27 auto sigma = _outer.sigma(); 28 (void)sigma; 29 highPrecision = ((((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) || 30 abs(rect.right()) > 16000.0) || 31 abs(rect.bottom()) > 16000.0) || 32 abs(rect.right() - rect.left()) > 16000.0) || 33 abs(rect.bottom() - rect.top()) > 16000.0; 34 fRectVar = 35 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, "rect"); 36 if (!highPrecision) { 37 fProxyRectHalfVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, 38 kHalf4_GrSLType, "proxyRectHalf"); 39 } 40 if (highPrecision) { 41 fProxyRectFloatVar = args.fUniformHandler->addUniform( 42 kFragment_GrShaderFlag, kFloat4_GrSLType, "proxyRectFloat"); 43 } 44 fProfileSizeVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, 45 "profileSize"); 46 fragBuilder->codeAppendf( 47 "/* key */ bool highPrecision = %s;\n@if (highPrecision) {\n float2 " 48 "translatedPos = sk_FragCoord.xy - %s.xy;\n float width = %s.z - %s.x;\n " 49 "float height = %s.w - %s.y;\n float2 smallDims = float2(width - float(%s), " 50 "height - float(%s));\n float center = float(2.0 * floor(%s / 2.0 + 0.25) - " 51 "1.0);\n float2 wh = smallDims - float2(center, center);\n half hcoord = " 52 "half((abs(translatedPos.x - 0.5 * width) - 0.5 * wh.x) / float(%s));\n half " 53 "hlookup = texture(%s, float2(float(hcoord), 0.5)).", 54 (highPrecision ? "true" : "false"), args.fUniformHandler->getUniformCStr(fRectVar), 55 args.fUniformHandler->getUniformCStr(fRectVar), 56 args.fUniformHandler->getUniformCStr(fRectVar), 57 args.fUniformHandler->getUniformCStr(fRectVar), 58 args.fUniformHandler->getUniformCStr(fRectVar), 59 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 60 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 61 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 62 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 63 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str()); 64 fragBuilder->codeAppendf( 65 "%s.w;\n half vcoord = half((abs(translatedPos.y - 0.5 * height) - 0.5 * wh.y) " 66 "/ float(%s));\n half vlookup = texture(%s, float2(float(vcoord), 0.5)).%s.w;\n " 67 " %s = (%s * hlookup) * vlookup;\n} else {\n half2 translatedPos = " 68 "half2(sk_FragCoord.xy - %s.xy);\n half width = half(%s.z - %s.x);\n half " 69 "height = half(%s.w - %s.y);\n half2 smallDims = half2(width - %s, height - " 70 "%s);\n half center = 2.0 * floor(%s / 2.0 + 0.25) - 1.0;\n half2 wh = " 71 "smallDims - half2(center, center);\n half ", 72 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), 73 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 74 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 75 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), 76 args.fOutputColor, args.fInputColor, args.fUniformHandler->getUniformCStr(fRectVar), 77 args.fUniformHandler->getUniformCStr(fRectVar), 78 args.fUniformHandler->getUniformCStr(fRectVar), 79 args.fUniformHandler->getUniformCStr(fRectVar), 80 args.fUniformHandler->getUniformCStr(fRectVar), 81 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 82 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 83 args.fUniformHandler->getUniformCStr(fProfileSizeVar)); 84 fragBuilder->codeAppendf( 85 "hcoord = (abs(translatedPos.x - 0.5 * width) - 0.5 * wh.x) / %s;\n half " 86 "hlookup = texture(%s, float2(float(hcoord), 0.5)).%s.w;\n half vcoord = " 87 "(abs(translatedPos.y - 0.5 * height) - 0.5 * wh.y) / %s;\n half vlookup = " 88 "texture(%s, float2(float(vcoord), 0.5)).%s.w;\n %s = (%s * hlookup) * " 89 "vlookup;\n}\n", 90 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 91 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 92 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), 93 args.fUniformHandler->getUniformCStr(fProfileSizeVar), 94 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), 95 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), 96 args.fOutputColor, args.fInputColor); 97 } 98 99 private: 100 void onSetData(const GrGLSLProgramDataManager& pdman, 101 const GrFragmentProcessor& _proc) override { 102 const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>(); 103 { pdman.set4fv(fRectVar, 1, reinterpret_cast<const float*>(&(_outer.rect()))); } 104 UniformHandle& rect = fRectVar; 105 (void)rect; 106 auto sigma = _outer.sigma(); 107 (void)sigma; 108 GrSurfaceProxy& blurProfileProxy = *_outer.textureSampler(0).proxy(); 109 GrTexture& blurProfile = *blurProfileProxy.peekTexture(); 110 (void)blurProfile; 111 UniformHandle& proxyRectHalf = fProxyRectHalfVar; 112 (void)proxyRectHalf; 113 UniformHandle& proxyRectFloat = fProxyRectFloatVar; 114 (void)proxyRectFloat; 115 UniformHandle& profileSize = fProfileSizeVar; 116 (void)profileSize; 117 118 pdman.set1f(profileSize, SkScalarCeilToScalar(6 * sigma)); 119 } 120 bool highPrecision = false; 121 UniformHandle fProxyRectHalfVar; 122 UniformHandle fProxyRectFloatVar; 123 UniformHandle fProfileSizeVar; 124 UniformHandle fRectVar; 125 }; 126 GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const { 127 return new GrGLSLRectBlurEffect(); 128 } 129 void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 130 GrProcessorKeyBuilder* b) const {} 131 bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { 132 const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>(); 133 (void)that; 134 if (fRect != that.fRect) return false; 135 if (fSigma != that.fSigma) return false; 136 if (fBlurProfile != that.fBlurProfile) return false; 137 return true; 138 } 139 GrRectBlurEffect::GrRectBlurEffect(const GrRectBlurEffect& src) 140 : INHERITED(kGrRectBlurEffect_ClassID, src.optimizationFlags()) 141 , fRect(src.fRect) 142 , fSigma(src.fSigma) 143 , fBlurProfile(src.fBlurProfile) { 144 this->setTextureSamplerCnt(1); 145 } 146 std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::clone() const { 147 return std::unique_ptr<GrFragmentProcessor>(new GrRectBlurEffect(*this)); 148 } 149 const GrFragmentProcessor::TextureSampler& GrRectBlurEffect::onTextureSampler(int index) const { 150 return IthTextureSampler(index, fBlurProfile); 151 } 152 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRectBlurEffect); 153 #if GR_TEST_UTILS 154 std::unique_ptr<GrFragmentProcessor> GrRectBlurEffect::TestCreate(GrProcessorTestData* data) { 155 float sigma = data->fRandom->nextRangeF(3, 8); 156 float width = data->fRandom->nextRangeF(200, 300); 157 float height = data->fRandom->nextRangeF(200, 300); 158 return GrRectBlurEffect::Make(data->proxyProvider(), *data->caps()->shaderCaps(), 159 SkRect::MakeWH(width, height), sigma); 160 } 161 #endif 162