| 1 | /* |
| 2 | * Copyright 2017 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 GrEllipseEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
| 11 | #include "GrEllipseEffect.h" |
| 12 | |
| 13 | #include "src/core/SkUtils.h" |
| 14 | #include "src/gpu/GrTexture.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 18 | #include "src/sksl/SkSLCPP.h" |
| 19 | #include "src/sksl/SkSLUtil.h" |
| 20 | class GrGLSLEllipseEffect : public GrGLSLFragmentProcessor { |
| 21 | public: |
| 22 | GrGLSLEllipseEffect() {} |
| 23 | void emitCode(EmitArgs& args) override { |
| 24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 25 | const GrEllipseEffect& _outer = args.fFp.cast<GrEllipseEffect>(); |
| 26 | (void)_outer; |
| 27 | auto edgeType = _outer.edgeType; |
| 28 | (void)edgeType; |
| 29 | auto center = _outer.center; |
| 30 | (void)center; |
| 31 | auto radii = _outer.radii; |
| 32 | (void)radii; |
| 33 | prevRadii = float2(-1.0); |
| 34 | medPrecision = !sk_Caps.floatIs32Bits; |
| 35 | ellipseVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 36 | kFloat4_GrSLType, "ellipse" ); |
| 37 | if (medPrecision) { |
| 38 | scaleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 39 | kFloat2_GrSLType, "scale" ); |
| 40 | } |
| 41 | fragBuilder->codeAppendf( |
| 42 | R"SkSL(float2 prevCenter; |
| 43 | float2 prevRadii = float2(%f, %f); |
| 44 | bool medPrecision = %s; |
| 45 | float2 d = sk_FragCoord.xy - %s.xy; |
| 46 | @if (medPrecision) { |
| 47 | d *= %s.y; |
| 48 | } |
| 49 | float2 Z = d * %s.zw; |
| 50 | float implicit = dot(Z, d) - 1.0; |
| 51 | float grad_dot = 4.0 * dot(Z, Z); |
| 52 | @if (medPrecision) { |
| 53 | grad_dot = max(grad_dot, 6.1036000261083245e-05); |
| 54 | } else { |
| 55 | grad_dot = max(grad_dot, 1.1754999560161448e-38); |
| 56 | } |
| 57 | float approx_dist = implicit * inversesqrt(grad_dot); |
| 58 | @if (medPrecision) { |
| 59 | approx_dist *= %s.x; |
| 60 | } |
| 61 | half alpha; |
| 62 | @switch (%d) { |
| 63 | case 0: |
| 64 | alpha = approx_dist > 0.0 ? 0.0 : 1.0; |
| 65 | break; |
| 66 | case 1: |
| 67 | alpha = clamp(0.5 - half(approx_dist), 0.0, 1.0); |
| 68 | break; |
| 69 | case 2: |
| 70 | alpha = approx_dist > 0.0 ? 1.0 : 0.0; |
| 71 | break; |
| 72 | case 3: |
| 73 | alpha = clamp(0.5 + half(approx_dist), 0.0, 1.0); |
| 74 | break; |
| 75 | default: |
| 76 | discard; |
| 77 | })SkSL" , |
| 78 | prevRadii.fX, prevRadii.fY, (medPrecision ? "true" : "false" ), |
| 79 | args.fUniformHandler->getUniformCStr(ellipseVar), |
| 80 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)" , |
| 81 | args.fUniformHandler->getUniformCStr(ellipseVar), |
| 82 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)" , |
| 83 | (int)_outer.edgeType); |
| 84 | SkString _sample4481 = this->invokeChild(0, args); |
| 85 | fragBuilder->codeAppendf( |
| 86 | R"SkSL( |
| 87 | half4 inputColor = %s; |
| 88 | %s = inputColor * alpha; |
| 89 | )SkSL" , |
| 90 | _sample4481.c_str(), args.fOutputColor); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 95 | const GrFragmentProcessor& _proc) override { |
| 96 | const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>(); |
| 97 | auto edgeType = _outer.edgeType; |
| 98 | (void)edgeType; |
| 99 | auto center = _outer.center; |
| 100 | (void)center; |
| 101 | auto radii = _outer.radii; |
| 102 | (void)radii; |
| 103 | UniformHandle& ellipse = ellipseVar; |
| 104 | (void)ellipse; |
| 105 | UniformHandle& scale = scaleVar; |
| 106 | (void)scale; |
| 107 | |
| 108 | if (radii != prevRadii || center != prevCenter) { |
| 109 | float invRXSqd; |
| 110 | float invRYSqd; |
| 111 | // If we're using a scale factor to work around precision issues, choose the larger |
| 112 | // radius as the scale factor. The inv radii need to be pre-adjusted by the scale |
| 113 | // factor. |
| 114 | if (scale.isValid()) { |
| 115 | if (radii.fX > radii.fY) { |
| 116 | invRXSqd = 1.f; |
| 117 | invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY); |
| 118 | pdman.set2f(scale, radii.fX, 1.f / radii.fX); |
| 119 | } else { |
| 120 | invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX); |
| 121 | invRYSqd = 1.f; |
| 122 | pdman.set2f(scale, radii.fY, 1.f / radii.fY); |
| 123 | } |
| 124 | } else { |
| 125 | invRXSqd = 1.f / (radii.fX * radii.fX); |
| 126 | invRYSqd = 1.f / (radii.fY * radii.fY); |
| 127 | } |
| 128 | pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd); |
| 129 | prevCenter = center; |
| 130 | prevRadii = radii; |
| 131 | } |
| 132 | } |
| 133 | SkPoint prevCenter = float2(0); |
| 134 | SkPoint prevRadii = float2(0); |
| 135 | bool medPrecision = false; |
| 136 | UniformHandle ellipseVar; |
| 137 | UniformHandle scaleVar; |
| 138 | }; |
| 139 | GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const { |
| 140 | return new GrGLSLEllipseEffect(); |
| 141 | } |
| 142 | void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 143 | GrProcessorKeyBuilder* b) const { |
| 144 | b->add32((uint32_t)edgeType); |
| 145 | } |
| 146 | bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 147 | const GrEllipseEffect& that = other.cast<GrEllipseEffect>(); |
| 148 | (void)that; |
| 149 | if (edgeType != that.edgeType) return false; |
| 150 | if (center != that.center) return false; |
| 151 | if (radii != that.radii) return false; |
| 152 | return true; |
| 153 | } |
| 154 | GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src) |
| 155 | : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags()) |
| 156 | , edgeType(src.edgeType) |
| 157 | , center(src.center) |
| 158 | , radii(src.radii) { |
| 159 | this->cloneAndRegisterAllChildProcessors(src); |
| 160 | } |
| 161 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const { |
| 162 | return std::make_unique<GrEllipseEffect>(*this); |
| 163 | } |
| 164 | #if GR_TEST_UTILS |
| 165 | SkString GrEllipseEffect::onDumpInfo() const { |
| 166 | return SkStringPrintf("(edgeType=%d, center=float2(%f, %f), radii=float2(%f, %f))" , |
| 167 | (int)edgeType, center.fX, center.fY, radii.fX, radii.fY); |
| 168 | } |
| 169 | #endif |
| 170 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect); |
| 171 | #if GR_TEST_UTILS |
| 172 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) { |
| 173 | SkPoint center; |
| 174 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 175 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 176 | SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f); |
| 177 | SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f); |
| 178 | bool success; |
| 179 | std::unique_ptr<GrFragmentProcessor> fp = testData->inputFP(); |
| 180 | do { |
| 181 | GrClipEdgeType et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 182 | std::tie(success, fp) = GrEllipseEffect::Make( |
| 183 | std::move(fp), et, center, SkPoint::Make(rx, ry), *testData->caps()->shaderCaps()); |
| 184 | } while (!success); |
| 185 | return fp; |
| 186 | } |
| 187 | #endif |
| 188 | |