Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrEllipseEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 11 | #include "GrEllipseEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #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 Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 19 | class GrGLSLEllipseEffect : public GrGLSLFragmentProcessor { |
| 20 | public: |
| 21 | GrGLSLEllipseEffect() {} |
| 22 | void emitCode(EmitArgs& args) override { |
| 23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 24 | const GrEllipseEffect& _outer = args.fFp.cast<GrEllipseEffect>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 25 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 26 | auto edgeType = _outer.edgeType; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 27 | (void)edgeType; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 28 | auto center = _outer.center; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 29 | (void)center; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 30 | auto radii = _outer.radii; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 31 | (void)radii; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 32 | prevRadii = float2(-1.0); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 33 | medPrecision = !sk_Caps.floatIs32Bits; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 34 | ellipseVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 35 | kFloat4_GrSLType, "ellipse"); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 36 | if (medPrecision) { |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 37 | scaleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 38 | kFloat2_GrSLType, "scale"); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 39 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 40 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 41 | R"SkSL(float2 prevCenter; |
| 42 | float2 prevRadii = float2(%f, %f); |
| 43 | bool medPrecision = %s; |
| 44 | float2 d = sk_FragCoord.xy - %s.xy; |
| 45 | @if (medPrecision) { |
| 46 | d *= %s.y; |
| 47 | } |
| 48 | float2 Z = d * %s.zw; |
| 49 | float implicit = dot(Z, d) - 1.0; |
| 50 | float grad_dot = 4.0 * dot(Z, Z); |
| 51 | @if (medPrecision) { |
| 52 | grad_dot = max(grad_dot, 6.1036000261083245e-05); |
| 53 | } else { |
| 54 | grad_dot = max(grad_dot, 1.1754999560161448e-38); |
| 55 | } |
| 56 | float approx_dist = implicit * inversesqrt(grad_dot); |
| 57 | @if (medPrecision) { |
| 58 | approx_dist *= %s.x; |
| 59 | } |
| 60 | half alpha; |
| 61 | @switch (%d) { |
| 62 | case 0: |
| 63 | alpha = approx_dist > 0.0 ? 0.0 : 1.0; |
| 64 | break; |
| 65 | case 1: |
| 66 | alpha = clamp(0.5 - half(approx_dist), 0.0, 1.0); |
| 67 | break; |
| 68 | case 2: |
| 69 | alpha = approx_dist > 0.0 ? 1.0 : 0.0; |
| 70 | break; |
| 71 | case 3: |
| 72 | alpha = clamp(0.5 + half(approx_dist), 0.0, 1.0); |
| 73 | break; |
| 74 | default: |
| 75 | discard; |
| 76 | })SkSL", |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 77 | prevRadii.fX, prevRadii.fY, (medPrecision ? "true" : "false"), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 78 | args.fUniformHandler->getUniformCStr(ellipseVar), |
| 79 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)", |
| 80 | args.fUniformHandler->getUniformCStr(ellipseVar), |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 81 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)", |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 82 | (int)_outer.edgeType); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 83 | SkString _input4481(args.fInputColor); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 84 | SkString _sample4481 = this->invokeChild(0, _input4481.c_str(), args); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 85 | fragBuilder->codeAppendf( |
| 86 | R"SkSL( |
| 87 | half4 inputColor = %s; |
| 88 | %s = inputColor * alpha; |
| 89 | )SkSL", |
| 90 | _sample4481.c_str(), args.fOutputColor); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 91 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 92 | |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 93 | private: |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 94 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 95 | const GrFragmentProcessor& _proc) override { |
| 96 | const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>(); |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 97 | auto edgeType = _outer.edgeType; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 98 | (void)edgeType; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 99 | auto center = _outer.center; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 100 | (void)center; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 101 | auto radii = _outer.radii; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 102 | (void)radii; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 103 | UniformHandle& ellipse = ellipseVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 104 | (void)ellipse; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 105 | UniformHandle& scale = scaleVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 106 | (void)scale; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 107 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 108 | if (radii != prevRadii || center != prevCenter) { |
| 109 | float invRXSqd; |
| 110 | float invRYSqd; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 111 | // If we're using a scale factor to work around precision issues, choose the larger |
| 112 | // radius as the scale factor. The inv radii need to be pre-adjusted by the scale |
| 113 | // factor. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 114 | if (scale.isValid()) { |
| 115 | if (radii.fX > radii.fY) { |
| 116 | invRXSqd = 1.f; |
| 117 | invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY); |
| 118 | pdman.set2f(scale, radii.fX, 1.f / radii.fX); |
| 119 | } else { |
| 120 | invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX); |
| 121 | invRYSqd = 1.f; |
| 122 | pdman.set2f(scale, radii.fY, 1.f / radii.fY); |
| 123 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 124 | } else { |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 125 | invRXSqd = 1.f / (radii.fX * radii.fX); |
| 126 | invRYSqd = 1.f / (radii.fY * radii.fY); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 127 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 128 | pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd); |
| 129 | prevCenter = center; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 130 | prevRadii = radii; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 131 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 132 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 133 | SkPoint prevCenter = float2(0); |
| 134 | SkPoint prevRadii = float2(0); |
Jim Van Verth | 20ae25c | 2019-03-29 08:50:41 -0400 | [diff] [blame] | 135 | bool medPrecision = false; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 136 | UniformHandle ellipseVar; |
| 137 | UniformHandle scaleVar; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 138 | }; |
| 139 | GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const { |
| 140 | return new GrGLSLEllipseEffect(); |
| 141 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 142 | void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 143 | GrProcessorKeyBuilder* b) const { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 144 | b->add32((int32_t)edgeType); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 145 | } |
| 146 | bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 147 | const GrEllipseEffect& that = other.cast<GrEllipseEffect>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 148 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 149 | if (edgeType != that.edgeType) return false; |
| 150 | if (center != that.center) return false; |
| 151 | if (radii != that.radii) return false; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 152 | return true; |
| 153 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 154 | GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 155 | : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 156 | , edgeType(src.edgeType) |
| 157 | , center(src.center) |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 158 | , radii(src.radii) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 159 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | 2fbe617 | 2020-06-08 13:28:43 -0400 | [diff] [blame] | 160 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 161 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const { |
| 162 | return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(*this)); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 163 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 164 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect); |
| 165 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 166 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) { |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 167 | SkPoint center; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 168 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 169 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
| 170 | SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f); |
| 171 | SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f); |
John Stiles | de975e3 | 2020-06-17 13:49:33 -0400 | [diff] [blame] | 172 | bool success; |
| 173 | std::unique_ptr<GrFragmentProcessor> fp; |
| 174 | do { |
| 175 | GrClipEdgeType et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 176 | std::tie(success, fp) = |
| 177 | GrEllipseEffect::Make(/*inputFP=*/nullptr, et, center, SkPoint::Make(rx, ry), |
| 178 | *testData->caps()->shaderCaps()); |
| 179 | } while (!success); |
| 180 | return fp; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 181 | } |
| 182 | #endif |