blob: 3aebd3b5923d956178e26b7af613a22fd22b86ea [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
Michael Ludwigb96cba32018-09-14 13:59:24 -0400125// The 2 point conical gradient can reject a pixel so it does change opacity
126// even if the input was opaque, so disable that optimization
127@optimizationFlags {
128 kNone_OptimizationFlags
129}
130
Michael Ludwig8f685082018-09-12 15:23:01 -0400131@make {
132 static std::unique_ptr<GrFragmentProcessor> Make(const SkTwoPointConicalGradient& gradient,
133 const GrFPArgs& args);
134}
135
136@cppEnd {
137 // .fp files do not let you reference outside enum definitions, so we have to explicitly map
138 // between the two compatible enum defs
139 GrTwoPointConicalGradientLayout::Type convert_type(
140 SkTwoPointConicalGradient::Type type) {
141 switch(type) {
142 case SkTwoPointConicalGradient::Type::kRadial:
143 return GrTwoPointConicalGradientLayout::Type::kRadial;
144 case SkTwoPointConicalGradient::Type::kStrip:
145 return GrTwoPointConicalGradientLayout::Type::kStrip;
146 case SkTwoPointConicalGradient::Type::kFocal:
147 return GrTwoPointConicalGradientLayout::Type::kFocal;
148 }
149 SkDEBUGFAIL("Should not be reachable");
150 return GrTwoPointConicalGradientLayout::Type::kRadial;
151 }
152
153 std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
154 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
155 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
156
157 // The focalData struct is only valid if isFocal is true
158 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
159 bool isFocal = grType == Type::kFocal;
160
161 // Calculate optimization switches from gradient specification
162 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
163 bool isWellBehaved = isFocal && focalData.isWellBehaved();
164 bool isSwapped = isFocal && focalData.isSwapped();
165 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
166
167 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
168 // However, all types start with the total inverse local matrix calculated from the shader
169 // and args
170 bool isRadiusIncreasing;
171 SkPoint focalParams; // really just a 2D tuple
172 SkMatrix matrix;
173
174 // Initialize the base matrix
175 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
176 return nullptr;
177 }
178
179 if (isFocal) {
180 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
181
182 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
183
184 matrix.postConcat(grad.getGradientMatrix());
185 } else if (grType == Type::kRadial) {
186 SkScalar dr = grad.getDiffRadius();
187 isRadiusIncreasing = dr >= 0;
188
189 SkScalar r0 = grad.getStartRadius() / dr;
190 focalParams.set(r0, r0 * r0);
191
192
193 // GPU radial matrix is different from the original matrix, since we map the diff radius
194 // to have |dr| = 1, so manually compute the final gradient matrix here.
195
196 // Map center to (0, 0)
197 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
198
199 // scale |diffRadius| to 1
200 matrix.postScale(1 / dr, 1 / dr);
201 } else { // kStrip
202 isRadiusIncreasing = false; // kStrip doesn't use this flag
203
204 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
205 focalParams.set(r0, r0 * r0);
206
207
208 matrix.postConcat(grad.getGradientMatrix());
209 }
210
211
212 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
213 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved,
214 isSwapped, isNativelyFocal, focalParams));
215 }
216}