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