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