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 | |
8 | /************************************************************************************************** |
9 | *** This file was autogenerated from GrEllipseEffect.fp; do not modify. |
10 | **************************************************************************************************/ |
11 | #include "GrEllipseEffect.h" |
12 | |
13 | #include "src/gpu/GrTexture.h" |
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" |
19 | class GrGLSLEllipseEffect : public GrGLSLFragmentProcessor { |
20 | public: |
21 | GrGLSLEllipseEffect() {} |
22 | void emitCode(EmitArgs& args) override { |
23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
24 | const GrEllipseEffect& _outer = args.fFp.cast<GrEllipseEffect>(); |
25 | (void)_outer; |
26 | auto edgeType = _outer.edgeType; |
27 | (void)edgeType; |
28 | auto center = _outer.center; |
29 | (void)center; |
30 | auto radii = _outer.radii; |
31 | (void)radii; |
32 | prevRadii = float2(-1.0); |
33 | medPrecision = !sk_Caps.floatIs32Bits; |
34 | ellipseVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
35 | kFloat4_GrSLType, "ellipse" ); |
36 | if (medPrecision) { |
37 | scaleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
38 | kFloat2_GrSLType, "scale" ); |
39 | } |
40 | fragBuilder->codeAppendf( |
41 | "float2 prevCenter;\nfloat2 prevRadii = float2(%f, %f);\nbool medPrecision = " |
42 | "%s;\nfloat2 d = sk_FragCoord.xy - %s.xy;\n@if (medPrecision) {\n d *= " |
43 | "%s.y;\n}\nfloat2 Z = d * %s.zw;\nfloat implicit = dot(Z, d) - 1.0;\nfloat " |
44 | "grad_dot = 4.0 * dot(Z, Z);\n@if (medPrecision) {\n grad_dot = max(grad_dot, " |
45 | "6.1036000261083245e-05);\n} else {\n grad_dot = max(grad_dot, " |
46 | "1.1754999560161448e-38);\n}\nfloat approx_dist = implicit * " |
47 | "inversesqrt(grad_dot);\n@if (medPrecision) {\n approx_dist *= %s.x;\n}\nhalf " |
48 | "alph" , |
49 | prevRadii.fX, prevRadii.fY, (medPrecision ? "true" : "false" ), |
50 | args.fUniformHandler->getUniformCStr(ellipseVar), |
51 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)" , |
52 | args.fUniformHandler->getUniformCStr(ellipseVar), |
53 | scaleVar.isValid() ? args.fUniformHandler->getUniformCStr(scaleVar) : "float2(0)" ); |
54 | fragBuilder->codeAppendf( |
55 | "a;\n@switch (%d) {\n case 0:\n alpha = approx_dist > 0.0 ? 0.0 : 1.0;\n " |
56 | " break;\n case 1:\n alpha = clamp(0.5 - half(approx_dist), 0.0, " |
57 | "1.0);\n break;\n case 2:\n alpha = approx_dist > 0.0 ? 1.0 : " |
58 | "0.0;\n break;\n case 3:\n alpha = clamp(0.5 + half(approx_dist), " |
59 | "0.0, 1.0);\n break;\n default:\n discard;\n}\n%s = %s * alpha;\n" , |
60 | (int)_outer.edgeType, args.fOutputColor, args.fInputColor); |
61 | } |
62 | |
63 | private: |
64 | void onSetData(const GrGLSLProgramDataManager& pdman, |
65 | const GrFragmentProcessor& _proc) override { |
66 | const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>(); |
67 | auto edgeType = _outer.edgeType; |
68 | (void)edgeType; |
69 | auto center = _outer.center; |
70 | (void)center; |
71 | auto radii = _outer.radii; |
72 | (void)radii; |
73 | UniformHandle& ellipse = ellipseVar; |
74 | (void)ellipse; |
75 | UniformHandle& scale = scaleVar; |
76 | (void)scale; |
77 | |
78 | if (radii != prevRadii || center != prevCenter) { |
79 | float invRXSqd; |
80 | float invRYSqd; |
81 | // If we're using a scale factor to work around precision issues, choose the larger |
82 | // radius as the scale factor. The inv radii need to be pre-adjusted by the scale |
83 | // factor. |
84 | if (scale.isValid()) { |
85 | if (radii.fX > radii.fY) { |
86 | invRXSqd = 1.f; |
87 | invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY); |
88 | pdman.set2f(scale, radii.fX, 1.f / radii.fX); |
89 | } else { |
90 | invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX); |
91 | invRYSqd = 1.f; |
92 | pdman.set2f(scale, radii.fY, 1.f / radii.fY); |
93 | } |
94 | } else { |
95 | invRXSqd = 1.f / (radii.fX * radii.fX); |
96 | invRYSqd = 1.f / (radii.fY * radii.fY); |
97 | } |
98 | pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd); |
99 | prevCenter = center; |
100 | prevRadii = radii; |
101 | } |
102 | } |
103 | SkPoint prevCenter = float2(0); |
104 | SkPoint prevRadii = float2(0); |
105 | bool medPrecision = false; |
106 | UniformHandle ellipseVar; |
107 | UniformHandle scaleVar; |
108 | }; |
109 | GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const { |
110 | return new GrGLSLEllipseEffect(); |
111 | } |
112 | void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
113 | GrProcessorKeyBuilder* b) const { |
114 | b->add32((int32_t)edgeType); |
115 | } |
116 | bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const { |
117 | const GrEllipseEffect& that = other.cast<GrEllipseEffect>(); |
118 | (void)that; |
119 | if (edgeType != that.edgeType) return false; |
120 | if (center != that.center) return false; |
121 | if (radii != that.radii) return false; |
122 | return true; |
123 | } |
124 | GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src) |
125 | : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags()) |
126 | , edgeType(src.edgeType) |
127 | , center(src.center) |
128 | , radii(src.radii) {} |
129 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const { |
130 | return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(*this)); |
131 | } |
132 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect); |
133 | #if GR_TEST_UTILS |
134 | std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) { |
135 | SkPoint center; |
136 | center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
137 | center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f); |
138 | SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f); |
139 | SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f); |
140 | GrClipEdgeType et; |
141 | do { |
142 | et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
143 | } while (GrClipEdgeType::kHairlineAA == et); |
144 | return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry), |
145 | *testData->caps()->shaderCaps()); |
146 | } |
147 | #endif |
148 | |