Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame^] | 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 GrCircleEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
| 11 | #include "GrCircleEffect.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 GrGLSLCircleEffect : public GrGLSLFragmentProcessor { |
| 19 | public: |
| 20 | GrGLSLCircleEffect() {} |
| 21 | void emitCode(EmitArgs& args) override { |
| 22 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 23 | const GrCircleEffect& _outer = args.fFp.cast<GrCircleEffect>(); |
| 24 | (void)_outer; |
| 25 | auto edgeType = _outer.edgeType; |
| 26 | (void)edgeType; |
| 27 | auto center = _outer.center; |
| 28 | (void)center; |
| 29 | auto radius = _outer.radius; |
| 30 | (void)radius; |
| 31 | prevRadius = -1.0; |
| 32 | circleVar = |
| 33 | args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "circle"); |
| 34 | fragBuilder->codeAppendf( |
| 35 | "half2 prevCenter;\nhalf prevRadius = %f;\nhalf d;\n@if (%d == 2 || %d == 3) {\n " |
| 36 | " d = half((length((float2(%s.xy) - sk_FragCoord.xy) * float(%s.w)) - 1.0) * " |
| 37 | "float(%s.z));\n} else {\n d = half((1.0 - length((float2(%s.xy) - " |
| 38 | "sk_FragCoord.xy) * float(%s.w))) * float(%s.z));\n}\n@if ((%d == 1 || %d == 3) || " |
| 39 | "%d == 4) {\n d = clamp(d, 0.0, 1.0);\n} else {\n d = d > 0.5 ? 1.0 : " |
| 40 | "0.0;\n}\n%s = %s * d;\n", |
| 41 | prevRadius, (int)_outer.edgeType, (int)_outer.edgeType, |
| 42 | args.fUniformHandler->getUniformCStr(circleVar), |
| 43 | args.fUniformHandler->getUniformCStr(circleVar), |
| 44 | args.fUniformHandler->getUniformCStr(circleVar), |
| 45 | args.fUniformHandler->getUniformCStr(circleVar), |
| 46 | args.fUniformHandler->getUniformCStr(circleVar), |
| 47 | args.fUniformHandler->getUniformCStr(circleVar), (int)_outer.edgeType, |
| 48 | (int)_outer.edgeType, (int)_outer.edgeType, args.fOutputColor, args.fInputColor); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 53 | const GrFragmentProcessor& _proc) override { |
| 54 | const GrCircleEffect& _outer = _proc.cast<GrCircleEffect>(); |
| 55 | auto edgeType = _outer.edgeType; |
| 56 | (void)edgeType; |
| 57 | auto center = _outer.center; |
| 58 | (void)center; |
| 59 | auto radius = _outer.radius; |
| 60 | (void)radius; |
| 61 | UniformHandle& circle = circleVar; |
| 62 | (void)circle; |
| 63 | |
| 64 | if (radius != prevRadius || center != prevCenter) { |
| 65 | SkScalar effectiveRadius = radius; |
| 66 | if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType)edgeType)) { |
| 67 | effectiveRadius -= 0.5f; |
| 68 | // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the |
| 69 | // shader. |
| 70 | effectiveRadius = SkTMax(0.001f, effectiveRadius); |
| 71 | } else { |
| 72 | effectiveRadius += 0.5f; |
| 73 | } |
| 74 | pdman.set4f(circle, center.fX, center.fY, effectiveRadius, |
| 75 | SkScalarInvert(effectiveRadius)); |
| 76 | prevCenter = center; |
| 77 | prevRadius = radius; |
| 78 | } |
| 79 | } |
| 80 | SkPoint prevCenter = half2(0); |
| 81 | float prevRadius = 0; |
| 82 | UniformHandle circleVar; |
| 83 | }; |
| 84 | GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const { |
| 85 | return new GrGLSLCircleEffect(); |
| 86 | } |
| 87 | void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 88 | GrProcessorKeyBuilder* b) const { |
| 89 | b->add32((int32_t)edgeType); |
| 90 | } |
| 91 | bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 92 | const GrCircleEffect& that = other.cast<GrCircleEffect>(); |
| 93 | (void)that; |
| 94 | if (edgeType != that.edgeType) return false; |
| 95 | if (center != that.center) return false; |
| 96 | if (radius != that.radius) return false; |
| 97 | return true; |
| 98 | } |
| 99 | GrCircleEffect::GrCircleEffect(const GrCircleEffect& src) |
| 100 | : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags()) |
| 101 | , edgeType(src.edgeType) |
| 102 | , center(src.center) |
| 103 | , radius(src.radius) {} |
| 104 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const { |
| 105 | return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(*this)); |
| 106 | } |
| 107 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect); |
| 108 | #if GR_TEST_UTILS |
| 109 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) { |
| 110 | SkPoint center; |
| 111 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 112 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 113 | SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f); |
| 114 | GrClipEdgeType et; |
| 115 | do { |
| 116 | et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 117 | } while (GrClipEdgeType::kHairlineAA == et); |
| 118 | return GrCircleEffect::Make(et, center, radius); |
| 119 | } |
| 120 | #endif |