| 1 |
// Graphite-specific vertex shader code |
| 2 |
|
| 3 |
const float $PI = 3.141592653589793238; |
| 4 |
|
| 5 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 6 |
// Support functions for tessellating path renderers |
| 7 |
|
| 8 |
const float $kCubicCurveType = 0; // skgpu::tess::kCubicCurveType |
| 9 |
const float $kConicCurveType = 1; // skgpu::tess::kConicCurveType |
| 10 |
const float $kTriangularConicCurveType = 2; // skgpu::tess::kTriangularConicCurveType |
| 11 |
|
| 12 |
// This function can be used on GPUs with infinity support to infer the curve type from the specific |
| 13 |
// path control-point encoding used by tessellating path renderers. Calling this function on a |
| 14 |
// platform that lacks infinity support may result in a shader compilation error. |
| 15 |
$pure float curve_type_using_inf_support(float4 p23) { |
| 16 |
return isinf(p23.z) ? $kTriangularConicCurveType : |
| 17 |
isinf(p23.w) ? $kConicCurveType : |
| 18 |
$kCubicCurveType; |
| 19 |
} |
| 20 |
|
| 21 |
$pure bool $is_conic_curve(float curveType) { |
| 22 |
return curveType != $kCubicCurveType; |
| 23 |
} |
| 24 |
|
| 25 |
$pure bool $is_triangular_conic_curve(float curveType) { |
| 26 |
return curveType == $kTriangularConicCurveType; |
| 27 |
} |
| 28 |
|
| 29 |
// Wang's formula gives the minimum number of evenly spaced (in the parametric sense) line segments |
| 30 |
// that a bezier curve must be chopped into in order to guarantee all lines stay within a distance |
| 31 |
// of "1/precision" pixels from the true curve. Its definition for a bezier curve of degree "n" is |
| 32 |
// as follows: |
| 33 |
// |
| 34 |
// maxLength = max([length(p[i+2] - 2p[i+1] + p[i]) for (0 <= i <= n-2)]) |
| 35 |
// numParametricSegments = sqrt(maxLength * precision * n*(n - 1)/8) |
| 36 |
// |
| 37 |
// (Goldman, Ron. (2003). 5.6.3 Wang's Formula. "Pyramid Algorithms: A Dynamic Programming Approach |
| 38 |
// to Curves and Surfaces for Geometric Modeling". Morgan Kaufmann Publishers.) |
| 39 |
|
| 40 |
const float $kDegree = 3; |
| 41 |
const float $kPrecision = 4; // Must match skgpu::tess::kPrecision |
| 42 |
const float $kLengthTerm = ($kDegree * ($kDegree - 1) / 8.0) * $kPrecision; |
| 43 |
const float $kLengthTermPow2 = (($kDegree * $kDegree) * (($kDegree - 1) * ($kDegree - 1)) / 64.0) * |
| 44 |
($kPrecision * $kPrecision); |
| 45 |
|
| 46 |
// Returns the length squared of the largest forward difference from Wang's cubic formula. |
| 47 |
$pure float $wangs_formula_max_fdiff_p2(float2 p0, float2 p1, float2 p2, float2 p3, |
| 48 |
float2x2 matrix) { |
| 49 |
float2 d0 = matrix * (fma(float2(-2), p1, p2) + p0); |
| 50 |
float2 d1 = matrix * (fma(float2(-2), p2, p3) + p1); |
| 51 |
return max(dot(d0,d0), dot(d1,d1)); |
| 52 |
} |
| 53 |
|
| 54 |
$pure float $wangs_formula_cubic(float2 p0, float2 p1, float2 p2, float2 p3, |
| 55 |
float2x2 matrix) { |
| 56 |
float m = $wangs_formula_max_fdiff_p2(p0, p1, p2, p3, matrix); |
| 57 |
return max(ceil(sqrt($kLengthTerm * sqrt(m))), 1.0); |
| 58 |
} |
| 59 |
|
| 60 |
$pure float $wangs_formula_cubic_log2(float2 p0, float2 p1, float2 p2, float2 p3, |
| 61 |
float2x2 matrix) { |
| 62 |
float m = $wangs_formula_max_fdiff_p2(p0, p1, p2, p3, matrix); |
| 63 |
return ceil(log2(max($kLengthTermPow2 * m, 1.0)) * .25); |
| 64 |
} |
| 65 |
|
| 66 |
$pure float $wangs_formula_conic_p2(float2 p0, float2 p1, float2 p2, float w) { |
| 67 |
// Translate the bounding box center to the origin. |
| 68 |
float2 C = (min(min(p0, p1), p2) + max(max(p0, p1), p2)) * 0.5; |
| 69 |
p0 -= C; |
| 70 |
p1 -= C; |
| 71 |
p2 -= C; |
| 72 |
|
| 73 |
// Compute max length. |
| 74 |
float m = sqrt(max(max(dot(p0,p0), dot(p1,p1)), dot(p2,p2))); |
| 75 |
|
| 76 |
// Compute forward differences. |
| 77 |
float2 dp = fma(float2(-2.0 * w), p1, p0) + p2; |
| 78 |
float dw = abs(fma(-2.0, w, 2.0)); |
| 79 |
|
| 80 |
// Compute numerator and denominator for parametric step size of linearization. Here, the |
| 81 |
// epsilon referenced from the cited paper is 1/precision. |
| 82 |
float rp_minus_1 = max(0.0, fma(m, $kPrecision, -1.0)); |
| 83 |
float numer = length(dp) * $kPrecision + rp_minus_1 * dw; |
| 84 |
float denom = 4 * min(w, 1.0); |
| 85 |
|
| 86 |
return numer/denom; |
| 87 |
} |
| 88 |
|
| 89 |
$pure float $wangs_formula_conic(float2 p0, float2 p1, float2 p2, float w) { |
| 90 |
float n2 = $wangs_formula_conic_p2(p0, p1, p2, w); |
| 91 |
return max(ceil(sqrt(n2)), 1.0); |
| 92 |
} |
| 93 |
|
| 94 |
$pure float $wangs_formula_conic_log2(float2 p0, float2 p1, float2 p2, float w) { |
| 95 |
float n2 = $wangs_formula_conic_p2(p0, p1, p2, w); |
| 96 |
return ceil(log2(max(n2, 1.0)) * .5); |
| 97 |
} |
| 98 |
|
| 99 |
// Returns the normalized difference between a and b, i.e. normalize(a - b), with care taken for |
| 100 |
// if 'a' and/or 'b' have large coordinates. |
| 101 |
$pure float2 $robust_normalize_diff(float2 a, float2 b) { |
| 102 |
float2 diff = a - b; |
| 103 |
if (diff == float2(0.0)) { |
| 104 |
return float2(0.0); |
| 105 |
} else { |
| 106 |
float invMag = 1.0 / max(abs(diff.x), abs(diff.y)); |
| 107 |
return normalize(invMag * diff); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Returns the cosine of the angle between a and b, assuming a and b are unit vectors already. |
| 112 |
// Guaranteed to be between [-1, 1]. |
| 113 |
$pure float $cosine_between_unit_vectors(float2 a, float2 b) { |
| 114 |
// Since a and b are assumed to be normalized, the cosine is equal to the dot product, although |
| 115 |
// we clamp that to ensure it falls within the expected range of [-1, 1]. |
| 116 |
return clamp(dot(a, b), -1.0, 1.0); |
| 117 |
} |
| 118 |
|
| 119 |
// Extends the middle radius to either the miter point, or the bevel edge if we surpassed the |
| 120 |
// miter limit and need to revert to a bevel join. |
| 121 |
$pure float $miter_extent(float cosTheta, float miterLimit) { |
| 122 |
float x = fma(cosTheta, .5, .5); |
| 123 |
return (x * miterLimit * miterLimit >= 1.0) ? inversesqrt(x) : sqrt(x); |
| 124 |
} |
| 125 |
|
| 126 |
// Returns the number of radial segments required for each radian of rotation, in order for the |
| 127 |
// curve to appear "smooth" as defined by the approximate device-space stroke radius. |
| 128 |
$pure float $num_radial_segments_per_radian(float approxDevStrokeRadius) { |
| 129 |
return .5 / acos(max(1.0 - (1.0 / $kPrecision) / approxDevStrokeRadius, -1.0)); |
| 130 |
} |
| 131 |
|
| 132 |
// Unlike mix(), this does not return b when t==1. But it otherwise seems to get better |
| 133 |
// precision than "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points. |
| 134 |
// We override this result anyway when t==1 so it shouldn't be a problem. |
| 135 |
$pure float $unchecked_mix(float a, float b, float T) { |
| 136 |
return fma(b - a, T, a); |
| 137 |
} |
| 138 |
$pure float2 $unchecked_mix(float2 a, float2 b, float T) { |
| 139 |
return fma(b - a, float2(T), a); |
| 140 |
} |
| 141 |
$pure float4 $unchecked_mix(float4 a, float4 b, float4 T) { |
| 142 |
return fma(b - a, T, a); |
| 143 |
} |
| 144 |
|
| 145 |
// Compute a vertex position for the curve described by p01 and p23 packed control points, |
| 146 |
// tessellated to the given resolve level, and assuming it will be drawn as a filled curve. |
| 147 |
$pure float2 tessellate_filled_curve(float2x2 vectorXform, |
| 148 |
float resolveLevel, float idxInResolveLevel, |
| 149 |
float4 p01, float4 p23, |
| 150 |
float curveType) { |
| 151 |
float2 localcoord; |
| 152 |
if ($is_triangular_conic_curve(curveType)) { |
| 153 |
// This patch is an exact triangle. |
| 154 |
localcoord = (resolveLevel != 0) ? p01.zw |
| 155 |
: (idxInResolveLevel != 0) ? p23.xy |
| 156 |
: p01.xy; |
| 157 |
} else { |
| 158 |
float2 p0=p01.xy, p1=p01.zw, p2=p23.xy, p3=p23.zw; |
| 159 |
float w = -1; // w < 0 tells us to treat the instance as an integral cubic. |
| 160 |
float maxResolveLevel; |
| 161 |
if ($is_conic_curve(curveType)) { |
| 162 |
// Conics are 3 points, with the weight in p3. |
| 163 |
w = p3.x; |
| 164 |
maxResolveLevel = $wangs_formula_conic_log2(vectorXform*p0, |
| 165 |
vectorXform*p1, |
| 166 |
vectorXform*p2, w); |
| 167 |
p1 *= w; // Unproject p1. |
| 168 |
p3 = p2; // Duplicate the endpoint for shared code that also runs on cubics. |
| 169 |
} else { |
| 170 |
// The patch is an integral cubic. |
| 171 |
maxResolveLevel = $wangs_formula_cubic_log2(p0, p1, p2, p3, vectorXform); |
| 172 |
} |
| 173 |
if (resolveLevel > maxResolveLevel) { |
| 174 |
// This vertex is at a higher resolve level than we need. Demote to a lower |
| 175 |
// resolveLevel, which will produce a degenerate triangle. |
| 176 |
idxInResolveLevel = floor(ldexp(idxInResolveLevel, |
| 177 |
int(maxResolveLevel - resolveLevel))); |
| 178 |
resolveLevel = maxResolveLevel; |
| 179 |
} |
| 180 |
// Promote our location to a discrete position in the maximum fixed resolve level. |
| 181 |
// This is extra paranoia to ensure we get the exact same fp32 coordinates for |
| 182 |
// colocated points from different resolve levels (e.g., the vertices T=3/4 and |
| 183 |
// T=6/8 should be exactly colocated). |
| 184 |
float fixedVertexID = floor(.5 + ldexp(idxInResolveLevel, int(5 - resolveLevel))); |
| 185 |
if (0 < fixedVertexID && fixedVertexID < 32) { |
| 186 |
float T = fixedVertexID * (1 / 32.0); |
| 187 |
|
| 188 |
// Evaluate at T. Use De Casteljau's for its accuracy and stability. |
| 189 |
float2 ab = mix(p0, p1, T); |
| 190 |
float2 bc = mix(p1, p2, T); |
| 191 |
float2 cd = mix(p2, p3, T); |
| 192 |
float2 abc = mix(ab, bc, T); |
| 193 |
float2 bcd = mix(bc, cd, T); |
| 194 |
float2 abcd = mix(abc, bcd, T); |
| 195 |
|
| 196 |
// Evaluate the conic weight at T. |
| 197 |
float u = mix(1.0, w, T); |
| 198 |
float v = w + 1 - u; // == mix(w, 1, T) |
| 199 |
float uv = mix(u, v, T); |
| 200 |
|
| 201 |
localcoord = (w < 0) ? /*cubic*/ abcd : /*conic*/ abc/uv; |
| 202 |
} else { |
| 203 |
localcoord = (fixedVertexID == 0) ? p0.xy : p3.xy; |
| 204 |
} |
| 205 |
} |
| 206 |
return localcoord; |
| 207 |
} |
| 208 |
|
| 209 |
// Device coords are in xy, local coords are in zw, since for now perspective isn't supported. |
| 210 |
$pure float4 tessellate_stroked_curve(float edgeID, float maxEdges, |
| 211 |
float2x2 affineMatrix, |
| 212 |
float2 translate, |
| 213 |
float maxScale /* derived from affineMatrix */, |
| 214 |
float4 p01, float4 p23, |
| 215 |
float2 lastControlPoint, |
| 216 |
float2 strokeParams, |
| 217 |
float curveType) { |
| 218 |
float2 p0=p01.xy, p1=p01.zw, p2=p23.xy, p3=p23.zw; |
| 219 |
float w = -1; // w<0 means the curve is an integral cubic. |
| 220 |
if ($is_conic_curve(curveType)) { |
| 221 |
// Conics are 3 points, with the weight in p3. |
| 222 |
w = p3.x; |
| 223 |
p3 = p2; // Setting p3 equal to p2 works for the remaining rotational logic. |
| 224 |
} |
| 225 |
|
| 226 |
// Call Wang's formula to determine parametric segments before transform points for hairlines |
| 227 |
// so that it is consistent with how the CPU tested the control points for chopping. |
| 228 |
float numParametricSegments; |
| 229 |
if (w < 0) { |
| 230 |
if (p0 == p1 && p2 == p3) { |
| 231 |
numParametricSegments = 1; // a line (or a circle if p1 == p2) |
| 232 |
} else { |
| 233 |
numParametricSegments = $wangs_formula_cubic(p0, p1, p2, p3, affineMatrix); |
| 234 |
} |
| 235 |
} else { |
| 236 |
if (p0 == p1 || p1 == p2) { |
| 237 |
numParametricSegments = 1; // a line (or a square if p0 == p2) |
| 238 |
} else { |
| 239 |
numParametricSegments = $wangs_formula_conic(affineMatrix * p0, |
| 240 |
affineMatrix * p1, |
| 241 |
affineMatrix * p2, w); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Matches skgpu::tess::StrokeParams |
| 246 |
float strokeRadius = strokeParams.x; |
| 247 |
float joinType = strokeParams.y; // <0 = round join, ==0 = bevel join, >0 encodes miter limit |
| 248 |
bool isHairline = strokeParams.x == 0.0; |
| 249 |
float numRadialSegmentsPerRadian; |
| 250 |
if (isHairline) { |
| 251 |
numRadialSegmentsPerRadian = $num_radial_segments_per_radian(0.5); |
| 252 |
strokeRadius = 0.5; |
| 253 |
} else { |
| 254 |
numRadialSegmentsPerRadian = $num_radial_segments_per_radian(maxScale * strokeParams.x); |
| 255 |
} |
| 256 |
|
| 257 |
if (isHairline) { |
| 258 |
// Hairline case. Transform the points before tessellation. We can still hold off on the |
| 259 |
// translate until the end; we just need to perform the scale and skew right now. |
| 260 |
p0 = affineMatrix * p0; |
| 261 |
p1 = affineMatrix * p1; |
| 262 |
p2 = affineMatrix * p2; |
| 263 |
p3 = affineMatrix * p3; |
| 264 |
lastControlPoint = affineMatrix * lastControlPoint; |
| 265 |
} |
| 266 |
|
| 267 |
// Find the starting and ending tangents (of the curve) and the tangent of the previous curve |
| 268 |
// that connects to it. |
| 269 |
float2 tan0 = $robust_normalize_diff((p0 == p1) ? ((p1 == p2) ? p3 : p2) : p1, p0); |
| 270 |
float2 tan1 = $robust_normalize_diff(p3, (p3 == p2) ? ((p2 == p1) ? p0 : p1) : p2); |
| 271 |
float2 prevTan = $robust_normalize_diff(p0, lastControlPoint); |
| 272 |
if (tan0 == float2(0)) { |
| 273 |
// The stroke is a point, which is a special case for square or circle caps. |
| 274 |
joinType = 0; |
| 275 |
if (w < 0) { |
| 276 |
// This is a special case representing a stroke-width circle, so treat it as a 180 |
| 277 |
// degree point stroke instead. |
| 278 |
tan0 = float2(1,0); |
| 279 |
tan1 = float2(-1,0); |
| 280 |
} else { |
| 281 |
// This is a special case representing a stroke-width square, or half-square cap |
| 282 |
// (depending on the value of lastControlPoint). |
| 283 |
w = -1; |
| 284 |
tan0 = prevTan; |
| 285 |
tan1 = prevTan; |
| 286 |
if (prevTan == float2(0.0, 0.0)) { |
| 287 |
// A square cap, so adjust both p0,p1 and p2,p3 to shift by the stroke radius in |
| 288 |
// opposite directions along the X axis |
| 289 |
p2 = p3 = p0 + strokeRadius * float2(1.0, 0.0); |
| 290 |
p0 = p1 = p0 - strokeRadius * float2(1.0, 0.0); |
| 291 |
prevTan = tan0 = tan1 = float2(1.0, 0.0); |
| 292 |
} else { |
| 293 |
// A half-square, so behave as a lineTo from p0 to a strokeRadius outset away from |
| 294 |
// the prior tangent point |
| 295 |
p2 = p3 = p0 + strokeRadius * prevTan; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
// Determine how many edges to give to the join. We emit the first and final edges |
| 301 |
// of the join twice: once full width and once restricted to half width. This guarantees |
| 302 |
// perfect seaming by matching the vertices from the join as well as from the strokes on |
| 303 |
// either side. |
| 304 |
float numEdgesInJoin; |
| 305 |
if (joinType >= 0 /*Is the join not a round type?*/) { |
| 306 |
// Bevel(0) and miter(+) joins get 1 and 2 segments respectively. |
| 307 |
// +2 because we emit the beginning and ending edges twice (see above comments). |
| 308 |
numEdgesInJoin = sign(joinType) + (1 + 2); |
| 309 |
} else { |
| 310 |
// NOTE: for circles, prevTan = (0,0), but this still works out to 180 degrees here |
| 311 |
float joinRads = acos($cosine_between_unit_vectors(prevTan, tan0)); |
| 312 |
float numRadialSegmentsInJoin = max(ceil(joinRads * numRadialSegmentsPerRadian), 1); |
| 313 |
// +2 because we emit the beginning and ending edges twice (see above comment). |
| 314 |
numEdgesInJoin = numRadialSegmentsInJoin + 2; |
| 315 |
// The stroke section needs at least two edges. Don't assign more to the join than |
| 316 |
// "maxEdges - 2". (This is only relevant when the ideal max edge count calculated |
| 317 |
// on the CPU had to be limited to maxEdges in the draw call). |
| 318 |
numEdgesInJoin = min(numEdgesInJoin, maxEdges - 2); |
| 319 |
} |
| 320 |
|
| 321 |
// Find which direction the curve turns. |
| 322 |
// NOTE: Since the curve is not allowed to inflect, we can just check F'(.5) x F''(.5). |
| 323 |
// NOTE: F'(.5) x F''(.5) has the same sign as (P2 - P0) x (P3 - P1) |
| 324 |
float turn = cross_length_2d(p2 - p0, p3 - p1); |
| 325 |
float combinedEdgeID = abs(edgeID) - numEdgesInJoin; |
| 326 |
if (combinedEdgeID < 0) { |
| 327 |
tan1 = tan0; |
| 328 |
// Don't let tan0 become zero. The code as-is isn't built to handle that case. tan0=0 |
| 329 |
// means the join is disabled, and to disable it with the existing code we can leave |
| 330 |
// tan0 equal to tan1. |
| 331 |
if (lastControlPoint != p0) { |
| 332 |
tan0 = prevTan; |
| 333 |
} |
| 334 |
turn = cross_length_2d(tan0, tan1); |
| 335 |
} |
| 336 |
|
| 337 |
// Calculate the curve's starting angle and rotation. |
| 338 |
float cosTheta = $cosine_between_unit_vectors(tan0, tan1); |
| 339 |
float rotation = acos(cosTheta); |
| 340 |
if (turn < 0) { |
| 341 |
// Adjust sign of rotation to match the direction the curve turns. |
| 342 |
rotation = -rotation; |
| 343 |
} |
| 344 |
|
| 345 |
float numRadialSegments; |
| 346 |
float strokeOutset = sign(edgeID); |
| 347 |
if (combinedEdgeID < 0) { |
| 348 |
// We belong to the preceding join. The first and final edges get duplicated, so we only |
| 349 |
// have "numEdgesInJoin - 2" segments. |
| 350 |
numRadialSegments = numEdgesInJoin - 2; |
| 351 |
numParametricSegments = 1; // Joins don't have parametric segments. |
| 352 |
p3 = p2 = p1 = p0; // Colocate all points on the junction point. |
| 353 |
// Shift combinedEdgeID to the range [-1, numRadialSegments]. This duplicates the first |
| 354 |
// edge and lands one edge at the very end of the join. (The duplicated final edge will |
| 355 |
// actually come from the section of our strip that belongs to the stroke.) |
| 356 |
combinedEdgeID += numRadialSegments + 1; |
| 357 |
if (combinedEdgeID < 0) { |
| 358 |
combinedEdgeID = 0; |
| 359 |
} else { |
| 360 |
// We normally restrict the join on one side of the junction, but if the tangents are |
| 361 |
// nearly equivalent this could theoretically result in bad seaming and/or cracks on the |
| 362 |
// side we don't put it on. If the tangents are nearly equivalent then we leave the join |
| 363 |
// double-sided. |
| 364 |
// NOTE: For square caps, the construction of tan0/tan1 ensure we emit a full width edge |
| 365 |
const float sinEpsilon = 1e-2; // ~= sin(180deg / 3000) |
| 366 |
bool tangentsNearlyParallel = |
| 367 |
(abs(turn) * inversesqrt(dot(tan0, tan0) * dot(tan1, tan1))) < sinEpsilon; |
| 368 |
if (!tangentsNearlyParallel || dot(tan0, tan1) < 0) { |
| 369 |
// There are two edges colocated at the beginning. Leave the first one double sided |
| 370 |
// for seaming with the previous stroke. (The double sided edge at the end will |
| 371 |
// actually come from the section of our strip that belongs to the stroke.) |
| 372 |
strokeOutset = (turn < 0) ? min(strokeOutset, 0) : max(strokeOutset, 0); |
| 373 |
} |
| 374 |
} |
| 375 |
} else { |
| 376 |
// We belong to the stroke. Unless numRadialSegmentsPerRadian is incredibly high, |
| 377 |
// clamping to maxCombinedSegments will be a no-op because the draw call was invoked with |
| 378 |
// sufficient vertices to cover the worst case scenario of 180 degree rotation. |
| 379 |
float maxCombinedSegments = maxEdges - numEdgesInJoin - 1; |
| 380 |
numRadialSegments = max(ceil(abs(rotation) * numRadialSegmentsPerRadian), 1); |
| 381 |
numRadialSegments = min(numRadialSegments, maxCombinedSegments); |
| 382 |
numParametricSegments = min(numParametricSegments, |
| 383 |
maxCombinedSegments - numRadialSegments + 1); |
| 384 |
} |
| 385 |
|
| 386 |
// Additional parameters for final tessellation evaluation. |
| 387 |
float radsPerSegment = rotation / numRadialSegments; |
| 388 |
float numCombinedSegments = numParametricSegments + numRadialSegments - 1; |
| 389 |
bool isFinalEdge = (combinedEdgeID >= numCombinedSegments); |
| 390 |
if (combinedEdgeID > numCombinedSegments) { |
| 391 |
strokeOutset = 0; // The strip has more edges than we need. Drop this one. |
| 392 |
} |
| 393 |
// Edge #2 extends to the miter point. |
| 394 |
if (abs(edgeID) == 2 && joinType > 0/*Is the join a miter type?*/) { |
| 395 |
strokeOutset *= $miter_extent(cosTheta, joinType/*miterLimit*/); |
| 396 |
} |
| 397 |
|
| 398 |
float2 tangent, strokeCoord; |
| 399 |
if (combinedEdgeID != 0 && !isFinalEdge) { |
| 400 |
// Compute the location and tangent direction of the stroke edge with the integral id |
| 401 |
// "combinedEdgeID", where combinedEdgeID is the sorted-order index of parametric and radial |
| 402 |
// edges. Start by finding the tangent function's power basis coefficients. These define a |
| 403 |
// tangent direction (scaled by some uniform value) as: |
| 404 |
// |T^2| |
| 405 |
// Tangent_Direction(T) = dx,dy = |A 2B C| * |T | |
| 406 |
// |. . .| |1 | |
| 407 |
float2 A, B, C = p1 - p0; |
| 408 |
float2 D = p3 - p0; |
| 409 |
if (w >= 0.0) { |
| 410 |
// P0..P2 represent a conic and P3==P2. The derivative of a conic has a cumbersome |
| 411 |
// order-4 denominator. However, this isn't necessary if we are only interested in a |
| 412 |
// vector in the same *direction* as a given tangent line. Since the denominator scales |
| 413 |
// dx and dy uniformly, we can throw it out completely after evaluating the derivative |
| 414 |
// with the standard quotient rule. This leaves us with a simpler quadratic function |
| 415 |
// that we use to find a tangent. |
| 416 |
C *= w; |
| 417 |
B = .5*D - C; |
| 418 |
A = (w - 1.0) * D; |
| 419 |
p1 *= w; |
| 420 |
} else { |
| 421 |
float2 E = p2 - p1; |
| 422 |
B = E - C; |
| 423 |
A = fma(float2(-3), E, D); |
| 424 |
} |
| 425 |
// FIXME(crbug.com/800804,skbug.com/40042642): Consider normalizing the exponents in A,B,C at |
| 426 |
// this point in order to prevent fp32 overflow. |
| 427 |
|
| 428 |
// Now find the coefficients that give a tangent direction from a parametric edge ID: |
| 429 |
// |
| 430 |
// |parametricEdgeID^2| |
| 431 |
// Tangent_Direction(parametricEdgeID) = dx,dy = |A B_ C_| * |parametricEdgeID | |
| 432 |
// |. . .| |1 | |
| 433 |
// |
| 434 |
float2 B_ = B * (numParametricSegments * 2.0); |
| 435 |
float2 C_ = C * (numParametricSegments * numParametricSegments); |
| 436 |
|
| 437 |
// Run a binary search to determine the highest parametric edge that is located on or before |
| 438 |
// the combinedEdgeID. A combined ID is determined by the sum of complete parametric and |
| 439 |
// radial segments behind it. i.e., find the highest parametric edge where: |
| 440 |
// |
| 441 |
// parametricEdgeID + floor(numRadialSegmentsAtParametricT) <= combinedEdgeID |
| 442 |
// |
| 443 |
float lastParametricEdgeID = 0.0; |
| 444 |
float maxParametricEdgeID = min(numParametricSegments - 1.0, combinedEdgeID); |
| 445 |
float negAbsRadsPerSegment = -abs(radsPerSegment); |
| 446 |
float maxRotation0 = (1.0 + combinedEdgeID) * abs(radsPerSegment); |
| 447 |
for (float exp = 32.0; exp >= 1.0; exp *= 0.5) { |
| 448 |
// Test the parametric edge at lastParametricEdgeID + (32, 16, 8, 4, 2, 1). |
| 449 |
float testParametricID = lastParametricEdgeID + exp; |
| 450 |
if (testParametricID <= maxParametricEdgeID) { |
| 451 |
float2 testTan = fma(float2(testParametricID), A, B_); |
| 452 |
testTan = fma(float2(testParametricID), testTan, C_); |
| 453 |
float cosRotation = dot(normalize(testTan), tan0); |
| 454 |
float maxRotation = fma(testParametricID, negAbsRadsPerSegment, maxRotation0); |
| 455 |
maxRotation = min(maxRotation, $PI); |
| 456 |
// Is rotation <= maxRotation? (i.e., is the number of complete radial segments |
| 457 |
// behind testT, + testParametricID <= combinedEdgeID?) |
| 458 |
if (cosRotation >= cos(maxRotation)) { |
| 459 |
// testParametricID is on or before the combinedEdgeID. Keep it! |
| 460 |
lastParametricEdgeID = testParametricID; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Find the T value of the parametric edge at lastParametricEdgeID. |
| 466 |
float parametricT = lastParametricEdgeID / numParametricSegments; |
| 467 |
|
| 468 |
// Now that we've identified the highest parametric edge on or before the |
| 469 |
// combinedEdgeID, the highest radial edge is easy: |
| 470 |
float lastRadialEdgeID = combinedEdgeID - lastParametricEdgeID; |
| 471 |
|
| 472 |
// Find the angle of tan0, i.e. the angle between tan0 and the positive x axis. |
| 473 |
float angle0 = acos(clamp(tan0.x, -1.0, 1.0)); |
| 474 |
angle0 = tan0.y >= 0.0 ? angle0 : -angle0; |
| 475 |
|
| 476 |
// Find the tangent vector on the edge at lastRadialEdgeID. By construction it is already |
| 477 |
// normalized. |
| 478 |
float radialAngle = fma(lastRadialEdgeID, radsPerSegment, angle0); |
| 479 |
tangent = float2(cos(radialAngle), sin(radialAngle)); |
| 480 |
float2 norm = float2(-tangent.y, tangent.x); |
| 481 |
|
| 482 |
// Find the T value where the tangent is orthogonal to norm. This is a quadratic: |
| 483 |
// |
| 484 |
// dot(norm, Tangent_Direction(T)) == 0 |
| 485 |
// |
| 486 |
// |T^2| |
| 487 |
// norm * |A 2B C| * |T | == 0 |
| 488 |
// |. . .| |1 | |
| 489 |
// |
| 490 |
float a=dot(norm,A), b_over_2=dot(norm,B), c=dot(norm,C); |
| 491 |
float discr_over_4 = max(b_over_2*b_over_2 - a*c, 0.0); |
| 492 |
float q = sqrt(discr_over_4); |
| 493 |
if (b_over_2 > 0.0) { |
| 494 |
q = -q; |
| 495 |
} |
| 496 |
q -= b_over_2; |
| 497 |
|
| 498 |
// Roots are q/a and c/q. Since each curve section does not inflect or rotate more than 180 |
| 499 |
// degrees, there can only be one tangent orthogonal to "norm" inside 0..1. Pick the root |
| 500 |
// nearest .5. |
| 501 |
float _5qa = -.5*q*a; |
| 502 |
float2 root = (abs(fma(q,q,_5qa)) < abs(fma(a,c,_5qa))) ? float2(q,a) : float2(c,q); |
| 503 |
|
| 504 |
// The root finder above can become unstable when lastRadialEdgeID == 0 (e.g., if there are |
| 505 |
// roots at exatly 0 and 1 both). radialT should always equal 0 in this case. |
| 506 |
float radialT = (lastRadialEdgeID != 0.0 && root.t != 0.0) |
| 507 |
? saturate(root.s / root.t) |
| 508 |
: 0.0; |
| 509 |
|
| 510 |
// Now that we've identified the T values of the last parametric and radial edges, our final |
| 511 |
// T value for combinedEdgeID is whichever is larger. |
| 512 |
float T = max(parametricT, radialT); |
| 513 |
|
| 514 |
// Evaluate the cubic at T. Use De Casteljau's for its accuracy and stability. |
| 515 |
float2 ab = $unchecked_mix(p0, p1, T); |
| 516 |
float2 bc = $unchecked_mix(p1, p2, T); |
| 517 |
float2 cd = $unchecked_mix(p2, p3, T); |
| 518 |
float2 abc = $unchecked_mix(ab, bc, T); |
| 519 |
float2 bcd = $unchecked_mix(bc, cd, T); |
| 520 |
float2 abcd = $unchecked_mix(abc, bcd, T); |
| 521 |
|
| 522 |
// Evaluate the conic weight at T. |
| 523 |
float u = $unchecked_mix(1.0, w, T); |
| 524 |
float v = w + 1 - u; // == mix(w, 1, T) |
| 525 |
float uv = $unchecked_mix(u, v, T); |
| 526 |
|
| 527 |
// If we went with T=parametricT, then update the tangent. Otherwise leave it at the radial |
| 528 |
// tangent found previously. (In the event that parametricT == radialT, we keep the radial |
| 529 |
// tangent.) |
| 530 |
if (T != radialT) { |
| 531 |
// We must re-normalize here because the tangent is determined by the curve coefficients |
| 532 |
tangent = w >= 0.0 ? $robust_normalize_diff(bc*u, ab*v) |
| 533 |
: $robust_normalize_diff(bcd, abc); |
| 534 |
} |
| 535 |
|
| 536 |
strokeCoord = (w >= 0.0) ? abc/uv : abcd; |
| 537 |
} else { |
| 538 |
// Edges at the beginning and end of the strip use exact endpoints and tangents. This |
| 539 |
// ensures crack-free seaming between instances. |
| 540 |
tangent = (combinedEdgeID == 0) ? tan0 : tan1; |
| 541 |
strokeCoord = (combinedEdgeID == 0) ? p0 : p3; |
| 542 |
} |
| 543 |
|
| 544 |
// At this point 'tangent' is normalized, so the orthogonal vector is also normalized. |
| 545 |
float2 ortho = float2(tangent.y, -tangent.x); |
| 546 |
strokeCoord += ortho * (strokeRadius * strokeOutset); |
| 547 |
|
| 548 |
if (isHairline) { |
| 549 |
// Hairline case. The scale and skew already happened before tessellation. |
| 550 |
// TODO: There's probably a more efficient way to tessellate the hairline that lets us |
| 551 |
// avoid inverting the affine matrix to get back to local coords, but it's just a 2x2 so |
| 552 |
// this works for now. |
| 553 |
return float4(strokeCoord + translate, inverse(affineMatrix) * strokeCoord); |
| 554 |
} else { |
| 555 |
// Normal case. Do the transform after tessellation. |
| 556 |
return float4(affineMatrix * strokeCoord + translate, strokeCoord); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
float4 analytic_rrect_vertex_fn(// Vertex Attributes |
| 561 |
uint cornerID, |
| 562 |
float2 position, |
| 563 |
float2 normal, |
| 564 |
float normalScale, |
| 565 |
float centerWeight, |
| 566 |
// Instance Attributes |
| 567 |
float4 xRadiiOrFlags, |
| 568 |
float4 radiiOrQuadXs, |
| 569 |
float4 ltrbOrQuadYs, |
| 570 |
float4 center, |
| 571 |
float depth, |
| 572 |
float3x3 localToDevice, |
| 573 |
// Varyings |
| 574 |
out float4 jacobian, |
| 575 |
out float4 edgeDistances, |
| 576 |
out float4 xRadii, |
| 577 |
out float4 yRadii, |
| 578 |
out float2 strokeParams, |
| 579 |
out float2 perPixelControl, |
| 580 |
// Render Step |
| 581 |
out float2 stepLocalCoords) { |
| 582 |
const float kMiterScale = 1.0; |
| 583 |
const float kBevelScale = 0.0; |
| 584 |
const float kRoundScale = 0.41421356237; // sqrt(2)-1 |
| 585 |
const float kEpsilon = 0.00024; // SK_ScalarNearlyZero |
| 586 |
|
| 587 |
// Default to miter'ed vertex positioning. Corners with sufficiently large corner radii, or |
| 588 |
// bevel'ed strokes will adjust vertex placement on a per corner basis. This will not affect |
| 589 |
// the final coverage calculations in the fragment shader. |
| 590 |
float joinScale = kMiterScale; |
| 591 |
|
| 592 |
// Calculate a rotated cornerID value to use for indexing within vectors that would normally |
| 593 |
// require ".yzwx" swizzling before indexing (e.g., if `var` is a container of 4, |
| 594 |
// `var.yzwx[cornerID]` is instead handled as `var[nextID]`). Though theoretically equivalent, |
| 595 |
// using the modified cornerID gets around a driver bug within Qualcomm Adreno 620 GPUs (used by |
| 596 |
// the Pixel 5) which leads to incorrect output when swizzling and indexing into a vector within |
| 597 |
// one line or operation. This issue may also exist on other devices as well. Since the |
| 598 |
// workaround is simple, just use this approach for all vertex shaders. |
| 599 |
uint nextID = (cornerID + 1) % 4; |
| 600 |
|
| 601 |
// Unpack instance-level state that determines the vertex placement and style of shape. |
| 602 |
bool bidirectionalCoverage = center.z <= 0.0; |
| 603 |
bool deviceSpaceDistances = false; |
| 604 |
float4 xs, ys; // ordered TL, TR, BR, BL |
| 605 |
float4 edgeAA = float4(1.0); // ordered L,T,R,B. 1 = AA, 0 = no AA |
| 606 |
bool strokedLine = false; |
| 607 |
if (xRadiiOrFlags.x < -1.0) { |
| 608 |
// Stroked [round] rect or line |
| 609 |
// If y > 0, unpack the line end points, otherwise unpack the rect edges |
| 610 |
strokedLine = xRadiiOrFlags.y > 0.0; |
| 611 |
xs = strokedLine ? ltrbOrQuadYs.LLRR : ltrbOrQuadYs.LRRL; |
| 612 |
ys = ltrbOrQuadYs.TTBB; |
| 613 |
|
| 614 |
if (xRadiiOrFlags.y < 0.0) { |
| 615 |
// A hairline [r]rect so the X radii are encoded as negative values in this field, |
| 616 |
// and Y radii are stored directly in the subsequent float4. |
| 617 |
xRadii = -xRadiiOrFlags - 2.0; |
| 618 |
yRadii = radiiOrQuadXs; |
| 619 |
|
| 620 |
// All hairlines use miter joins (join style > 0) |
| 621 |
strokeParams = float2(0.0, 1.0); |
| 622 |
} else { |
| 623 |
xRadii = radiiOrQuadXs; |
| 624 |
yRadii = xRadii; // regular strokes are circular |
| 625 |
strokeParams = xRadiiOrFlags.zw; |
| 626 |
|
| 627 |
// `sign(strokeParams.y)` evaluates to kMiterScale (1.0) when the |
| 628 |
// input is positive, and kBevelScale (0.0) when it is zero. |
| 629 |
// kRoundScale uses the stroke radius to round rectangular corners. |
| 630 |
joinScale = (strokeParams.y < 0.0) ? kRoundScale |
| 631 |
: sign(strokeParams.y); |
| 632 |
} |
| 633 |
} else if (any(greaterThan(xRadiiOrFlags, float4(0.0)))) { |
| 634 |
// Filled round rect |
| 635 |
xs = ltrbOrQuadYs.LRRL; |
| 636 |
ys = ltrbOrQuadYs.TTBB; |
| 637 |
|
| 638 |
xRadii = xRadiiOrFlags; |
| 639 |
yRadii = radiiOrQuadXs; |
| 640 |
|
| 641 |
strokeParams = float2(0.0, -1.0); // A negative join style is "round" |
| 642 |
} else { |
| 643 |
// Per-edge quadrilateral, so we have to calculate the corner's basis from the |
| 644 |
// quad's edges. |
| 645 |
xs = radiiOrQuadXs; |
| 646 |
ys = ltrbOrQuadYs; |
| 647 |
edgeAA = -xRadiiOrFlags; // AA flags needed to be < 0 on upload, so flip the sign. |
| 648 |
|
| 649 |
xRadii = float4(0.0); |
| 650 |
yRadii = float4(0.0); |
| 651 |
|
| 652 |
strokeParams = float2(0.0, 1.0); // Will be ignored, but set to a "miter" |
| 653 |
deviceSpaceDistances = true; |
| 654 |
} |
| 655 |
|
| 656 |
// Adjust state on a per-corner basis |
| 657 |
float2 cornerRadii = float2(xRadii[cornerID], yRadii[cornerID]); |
| 658 |
if (cornerID % 2 != 0) { |
| 659 |
// Corner radii are uploaded in the local coordinate frame, but vertex placement happens |
| 660 |
// in a consistent winding before transforming to final local coords, so swap the |
| 661 |
// radii for odd corners. |
| 662 |
cornerRadii = cornerRadii.yx; |
| 663 |
} |
| 664 |
|
| 665 |
float2 cornerAspectRatio = float2(1.0); |
| 666 |
if (all(greaterThan(cornerRadii, float2(0.0)))) { |
| 667 |
// Position vertices for an elliptical corner; overriding any previous join style since |
| 668 |
// that only applies when radii are 0. |
| 669 |
joinScale = kRoundScale; |
| 670 |
cornerAspectRatio = cornerRadii.yx; |
| 671 |
} |
| 672 |
|
| 673 |
// Calculate the local edge vectors, ordered L, T, R, B starting from the bottom left point. |
| 674 |
// For quadrilaterals these are not necessarily axis-aligned, but in all cases they orient |
| 675 |
// the +X/+Y normalized vertex template for each corner. |
| 676 |
float4 dx = xs - xs.wxyz; |
| 677 |
float4 dy = ys - ys.wxyz; |
| 678 |
// This is a specialized application of `robust_normalized_diff` for a quad, with an extra |
| 679 |
// max against 1.0 to not scale small edges. This is to avoid overflows for extremely large |
| 680 |
// coordinates when squaring dx or dy. |
| 681 |
float4 invMag = 1.0 / max(abs(dx), max(abs(dy), float4(1.0))); |
| 682 |
dx *= invMag; |
| 683 |
dy *= invMag; |
| 684 |
float4 edgeSquaredLen = dx*dx + dy*dy; |
| 685 |
|
| 686 |
float4 edgeMask = sign(edgeSquaredLen); // 0 for zero-length edge, 1 for non-zero edge. |
| 687 |
float4 edgeBias = float4(0.0); // adjustment to edge distance for butt cap correction |
| 688 |
float2 strokeRadius = float2(strokeParams.x); |
| 689 |
if (any(equal(edgeMask, float4(0.0)))) { |
| 690 |
// Must clean up (dx,dy) depending on the empty edge configuration |
| 691 |
if (all(equal(edgeMask, float4(0.0)))) { |
| 692 |
// A point so use the canonical basis |
| 693 |
dx = float4( 0.0, 1.0, 0.0, -1.0); |
| 694 |
dy = float4(-1.0, 0.0, 1.0, 0.0); |
| 695 |
edgeSquaredLen = float4(1.0); |
| 696 |
} else { |
| 697 |
// Triangles (3 non-zero edges) copy the adjacent edge. Otherwise it's a line so |
| 698 |
// replace empty edges with the left-hand normal vector of the adjacent edge. |
| 699 |
bool triangle = (edgeMask[0] + edgeMask[1] + edgeMask[2] + edgeMask[3]) > 2.5; |
| 700 |
float4 edgeX = triangle ? dx.yzwx : dy.yzwx; |
| 701 |
float4 edgeY = triangle ? dy.yzwx : -dx.yzwx; |
| 702 |
|
| 703 |
dx = mix(edgeX, dx, edgeMask); |
| 704 |
dy = mix(edgeY, dy, edgeMask); |
| 705 |
edgeSquaredLen = mix(edgeSquaredLen.yzwx, edgeSquaredLen, edgeMask); |
| 706 |
edgeAA = mix(edgeAA.yzwx, edgeAA, edgeMask); |
| 707 |
|
| 708 |
if (!triangle && joinScale == kBevelScale) { |
| 709 |
// Don't outset by stroke radius for butt caps on the zero-length edge, but |
| 710 |
// adjust edgeBias and strokeParams to calculate an AA miter'ed shape with the |
| 711 |
// non-uniform stroke outset. |
| 712 |
|
| 713 |
strokeRadius *= float2(edgeMask[cornerID], edgeMask[nextID]); |
| 714 |
edgeBias = (edgeMask - 1.0) * strokeParams.x; |
| 715 |
strokeParams.y = 1.0; |
| 716 |
joinScale = kMiterScale; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
float4 inverseEdgeLen = inversesqrt(edgeSquaredLen); |
| 722 |
dx *= inverseEdgeLen; |
| 723 |
dy *= inverseEdgeLen; |
| 724 |
|
| 725 |
// Calculate local coordinate for the vertex (relative to xAxis and yAxis at first). |
| 726 |
float2 xAxis = -float2(dx[nextID], dy[nextID]); |
| 727 |
float2 yAxis = float2(dx[cornerID], dy[cornerID]); |
| 728 |
float2 localPos; |
| 729 |
bool snapToCenter = false; |
| 730 |
if (normalScale < 0.0) { |
| 731 |
// Vertex is inset from the base shape, so we scale by (cornerRadii - strokeRadius) |
| 732 |
// and have to check for the possibility of an inner miter. It is always inset by an |
| 733 |
// additional conservative AA amount. |
| 734 |
if (center.w < 0.0 || centerWeight * center.z != 0.0) { |
| 735 |
snapToCenter = true; |
| 736 |
} else { |
| 737 |
float localAARadius = center.w; |
| 738 |
float2 insetRadii = |
| 739 |
cornerRadii + (bidirectionalCoverage ? -strokeRadius : strokeRadius); |
| 740 |
if (joinScale == kMiterScale || |
| 741 |
any(lessThanEqual(insetRadii, float2(localAARadius)))) { |
| 742 |
// Miter the inset position |
| 743 |
localPos = (insetRadii - localAARadius); |
| 744 |
} else { |
| 745 |
localPos = insetRadii*position - localAARadius*normal; |
| 746 |
} |
| 747 |
} |
| 748 |
} else { |
| 749 |
// Vertex is outset from the base shape (and possibly with an additional AA outset later |
| 750 |
// in device space). |
| 751 |
localPos = (cornerRadii + strokeRadius) * (position + joinScale*position.yx); |
| 752 |
} |
| 753 |
|
| 754 |
if (snapToCenter) { |
| 755 |
// Center is already relative to true local coords, not the corner basis. |
| 756 |
localPos = center.xy; |
| 757 |
} else { |
| 758 |
// Transform from corner basis to true local coords. |
| 759 |
localPos -= cornerRadii; |
| 760 |
localPos = float2(xs[cornerID], ys[cornerID]) + xAxis*localPos.x + yAxis*localPos.y; |
| 761 |
} |
| 762 |
|
| 763 |
// Calculate edge distances and device space coordinate for the vertex |
| 764 |
edgeDistances = dy*(xs - localPos.x) - dx*(ys - localPos.y) + edgeBias; |
| 765 |
|
| 766 |
// NOTE: This 3x3 inverse is different than just taking the 1st two columns of the 4x4 |
| 767 |
// inverse of the original SkM44 local-to-device matrix. We could calculate the 3x3 inverse |
| 768 |
// and upload it, but it does not seem to be a bottleneck and saves on bandwidth to |
| 769 |
// calculate it here instead. |
| 770 |
float3x3 deviceToLocal = inverse(localToDevice); |
| 771 |
float3 devPos = localToDevice * localPos.xy1; |
| 772 |
jacobian = float4(deviceToLocal[0].xy - deviceToLocal[0].z*localPos, |
| 773 |
deviceToLocal[1].xy - deviceToLocal[1].z*localPos); |
| 774 |
|
| 775 |
if (deviceSpaceDistances) { |
| 776 |
// Apply the Jacobian in the vertex shader so any quadrilateral normals do not have to |
| 777 |
// be passed to the fragment shader. However, it's important to use the Jacobian at a |
| 778 |
// vertex on the edge, not the current vertex's Jacobian. |
| 779 |
float4 gx = -dy*(deviceToLocal[0].x - deviceToLocal[0].z*xs) + |
| 780 |
dx*(deviceToLocal[0].y - deviceToLocal[0].z*ys); |
| 781 |
float4 gy = -dy*(deviceToLocal[1].x - deviceToLocal[1].z*xs) + |
| 782 |
dx*(deviceToLocal[1].y - deviceToLocal[1].z*ys); |
| 783 |
// NOTE: The gradient is missing a W term so edgeDistances must still be multiplied by |
| 784 |
// 1/w in the fragment shader. The same goes for the encoded coverage scale. |
| 785 |
edgeDistances *= inversesqrt(gx*gx + gy*gy); |
| 786 |
|
| 787 |
// Bias non-AA edge distances by device W so its coverage contribution is >= 1.0 |
| 788 |
edgeDistances += (1 - edgeAA)*abs(devPos.z); |
| 789 |
|
| 790 |
// Mixed edge AA shapes do not use subpixel scale+bias for coverage, since they tile |
| 791 |
// to a large shape of unknown--but likely not subpixel--size. Triangles and quads do |
| 792 |
// not use subpixel coverage since the scale+bias is not constant over the shape, but |
| 793 |
// we can't evaluate per-fragment since we aren't passing down their arbitrary normals. |
| 794 |
bool subpixelCoverage = edgeAA == float4(1.0) && |
| 795 |
dot(abs(dx*dx.yzwx + dy*dy.yzwx), float4(1.0)) < kEpsilon; |
| 796 |
if (subpixelCoverage) { |
| 797 |
// Reconstructs the actual device-space width and height for all rectangle vertices. |
| 798 |
float2 dim = edgeDistances.xy + edgeDistances.zw; |
| 799 |
perPixelControl.y = 1.0 + min(min(dim.x, dim.y), abs(devPos.z)); |
| 800 |
} else { |
| 801 |
perPixelControl.y = 1.0 + abs(devPos.z); // standard 1px width pre W division. |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
// Only outset for a vertex that is in front of the w=0 plane to avoid dealing with outset |
| 806 |
// triangles rasterizing differently from the main triangles as w crosses 0. |
| 807 |
if (normalScale > 0.0 && devPos.z > 0.0) { |
| 808 |
// Note that when there's no perspective, the jacobian is equivalent to the normal |
| 809 |
// matrix (inverse transpose), but produces correct results when there's perspective |
| 810 |
// because it accounts for the position's influence on a line's projected direction. |
| 811 |
float2x2 J = float2x2(jacobian); |
| 812 |
|
| 813 |
float2 edgeAANormal = float2(edgeAA[cornerID], edgeAA[nextID]) * normal; |
| 814 |
float2 nx = cornerAspectRatio.x * edgeAANormal.x * perp(-yAxis) * J; |
| 815 |
float2 ny = cornerAspectRatio.y * edgeAANormal.y * perp( xAxis) * J; |
| 816 |
|
| 817 |
bool isMidVertex = all(notEqual(edgeAANormal, float2(0))); |
| 818 |
if (joinScale == kMiterScale && isMidVertex) { |
| 819 |
// Produce a bisecting vector in device space. |
| 820 |
nx = normalize(nx); |
| 821 |
ny = normalize(ny); |
| 822 |
if (dot(nx, ny) < -0.8) { |
| 823 |
// Normals are in nearly opposite directions, so adjust to avoid float error. |
| 824 |
float s = sign(cross_length_2d(nx, ny)); |
| 825 |
nx = s*perp(nx); |
| 826 |
ny = -s*perp(ny); |
| 827 |
} |
| 828 |
} |
| 829 |
// Adding the normal components together directly results in what we'd have |
| 830 |
// calculated if we'd just transformed 'normal' in one go, assuming they weren't |
| 831 |
// normalized in the if-block above. If they were normalized, the sum equals the |
| 832 |
// bisector between the original nx and ny. |
| 833 |
// |
| 834 |
// We multiply by W so that after perspective division the new point is offset by the |
| 835 |
// now-unit normal. |
| 836 |
// NOTE: (nx + ny) can become the zero vector if the device outset is for an edge |
| 837 |
// marked as non-AA. In this case normalize() could produce the zero vector or NaN. |
| 838 |
// Until a counter-example is found, GPUs seem to discard triangles with NaN vertices, |
| 839 |
// which has the same effect as outsetting by the zero vector with this mesh, so we |
| 840 |
// don't bother guarding the normalize() (yet). |
| 841 |
devPos.xy += devPos.z * normalize(nx + ny); |
| 842 |
|
| 843 |
// By construction these points are 1px away from the outer edge in device space. |
| 844 |
if (deviceSpaceDistances) { |
| 845 |
// Apply directly to edgeDistances to save work per pixel later on. |
| 846 |
edgeDistances -= devPos.z; |
| 847 |
} else { |
| 848 |
// Otherwise store separately so edgeDistances can be used to reconstruct corner pos |
| 849 |
perPixelControl.y = -devPos.z; |
| 850 |
} |
| 851 |
} else if (!deviceSpaceDistances) { |
| 852 |
// Triangles are within the original shape so there's no additional outsetting to |
| 853 |
// take into account for coverage calculations. |
| 854 |
perPixelControl.y = 0.0; |
| 855 |
} |
| 856 |
|
| 857 |
perPixelControl.x = (centerWeight != 0.0) |
| 858 |
// A positive value signals that a pixel is trivially full coverage. |
| 859 |
? 1.0 |
| 860 |
// A negative value signals bidirectional coverage, and a zero value signals a solid |
| 861 |
// interior with per-pixel coverage. |
| 862 |
: bidirectionalCoverage ? -1.0 : 0.0; |
| 863 |
|
| 864 |
// The fragment shader operates in a canonical basis (x-axis = (1,0), y-axis = (0,1)). For |
| 865 |
// stroked lines, incorporate their local orientation into the Jacobian to preserve this. |
| 866 |
if (strokedLine) { |
| 867 |
// The updated Jacobian is J' = B^-1 * J, where B is float2x2(xAxis, yAxis) for the |
| 868 |
// top-left corner (so that B^-1 is constant over the whole shape). Since it's a line |
| 869 |
// the basis was constructed to be orthonormal, det(B) = 1 and B^-1 is trivial. |
| 870 |
// NOTE: float2x2 is column-major. |
| 871 |
jacobian = float4(float2x2(dy[0], -dy[1], -dx[0], dx[1]) * float2x2(jacobian)); |
| 872 |
} |
| 873 |
|
| 874 |
// Write out final results |
| 875 |
stepLocalCoords = localPos; |
| 876 |
return float4(devPos.xy, devPos.z*depth, devPos.z); |
| 877 |
} |
| 878 |
|
| 879 |
float4 per_edge_aa_quad_vertex_fn(// Vertex Attributes |
| 880 |
uint cornerID, |
| 881 |
float2 normal, |
| 882 |
// Instance Attributes |
| 883 |
float4 edgeAA, |
| 884 |
float4 xs, // ordered TL, TR, BR, BL |
| 885 |
float4 ys, |
| 886 |
float depth, |
| 887 |
float3x3 localToDevice, |
| 888 |
// Varyings |
| 889 |
out float4 edgeDistances, |
| 890 |
// Render Step |
| 891 |
out float2 stepLocalCoords) { |
| 892 |
const float kEpsilon = 0.00024; // SK_ScalarNearlyZero |
| 893 |
|
| 894 |
// Calculate a rotated cornerID value to use for indexing within vectors that would normally |
| 895 |
// require ".yzwx" swizzling (see similar note in analytic_rrect_vertex_fn for explanation). |
| 896 |
uint nextID = (cornerID + 1) % 4; |
| 897 |
|
| 898 |
// Calculate the local edge vectors, ordered L, T, R, B starting from the bottom left point. |
| 899 |
// For quadrilaterals these are not necessarily axis-aligned, but in all cases they orient |
| 900 |
// the +X/+Y normalized vertex template for each corner. |
| 901 |
float4 dx = xs - xs.wxyz; |
| 902 |
float4 dy = ys - ys.wxyz; |
| 903 |
// This is a specialized application of `robust_normalized_diff` for a quad, with an extra |
| 904 |
// max against 1.0 to not scale small edges. This is to avoid overflows for extremely large |
| 905 |
// coordinates when squaring dx or dy. |
| 906 |
float4 invMag = 1.0 / max(abs(dx), max(abs(dy), float4(1.0))); |
| 907 |
dx *= invMag; |
| 908 |
dy *= invMag; |
| 909 |
float4 edgeSquaredLen = dx*dx + dy*dy; |
| 910 |
|
| 911 |
float4 edgeMask = sign(edgeSquaredLen); // 0 for zero-length edge, 1 for non-zero edge. |
| 912 |
if (any(equal(edgeMask, float4(0.0)))) { |
| 913 |
// Must clean up (dx,dy) depending on the empty edge configuration |
| 914 |
if (all(equal(edgeMask, float4(0.0)))) { |
| 915 |
// A point so use the canonical basis |
| 916 |
dx = float4( 0.0, 1.0, 0.0, -1.0); |
| 917 |
dy = float4(-1.0, 0.0, 1.0, 0.0); |
| 918 |
edgeSquaredLen = float4(1.0); |
| 919 |
} else { |
| 920 |
// Triangles (3 non-zero edges) copy the adjacent edge. Otherwise it's a line so |
| 921 |
// replace empty edges with the left-hand normal vector of the adjacent edge. |
| 922 |
bool triangle = (edgeMask[0] + edgeMask[1] + edgeMask[2] + edgeMask[3]) > 2.5; |
| 923 |
float4 edgeX = triangle ? dx.yzwx : dy.yzwx; |
| 924 |
float4 edgeY = triangle ? dy.yzwx : -dx.yzwx; |
| 925 |
|
| 926 |
dx = mix(edgeX, dx, edgeMask); |
| 927 |
dy = mix(edgeY, dy, edgeMask); |
| 928 |
edgeSquaredLen = mix(edgeSquaredLen.yzwx, edgeSquaredLen, edgeMask); |
| 929 |
edgeAA = mix(edgeAA.yzwx, edgeAA, edgeMask); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
float4 inverseEdgeLen = inversesqrt(edgeSquaredLen); |
| 934 |
dx *= inverseEdgeLen; |
| 935 |
dy *= inverseEdgeLen; |
| 936 |
|
| 937 |
// Calculate local coordinate for the vertex (relative to xAxis and yAxis at first). |
| 938 |
float2 xAxis = -float2(dx[nextID], dy[nextID]); |
| 939 |
float2 yAxis = float2(dx[cornerID], dy[cornerID]); |
| 940 |
|
| 941 |
// Vertex is outset from the base shape (and possibly with an additional AA outset later |
| 942 |
// in device space). |
| 943 |
float2 localPos = float2(xs[cornerID], ys[cornerID]); |
| 944 |
|
| 945 |
// Calculate edge distances and device space coordinate for the vertex |
| 946 |
edgeDistances = dy*(xs - localPos.x) - dx*(ys - localPos.y); |
| 947 |
|
| 948 |
// NOTE: This 3x3 inverse is different than just taking the 1st two columns of the 4x4 |
| 949 |
// inverse of the original SkM44 local-to-device matrix. We could calculate the 3x3 inverse |
| 950 |
// and upload it, but it does not seem to be a bottleneck and saves on bandwidth to |
| 951 |
// calculate it here instead. |
| 952 |
float3x3 deviceToLocal = inverse(localToDevice); |
| 953 |
float3 devPos = localToDevice * localPos.xy1; |
| 954 |
|
| 955 |
// Apply the Jacobian in the vertex shader so any quadrilateral normals do not have to |
| 956 |
// be passed to the fragment shader. However, it's important to use the Jacobian at a |
| 957 |
// vertex on the edge, not the current vertex's Jacobian. |
| 958 |
float4 gx = -dy*(deviceToLocal[0].x - deviceToLocal[0].z*xs) + |
| 959 |
dx*(deviceToLocal[0].y - deviceToLocal[0].z*ys); |
| 960 |
float4 gy = -dy*(deviceToLocal[1].x - deviceToLocal[1].z*xs) + |
| 961 |
dx*(deviceToLocal[1].y - deviceToLocal[1].z*ys); |
| 962 |
// NOTE: The gradient is missing a W term so edgeDistances must still be multiplied by |
| 963 |
// 1/w in the fragment shader. The same goes for the encoded coverage scale. |
| 964 |
edgeDistances *= inversesqrt(gx*gx + gy*gy); |
| 965 |
|
| 966 |
// Bias non-AA edge distances by device W so its coverage contribution is >= 1.0 |
| 967 |
// Add additional 1/2 bias here so we don't have to do so in the fragment shader. |
| 968 |
edgeDistances += (1.5 - edgeAA)*abs(devPos.z); |
| 969 |
|
| 970 |
// Only outset for a vertex that is in front of the w=0 plane to avoid dealing with outset |
| 971 |
// triangles rasterizing differently from the main triangles as w crosses 0. |
| 972 |
if (any(notEqual(normal, float2(0.0))) && devPos.z > 0.0) { |
| 973 |
// Note that when there's no perspective, the jacobian is equivalent to the normal |
| 974 |
// matrix (inverse transpose), but produces correct results when there's perspective |
| 975 |
// because it accounts for the position's influence on a line's projected direction. |
| 976 |
float2x2 J = float2x2(deviceToLocal[0].xy - deviceToLocal[0].z*localPos, |
| 977 |
deviceToLocal[1].xy - deviceToLocal[1].z*localPos); |
| 978 |
|
| 979 |
float2 edgeAANormal = float2(edgeAA[cornerID], edgeAA[nextID]) * normal; |
| 980 |
float2 nx = edgeAANormal.x * perp(-yAxis) * J; |
| 981 |
float2 ny = edgeAANormal.y * perp( xAxis) * J; |
| 982 |
|
| 983 |
bool isMidVertex = all(notEqual(edgeAANormal, float2(0))); |
| 984 |
if (isMidVertex) { |
| 985 |
// Produce a bisecting vector in device space. |
| 986 |
nx = normalize(nx); |
| 987 |
ny = normalize(ny); |
| 988 |
if (dot(nx, ny) < -0.8) { |
| 989 |
// Normals are in nearly opposite directions, so adjust to avoid float error. |
| 990 |
float s = sign(cross_length_2d(nx, ny)); |
| 991 |
nx = s*perp(nx); |
| 992 |
ny = -s*perp(ny); |
| 993 |
} |
| 994 |
} |
| 995 |
// Adding the normal components together directly results in what we'd have |
| 996 |
// calculated if we'd just transformed 'normal' in one go, assuming they weren't |
| 997 |
// normalized in the if-block above. If they were normalized, the sum equals the |
| 998 |
// bisector between the original nx and ny. |
| 999 |
// |
| 1000 |
// We multiply by W so that after perspective division the new point is offset by the |
| 1001 |
// now-unit normal. |
| 1002 |
// NOTE: (nx + ny) can become the zero vector if the device outset is for an edge |
| 1003 |
// marked as non-AA. In this case normalize() could produce the zero vector or NaN. |
| 1004 |
// Until a counter-example is found, GPUs seem to discard triangles with NaN vertices, |
| 1005 |
// which has the same effect as outsetting by the zero vector with this mesh, so we |
| 1006 |
// don't bother guarding the normalize() (yet). |
| 1007 |
devPos.xy += devPos.z * normalize(nx + ny); |
| 1008 |
|
| 1009 |
// By construction these points are 1px away from the outer edge in device space. |
| 1010 |
// Apply directly to edgeDistances to save work per pixel later on. |
| 1011 |
edgeDistances -= devPos.z; |
| 1012 |
} |
| 1013 |
|
| 1014 |
// Write out final results |
| 1015 |
stepLocalCoords = localPos; |
| 1016 |
return float4(devPos.xy, devPos.z*depth, devPos.z); |
| 1017 |
} |
| 1018 |
|
| 1019 |
float4 circular_arc_vertex_fn(float3 position, |
| 1020 |
// Instance Attributes |
| 1021 |
float4 centerScales, |
| 1022 |
float3 radiiAndFlags, |
| 1023 |
float3 geoClipPlane, |
| 1024 |
float3 fragClipPlane0, |
| 1025 |
float3 fragClipPlane1, |
| 1026 |
float4 inRoundCapPos, |
| 1027 |
float inRoundCapRadius, |
| 1028 |
float depth, |
| 1029 |
float3x3 localToDevice, |
| 1030 |
// Varyings |
| 1031 |
out float4 circleEdge, |
| 1032 |
out float3 clipPlane, |
| 1033 |
out float3 isectPlane, |
| 1034 |
out float3 unionPlane, |
| 1035 |
out float roundCapRadius, |
| 1036 |
out float4 roundCapPos, |
| 1037 |
// Render Step |
| 1038 |
out float2 stepLocalCoords) { |
| 1039 |
// TODO: clip offset against clip planes |
| 1040 |
float2 localCenter = centerScales.xy; |
| 1041 |
float2 localPos = localCenter; |
| 1042 |
// do geometric clip in normalized space |
| 1043 |
float dist = min(dot(position.xy, geoClipPlane.xy) + geoClipPlane.z, 0); |
| 1044 |
position.xy -= geoClipPlane.xy * dist; |
| 1045 |
// Get the new length to use below for scaling the offset |
| 1046 |
// (origLength is the initial length of position.xy). |
| 1047 |
float offsetScale = length(position.xy); |
| 1048 |
|
| 1049 |
// scale and translate to local space |
| 1050 |
if (position.z > 0) { |
| 1051 |
localPos += position.xy * centerScales.z; |
| 1052 |
} else { |
| 1053 |
localPos += position.xy * centerScales.w; |
| 1054 |
} |
| 1055 |
|
| 1056 |
float3 devPos = localToDevice * localPos.xy1; |
| 1057 |
float3 devCenter = localToDevice * localCenter.xy1; |
| 1058 |
float2 offset = devPos.xy - devCenter.xy; |
| 1059 |
// offset for AA and correct length of offset |
| 1060 |
if (offset != float2(0)) { |
| 1061 |
offset = normalize(offset); |
| 1062 |
devPos.xy += position.z*offset; |
| 1063 |
if (position.z > 0) { |
| 1064 |
// Scale using distance from center of unit octagon to the vertex |
| 1065 |
// Because of geometry clipping we need to scale by 1.0823922*newLength/origLength |
| 1066 |
// But the original length is 1.0823922 so the offsetScale is just newLength |
| 1067 |
offset *= offsetScale; |
| 1068 |
} else { |
| 1069 |
// Because of geometry clipping we need to scale by innerRadius*newLength/origLength |
| 1070 |
// But the original length is 1 so this is just innerRadius*newLength |
| 1071 |
offset *= offsetScale*radiiAndFlags.y; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
circleEdge = float4(offset, radiiAndFlags.xy); |
| 1076 |
if (radiiAndFlags.z > 0) { |
| 1077 |
clipPlane = fragClipPlane0; |
| 1078 |
isectPlane = fragClipPlane1; |
| 1079 |
unionPlane = float3(0, 0, 0); |
| 1080 |
} else { |
| 1081 |
clipPlane = fragClipPlane0; |
| 1082 |
isectPlane = float3(0, 0, 1); |
| 1083 |
unionPlane = fragClipPlane1; |
| 1084 |
} |
| 1085 |
roundCapRadius = inRoundCapRadius; |
| 1086 |
roundCapPos = inRoundCapPos; |
| 1087 |
stepLocalCoords = localPos; |
| 1088 |
|
| 1089 |
// We assume no perspective |
| 1090 |
return float4(devPos.xy, depth, 1); |
| 1091 |
} |
| 1092 |
|
| 1093 |
float4 text_vertex_fn(float2 baseCoords, |
| 1094 |
// Uniforms |
| 1095 |
float4x4 subRunDeviceMatrix, |
| 1096 |
float4x4 deviceToLocal, |
| 1097 |
float2 atlasSizeInv, |
| 1098 |
// Instance Attributes |
| 1099 |
float2 size, |
| 1100 |
float2 uvPos, |
| 1101 |
float2 xyPos, |
| 1102 |
float strikeToSourceScale, |
| 1103 |
float depth, |
| 1104 |
// Varyings |
| 1105 |
out float2 textureCoords, |
| 1106 |
out float2 unormTexCoords, // used as varying in SDFText |
| 1107 |
// Render Step |
| 1108 |
out float2 stepLocalCoords) { |
| 1109 |
baseCoords.xy *= float2(size); |
| 1110 |
|
| 1111 |
// Sub runs have a decomposed transform and are sometimes already transformed into device |
| 1112 |
// space, in which `subRunCoords` represents the bounds projected to device space without |
| 1113 |
// the local-to-device translation and `subRunDeviceMatrix` contains the translation. |
| 1114 |
float2 subRunCoords = strikeToSourceScale * baseCoords + xyPos; |
| 1115 |
float4 position = subRunDeviceMatrix * subRunCoords.xy01; |
| 1116 |
|
| 1117 |
// Calculate the local coords used for shading. |
| 1118 |
// TODO(b/246963258): This is incorrect if the transform has perspective, which would |
| 1119 |
// require a division + a valid z coordinate (which is currently set to 0). |
| 1120 |
stepLocalCoords = (deviceToLocal * position).xy; |
| 1121 |
|
| 1122 |
unormTexCoords = baseCoords + uvPos; |
| 1123 |
textureCoords = unormTexCoords * atlasSizeInv; |
| 1124 |
|
| 1125 |
return float4(position.xy, depth*position.w, position.w); |
| 1126 |
} |
| 1127 |
|
| 1128 |
float4 coverage_mask_vertex_fn(float2 quadCoords, |
| 1129 |
// Uniforms |
| 1130 |
float3x3 maskToDeviceRemainder, |
| 1131 |
// Instance Attributes |
| 1132 |
float4 drawBounds, |
| 1133 |
float4 maskBoundsIn, |
| 1134 |
float2 deviceOrigin, |
| 1135 |
float depth, |
| 1136 |
float3x3 deviceToLocal, |
| 1137 |
// Varyings |
| 1138 |
out float4 maskBounds, |
| 1139 |
out float2 textureCoords, |
| 1140 |
out half invert, |
| 1141 |
// Render Step |
| 1142 |
out float2 stepLocalCoords) { |
| 1143 |
// An atlas shape is an axis-aligned rectangle tessellated as a triangle strip. |
| 1144 |
// |
| 1145 |
// The bounds coordinates are in an intermediate space, pixel-aligned with the mask texture |
| 1146 |
// that's sampled in the fragment shader. The coords must be transformed by both |
| 1147 |
// maskToDeviceRemainder and translated by deviceOrigin to get device coords. |
| 1148 |
textureCoords = mix(drawBounds.xy, drawBounds.zw, quadCoords); |
| 1149 |
float3 drawCoords = maskToDeviceRemainder*((textureCoords + deviceOrigin).xy1); |
| 1150 |
|
| 1151 |
// Local coordinates used for shading are derived from the final device coords and the inverse |
| 1152 |
// of the original local-to-device matrix. |
| 1153 |
float3 localCoords = deviceToLocal * drawCoords; |
| 1154 |
// TODO: Support float3 local coordinates if the matrix has perspective so that W is |
| 1155 |
// interpolated correctly to the fragment shader. |
| 1156 |
stepLocalCoords = localCoords.xy / localCoords.z; |
| 1157 |
|
| 1158 |
// For an inverse fill, `textureCoords` will get clamped to `maskBounds` and the edge pixels |
| 1159 |
// will always land on a 0-coverage border pixel assuming the atlas was prepared with 1px |
| 1160 |
// padding around each mask entry. This includes inverse fills where the mask was fully clipped |
| 1161 |
// out, since then maskBounds.RBLT == (0,0,-1,-1) and we sample the top-left-most pixel of the |
| 1162 |
// atlas, which is guaranteed to be transparent. |
| 1163 |
if (all(lessThanEqual(maskBoundsIn.LT, maskBoundsIn.RB))) { |
| 1164 |
// Regular fill |
| 1165 |
maskBounds = maskBoundsIn; |
| 1166 |
invert = 0; |
| 1167 |
} else { |
| 1168 |
// Re-arrange the mask bounds to sorted order for texture clamping in the fragment shader |
| 1169 |
maskBounds = maskBoundsIn.RBLT; |
| 1170 |
invert = 1; |
| 1171 |
} |
| 1172 |
|
| 1173 |
return float4(drawCoords.xy, depth*drawCoords.z, drawCoords.z); |
| 1174 |
} |
| 1175 |
|
| 1176 |
float4 cover_bounds_vertex_fn(float2 corner, |
| 1177 |
float4 bounds, |
| 1178 |
float depth, |
| 1179 |
float3x3 matrix, |
| 1180 |
out float2 stepLocalCoords) { |
| 1181 |
if (all(lessThanEqual(bounds.LT, bounds.RB))) { |
| 1182 |
// A regular fill |
| 1183 |
corner = mix(bounds.LT, bounds.RB, corner); |
| 1184 |
float3 devCorner = matrix * corner.xy1; |
| 1185 |
stepLocalCoords = corner; |
| 1186 |
return float4(devCorner.xy, depth*devCorner.z, devCorner.z); |
| 1187 |
} else { |
| 1188 |
// An inverse fill |
| 1189 |
corner = mix(bounds.RB, bounds.LT, corner); |
| 1190 |
// TODO(b/351923375): Get the 3x3 inverse of the local-to-device transform from the CPU |
| 1191 |
// if it can be computed fast enough on the CPU from the cached 4x4 inverse. |
| 1192 |
float3 localCoords = inverse(matrix) * corner.xy1; |
| 1193 |
// Dividing the inverse mapped local coords by its homogenous coordinate reconstructs the |
| 1194 |
// original local coords. |
| 1195 |
float invW = 1.0 / localCoords.z; |
| 1196 |
stepLocalCoords = localCoords.xy * invW; |
| 1197 |
|
| 1198 |
// 1/W also happens to be equal to (matrix*stepLocalCoords.xy1).z, which is the device-space |
| 1199 |
// homogenous coordinate we want perspective interpolation to respect. We multiply the |
| 1200 |
// output position by 1/W and set the output position's homogenous coord to that same 1/W |
| 1201 |
// which ensures the projected vertices are still the device-space corners, but |
| 1202 |
// stepLocalCoords will be correctly perspective interpolated by HW. |
| 1203 |
return float4(corner*invW, depth*invW, invW); |
| 1204 |
} |
| 1205 |
} |