blob: 133cf7b20dc273b17a6ed534612eb20b557bf025 [file] [log] [blame]
Michael Ludwig8f685082018-09-12 15:23:01 -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
8// Equivalent to SkTwoPointConicalGradient::Type
9enum class Type {
10 kRadial, kStrip, kFocal
11};
12
13in half4x4 gradientMatrix;
14
15layout(key) in Type type;
16layout(key) in bool isRadiusIncreasing;
17
18// Focal-specific optimizations
19layout(key) in bool isFocalOnCircle;
20layout(key) in bool isWellBehaved;
21layout(key) in bool isSwapped;
22layout(key) in bool isNativelyFocal;
23
24// focalParams is interpreted differently depending on if type is focal or degenerate when
25// degenerate, focalParams = (r0, r0^2), so strips will use .y and kRadial will use .x when focal,
26// focalParams = (1/r1, focalX = r0/(r0-r1)) The correct parameters are calculated once in Make for
27// each FP
28layout(tracked) in uniform half2 focalParams;
29
30@coordTransform {
31 gradientMatrix
32}
33
34void main() {
35 half2 p = sk_TransformedCoords2D[0];
36 half t = -1;
37 half v = 1; // validation flag, set to negative to discard fragment later
38
39 @switch(type) {
40 case Type::kStrip: {
41 half r0_2 = focalParams.y;
42 t = r0_2 - p.y * p.y;
43 if (t >= 0) {
44 t = p.x + sqrt(t);
45 } else {
46 v = -1;
47 }
48 }
49 break;
50 case Type::kRadial: {
51 half r0 = focalParams.x;
52 @if(isRadiusIncreasing) {
53 t = length(p) - r0;
54 } else {
55 t = -length(p) - r0;
56 }
57 }
58 break;
59 case Type::kFocal: {
60 half invR1 = focalParams.x;
61 half fx = focalParams.y;
62
63 half x_t = -1;
64 @if (isFocalOnCircle) {
65 x_t = dot(p, p) / p.x;
66 } else if (isWellBehaved) {
67 x_t = length(p) - p.x * invR1;
68 } else {
69 half temp = p.x * p.x - p.y * p.y;
70
71 // Only do sqrt if temp >= 0; this is significantly slower than checking temp >= 0
72 // in the if statement that checks r(t) >= 0. But GPU may break if we sqrt a
73 // negative float. (Although I havevn't observed that on any devices so far, and the
74 // old approach also does sqrt negative value without a check.) If the performance
75 // is really critical, maybe we should just compute the area where temp and x_t are
76 // always valid and drop all these ifs.
77 if (temp >= 0) {
78 @if(isSwapped || !isRadiusIncreasing) {
79 x_t = -sqrt(temp) - p.x * invR1;
80 } else {
81 x_t = sqrt(temp) - p.x * invR1;
82 }
83 }
84 }
85
86 // The final calculation of t from x_t has lots of static optimizations but only do them
87 // when x_t is positive (which can be assumed true if isWellBehaved is true)
88 @if (!isWellBehaved) {
89 // This will still calculate t even though it will be ignored later in the pipeline
90 // to avoid a branch
91 if (x_t <= 0.0) {
92 v = -1;
93 }
94 }
95 @if (isRadiusIncreasing) {
96 @if (isNativelyFocal) {
97 t = x_t;
98 } else {
99 t = x_t + fx;
100 }
101 } else {
102 @if (isNativelyFocal) {
103 t = -x_t;
104 } else {
105 t = -x_t + fx;
106 }
107 }
108
109 @if(isSwapped) {
110 t = 1 - t;
111 }
112 }
113 break;
114 }
115
116 sk_OutColor = half4(t, v, 0, 0);
117}
118
119//////////////////////////////////////////////////////////////////////////////
120
121@header {
122 #include "SkTwoPointConicalGradient.h"
123}
124
125@make {
126 static std::unique_ptr<GrFragmentProcessor> Make(const SkTwoPointConicalGradient& gradient,
127 const GrFPArgs& args);
128}
129
130@cppEnd {
131 // .fp files do not let you reference outside enum definitions, so we have to explicitly map
132 // between the two compatible enum defs
133 GrTwoPointConicalGradientLayout::Type convert_type(
134 SkTwoPointConicalGradient::Type type) {
135 switch(type) {
136 case SkTwoPointConicalGradient::Type::kRadial:
137 return GrTwoPointConicalGradientLayout::Type::kRadial;
138 case SkTwoPointConicalGradient::Type::kStrip:
139 return GrTwoPointConicalGradientLayout::Type::kStrip;
140 case SkTwoPointConicalGradient::Type::kFocal:
141 return GrTwoPointConicalGradientLayout::Type::kFocal;
142 }
143 SkDEBUGFAIL("Should not be reachable");
144 return GrTwoPointConicalGradientLayout::Type::kRadial;
145 }
146
147 std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
148 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
149 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
150
151 // The focalData struct is only valid if isFocal is true
152 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
153 bool isFocal = grType == Type::kFocal;
154
155 // Calculate optimization switches from gradient specification
156 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
157 bool isWellBehaved = isFocal && focalData.isWellBehaved();
158 bool isSwapped = isFocal && focalData.isSwapped();
159 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
160
161 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
162 // However, all types start with the total inverse local matrix calculated from the shader
163 // and args
164 bool isRadiusIncreasing;
165 SkPoint focalParams; // really just a 2D tuple
166 SkMatrix matrix;
167
168 // Initialize the base matrix
169 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
170 return nullptr;
171 }
172
173 if (isFocal) {
174 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
175
176 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
177
178 matrix.postConcat(grad.getGradientMatrix());
179 } else if (grType == Type::kRadial) {
180 SkScalar dr = grad.getDiffRadius();
181 isRadiusIncreasing = dr >= 0;
182
183 SkScalar r0 = grad.getStartRadius() / dr;
184 focalParams.set(r0, r0 * r0);
185
186
187 // GPU radial matrix is different from the original matrix, since we map the diff radius
188 // to have |dr| = 1, so manually compute the final gradient matrix here.
189
190 // Map center to (0, 0)
191 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
192
193 // scale |diffRadius| to 1
194 matrix.postScale(1 / dr, 1 / dr);
195 } else { // kStrip
196 isRadiusIncreasing = false; // kStrip doesn't use this flag
197
198 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
199 focalParams.set(r0, r0 * r0);
200
201
202 matrix.postConcat(grad.getGradientMatrix());
203 }
204
205
206 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
207 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved,
208 isSwapped, isNativelyFocal, focalParams));
209 }
210}