Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 13 | #include "src/core/SkUtils.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 14 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #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 Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 20 | class GrGLSLAARectEffect : public GrGLSLFragmentProcessor { |
| 21 | public: |
| 22 | GrGLSLAARectEffect() {} |
| 23 | void emitCode(EmitArgs& args) override { |
| 24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 25 | const GrAARectEffect& _outer = args.fFp.cast<GrAARectEffect>(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 26 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 27 | auto edgeType = _outer.edgeType; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 28 | (void)edgeType; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 29 | auto rect = _outer.rect; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 30 | (void)rect; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 31 | prevRect = float4(-1.0); |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 32 | rectUniformVar = args.fUniformHandler->addUniform( |
| 33 | &_outer, kFragment_GrShaderFlag, kFloat4_GrSLType, "rectUniform"); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 34 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 35 | R"SkSL(float4 prevRect = float4(%f, %f, %f, %f); |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 36 | half coverage; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 37 | @switch (%d) { |
| 38 | case 0: |
| 39 | case 2: |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 40 | coverage = half(all(greaterThan(float4(sk_FragCoord.xy, %s.zw), float4(%s.xy, sk_FragCoord.xy))) ? 1 : 0); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 41 | break; |
| 42 | default: |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 43 | 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 Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 46 | } |
| 47 | @if (%d == 2 || %d == 3) { |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 48 | coverage = 1.0 - coverage; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 49 | })SkSL", |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 50 | prevRect.left(), |
| 51 | prevRect.top(), |
| 52 | prevRect.right(), |
| 53 | prevRect.bottom(), |
| 54 | (int)_outer.edgeType, |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 55 | args.fUniformHandler->getUniformCStr(rectUniformVar), |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 56 | args.fUniformHandler->getUniformCStr(rectUniformVar), |
| 57 | args.fUniformHandler->getUniformCStr(rectUniformVar), |
| 58 | (int)_outer.edgeType, |
John Stiles | f79ef18 | 2020-06-05 13:38:16 -0400 | [diff] [blame] | 59 | (int)_outer.edgeType); |
John Stiles | 6e7cfaf | 2020-11-03 14:07:38 -0500 | [diff] [blame] | 60 | SkString _sample0 = this->invokeChild(0, args); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 61 | fragBuilder->codeAppendf( |
| 62 | R"SkSL( |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 63 | return %s * coverage; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 64 | )SkSL", |
John Stiles | e28f509 | 2020-12-14 14:01:44 -0500 | [diff] [blame] | 65 | _sample0.c_str()); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
| 69 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 70 | const GrFragmentProcessor& _proc) override { |
| 71 | const GrAARectEffect& _outer = _proc.cast<GrAARectEffect>(); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 72 | auto edgeType = _outer.edgeType; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 73 | (void)edgeType; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 74 | auto rect = _outer.rect; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 75 | (void)rect; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 76 | UniformHandle& rectUniform = rectUniformVar; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 77 | (void)rectUniform; |
| 78 | |
Brian Salomon | 066f411 | 2021-02-23 10:55:22 -0500 | [diff] [blame] | 79 | SkASSERT(rect.isSorted()); |
Brian Salomon | d00d351 | 2021-02-24 12:11:04 -0500 | [diff] [blame] | 80 | // 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 Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 84 | if (newRect != prevRect) { |
| 85 | pdman.set4f(rectUniform, newRect.fLeft, newRect.fTop, newRect.fRight, newRect.fBottom); |
| 86 | prevRect = newRect; |
| 87 | } |
| 88 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 89 | SkRect prevRect = float4(0); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 90 | UniformHandle rectUniformVar; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 91 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 92 | std::unique_ptr<GrGLSLFragmentProcessor> GrAARectEffect::onMakeProgramImpl() const { |
| 93 | return std::make_unique<GrGLSLAARectEffect>(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 94 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 95 | void GrAARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 96 | GrProcessorKeyBuilder* b) const { |
Brian Osman | 4c59437 | 2021-03-02 16:57:09 -0500 | [diff] [blame] | 97 | b->addBits(2, (uint32_t)edgeType, "edgeType"); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 98 | } |
| 99 | bool GrAARectEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 100 | const GrAARectEffect& that = other.cast<GrAARectEffect>(); |
| 101 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 102 | if (edgeType != that.edgeType) return false; |
| 103 | if (rect != that.rect) return false; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | GrAARectEffect::GrAARectEffect(const GrAARectEffect& src) |
| 107 | : INHERITED(kGrAARectEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 108 | , edgeType(src.edgeType) |
John Stiles | f79ef18 | 2020-06-05 13:38:16 -0400 | [diff] [blame] | 109 | , rect(src.rect) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 110 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | f79ef18 | 2020-06-05 13:38:16 -0400 | [diff] [blame] | 111 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 112 | std::unique_ptr<GrFragmentProcessor> GrAARectEffect::clone() const { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 113 | return std::make_unique<GrAARectEffect>(*this); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 114 | } |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 115 | #if GR_TEST_UTILS |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 116 | SkString GrAARectEffect::onDumpInfo() const { |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 117 | 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 Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 123 | } |
| 124 | #endif |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 125 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAARectEffect); |
| 126 | #if GR_TEST_UTILS |
| 127 | std::unique_ptr<GrFragmentProcessor> GrAARectEffect::TestCreate(GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 128 | SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(), |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 129 | d->fRandom->nextSScalar1(), |
| 130 | d->fRandom->nextSScalar1(), |
| 131 | d->fRandom->nextSScalar1()); |
Brian Salomon | 066f411 | 2021-02-23 10:55:22 -0500 | [diff] [blame] | 132 | rect.sort(); |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 133 | GrClipEdgeType edgeType = |
| 134 | static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 135 | |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 136 | return GrAARectEffect::Make(d->inputFP(), edgeType, rect); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 137 | } |
| 138 | #endif |