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" |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "glsl/GrGLSLProgramBuilder.h" |
Ethan Nicholas | 2d5f9b3 | 2017-12-13 14:36:14 -0500 | [diff] [blame] | 15 | #include "GrTexture.h" |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 16 | #include "SkSLCPP.h" |
| 17 | #include "SkSLUtil.h" |
| 18 | class GrGLSLEllipseEffect : public GrGLSLFragmentProcessor { |
| 19 | public: |
| 20 | GrGLSLEllipseEffect() {} |
| 21 | void emitCode(EmitArgs& args) override { |
| 22 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 23 | const GrEllipseEffect& _outer = args.fFp.cast<GrEllipseEffect>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 24 | (void)_outer; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 25 | auto edgeType = _outer.edgeType(); |
| 26 | (void)edgeType; |
| 27 | auto center = _outer.center(); |
| 28 | (void)center; |
| 29 | auto radii = _outer.radii(); |
| 30 | (void)radii; |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 31 | prevRadii = float2(-1.0); |
| 32 | useScale = !sk_Caps.floatIs32Bits; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 33 | fEllipseVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 34 | kDefault_GrSLPrecision, "ellipse"); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 35 | if (useScale) { |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 36 | fScaleVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat2_GrSLType, |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 37 | kDefault_GrSLPrecision, "scale"); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 38 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 39 | fragBuilder->codeAppendf( |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 40 | "float2 prevCenter;\nfloat2 prevRadii = float2(%f, %f);\nbool useScale = " |
| 41 | "%s;\nfloat2 d = sk_FragCoord.xy - %s.xy;\n@if (useScale) {\n d *= " |
| 42 | "%s.y;\n}\nfloat2 Z = d * %s.zw;\nfloat implicit = dot(Z, d) - 1.0;\nfloat " |
| 43 | "grad_dot = 4.0 * dot(Z, Z);\ngrad_dot = max(grad_dot, 0.0001);\nfloat approx_dist " |
| 44 | "= implicit * inversesqrt(grad_dot);\n@if (useScale) {\n approx_dist *= " |
| 45 | "%s.x;\n}\nhalf alpha;\n@switch (%d) {\n case 0:\n alpha = " |
| 46 | "half(approx_dist > 0.0 ? 0.0 : 1.0);\n break;\n case 1:\n ", |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 47 | prevRadii.fX, prevRadii.fY, (useScale ? "true" : "false"), |
| 48 | args.fUniformHandler->getUniformCStr(fEllipseVar), |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 49 | fScaleVar.isValid() ? args.fUniformHandler->getUniformCStr(fScaleVar) : "float2(0)", |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 50 | args.fUniformHandler->getUniformCStr(fEllipseVar), |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 51 | fScaleVar.isValid() ? args.fUniformHandler->getUniformCStr(fScaleVar) : "float2(0)", |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 52 | (int)_outer.edgeType()); |
Ethan Nicholas | d982d05 | 2017-10-16 10:22:03 -0400 | [diff] [blame] | 53 | fragBuilder->codeAppendf( |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 54 | " alpha = half(clamp(0.5 - approx_dist, 0.0, 1.0));\n break;\n case " |
| 55 | "2:\n alpha = half(approx_dist > 0.0 ? 1.0 : 0.0);\n break;\n " |
| 56 | "case 3:\n alpha = half(clamp(0.5 + approx_dist, 0.0, 1.0));\n " |
| 57 | "break;\n default:\n discard;\n}\n%s = %s * alpha;\n", |
Michael Ludwig | 231de03 | 2018-08-30 14:33:01 -0400 | [diff] [blame] | 58 | args.fOutputColor, args.fInputColor); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 59 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 60 | |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 61 | private: |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 62 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 63 | const GrFragmentProcessor& _proc) override { |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 64 | const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>(); |
| 65 | auto edgeType = _outer.edgeType(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 66 | (void)edgeType; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 67 | auto center = _outer.center(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 68 | (void)center; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 69 | auto radii = _outer.radii(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 70 | (void)radii; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 71 | UniformHandle& ellipse = fEllipseVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 72 | (void)ellipse; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 73 | UniformHandle& scale = fScaleVar; |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 74 | (void)scale; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 75 | |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 76 | if (radii != prevRadii || center != prevCenter) { |
| 77 | float invRXSqd; |
| 78 | float invRYSqd; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 79 | // If we're using a scale factor to work around precision issues, choose the larger |
| 80 | // radius as the scale factor. The inv radii need to be pre-adjusted by the scale |
| 81 | // factor. |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 82 | if (scale.isValid()) { |
| 83 | if (radii.fX > radii.fY) { |
| 84 | invRXSqd = 1.f; |
| 85 | invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY); |
| 86 | pdman.set2f(scale, radii.fX, 1.f / radii.fX); |
| 87 | } else { |
| 88 | invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX); |
| 89 | invRYSqd = 1.f; |
| 90 | pdman.set2f(scale, radii.fY, 1.f / radii.fY); |
| 91 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 92 | } else { |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 93 | invRXSqd = 1.f / (radii.fX * radii.fX); |
| 94 | invRYSqd = 1.f / (radii.fY * radii.fY); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 95 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 96 | pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd); |
| 97 | prevCenter = center; |
| 98 | prevRadii = radii; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 99 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 100 | } |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 101 | SkPoint prevCenter = float2(0); |
| 102 | SkPoint prevRadii = float2(0); |
| 103 | bool useScale = false; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 104 | UniformHandle fEllipseVar; |
| 105 | UniformHandle fScaleVar; |
| 106 | }; |
| 107 | GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const { |
| 108 | return new GrGLSLEllipseEffect(); |
| 109 | } |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 110 | void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 111 | GrProcessorKeyBuilder* b) const { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 112 | b->add32((int32_t)fEdgeType); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 113 | } |
| 114 | bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 115 | const GrEllipseEffect& that = other.cast<GrEllipseEffect>(); |
Ethan Nicholas | b7e8c3b | 2017-07-19 13:54:20 -0400 | [diff] [blame] | 116 | (void)that; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 117 | if (fEdgeType != that.fEdgeType) return false; |
| 118 | if (fCenter != that.fCenter) return false; |
| 119 | if (fRadii != that.fRadii) return false; |
| 120 | return true; |
| 121 | } |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 122 | GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 123 | : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags()) |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 124 | , fEdgeType(src.fEdgeType) |
| 125 | , fCenter(src.fCenter) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 126 | , fRadii(src.fRadii) {} |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 127 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const { |
| 128 | return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(*this)); |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 129 | } |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 130 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect); |
| 131 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 132 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) { |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 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 rx = testData->fRandom->nextRangeF(0.f, 1000.f); |
| 137 | SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f); |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 138 | GrClipEdgeType et; |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 139 | do { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 140 | et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
| 141 | } while (GrClipEdgeType::kHairlineAA == et); |
Brian Salomon | 1447177 | 2017-12-05 10:35:15 -0500 | [diff] [blame] | 142 | return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry), |
| 143 | *testData->caps()->shaderCaps()); |
Ethan Nicholas | 420f156 | 2017-07-14 13:11:38 -0400 | [diff] [blame] | 144 | } |
| 145 | #endif |