blob: bc4fcef07a5bda2d0b740057d60cec7935446acc [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 Stiles50819422020-06-18 13:00:38 -040032 rectUniformVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
33 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",
50 prevRect.left(), prevRect.top(), prevRect.right(), prevRect.bottom(),
51 (int)_outer.edgeType, args.fUniformHandler->getUniformCStr(rectUniformVar),
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040052 args.fUniformHandler->getUniformCStr(rectUniformVar),
John Stiles50819422020-06-18 13:00:38 -040053 args.fUniformHandler->getUniformCStr(rectUniformVar), (int)_outer.edgeType,
John Stilesf79ef182020-06-05 13:38:16 -040054 (int)_outer.edgeType);
John Stiles6e7cfaf2020-11-03 14:07:38 -050055 SkString _sample0 = this->invokeChild(0, args);
John Stiles50819422020-06-18 13:00:38 -040056 fragBuilder->codeAppendf(
57 R"SkSL(
Brian Salomond00d3512021-02-24 12:11:04 -050058return %s * coverage;
John Stiles50819422020-06-18 13:00:38 -040059)SkSL",
John Stilese28f5092020-12-14 14:01:44 -050060 _sample0.c_str());
Ethan Nicholaseace9352018-10-15 20:09:54 +000061 }
62
63private:
64 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050065 const GrFragmentProcessor& _proc) override {
66 const GrAARectEffect& _outer = _proc.cast<GrAARectEffect>();
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040067 auto edgeType = _outer.edgeType;
Ethan Nicholaseace9352018-10-15 20:09:54 +000068 (void)edgeType;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040069 auto rect = _outer.rect;
Ethan Nicholaseace9352018-10-15 20:09:54 +000070 (void)rect;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040071 UniformHandle& rectUniform = rectUniformVar;
Ethan Nicholaseace9352018-10-15 20:09:54 +000072 (void)rectUniform;
73
Brian Salomon066f4112021-02-23 10:55:22 -050074 SkASSERT(rect.isSorted());
Brian Salomond00d3512021-02-24 12:11:04 -050075 // The AA math in the shader evaluates to 0 at the uploaded coordinates, so outset by 0.5
76 // to interpolate from 0 at a half pixel inset and 1 at a half pixel outset of rect.
77 const SkRect& newRect =
78 GrProcessorEdgeTypeIsAA(edgeType) ? rect.makeOutset(.5f, .5f) : rect;
Ethan Nicholaseace9352018-10-15 20:09:54 +000079 if (newRect != prevRect) {
80 pdman.set4f(rectUniform, newRect.fLeft, newRect.fTop, newRect.fRight, newRect.fBottom);
81 prevRect = newRect;
82 }
83 }
Mike Kleind6ab77a2019-03-21 08:18:24 -050084 SkRect prevRect = float4(0);
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040085 UniformHandle rectUniformVar;
Ethan Nicholaseace9352018-10-15 20:09:54 +000086};
Brian Salomon18ab2032021-02-23 10:07:05 -050087std::unique_ptr<GrGLSLFragmentProcessor> GrAARectEffect::onMakeProgramImpl() const {
88 return std::make_unique<GrGLSLAARectEffect>();
Ethan Nicholaseace9352018-10-15 20:09:54 +000089}
Mike Kleind6ab77a2019-03-21 08:18:24 -050090void GrAARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholaseace9352018-10-15 20:09:54 +000091 GrProcessorKeyBuilder* b) const {
Brian Osman4c594372021-03-02 16:57:09 -050092 b->addBits(2, (uint32_t)edgeType, "edgeType");
Ethan Nicholaseace9352018-10-15 20:09:54 +000093}
94bool GrAARectEffect::onIsEqual(const GrFragmentProcessor& other) const {
95 const GrAARectEffect& that = other.cast<GrAARectEffect>();
96 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040097 if (edgeType != that.edgeType) return false;
98 if (rect != that.rect) return false;
Ethan Nicholaseace9352018-10-15 20:09:54 +000099 return true;
100}
101GrAARectEffect::GrAARectEffect(const GrAARectEffect& src)
102 : INHERITED(kGrAARectEffect_ClassID, src.optimizationFlags())
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400103 , edgeType(src.edgeType)
John Stilesf79ef182020-06-05 13:38:16 -0400104 , rect(src.rect) {
Brian Osman12c5d292020-07-13 16:11:35 -0400105 this->cloneAndRegisterAllChildProcessors(src);
John Stilesf79ef182020-06-05 13:38:16 -0400106}
Ethan Nicholaseace9352018-10-15 20:09:54 +0000107std::unique_ptr<GrFragmentProcessor> GrAARectEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -0400108 return std::make_unique<GrAARectEffect>(*this);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000109}
John Stiles8d9bf642020-08-12 15:07:45 -0400110#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -0400111SkString GrAARectEffect::onDumpInfo() const {
112 return SkStringPrintf("(edgeType=%d, rect=float4(%f, %f, %f, %f))", (int)edgeType, rect.left(),
113 rect.top(), rect.right(), rect.bottom());
John Stiles47b4e222020-08-12 09:56:50 -0400114}
115#endif
Ethan Nicholaseace9352018-10-15 20:09:54 +0000116GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAARectEffect);
117#if GR_TEST_UTILS
118std::unique_ptr<GrFragmentProcessor> GrAARectEffect::TestCreate(GrProcessorTestData* d) {
Mike Kleind6ab77a2019-03-21 08:18:24 -0500119 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
Ethan Nicholaseace9352018-10-15 20:09:54 +0000120 d->fRandom->nextSScalar1(),
121 d->fRandom->nextSScalar1(),
122 d->fRandom->nextSScalar1());
Brian Salomon066f4112021-02-23 10:55:22 -0500123 rect.sort();
John Stiles6609cb62020-07-17 14:52:12 -0400124 GrClipEdgeType edgeType =
125 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
Ethan Nicholaseace9352018-10-15 20:09:54 +0000126
John Stiles6609cb62020-07-17 14:52:12 -0400127 return GrAARectEffect::Make(d->inputFP(), edgeType, rect);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000128}
129#endif