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 medPrecision = !sk_Caps.floatIs32Bits;
33 fEllipseVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
34 "ellipse");
35 if (medPrecision) {
36 fScaleVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat2_GrSLType,
37 "scale");
38 }
39 fragBuilder->codeAppendf(
40 "float2 prevCenter;\nfloat2 prevRadii = float2(%f, %f);\nbool medPrecision = "
41 "%s;\nfloat2 d = sk_FragCoord.xy - %s.xy;\n@if (medPrecision) {\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);\n@if (medPrecision) {\n grad_dot = max(grad_dot, "
44 "6.1036000000000003e-05);\n} else {\n grad_dot = max(grad_dot, "
45 "1.1755e-38);\n}\nfloat approx_dist = implicit * inversesqrt(grad_dot);\n@if "
46 "(medPrecision) {\n approx_dist *= %s.x;\n}\nhalf alpha;\n@switch ",
47 prevRadii.fX, prevRadii.fY, (medPrecision ? "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)
52 : "float2(0)");
53 fragBuilder->codeAppendf(
54 "(%d) {\n case 0:\n alpha = approx_dist > 0.0 ? 0.0 : 1.0;\n "
55 "break;\n case 1:\n alpha = clamp(0.5 - half(approx_dist), 0.0, 1.0);\n "
56 " break;\n case 2:\n alpha = approx_dist > 0.0 ? 1.0 : 0.0;\n "
57 " break;\n case 3:\n alpha = clamp(0.5 + half(approx_dist), 0.0, 1.0);\n "
58 " break;\n default:\n discard;\n}\n%s = %s * alpha;\n",
59 (int)_outer.edgeType(), args.fOutputColor, args.fInputColor);
60 }
61
62 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)63 void onSetData(const GrGLSLProgramDataManager& pdman,
64 const GrFragmentProcessor& _proc) override {
65 const GrEllipseEffect& _outer = _proc.cast<GrEllipseEffect>();
66 auto edgeType = _outer.edgeType();
67 (void)edgeType;
68 auto center = _outer.center();
69 (void)center;
70 auto radii = _outer.radii();
71 (void)radii;
72 UniformHandle& ellipse = fEllipseVar;
73 (void)ellipse;
74 UniformHandle& scale = fScaleVar;
75 (void)scale;
76
77 if (radii != prevRadii || center != prevCenter) {
78 float invRXSqd;
79 float invRYSqd;
80 // If we're using a scale factor to work around precision issues, choose the larger
81 // radius as the scale factor. The inv radii need to be pre-adjusted by the scale
82 // factor.
83 if (scale.isValid()) {
84 if (radii.fX > radii.fY) {
85 invRXSqd = 1.f;
86 invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY);
87 pdman.set2f(scale, radii.fX, 1.f / radii.fX);
88 } else {
89 invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX);
90 invRYSqd = 1.f;
91 pdman.set2f(scale, radii.fY, 1.f / radii.fY);
92 }
93 } else {
94 invRXSqd = 1.f / (radii.fX * radii.fX);
95 invRYSqd = 1.f / (radii.fY * radii.fY);
96 }
97 pdman.set4f(ellipse, center.fX, center.fY, invRXSqd, invRYSqd);
98 prevCenter = center;
99 prevRadii = radii;
100 }
101 }
102 SkPoint prevCenter = float2(0);
103 SkPoint prevRadii = float2(0);
104 bool medPrecision = false;
105 UniformHandle fEllipseVar;
106 UniformHandle fScaleVar;
107 };
onCreateGLSLInstance() const108 GrGLSLFragmentProcessor* GrEllipseEffect::onCreateGLSLInstance() const {
109 return new GrGLSLEllipseEffect();
110 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const111 void GrEllipseEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
112 GrProcessorKeyBuilder* b) const {
113 b->add32((int32_t)fEdgeType);
114 }
onIsEqual(const GrFragmentProcessor & other) const115 bool GrEllipseEffect::onIsEqual(const GrFragmentProcessor& other) const {
116 const GrEllipseEffect& that = other.cast<GrEllipseEffect>();
117 (void)that;
118 if (fEdgeType != that.fEdgeType) return false;
119 if (fCenter != that.fCenter) return false;
120 if (fRadii != that.fRadii) return false;
121 return true;
122 }
GrEllipseEffect(const GrEllipseEffect & src)123 GrEllipseEffect::GrEllipseEffect(const GrEllipseEffect& src)
124 : INHERITED(kGrEllipseEffect_ClassID, src.optimizationFlags())
125 , fEdgeType(src.fEdgeType)
126 , fCenter(src.fCenter)
127 , fRadii(src.fRadii) {}
clone() const128 std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::clone() const {
129 return std::unique_ptr<GrFragmentProcessor>(new GrEllipseEffect(*this));
130 }
131 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrEllipseEffect);
132 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * testData)133 std::unique_ptr<GrFragmentProcessor> GrEllipseEffect::TestCreate(GrProcessorTestData* testData) {
134 SkPoint center;
135 center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
136 center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
137 SkScalar rx = testData->fRandom->nextRangeF(0.f, 1000.f);
138 SkScalar ry = testData->fRandom->nextRangeF(0.f, 1000.f);
139 GrClipEdgeType et;
140 do {
141 et = (GrClipEdgeType)testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
142 } while (GrClipEdgeType::kHairlineAA == et);
143 return GrEllipseEffect::Make(et, center, SkPoint::Make(rx, ry),
144 *testData->caps()->shaderCaps());
145 }
146 #endif
147