1 /*
2 * Copyright 2018 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 GrSweepGradientLayout.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrSweepGradientLayout.h"
12
13 #include "include/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 GrGLSLSweepGradientLayout : public GrGLSLFragmentProcessor {
20 public:
GrGLSLSweepGradientLayout()21 GrGLSLSweepGradientLayout() {}
emitCode(EmitArgs & args)22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrSweepGradientLayout& _outer = args.fFp.cast<GrSweepGradientLayout>();
25 (void)_outer;
26 auto gradientMatrix = _outer.gradientMatrix;
27 (void)gradientMatrix;
28 auto bias = _outer.bias;
29 (void)bias;
30 auto scale = _outer.scale;
31 (void)scale;
32 biasVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "bias");
33 scaleVar =
34 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "scale");
35 SkString sk_TransformedCoords2D_0 =
36 fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);
37 fragBuilder->codeAppendf(
38 "half angle;\nif (sk_Caps.atan2ImplementedAsAtanYOverX) {\n angle = half(2.0 * "
39 "atan(-%s.y, length(%s) - %s.x));\n} else {\n angle = half(atan(-%s.y, "
40 "-%s.x));\n}\nhalf t = ((angle * 0.15915493667125702 + 0.5) + %s) * %s;\n%s = "
41 "half4(t, 1.0, 0.0, 0.0);\n",
42 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
43 : "_coords",
44 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
45 : "_coords",
46 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
47 : "_coords",
48 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
49 : "_coords",
50 _outer.computeLocalCoordsInVertexShader() ? sk_TransformedCoords2D_0.c_str()
51 : "_coords",
52 args.fUniformHandler->getUniformCStr(biasVar),
53 args.fUniformHandler->getUniformCStr(scaleVar), args.fOutputColor);
54 }
55
56 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)57 void onSetData(const GrGLSLProgramDataManager& pdman,
58 const GrFragmentProcessor& _proc) override {
59 const GrSweepGradientLayout& _outer = _proc.cast<GrSweepGradientLayout>();
60 {
61 float biasValue = _outer.bias;
62 if (biasPrev != biasValue) {
63 biasPrev = biasValue;
64 pdman.set1f(biasVar, biasValue);
65 }
66 float scaleValue = _outer.scale;
67 if (scalePrev != scaleValue) {
68 scalePrev = scaleValue;
69 pdman.set1f(scaleVar, scaleValue);
70 }
71 }
72 }
73 float biasPrev = SK_FloatNaN;
74 float scalePrev = SK_FloatNaN;
75 UniformHandle biasVar;
76 UniformHandle scaleVar;
77 };
onCreateGLSLInstance() const78 GrGLSLFragmentProcessor* GrSweepGradientLayout::onCreateGLSLInstance() const {
79 return new GrGLSLSweepGradientLayout();
80 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const81 void GrSweepGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
82 GrProcessorKeyBuilder* b) const {}
onIsEqual(const GrFragmentProcessor & other) const83 bool GrSweepGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
84 const GrSweepGradientLayout& that = other.cast<GrSweepGradientLayout>();
85 (void)that;
86 if (gradientMatrix != that.gradientMatrix) return false;
87 if (bias != that.bias) return false;
88 if (scale != that.scale) return false;
89 return true;
90 }
GrSweepGradientLayout(const GrSweepGradientLayout & src)91 GrSweepGradientLayout::GrSweepGradientLayout(const GrSweepGradientLayout& src)
92 : INHERITED(kGrSweepGradientLayout_ClassID, src.optimizationFlags())
93 , fCoordTransform0(src.fCoordTransform0)
94 , gradientMatrix(src.gradientMatrix)
95 , bias(src.bias)
96 , scale(src.scale) {
97 this->addCoordTransform(&fCoordTransform0);
98 }
clone() const99 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::clone() const {
100 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(*this));
101 }
102 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradientLayout);
103 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)104 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::TestCreate(GrProcessorTestData* d) {
105 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
106 SkPoint center = {d->fRandom->nextRangeScalar(0.0f, scale),
107 d->fRandom->nextRangeScalar(0.0f, scale)};
108
109 GrGradientShader::RandomParams params(d->fRandom);
110 auto shader = params.fUseColors4f
111 ? SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f,
112 params.fColorSpace, params.fStops,
113 params.fColorCount)
114 : SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors,
115 params.fStops, params.fColorCount);
116 GrTest::TestAsFPArgs asFPArgs(d);
117 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
118 GrAlwaysAssert(fp);
119 return fp;
120 }
121 #endif
122
Make(const SkSweepGradient & grad,const GrFPArgs & args)123 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(const SkSweepGradient& grad,
124 const GrFPArgs& args) {
125 SkMatrix matrix;
126 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
127 return nullptr;
128 }
129 matrix.postConcat(grad.getGradientMatrix());
130 return std::unique_ptr<GrFragmentProcessor>(
131 new GrSweepGradientLayout(matrix, grad.getTBias(), grad.getTScale()));
132 }
133