blob: b85ad8f772ca567c9ddc65b7b144ecd8d4539df6 [file] [log] [blame]
Ethan Nicholas21a9b562019-04-10 12:06:19 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050012
Greg Daniel456f9b52020-03-05 19:14:18 +000013#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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 Nicholas21a9b562019-04-10 12:06:19 -040019class GrGLSLCircleEffect : public GrGLSLFragmentProcessor {
20public:
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 Nicholas16464c32020-04-06 13:53:05 -040033 circleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
34 kFloat4_GrSLType, "circle");
Ethan Nicholas21a9b562019-04-10 12:06:19 -040035 fragBuilder->codeAppendf(
Brian Salomon6eeaf922019-05-09 13:07:21 -040036 "float2 prevCenter;\nfloat prevRadius = %f;\nhalf d;\n@if (%d == 2 || %d == 3) {\n "
37 " d = half((length((%s.xy - sk_FragCoord.xy) * %s.w) - 1.0) * %s.z);\n} else {\n "
John Stiles2fbe6172020-06-08 13:28:43 -040038 " d = half((1.0 - length((%s.xy - sk_FragCoord.xy) * %s.w)) * %s.z);\n}",
Ethan Nicholas21a9b562019-04-10 12:06:19 -040039 prevRadius, (int)_outer.edgeType, (int)_outer.edgeType,
40 args.fUniformHandler->getUniformCStr(circleVar),
41 args.fUniformHandler->getUniformCStr(circleVar),
42 args.fUniformHandler->getUniformCStr(circleVar),
43 args.fUniformHandler->getUniformCStr(circleVar),
44 args.fUniformHandler->getUniformCStr(circleVar),
John Stiles2fbe6172020-06-08 13:28:43 -040045 args.fUniformHandler->getUniformCStr(circleVar));
46 SkString _input2569 = SkStringPrintf("%s", args.fInputColor);
47 SkString _sample2569;
48 if (_outer.inputFP_index >= 0) {
49 _sample2569 = this->invokeChild(_outer.inputFP_index, _input2569.c_str(), args);
50 } else {
51 _sample2569 = _input2569;
52 }
53 fragBuilder->codeAppendf(
54 "\nhalf4 inputColor = %s;\n@if ((%d == 1 || %d == 3) || %d == 4) {\n %s = "
55 "inputColor * clamp(d, 0.0, 1.0);\n} else {\n %s = d > 0.5 ? inputColor : "
56 "half4(0.0);\n}\n",
57 _sample2569.c_str(), (int)_outer.edgeType, (int)_outer.edgeType,
58 (int)_outer.edgeType, args.fOutputColor, args.fOutputColor);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040059 }
60
61private:
62 void onSetData(const GrGLSLProgramDataManager& pdman,
63 const GrFragmentProcessor& _proc) override {
64 const GrCircleEffect& _outer = _proc.cast<GrCircleEffect>();
65 auto edgeType = _outer.edgeType;
66 (void)edgeType;
67 auto center = _outer.center;
68 (void)center;
69 auto radius = _outer.radius;
70 (void)radius;
71 UniformHandle& circle = circleVar;
72 (void)circle;
73
74 if (radius != prevRadius || center != prevCenter) {
75 SkScalar effectiveRadius = radius;
76 if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType)edgeType)) {
77 effectiveRadius -= 0.5f;
78 // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the
79 // shader.
Brian Osman788b9162020-02-07 10:36:46 -050080 effectiveRadius = std::max(0.001f, effectiveRadius);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040081 } else {
82 effectiveRadius += 0.5f;
83 }
84 pdman.set4f(circle, center.fX, center.fY, effectiveRadius,
85 SkScalarInvert(effectiveRadius));
86 prevCenter = center;
87 prevRadius = radius;
88 }
89 }
Brian Salomon6eeaf922019-05-09 13:07:21 -040090 SkPoint prevCenter = float2(0);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040091 float prevRadius = 0;
92 UniformHandle circleVar;
93};
94GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const {
95 return new GrGLSLCircleEffect();
96}
97void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
98 GrProcessorKeyBuilder* b) const {
99 b->add32((int32_t)edgeType);
100}
101bool GrCircleEffect::onIsEqual(const GrFragmentProcessor& other) const {
102 const GrCircleEffect& that = other.cast<GrCircleEffect>();
103 (void)that;
104 if (edgeType != that.edgeType) return false;
105 if (center != that.center) return false;
106 if (radius != that.radius) return false;
107 return true;
108}
109GrCircleEffect::GrCircleEffect(const GrCircleEffect& src)
110 : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags())
111 , edgeType(src.edgeType)
112 , center(src.center)
John Stiles2fbe6172020-06-08 13:28:43 -0400113 , radius(src.radius) {
John Stiles88183902020-06-10 16:40:38 -0400114 if (src.inputFP_index >= 0) {
John Stiles3779f442020-06-15 10:48:49 -0400115 inputFP_index = this->cloneAndRegisterChildProcessor(src.childProcessor(src.inputFP_index));
John Stiles2fbe6172020-06-08 13:28:43 -0400116 }
117}
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400118std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const {
119 return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(*this));
120}
121GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect);
122#if GR_TEST_UTILS
123std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) {
124 SkPoint center;
125 center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
126 center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
127 SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f);
128 GrClipEdgeType et;
129 do {
130 et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
131 } while (GrClipEdgeType::kHairlineAA == et);
John Stiles2fbe6172020-06-08 13:28:43 -0400132 return GrCircleEffect::Make(/*inputFP=*/nullptr, et, center, radius);
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400133}
134#endif