blob: 5eb0719db0e9cca54f5b40e7f6b2b0602e5ef34f [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
John Stiles45f5b032020-07-27 17:31:29 -040013#include "src/core/SkUtils.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Ethan Nicholas21a9b562019-04-10 12:06:19 -040020class GrGLSLCircleEffect : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLCircleEffect() {}
23 void emitCode(EmitArgs& args) override {
24 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
25 const GrCircleEffect& _outer = args.fFp.cast<GrCircleEffect>();
26 (void)_outer;
27 auto edgeType = _outer.edgeType;
28 (void)edgeType;
29 auto center = _outer.center;
30 (void)center;
31 auto radius = _outer.radius;
32 (void)radius;
33 prevRadius = -1.0;
Ethan Nicholas16464c32020-04-06 13:53:05 -040034 circleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
35 kFloat4_GrSLType, "circle");
Ethan Nicholas21a9b562019-04-10 12:06:19 -040036 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040037 R"SkSL(float2 prevCenter;
38float prevRadius = %f;
39half d;
40@if (%d == 2 || %d == 3) {
41 d = half((length((%s.xy - sk_FragCoord.xy) * %s.w) - 1.0) * %s.z);
42} else {
43 d = half((1.0 - length((%s.xy - sk_FragCoord.xy) * %s.w)) * %s.z);
44})SkSL",
Ethan Nicholas21a9b562019-04-10 12:06:19 -040045 prevRadius, (int)_outer.edgeType, (int)_outer.edgeType,
46 args.fUniformHandler->getUniformCStr(circleVar),
47 args.fUniformHandler->getUniformCStr(circleVar),
48 args.fUniformHandler->getUniformCStr(circleVar),
49 args.fUniformHandler->getUniformCStr(circleVar),
50 args.fUniformHandler->getUniformCStr(circleVar),
John Stiles2fbe6172020-06-08 13:28:43 -040051 args.fUniformHandler->getUniformCStr(circleVar));
John Stiles6e7cfaf2020-11-03 14:07:38 -050052 SkString _sample0 = this->invokeChild(0, args);
John Stiles2fbe6172020-06-08 13:28:43 -040053 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040054 R"SkSL(
55half4 inputColor = %s;
56@if (%d == 1 || %d == 3) {
John Stiles5e7c0402020-10-20 19:43:18 -040057 return inputColor * clamp(d, 0.0, 1.0);
John Stiles50819422020-06-18 13:00:38 -040058} else {
John Stiles5e7c0402020-10-20 19:43:18 -040059 return d > 0.5 ? inputColor : half4(0.0);
John Stiles50819422020-06-18 13:00:38 -040060}
61)SkSL",
John Stiles6e7cfaf2020-11-03 14:07:38 -050062 _sample0.c_str(), (int)_outer.edgeType, (int)_outer.edgeType);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040063 }
64
65private:
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 Osman788b9162020-02-07 10:36:46 -050084 effectiveRadius = std::max(0.001f, effectiveRadius);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040085 } 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 Salomon6eeaf922019-05-09 13:07:21 -040094 SkPoint prevCenter = float2(0);
Ethan Nicholas21a9b562019-04-10 12:06:19 -040095 float prevRadius = 0;
96 UniformHandle circleVar;
97};
98GrGLSLFragmentProcessor* GrCircleEffect::onCreateGLSLInstance() const {
99 return new GrGLSLCircleEffect();
100}
101void GrCircleEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
102 GrProcessorKeyBuilder* b) const {
John Stiles45f5b032020-07-27 17:31:29 -0400103 b->add32((uint32_t)edgeType);
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400104}
105bool 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}
John Stiles5e7c0402020-10-20 19:43:18 -0400113bool GrCircleEffect::usesExplicitReturn() const { return true; }
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400114GrCircleEffect::GrCircleEffect(const GrCircleEffect& src)
115 : INHERITED(kGrCircleEffect_ClassID, src.optimizationFlags())
116 , edgeType(src.edgeType)
117 , center(src.center)
John Stiles2fbe6172020-06-08 13:28:43 -0400118 , radius(src.radius) {
Brian Osman12c5d292020-07-13 16:11:35 -0400119 this->cloneAndRegisterAllChildProcessors(src);
John Stiles2fbe6172020-06-08 13:28:43 -0400120}
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400121std::unique_ptr<GrFragmentProcessor> GrCircleEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400122 return std::make_unique<GrCircleEffect>(*this);
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400123}
John Stiles8d9bf642020-08-12 15:07:45 -0400124#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400125SkString GrCircleEffect::onDumpInfo() const {
126 return SkStringPrintf("(edgeType=%d, center=float2(%f, %f), radius=%f)", (int)edgeType,
127 center.fX, center.fY, radius);
John Stiles47b4e222020-08-12 09:56:50 -0400128}
129#endif
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400130GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleEffect);
131#if GR_TEST_UTILS
132std::unique_ptr<GrFragmentProcessor> GrCircleEffect::TestCreate(GrProcessorTestData* testData) {
133 SkPoint center;
134 center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
135 center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
136 SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f);
John Stilesde975e32020-06-17 13:49:33 -0400137 bool success;
John Stiles6609cb62020-07-17 14:52:12 -0400138 std::unique_ptr<GrFragmentProcessor> fp = testData->inputFP();
John Stilesde975e32020-06-17 13:49:33 -0400139 do {
140 GrClipEdgeType et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
John Stiles6609cb62020-07-17 14:52:12 -0400141 std::tie(success, fp) = GrCircleEffect::Make(std::move(fp), et, center, radius);
John Stilesde975e32020-06-17 13:49:33 -0400142 } while (!success);
143 return fp;
Ethan Nicholas21a9b562019-04-10 12:06:19 -0400144}
145#endif