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 "src/core/SkUtils.h" |
14 | #include "src/gpu/GrTexture.h" |
15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
18 | #include "src/sksl/SkSLCPP.h" |
19 | #include "src/sksl/SkSLUtil.h" |
20 | class GrGLSLSweepGradientLayout : public GrGLSLFragmentProcessor { |
21 | public: |
22 | GrGLSLSweepGradientLayout() {} |
23 | void emitCode(EmitArgs& args) override { |
24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
25 | const GrSweepGradientLayout& _outer = args.fFp.cast<GrSweepGradientLayout>(); |
26 | (void)_outer; |
27 | auto bias = _outer.bias; |
28 | (void)bias; |
29 | auto scale = _outer.scale; |
30 | (void)scale; |
31 | biasVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, kHalf_GrSLType, |
32 | "bias" ); |
33 | scaleVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, kHalf_GrSLType, |
34 | "scale" ); |
35 | fragBuilder->codeAppendf( |
36 | R"SkSL(half angle; |
37 | if (sk_Caps.atan2ImplementedAsAtanYOverX) { |
38 | angle = half(2.0 * atan(-%s.y, length(%s) - %s.x)); |
39 | } else { |
40 | angle = half(atan(-%s.y, -%s.x)); |
41 | } |
42 | half t = ((angle * 0.15915493667125702 + 0.5) + %s) * %s; |
43 | %s = half4(t, 1.0, 0.0, 0.0); |
44 | )SkSL" , |
45 | args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, |
46 | args.fSampleCoord, args.fUniformHandler->getUniformCStr(biasVar), |
47 | args.fUniformHandler->getUniformCStr(scaleVar), args.fOutputColor); |
48 | } |
49 | |
50 | private: |
51 | void onSetData(const GrGLSLProgramDataManager& pdman, |
52 | const GrFragmentProcessor& _proc) override { |
53 | const GrSweepGradientLayout& _outer = _proc.cast<GrSweepGradientLayout>(); |
54 | { |
55 | float biasValue = _outer.bias; |
56 | if (biasPrev != biasValue) { |
57 | biasPrev = biasValue; |
58 | pdman.set1f(biasVar, biasValue); |
59 | } |
60 | float scaleValue = _outer.scale; |
61 | if (scalePrev != scaleValue) { |
62 | scalePrev = scaleValue; |
63 | pdman.set1f(scaleVar, scaleValue); |
64 | } |
65 | } |
66 | } |
67 | float biasPrev = SK_FloatNaN; |
68 | float scalePrev = SK_FloatNaN; |
69 | UniformHandle biasVar; |
70 | UniformHandle scaleVar; |
71 | }; |
72 | GrGLSLFragmentProcessor* GrSweepGradientLayout::onCreateGLSLInstance() const { |
73 | return new GrGLSLSweepGradientLayout(); |
74 | } |
75 | void GrSweepGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
76 | GrProcessorKeyBuilder* b) const {} |
77 | bool GrSweepGradientLayout::onIsEqual(const GrFragmentProcessor& other) const { |
78 | const GrSweepGradientLayout& that = other.cast<GrSweepGradientLayout>(); |
79 | (void)that; |
80 | if (bias != that.bias) return false; |
81 | if (scale != that.scale) return false; |
82 | return true; |
83 | } |
84 | GrSweepGradientLayout::GrSweepGradientLayout(const GrSweepGradientLayout& src) |
85 | : INHERITED(kGrSweepGradientLayout_ClassID, src.optimizationFlags()) |
86 | , bias(src.bias) |
87 | , scale(src.scale) { |
88 | this->cloneAndRegisterAllChildProcessors(src); |
89 | this->setUsesSampleCoordsDirectly(); |
90 | } |
91 | std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::clone() const { |
92 | return std::make_unique<GrSweepGradientLayout>(*this); |
93 | } |
94 | #if GR_TEST_UTILS |
95 | SkString GrSweepGradientLayout::onDumpInfo() const { |
96 | return SkStringPrintf("(bias=%f, scale=%f)" , bias, scale); |
97 | } |
98 | #endif |
99 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradientLayout); |
100 | #if GR_TEST_UTILS |
101 | std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::TestCreate(GrProcessorTestData* d) { |
102 | SkScalar scale = GrGradientShader::RandomParams::kGradientScale; |
103 | SkPoint center = {d->fRandom->nextRangeScalar(0.0f, scale), |
104 | d->fRandom->nextRangeScalar(0.0f, scale)}; |
105 | |
106 | GrGradientShader::RandomParams params(d->fRandom); |
107 | auto shader = params.fUseColors4f |
108 | ? SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f, |
109 | params.fColorSpace, params.fStops, |
110 | params.fColorCount) |
111 | : SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors, |
112 | params.fStops, params.fColorCount); |
113 | GrTest::TestAsFPArgs asFPArgs(d); |
114 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
115 | SkASSERT_RELEASE(fp); |
116 | return fp; |
117 | } |
118 | #endif |
119 | |
120 | std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(const SkSweepGradient& grad, |
121 | const GrFPArgs& args) { |
122 | SkMatrix matrix; |
123 | if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) { |
124 | return nullptr; |
125 | } |
126 | matrix.postConcat(grad.getGradientMatrix()); |
127 | return GrMatrixEffect::Make( |
128 | matrix, std::unique_ptr<GrFragmentProcessor>( |
129 | new GrSweepGradientLayout(grad.getTBias(), grad.getTScale()))); |
130 | } |
131 | |