blob: d8782d2bb1f009bf70c3d04d183738b4fabb4c30 [file] [log] [blame]
Ethan Nicholaseace9352018-10-15 20:09:54 +00001/*
2 * Copyright 2018 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 GrAARectEffect.fp; do not modify.
10 **************************************************************************************************/
11#include "GrAARectEffect.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 Nicholaseace9352018-10-15 20:09:54 +000020class GrGLSLAARectEffect : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLAARectEffect() {}
23 void emitCode(EmitArgs& args) override {
24 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Mike Kleind6ab77a2019-03-21 08:18:24 -050025 const GrAARectEffect& _outer = args.fFp.cast<GrAARectEffect>();
Ethan Nicholaseace9352018-10-15 20:09:54 +000026 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040027 auto edgeType = _outer.edgeType;
Ethan Nicholaseace9352018-10-15 20:09:54 +000028 (void)edgeType;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040029 auto rect = _outer.rect;
Ethan Nicholaseace9352018-10-15 20:09:54 +000030 (void)rect;
Mike Kleind6ab77a2019-03-21 08:18:24 -050031 prevRect = float4(-1.0);
John Stiles9b660ef2021-03-04 15:45:00 -050032 rectUniformVar = args.fUniformHandler->addUniform(
33 &_outer, kFragment_GrShaderFlag, kFloat4_GrSLType, "rectUniform");
Ethan Nicholaseace9352018-10-15 20:09:54 +000034 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040035 R"SkSL(float4 prevRect = float4(%f, %f, %f, %f);
Brian Salomond00d3512021-02-24 12:11:04 -050036half coverage;
John Stiles50819422020-06-18 13:00:38 -040037@switch (%d) {
38 case 0:
39 case 2:
Brian Salomond00d3512021-02-24 12:11:04 -050040 coverage = half(all(greaterThan(float4(sk_FragCoord.xy, %s.zw), float4(%s.xy, sk_FragCoord.xy))) ? 1 : 0);
John Stiles50819422020-06-18 13:00:38 -040041 break;
42 default:
Brian Salomond00d3512021-02-24 12:11:04 -050043 half4 dists4 = clamp(half4(1.0, 1.0, -1.0, -1.0) * half4(sk_FragCoord.xyxy - %s), 0.0, 1.0);
44 half2 dists2 = (dists4.xy + dists4.zw) - 1.0;
45 coverage = dists2.x * dists2.y;
John Stiles50819422020-06-18 13:00:38 -040046}
47@if (%d == 2 || %d == 3) {
Brian Salomond00d3512021-02-24 12:11:04 -050048 coverage = 1.0 - coverage;
John Stiles50819422020-06-18 13:00:38 -040049})SkSL",
John Stiles9b660ef2021-03-04 15:45:00 -050050 prevRect.left(),
51 prevRect.top(),
52 prevRect.right(),
53 prevRect.bottom(),
54 (int)_outer.edgeType,
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040055 args.fUniformHandler->getUniformCStr(rectUniformVar),
John Stiles9b660ef2021-03-04 15:45:00 -050056 args.fUniformHandler->getUniformCStr(rectUniformVar),
57 args.fUniformHandler->getUniformCStr(rectUniformVar),
58 (int)_outer.edgeType,
John Stilesf79ef182020-06-05 13:38:16 -040059 (int)_outer.edgeType);
John Stiles6e7cfaf2020-11-03 14:07:38 -050060 SkString _sample0 = this->invokeChild(0, args);
John Stiles50819422020-06-18 13:00:38 -040061 fragBuilder->codeAppendf(
62 R"SkSL(
Brian Salomond00d3512021-02-24 12:11:04 -050063return %s * coverage;
John Stiles50819422020-06-18 13:00:38 -040064)SkSL",
John Stilese28f5092020-12-14 14:01:44 -050065 _sample0.c_str());
Ethan Nicholaseace9352018-10-15 20:09:54 +000066 }
67
68private:
69 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050070 const GrFragmentProcessor& _proc) override {
71 const GrAARectEffect& _outer = _proc.cast<GrAARectEffect>();
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040072 auto edgeType = _outer.edgeType;
Ethan Nicholaseace9352018-10-15 20:09:54 +000073 (void)edgeType;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040074 auto rect = _outer.rect;
Ethan Nicholaseace9352018-10-15 20:09:54 +000075 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040076 UniformHandle& rectUniform = rectUniformVar;
Ethan Nicholaseace9352018-10-15 20:09:54 +000077 (void)rectUniform;
78
Brian Salomon066f4112021-02-23 10:55:22 -050079 SkASSERT(rect.isSorted());
Brian Salomond00d3512021-02-24 12:11:04 -050080 // The AA math in the shader evaluates to 0 at the uploaded coordinates, so outset by 0.5
81 // to interpolate from 0 at a half pixel inset and 1 at a half pixel outset of rect.
82 const SkRect& newRect =
83 GrProcessorEdgeTypeIsAA(edgeType) ? rect.makeOutset(.5f, .5f) : rect;
Ethan Nicholaseace9352018-10-15 20:09:54 +000084 if (newRect != prevRect) {
85 pdman.set4f(rectUniform, newRect.fLeft, newRect.fTop, newRect.fRight, newRect.fBottom);
86 prevRect = newRect;
87 }
88 }
Mike Kleind6ab77a2019-03-21 08:18:24 -050089 SkRect prevRect = float4(0);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040090 UniformHandle rectUniformVar;
Ethan Nicholaseace9352018-10-15 20:09:54 +000091};
Brian Salomon18ab2032021-02-23 10:07:05 -050092std::unique_ptr<GrGLSLFragmentProcessor> GrAARectEffect::onMakeProgramImpl() const {
93 return std::make_unique<GrGLSLAARectEffect>();
Ethan Nicholaseace9352018-10-15 20:09:54 +000094}
Mike Kleind6ab77a2019-03-21 08:18:24 -050095void GrAARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholaseace9352018-10-15 20:09:54 +000096 GrProcessorKeyBuilder* b) const {
Brian Osman4c594372021-03-02 16:57:09 -050097 b->addBits(2, (uint32_t)edgeType, "edgeType");
Ethan Nicholaseace9352018-10-15 20:09:54 +000098}
99bool GrAARectEffect::onIsEqual(const GrFragmentProcessor& other) const {
100 const GrAARectEffect& that = other.cast<GrAARectEffect>();
101 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400102 if (edgeType != that.edgeType) return false;
103 if (rect != that.rect) return false;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000104 return true;
105}
106GrAARectEffect::GrAARectEffect(const GrAARectEffect& src)
107 : INHERITED(kGrAARectEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400108 , edgeType(src.edgeType)
John Stilesf79ef182020-06-05 13:38:16 -0400109 , rect(src.rect) {
Brian Osman12c5d292020-07-13 16:11:35 -0400110 this->cloneAndRegisterAllChildProcessors(src);
John Stilesf79ef182020-06-05 13:38:16 -0400111}
Ethan Nicholaseace9352018-10-15 20:09:54 +0000112std::unique_ptr<GrFragmentProcessor> GrAARectEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400113 return std::make_unique<GrAARectEffect>(*this);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000114}
John Stiles8d9bf642020-08-12 15:07:45 -0400115#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400116SkString GrAARectEffect::onDumpInfo() const {
John Stiles9b660ef2021-03-04 15:45:00 -0500117 return SkStringPrintf("(edgeType=%d, rect=float4(%f, %f, %f, %f))",
118 (int)edgeType,
119 rect.left(),
120 rect.top(),
121 rect.right(),
122 rect.bottom());
John Stiles47b4e222020-08-12 09:56:50 -0400123}
124#endif
Ethan Nicholaseace9352018-10-15 20:09:54 +0000125GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAARectEffect);
126#if GR_TEST_UTILS
127std::unique_ptr<GrFragmentProcessor> GrAARectEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500128 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000129 d->fRandom->nextSScalar1(),
130 d->fRandom->nextSScalar1(),
131 d->fRandom->nextSScalar1());
Brian Salomon066f4112021-02-23 10:55:22 -0500132 rect.sort();
John Stiles6609cb62020-07-17 14:52:12 -0400133 GrClipEdgeType edgeType =
134 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
Ethan Nicholaseace9352018-10-15 20:09:54 +0000135
John Stiles6609cb62020-07-17 14:52:12 -0400136 return GrAARectEffect::Make(d->inputFP(), edgeType, rect);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000137}
138#endif