blob: eff712f6ff94338e7e230d306ebdb78f3011a9eb [file] [log] [blame]
Michael Ludwig24d438b2018-09-12 15:22:50 -04001/*
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
8in half4x4 gradientMatrix;
9
10layout(tracked) in uniform half bias;
11layout(tracked) in uniform half scale;
12
13@coordTransform {
14 gradientMatrix
15}
16
17void main() {
18 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
19 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
20 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
21 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
22 // using atan instead.
23 half angle;
24 if (sk_Caps.atan2ImplementedAsAtanYOverX) {
25 angle = 2 * atan(-sk_TransformedCoords2D[0].y,
26 length(sk_TransformedCoords2D[0]) - sk_TransformedCoords2D[0].x);
27 } else {
28 angle = atan(-sk_TransformedCoords2D[0].y, -sk_TransformedCoords2D[0].x);
29 }
30
31 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
Michael Ludwig8f685082018-09-12 15:23:01 -040032 half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;
33 sk_OutColor = half4(t, 1, 0, 0); // y = 1 for always valid
Michael Ludwig24d438b2018-09-12 15:22:50 -040034}
35
36//////////////////////////////////////////////////////////////////////////////
37
38@header {
39 #include "SkSweepGradient.h"
40}
41
42@make {
43 static std::unique_ptr<GrFragmentProcessor> Make(const SkSweepGradient& gradient,
44 const GrFPArgs& args);
45}
46
47@cppEnd {
48 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(
49 const SkSweepGradient& grad, const GrFPArgs& args) {
50 SkMatrix matrix;
51 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
52 return nullptr;
53 }
54 matrix.postConcat(grad.getGradientMatrix());
55 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(
56 matrix, grad.getTBias(), grad.getTScale()));
57 }
58}