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 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLProgramBuilder.h"
15 #include "GrTexture.h"
16 #include "SkSLCPP.h"
17 #include "SkSLUtil.h"
18 class GrGLSLEllipseEffect : public GrGLSLFragmentProcessor {
19 public:
GrGLSLEllipseEffect()20 GrGLSLEllipseEffect() {}
emitCode(EmitArgs & args)21 void emitCode(EmitArgs& args) override {
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23 const GrEllipseEffect& _outer = args.fFp.cast<GrEllipseEffect>();
24 (void)_outer;
25 auto edgeType = _outer.edgeType();
26 (void)edgeType;
27 auto center = _outer.center();
28 (void)center;
29 auto radii = _outer.radii();
30 (void)radii;
31 prevRadii = float2(-1.0);
32 useScale = !sk_Caps.floatIs32Bits;
33 fEllipseVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
34 kDefault_GrSLPrecision, "ellipse");
35 if (useScale) {
36 fScaleVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat2_GrSLType,
37 kDefault_GrSLPrecision, "scale");
38 }
39 fragBuilder->codeAppendf(
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 ",
47 prevRadii.fX, prevRadii.fY, (useScale ? "true" : "false"),
48 args.fUniformHandler->getUniformCStr(fEllipseVar),
49 fScaleVar.isValid() ? args.fUniformHandler->getUniformCStr(fScaleVar) : "float2(0)",
50 args.fUniformHandler->getUniformCStr(fEllipseVar),
51 fScaleVar.isValid() ? args.fUniformHandler->getUniformCStr(fScaleVar) : "float2(0)",
52 (int)_outer.edgeType());
53 fragBuilder->codeAppendf(
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",
58 args.fOutputColor, args.fInputColor);
59 }
60
61 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)62 void onSetData(const GrGLSLProgramDataManager& pdman,
63 const GrFragmentProcessor& _proc) override {
64 const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>();
65 auto edgeType = _outer.edgeType();
66 (void)edgeType;
67 auto center = _outer.center();
68 (void)center;
69 auto radii = _outer.radii();
70 (void)radii;
71 UniformHandle& ellipse = fEllipseVar;
72 (void)ellipse;
73 UniformHandle& scale = fScaleVar;
74 (void)scale;
75
76 if (radii != prevRadii || center != prevCenter) {
77 float invRXSqd;
78 float invRYSqd;
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.
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 }
92 } else {
93 invRXSqd = 1.f / (radii.fX * radii.fX);
94 invRYSqd = 1.f / (radii.fY * radii.fY);
95 }
96 pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd);
97 prevCenter = center;
98 prevRadii = radii;
99 }
100 }
101 SkPoint prevCenter = float2(0);
102 SkPoint prevRadii = float2(0);
103 bool useScale = false;
104 UniformHandle fEllipseVar;
105 UniformHandle fScaleVar;
106 };
onCreateGLSLInstance() const107 GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const {
108 return new GrGLSLEllipseEffect();
109 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const110 void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
111 GrProcessorKeyBuilder* b) const {
112 b->add32((int32_t)fEdgeType);
113 }
onIsEqual(const GrFragmentProcessor & other) const114 bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const {
115 const GrEllipseEffect& that = other.cast<GrEllipseEffect>();
116 (void)that;
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 }
GrEllipseEffect(const GrEllipseEffect & src)122 GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src)
123 : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags())
124 , fEdgeType(src.fEdgeType)
125 , fCenter(src.fCenter)
126 , fRadii(src.fRadii) {}
clone() const127 std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const {
128 return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(*this));
129 }
130 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect);
131 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * testData)132 std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) {
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);
138 GrClipEdgeType et;
139 do {
140 et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
141 } while (GrClipEdgeType::kHairlineAA == et);
142 return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry),
143 *testData->caps()->shaderCaps());
144 }
145 #endif
146