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 | |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 33 | circleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 34 | kFloat4_GrSLType, "circle"); |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 35 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 36 | R"SkSL(float2 prevCenter; |
| 37 | float prevRadius = %f; |
| 38 | half d; |
| 39 | @if (%d == 2 || %d == 3) { |
| 40 | d = half((length((%s.xy - sk_FragCoord.xy) * %s.w) - 1.0) * %s.z); |
| 41 | } else { |
| 42 | d = half((1.0 - length((%s.xy - sk_FragCoord.xy) * %s.w)) * %s.z); |
| 43 | })SkSL", |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 44 | prevRadius, (int)_outer.edgeType, (int)_outer.edgeType, |
| 45 | args.fUniformHandler->getUniformCStr(circleVar), |
| 46 | args.fUniformHandler->getUniformCStr(circleVar), |
| 47 | args.fUniformHandler->getUniformCStr(circleVar), |
| 48 | args.fUniformHandler->getUniformCStr(circleVar), |
| 49 | args.fUniformHandler->getUniformCStr(circleVar), |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 50 | args.fUniformHandler->getUniformCStr(circleVar)); |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame^] | 51 | SkString _sample2509 = this->invokeChild(0, args); |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 52 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 53 | R"SkSL( |
| 54 | half4 inputColor = %s; |
| 55 | @if (%d == 1 || %d == 3) { |
| 56 | %s = inputColor * clamp(d, 0.0, 1.0); |
| 57 | } else { |
| 58 | %s = d > 0.5 ? inputColor : half4(0.0); |
| 59 | } |
| 60 | )SkSL", |
John Stiles | 72e5764 | 2020-06-24 10:42:35 -0400 | [diff] [blame] | 61 | _sample2509.c_str(), (int)_outer.edgeType, (int)_outer.edgeType, args.fOutputColor, |
John Stiles | 3b2c06c | 2020-06-17 12:45:57 -0400 | [diff] [blame] | 62 | args.fOutputColor); |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
| 66 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 67 | const GrFragmentProcessor& _proc) override { |
| 68 | const GrCircleEffect& _outer = _proc.cast<GrCircleEffect>(); |
| 69 | auto edgeType = _outer.edgeType; |
| 70 | (void)edgeType; |
| 71 | auto center = _outer.center; |
| 72 | (void)center; |
| 73 | auto radius = _outer.radius; |
| 74 | (void)radius; |
| 75 | UniformHandle& circle = circleVar; |
| 76 | (void)circle; |
| 77 | |
| 78 | if (radius != prevRadius || center != prevCenter) { |
| 79 | SkScalar effectiveRadius = radius; |
| 80 | if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType)edgeType)) { |
| 81 | effectiveRadius -= 0.5f; |
| 82 | // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the |
| 83 | // shader. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 84 | effectiveRadius = std::max(0.001f, effectiveRadius); |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 85 | } else { |
| 86 | effectiveRadius += 0.5f; |
| 87 | } |
| 88 | pdman.set4f(circle, center.fX, center.fY, effectiveRadius, |
| 89 | SkScalarInvert(effectiveRadius)); |
| 90 | prevCenter = center; |
| 91 | prevRadius = radius; |
| 92 | } |
| 93 | } |
Brian Salomon | 6eeaf92 | 2019-05-09 13:07:21 -0400 | [diff] [blame] | 94 | SkPoint prevCenter = float2(0); |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 95 | float prevRadius = 0; |
| 96 | UniformHandle circleVar; |
| 97 | }; |
| 98 | GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const { |
| 99 | return new GrGLSLCircleEffect(); |
| 100 | } |
| 101 | void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 102 | GrProcessorKeyBuilder* b) const { |
| 103 | b->add32((int32_t)edgeType); |
| 104 | } |
| 105 | bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 106 | const GrCircleEffect& that = other.cast<GrCircleEffect>(); |
| 107 | (void)that; |
| 108 | if (edgeType != that.edgeType) return false; |
| 109 | if (center != that.center) return false; |
| 110 | if (radius != that.radius) return false; |
| 111 | return true; |
| 112 | } |
| 113 | GrCircleEffect::GrCircleEffect(const GrCircleEffect& src) |
| 114 | : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags()) |
| 115 | , edgeType(src.edgeType) |
| 116 | , center(src.center) |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 117 | , radius(src.radius) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 118 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 119 | } |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 120 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const { |
| 121 | return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(*this)); |
| 122 | } |
| 123 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect); |
| 124 | #if GR_TEST_UTILS |
| 125 | std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) { |
| 126 | SkPoint center; |
| 127 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 128 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 129 | SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f); |
John Stiles | de975e3 | 2020-06-17 13:49:33 -0400 | [diff] [blame] | 130 | bool success; |
| 131 | std::unique_ptr<GrFragmentProcessor> fp; |
| 132 | do { |
| 133 | GrClipEdgeType et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 134 | std::tie(success, fp) = GrCircleEffect::Make(/*inputFP=*/nullptr, et, center, radius); |
| 135 | } while (!success); |
| 136 | return fp; |
Ethan Nicholas | 21a9b56 | 2019-04-10 12:06:19 -0400 | [diff] [blame] | 137 | } |
| 138 | #endif |