1/* Functions useful for debugging:
2
3// A heat map color for debugging (blue -> cyan -> green -> yellow -> red).
4fn heat_map_color(value: f32, minValue: f32, maxValue: f32, position: vec2<f32>) -> vec4<f32> {
5 // Normalize value to 0-1 range
6 let t = clamp((value - minValue) / (maxValue - minValue), 0.0, 1.0);
7
8 // Heat map color calculation
9 let r = t * t;
10 let g = 4.0 * t * (1.0 - t);
11 let b = (1.0 - t) * (1.0 - t);
12 let heat_color = vec3<f32>(r, g, b);
13
14 // Create a checkerboard pattern (black and white)
15 let sum = floor(position.x / 3) + floor(position.y / 3);
16 let is_odd = fract(sum * 0.5); // 0.0 for even, 0.5 for odd
17 let checker_value = is_odd * 2.0; // 0.0 for even, 1.0 for odd
18 let checker_color = vec3<f32>(checker_value);
19
20 // Determine if value is in range (1.0 if in range, 0.0 if out of range)
21 let in_range = step(minValue, value) * step(value, maxValue);
22
23 // Mix checkerboard and heat map based on whether value is in range
24 let final_color = mix(checker_color, heat_color, in_range);
25
26 return vec4<f32>(final_color, 1.0);
27}
28
29*/
30
31struct GlobalParams {
32 viewport_size: vec2<f32>,
33 premultiplied_alpha: u32,
34 pad: u32,
35}
36
37var<uniform> globals: GlobalParams;
38var t_sprite: texture_2d<f32>;
39var s_sprite: sampler;
40
41const M_PI_F: f32 = 3.1415926;
42const GRAYSCALE_FACTORS: vec3<f32> = vec3<f32>(0.2126, 0.7152, 0.0722);
43
44struct Bounds {
45 origin: vec2<f32>,
46 size: vec2<f32>,
47}
48
49struct Corners {
50 top_left: f32,
51 top_right: f32,
52 bottom_right: f32,
53 bottom_left: f32,
54}
55
56struct Edges {
57 top: f32,
58 right: f32,
59 bottom: f32,
60 left: f32,
61}
62
63struct Hsla {
64 h: f32,
65 s: f32,
66 l: f32,
67 a: f32,
68}
69
70struct LinearColorStop {
71 color: Hsla,
72 percentage: f32,
73}
74
75struct Background {
76 // 0u is Solid
77 // 1u is LinearGradient
78 // 2u is PatternSlash
79 tag: u32,
80 // 0u is sRGB linear color
81 // 1u is Oklab color
82 color_space: u32,
83 solid: Hsla,
84 gradient_angle_or_pattern_height: f32,
85 colors: array<LinearColorStop, 2>,
86 pad: u32,
87}
88
89struct AtlasTextureId {
90 index: u32,
91 kind: u32,
92}
93
94struct AtlasBounds {
95 origin: vec2<i32>,
96 size: vec2<i32>,
97}
98
99struct AtlasTile {
100 texture_id: AtlasTextureId,
101 tile_id: u32,
102 padding: u32,
103 bounds: AtlasBounds,
104}
105
106struct TransformationMatrix {
107 rotation_scale: mat2x2<f32>,
108 translation: vec2<f32>,
109}
110
111fn to_device_position_impl(position: vec2<f32>) -> vec4<f32> {
112 let device_position = position / globals.viewport_size * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
113 return vec4<f32>(device_position, 0.0, 1.0);
114}
115
116fn to_device_position(unit_vertex: vec2<f32>, bounds: Bounds) -> vec4<f32> {
117 let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
118 return to_device_position_impl(position);
119}
120
121fn to_device_position_transformed(unit_vertex: vec2<f32>, bounds: Bounds, transform: TransformationMatrix) -> vec4<f32> {
122 let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
123 //Note: Rust side stores it as row-major, so transposing here
124 let transformed = transpose(transform.rotation_scale) * position + transform.translation;
125 return to_device_position_impl(transformed);
126}
127
128fn to_tile_position(unit_vertex: vec2<f32>, tile: AtlasTile) -> vec2<f32> {
129 let atlas_size = vec2<f32>(textureDimensions(t_sprite, 0));
130 return (vec2<f32>(tile.bounds.origin) + unit_vertex * vec2<f32>(tile.bounds.size)) / atlas_size;
131}
132
133fn distance_from_clip_rect_impl(position: vec2<f32>, clip_bounds: Bounds) -> vec4<f32> {
134 let tl = position - clip_bounds.origin;
135 let br = clip_bounds.origin + clip_bounds.size - position;
136 return vec4<f32>(tl.x, br.x, tl.y, br.y);
137}
138
139fn distance_from_clip_rect(unit_vertex: vec2<f32>, bounds: Bounds, clip_bounds: Bounds) -> vec4<f32> {
140 let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
141 return distance_from_clip_rect_impl(position, clip_bounds);
142}
143
144// https://gamedev.stackexchange.com/questions/92015/optimized-linear-to-srgb-glsl
145fn srgb_to_linear(srgb: vec3<f32>) -> vec3<f32> {
146 let cutoff = srgb < vec3<f32>(0.04045);
147 let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
148 let lower = srgb / vec3<f32>(12.92);
149 return select(higher, lower, cutoff);
150}
151
152fn linear_to_srgb(linear: vec3<f32>) -> vec3<f32> {
153 let cutoff = linear < vec3<f32>(0.0031308);
154 let higher = vec3<f32>(1.055) * pow(linear, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
155 let lower = linear * vec3<f32>(12.92);
156 return select(higher, lower, cutoff);
157}
158
159/// Convert a linear color to sRGBA space.
160fn linear_to_srgba(color: vec4<f32>) -> vec4<f32> {
161 return vec4<f32>(linear_to_srgb(color.rgb), color.a);
162}
163
164/// Convert a sRGBA color to linear space.
165fn srgba_to_linear(color: vec4<f32>) -> vec4<f32> {
166 return vec4<f32>(srgb_to_linear(color.rgb), color.a);
167}
168
169/// Hsla to linear RGBA conversion.
170fn hsla_to_rgba(hsla: Hsla) -> vec4<f32> {
171 let h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
172 let s = hsla.s;
173 let l = hsla.l;
174 let a = hsla.a;
175
176 let c = (1.0 - abs(2.0 * l - 1.0)) * s;
177 let x = c * (1.0 - abs(h % 2.0 - 1.0));
178 let m = l - c / 2.0;
179 var color = vec3<f32>(m);
180
181 if (h >= 0.0 && h < 1.0) {
182 color.r += c;
183 color.g += x;
184 } else if (h >= 1.0 && h < 2.0) {
185 color.r += x;
186 color.g += c;
187 } else if (h >= 2.0 && h < 3.0) {
188 color.g += c;
189 color.b += x;
190 } else if (h >= 3.0 && h < 4.0) {
191 color.g += x;
192 color.b += c;
193 } else if (h >= 4.0 && h < 5.0) {
194 color.r += x;
195 color.b += c;
196 } else {
197 color.r += c;
198 color.b += x;
199 }
200
201 // Input colors are assumed to be in sRGB space,
202 // but blending and rendering needs to happen in linear space.
203 // The output will be converted to sRGB by either the target
204 // texture format or the swapchain color space.
205 let linear = srgb_to_linear(color);
206 return vec4<f32>(linear, a);
207}
208
209/// Convert a linear sRGB to Oklab space.
210/// Reference: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
211fn linear_srgb_to_oklab(color: vec4<f32>) -> vec4<f32> {
212 let l = 0.4122214708 * color.r + 0.5363325363 * color.g + 0.0514459929 * color.b;
213 let m = 0.2119034982 * color.r + 0.6806995451 * color.g + 0.1073969566 * color.b;
214 let s = 0.0883024619 * color.r + 0.2817188376 * color.g + 0.6299787005 * color.b;
215
216 let l_ = pow(l, 1.0 / 3.0);
217 let m_ = pow(m, 1.0 / 3.0);
218 let s_ = pow(s, 1.0 / 3.0);
219
220 return vec4<f32>(
221 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
222 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
223 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_,
224 color.a
225 );
226}
227
228/// Convert an Oklab color to linear sRGB space.
229fn oklab_to_linear_srgb(color: vec4<f32>) -> vec4<f32> {
230 let l_ = color.r + 0.3963377774 * color.g + 0.2158037573 * color.b;
231 let m_ = color.r - 0.1055613458 * color.g - 0.0638541728 * color.b;
232 let s_ = color.r - 0.0894841775 * color.g - 1.2914855480 * color.b;
233
234 let l = l_ * l_ * l_;
235 let m = m_ * m_ * m_;
236 let s = s_ * s_ * s_;
237
238 return vec4<f32>(
239 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
240 -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
241 -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
242 color.a
243 );
244}
245
246fn over(below: vec4<f32>, above: vec4<f32>) -> vec4<f32> {
247 let alpha = above.a + below.a * (1.0 - above.a);
248 let color = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
249 return vec4<f32>(color, alpha);
250}
251
252// A standard gaussian function, used for weighting samples
253fn gaussian(x: f32, sigma: f32) -> f32{
254 return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * M_PI_F) * sigma);
255}
256
257// This approximates the error function, needed for the gaussian integral
258fn erf(v: vec2<f32>) -> vec2<f32> {
259 let s = sign(v);
260 let a = abs(v);
261 let r1 = 1.0 + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a;
262 let r2 = r1 * r1;
263 return s - s / (r2 * r2);
264}
265
266fn blur_along_x(x: f32, y: f32, sigma: f32, corner: f32, half_size: vec2<f32>) -> f32 {
267 let delta = min(half_size.y - corner - abs(y), 0.0);
268 let curved = half_size.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
269 let integral = 0.5 + 0.5 * erf((x + vec2<f32>(-curved, curved)) * (sqrt(0.5) / sigma));
270 return integral.y - integral.x;
271}
272
273// Selects corner radius based on quadrant.
274fn pick_corner_radius(center_to_point: vec2<f32>, radii: Corners) -> f32 {
275 if (center_to_point.x < 0.0) {
276 if (center_to_point.y < 0.0) {
277 return radii.top_left;
278 } else {
279 return radii.bottom_left;
280 }
281 } else {
282 if (center_to_point.y < 0.0) {
283 return radii.top_right;
284 } else {
285 return radii.bottom_right;
286 }
287 }
288}
289
290// Signed distance of the point to the quad's border - positive outside the
291// border, and negative inside.
292//
293// See comments on similar code using `quad_sdf_impl` in `fs_quad` for
294// explanation.
295fn quad_sdf(point: vec2<f32>, bounds: Bounds, corner_radii: Corners) -> f32 {
296 let half_size = bounds.size / 2.0;
297 let center = bounds.origin + half_size;
298 let center_to_point = point - center;
299 let corner_radius = pick_corner_radius(center_to_point, corner_radii);
300 let corner_to_point = abs(center_to_point) - half_size;
301 let corner_center_to_point = corner_to_point + corner_radius;
302 return quad_sdf_impl(corner_center_to_point, corner_radius);
303}
304
305fn quad_sdf_impl(corner_center_to_point: vec2<f32>, corner_radius: f32) -> f32 {
306 if (corner_radius == 0.0) {
307 // Fast path for unrounded corners.
308 return max(corner_center_to_point.x, corner_center_to_point.y);
309 } else {
310 // Signed distance of the point from a quad that is inset by corner_radius.
311 // It is negative inside this quad, and positive outside.
312 let signed_distance_to_inset_quad =
313 // 0 inside the inset quad, and positive outside.
314 length(max(vec2<f32>(0.0), corner_center_to_point)) +
315 // 0 outside the inset quad, and negative inside.
316 min(0.0, max(corner_center_to_point.x, corner_center_to_point.y));
317
318 return signed_distance_to_inset_quad - corner_radius;
319 }
320}
321
322// Abstract away the final color transformation based on the
323// target alpha compositing mode.
324fn blend_color(color: vec4<f32>, alpha_factor: f32) -> vec4<f32> {
325 let alpha = color.a * alpha_factor;
326 let multiplier = select(1.0, alpha, globals.premultiplied_alpha != 0u);
327 return vec4<f32>(color.rgb * multiplier, alpha);
328}
329
330
331struct GradientColor {
332 solid: vec4<f32>,
333 color0: vec4<f32>,
334 color1: vec4<f32>,
335}
336
337fn prepare_gradient_color(tag: u32, color_space: u32,
338 solid: Hsla, colors: array<LinearColorStop, 2>) -> GradientColor {
339 var result = GradientColor();
340
341 if (tag == 0u || tag == 2u) {
342 result.solid = hsla_to_rgba(solid);
343 } else if (tag == 1u) {
344 // The hsla_to_rgba is returns a linear sRGB color
345 result.color0 = hsla_to_rgba(colors[0].color);
346 result.color1 = hsla_to_rgba(colors[1].color);
347
348 // Prepare color space in vertex for avoid conversion
349 // in fragment shader for performance reasons
350 if (color_space == 0u) {
351 // sRGB
352 result.color0 = linear_to_srgba(result.color0);
353 result.color1 = linear_to_srgba(result.color1);
354 } else if (color_space == 1u) {
355 // Oklab
356 result.color0 = linear_srgb_to_oklab(result.color0);
357 result.color1 = linear_srgb_to_oklab(result.color1);
358 }
359 }
360
361 return result;
362}
363
364fn gradient_color(background: Background, position: vec2<f32>, bounds: Bounds,
365 solid_color: vec4<f32>, color0: vec4<f32>, color1: vec4<f32>) -> vec4<f32> {
366 var background_color = vec4<f32>(0.0);
367
368 switch (background.tag) {
369 default: {
370 return solid_color;
371 }
372 case 1u: {
373 // Linear gradient background.
374 // -90 degrees to match the CSS gradient angle.
375 let angle = background.gradient_angle_or_pattern_height;
376 let radians = (angle % 360.0 - 90.0) * M_PI_F / 180.0;
377 var direction = vec2<f32>(cos(radians), sin(radians));
378 let stop0_percentage = background.colors[0].percentage;
379 let stop1_percentage = background.colors[1].percentage;
380
381 // Expand the short side to be the same as the long side
382 if (bounds.size.x > bounds.size.y) {
383 direction.y *= bounds.size.y / bounds.size.x;
384 } else {
385 direction.x *= bounds.size.x / bounds.size.y;
386 }
387
388 // Get the t value for the linear gradient with the color stop percentages.
389 let half_size = bounds.size / 2.0;
390 let center = bounds.origin + half_size;
391 let center_to_point = position - center;
392 var t = dot(center_to_point, direction) / length(direction);
393 // Check the direct to determine the use x or y
394 if (abs(direction.x) > abs(direction.y)) {
395 t = (t + half_size.x) / bounds.size.x;
396 } else {
397 t = (t + half_size.y) / bounds.size.y;
398 }
399
400 // Adjust t based on the stop percentages
401 t = (t - stop0_percentage) / (stop1_percentage - stop0_percentage);
402 t = clamp(t, 0.0, 1.0);
403
404 switch (background.color_space) {
405 default: {
406 background_color = srgba_to_linear(mix(color0, color1, t));
407 }
408 case 1u: {
409 let oklab_color = mix(color0, color1, t);
410 background_color = oklab_to_linear_srgb(oklab_color);
411 }
412 }
413 }
414 case 2u: {
415 let gradient_angle_or_pattern_height = background.gradient_angle_or_pattern_height;
416 let pattern_width = (gradient_angle_or_pattern_height / 65535.0f) / 255.0f;
417 let pattern_interval = (gradient_angle_or_pattern_height % 65535.0f) / 255.0f;
418 let pattern_height = pattern_width + pattern_interval;
419 let stripe_angle = M_PI_F / 4.0;
420 let pattern_period = pattern_height * sin(stripe_angle);
421 let rotation = mat2x2<f32>(
422 cos(stripe_angle), -sin(stripe_angle),
423 sin(stripe_angle), cos(stripe_angle)
424 );
425 let relative_position = position - bounds.origin;
426 let rotated_point = rotation * relative_position;
427 let pattern = rotated_point.x % pattern_period;
428 let distance = min(pattern, pattern_period - pattern) - pattern_period * (pattern_width / pattern_height) / 2.0f;
429 background_color = solid_color;
430 background_color.a *= saturate(0.5 - distance);
431 }
432 }
433
434 return background_color;
435}
436
437// --- quads --- //
438
439struct Quad {
440 order: u32,
441 border_style: u32,
442 bounds: Bounds,
443 content_mask: Bounds,
444 background: Background,
445 border_color: Hsla,
446 corner_radii: Corners,
447 border_widths: Edges,
448}
449var<storage, read> b_quads: array<Quad>;
450
451struct QuadVarying {
452 @builtin(position) position: vec4<f32>,
453 @location(0) @interpolate(flat) border_color: vec4<f32>,
454 @location(1) @interpolate(flat) quad_id: u32,
455 // TODO: use `clip_distance` once Naga supports it
456 @location(2) clip_distances: vec4<f32>,
457 @location(3) @interpolate(flat) background_solid: vec4<f32>,
458 @location(4) @interpolate(flat) background_color0: vec4<f32>,
459 @location(5) @interpolate(flat) background_color1: vec4<f32>,
460}
461
462@vertex
463fn vs_quad(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> QuadVarying {
464 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
465 let quad = b_quads[instance_id];
466
467 var out = QuadVarying();
468 out.position = to_device_position(unit_vertex, quad.bounds);
469
470 let gradient = prepare_gradient_color(
471 quad.background.tag,
472 quad.background.color_space,
473 quad.background.solid,
474 quad.background.colors
475 );
476 out.background_solid = gradient.solid;
477 out.background_color0 = gradient.color0;
478 out.background_color1 = gradient.color1;
479 out.border_color = hsla_to_rgba(quad.border_color);
480 out.quad_id = instance_id;
481 out.clip_distances = distance_from_clip_rect(unit_vertex, quad.bounds, quad.content_mask);
482 return out;
483}
484
485@fragment
486fn fs_quad(input: QuadVarying) -> @location(0) vec4<f32> {
487 // Alpha clip first, since we don't have `clip_distance`.
488 if (any(input.clip_distances < vec4<f32>(0.0))) {
489 return vec4<f32>(0.0);
490 }
491
492 let quad = b_quads[input.quad_id];
493
494 let background_color = gradient_color(quad.background, input.position.xy, quad.bounds,
495 input.background_solid, input.background_color0, input.background_color1);
496
497 let unrounded = quad.corner_radii.top_left == 0.0 &&
498 quad.corner_radii.bottom_left == 0.0 &&
499 quad.corner_radii.top_right == 0.0 &&
500 quad.corner_radii.bottom_right == 0.0;
501
502 // Fast path when the quad is not rounded and doesn't have any border
503 if (quad.border_widths.top == 0.0 &&
504 quad.border_widths.left == 0.0 &&
505 quad.border_widths.right == 0.0 &&
506 quad.border_widths.bottom == 0.0 &&
507 unrounded) {
508 return blend_color(background_color, 1.0);
509 }
510
511 let size = quad.bounds.size;
512 let half_size = size / 2.0;
513 let point = input.position.xy - quad.bounds.origin;
514 let center_to_point = point - half_size;
515
516 // Signed distance field threshold for inclusion of pixels. 0.5 is the
517 // minimum distance between the center of the pixel and the edge.
518 let antialias_threshold = 0.5;
519
520 // Radius of the nearest corner
521 let corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
522
523 // Width of the nearest borders
524 let border = vec2<f32>(
525 select(
526 quad.border_widths.right,
527 quad.border_widths.left,
528 center_to_point.x < 0.0),
529 select(
530 quad.border_widths.bottom,
531 quad.border_widths.top,
532 center_to_point.y < 0.0));
533
534 // 0-width borders are reduced so that `inner_sdf >= antialias_threshold`.
535 // The purpose of this is to not draw antialiasing pixels in this case.
536 let reduced_border =
537 vec2<f32>(select(border.x, -antialias_threshold, border.x == 0.0),
538 select(border.y, -antialias_threshold, border.y == 0.0));
539
540 // Vector from the corner of the quad bounds to the point, after mirroring
541 // the point into the bottom right quadrant. Both components are <= 0.
542 let corner_to_point = abs(center_to_point) - half_size;
543
544 // Vector from the point to the center of the rounded corner's circle, also
545 // mirrored into bottom right quadrant.
546 let corner_center_to_point = corner_to_point + corner_radius;
547
548 // Whether the nearest point on the border is rounded
549 let is_near_rounded_corner =
550 corner_center_to_point.x >= 0 &&
551 corner_center_to_point.y >= 0;
552
553 // Vector from straight border inner corner to point.
554 let straight_border_inner_corner_to_point = corner_to_point + reduced_border;
555
556 // Whether the point is beyond the inner edge of the straight border.
557 let is_beyond_inner_straight_border =
558 straight_border_inner_corner_to_point.x > 0 ||
559 straight_border_inner_corner_to_point.y > 0;
560
561 // Whether the point is far enough inside the quad, such that the pixels are
562 // not affected by the straight border.
563 let is_within_inner_straight_border =
564 straight_border_inner_corner_to_point.x < -antialias_threshold &&
565 straight_border_inner_corner_to_point.y < -antialias_threshold;
566
567 // Fast path for points that must be part of the background.
568 //
569 // This could be optimized further for large rounded corners by including
570 // points in an inscribed rectangle, or some other quick linear check.
571 // However, that might negatively impact performance in the case of
572 // reasonable sizes for rounded corners.
573 if (is_within_inner_straight_border && !is_near_rounded_corner) {
574 return blend_color(background_color, 1.0);
575 }
576
577 // Signed distance of the point to the outside edge of the quad's border. It
578 // is positive outside this edge, and negative inside.
579 let outer_sdf = quad_sdf_impl(corner_center_to_point, corner_radius);
580
581 // Approximate signed distance of the point to the inside edge of the quad's
582 // border. It is negative outside this edge (within the border), and
583 // positive inside.
584 //
585 // This is not always an accurate signed distance:
586 // * The rounded portions with varying border width use an approximation of
587 // nearest-point-on-ellipse.
588 // * When it is quickly known to be outside the edge, -1.0 is used.
589 var inner_sdf = 0.0;
590 if (corner_center_to_point.x <= 0 || corner_center_to_point.y <= 0) {
591 // Fast paths for straight borders.
592 inner_sdf = -max(straight_border_inner_corner_to_point.x,
593 straight_border_inner_corner_to_point.y);
594 } else if (is_beyond_inner_straight_border) {
595 // Fast path for points that must be outside the inner edge.
596 inner_sdf = -1.0;
597 } else if (reduced_border.x == reduced_border.y) {
598 // Fast path for circular inner edge.
599 inner_sdf = -(outer_sdf + reduced_border.x);
600 } else {
601 let ellipse_radii = max(vec2<f32>(0.0), corner_radius - reduced_border);
602 inner_sdf = quarter_ellipse_sdf(corner_center_to_point, ellipse_radii);
603 }
604
605 // Negative when inside the border
606 let border_sdf = max(inner_sdf, outer_sdf);
607
608 var color = background_color;
609 if (border_sdf < antialias_threshold) {
610 var border_color = input.border_color;
611
612 // Dashed border logic when border_style == 1
613 if (quad.border_style == 1) {
614 // Position along the perimeter in "dash space", where each dash
615 // period has length 1
616 var t = 0.0;
617
618 // Total number of dash periods, so that the dash spacing can be
619 // adjusted to evenly divide it
620 var max_t = 0.0;
621
622 // Border width is proportional to dash size. This is the behavior
623 // used by browsers, but also avoids dashes from different segments
624 // overlapping when dash size is smaller than the border width.
625 //
626 // Dash pattern: (2 * border width) dash, (1 * border width) gap
627 let dash_length_per_width = 2.0;
628 let dash_gap_per_width = 1.0;
629 let dash_period_per_width = dash_length_per_width + dash_gap_per_width;
630
631 // Since the dash size is determined by border width, the density of
632 // dashes varies. Multiplying a pixel distance by this returns a
633 // position in dash space - it has units (dash period / pixels). So
634 // a dash velocity of (1 / 10) is 1 dash every 10 pixels.
635 var dash_velocity = 0.0;
636
637 // Dividing this by the border width gives the dash velocity
638 let dv_numerator = 1.0 / dash_period_per_width;
639
640 if (unrounded) {
641 // When corners aren't rounded, the dashes are separately laid
642 // out on each straight line, rather than around the whole
643 // perimeter. This way each line starts and ends with a dash.
644 let is_horizontal =
645 corner_center_to_point.x <
646 corner_center_to_point.y;
647 let border_width = select(border.y, border.x, is_horizontal);
648 dash_velocity = dv_numerator / border_width;
649 t = select(point.y, point.x, is_horizontal) * dash_velocity;
650 max_t = select(size.y, size.x, is_horizontal) * dash_velocity;
651 } else {
652 // When corners are rounded, the dashes are laid out clockwise
653 // around the whole perimeter.
654
655 let r_tr = quad.corner_radii.top_right;
656 let r_br = quad.corner_radii.bottom_right;
657 let r_bl = quad.corner_radii.bottom_left;
658 let r_tl = quad.corner_radii.top_left;
659
660 let w_t = quad.border_widths.top;
661 let w_r = quad.border_widths.right;
662 let w_b = quad.border_widths.bottom;
663 let w_l = quad.border_widths.left;
664
665 // Straight side dash velocities
666 let dv_t = select(dv_numerator / w_t, 0.0, w_t <= 0.0);
667 let dv_r = select(dv_numerator / w_r, 0.0, w_r <= 0.0);
668 let dv_b = select(dv_numerator / w_b, 0.0, w_b <= 0.0);
669 let dv_l = select(dv_numerator / w_l, 0.0, w_l <= 0.0);
670
671 // Straight side lengths in dash space
672 let s_t = (size.x - r_tl - r_tr) * dv_t;
673 let s_r = (size.y - r_tr - r_br) * dv_r;
674 let s_b = (size.x - r_br - r_bl) * dv_b;
675 let s_l = (size.y - r_bl - r_tl) * dv_l;
676
677 let corner_dash_velocity_tr = corner_dash_velocity(dv_t, dv_r);
678 let corner_dash_velocity_br = corner_dash_velocity(dv_b, dv_r);
679 let corner_dash_velocity_bl = corner_dash_velocity(dv_b, dv_l);
680 let corner_dash_velocity_tl = corner_dash_velocity(dv_t, dv_l);
681
682 // Corner lengths in dash space
683 let c_tr = r_tr * (M_PI_F / 2.0) * corner_dash_velocity_tr;
684 let c_br = r_br * (M_PI_F / 2.0) * corner_dash_velocity_br;
685 let c_bl = r_bl * (M_PI_F / 2.0) * corner_dash_velocity_bl;
686 let c_tl = r_tl * (M_PI_F / 2.0) * corner_dash_velocity_tl;
687
688 // Cumulative dash space upto each segment
689 let upto_tr = s_t;
690 let upto_r = upto_tr + c_tr;
691 let upto_br = upto_r + s_r;
692 let upto_b = upto_br + c_br;
693 let upto_bl = upto_b + s_b;
694 let upto_l = upto_bl + c_bl;
695 let upto_tl = upto_l + s_l;
696 max_t = upto_tl + c_tl;
697
698 if (is_near_rounded_corner) {
699 let radians = atan2(corner_center_to_point.y,
700 corner_center_to_point.x);
701 let corner_t = radians * corner_radius;
702
703 if (center_to_point.x >= 0.0) {
704 if (center_to_point.y < 0.0) {
705 dash_velocity = corner_dash_velocity_tr;
706 // Subtracted because radians is pi/2 to 0 when
707 // going clockwise around the top right corner,
708 // since the y axis has been flipped
709 t = upto_r - corner_t * dash_velocity;
710 } else {
711 dash_velocity = corner_dash_velocity_br;
712 // Added because radians is 0 to pi/2 when going
713 // clockwise around the bottom-right corner
714 t = upto_br + corner_t * dash_velocity;
715 }
716 } else {
717 if (center_to_point.y >= 0.0) {
718 dash_velocity = corner_dash_velocity_bl;
719 // Subtracted because radians is pi/2 to 0 when
720 // going clockwise around the bottom-left corner,
721 // since the x axis has been flipped
722 t = upto_l - corner_t * dash_velocity;
723 } else {
724 dash_velocity = corner_dash_velocity_tl;
725 // Added because radians is 0 to pi/2 when going
726 // clockwise around the top-left corner, since both
727 // axis were flipped
728 t = upto_tl + corner_t * dash_velocity;
729 }
730 }
731 } else {
732 // Straight borders
733 let is_horizontal =
734 corner_center_to_point.x <
735 corner_center_to_point.y;
736 if (is_horizontal) {
737 if (center_to_point.y < 0.0) {
738 dash_velocity = dv_t;
739 t = (point.x - r_tl) * dash_velocity;
740 } else {
741 dash_velocity = dv_b;
742 t = upto_bl - (point.x - r_bl) * dash_velocity;
743 }
744 } else {
745 if (center_to_point.x < 0.0) {
746 dash_velocity = dv_l;
747 t = upto_tl - (point.y - r_tl) * dash_velocity;
748 } else {
749 dash_velocity = dv_r;
750 t = upto_r + (point.y - r_tr) * dash_velocity;
751 }
752 }
753 }
754 }
755
756 let dash_length = dash_length_per_width / dash_period_per_width;
757 let desired_dash_gap = dash_gap_per_width / dash_period_per_width;
758
759 // Straight borders should start and end with a dash, so max_t is
760 // reduced to cause this.
761 max_t -= select(0.0, dash_length, unrounded);
762 if (max_t >= 1.0) {
763 // Adjust dash gap to evenly divide max_t.
764 let dash_count = floor(max_t);
765 let dash_period = max_t / dash_count;
766 border_color.a *= dash_alpha(
767 t,
768 dash_period,
769 dash_length,
770 dash_velocity,
771 antialias_threshold);
772 } else if (unrounded) {
773 // When there isn't enough space for the full gap between the
774 // two start / end dashes of a straight border, reduce gap to
775 // make them fit.
776 let dash_gap = max_t - dash_length;
777 if (dash_gap > 0.0) {
778 let dash_period = dash_length + dash_gap;
779 border_color.a *= dash_alpha(
780 t,
781 dash_period,
782 dash_length,
783 dash_velocity,
784 antialias_threshold);
785 }
786 }
787 }
788
789 // Blend the border on top of the background and then linearly interpolate
790 // between the two as we slide inside the background.
791 let blended_border = over(background_color, border_color);
792 color = mix(background_color, blended_border,
793 saturate(antialias_threshold - inner_sdf));
794 }
795
796 return blend_color(color, saturate(antialias_threshold - outer_sdf));
797}
798
799// Returns the dash velocity of a corner given the dash velocity of the two
800// sides, by returning the slower velocity (larger dashes).
801//
802// Since 0 is used for dash velocity when the border width is 0 (instead of
803// +inf), this returns the other dash velocity in that case.
804//
805// An alternative to this might be to appropriately interpolate the dash
806// velocity around the corner, but that seems overcomplicated.
807fn corner_dash_velocity(dv1: f32, dv2: f32) -> f32 {
808 if (dv1 == 0.0) {
809 return dv2;
810 } else if (dv2 == 0.0) {
811 return dv1;
812 } else {
813 return min(dv1, dv2);
814 }
815}
816
817// Returns alpha used to render antialiased dashes.
818// `t` is within the dash when `fmod(t, period) < length`.
819fn dash_alpha(t: f32, period: f32, length: f32, dash_velocity: f32, antialias_threshold: f32) -> f32 {
820 let half_period = period / 2;
821 let half_length = length / 2;
822 // Value in [-half_period, half_period].
823 // The dash is in [-half_length, half_length].
824 let centered = fmod(t + half_period - half_length, period) - half_period;
825 // Signed distance for the dash, negative values are inside the dash.
826 let signed_distance = abs(centered) - half_length;
827 // Antialiased alpha based on the signed distance.
828 return saturate(antialias_threshold - signed_distance / dash_velocity);
829}
830
831// This approximates distance to the nearest point to a quarter ellipse in a way
832// that is sufficient for anti-aliasing when the ellipse is not very eccentric.
833// The components of `point` are expected to be positive.
834//
835// Negative on the outside and positive on the inside.
836fn quarter_ellipse_sdf(point: vec2<f32>, radii: vec2<f32>) -> f32 {
837 // Scale the space to treat the ellipse like a unit circle.
838 let circle_vec = point / radii;
839 let unit_circle_sdf = length(circle_vec) - 1.0;
840 // Approximate up-scaling of the length by using the average of the radii.
841 //
842 // TODO: A better solution would be to use the gradient of the implicit
843 // function for an ellipse to approximate a scaling factor.
844 return unit_circle_sdf * (radii.x + radii.y) * -0.5;
845}
846
847// Modulus that has the same sign as `a`.
848fn fmod(a: f32, b: f32) -> f32 {
849 return a - b * trunc(a / b);
850}
851
852// --- shadows --- //
853
854struct Shadow {
855 order: u32,
856 blur_radius: f32,
857 bounds: Bounds,
858 corner_radii: Corners,
859 content_mask: Bounds,
860 color: Hsla,
861}
862var<storage, read> b_shadows: array<Shadow>;
863
864struct ShadowVarying {
865 @builtin(position) position: vec4<f32>,
866 @location(0) @interpolate(flat) color: vec4<f32>,
867 @location(1) @interpolate(flat) shadow_id: u32,
868 //TODO: use `clip_distance` once Naga supports it
869 @location(3) clip_distances: vec4<f32>,
870}
871
872@vertex
873fn vs_shadow(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> ShadowVarying {
874 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
875 var shadow = b_shadows[instance_id];
876
877 let margin = 3.0 * shadow.blur_radius;
878 // Set the bounds of the shadow and adjust its size based on the shadow's
879 // spread radius to achieve the spreading effect
880 shadow.bounds.origin -= vec2<f32>(margin);
881 shadow.bounds.size += 2.0 * vec2<f32>(margin);
882
883 var out = ShadowVarying();
884 out.position = to_device_position(unit_vertex, shadow.bounds);
885 out.color = hsla_to_rgba(shadow.color);
886 out.shadow_id = instance_id;
887 out.clip_distances = distance_from_clip_rect(unit_vertex, shadow.bounds, shadow.content_mask);
888 return out;
889}
890
891@fragment
892fn fs_shadow(input: ShadowVarying) -> @location(0) vec4<f32> {
893 // Alpha clip first, since we don't have `clip_distance`.
894 if (any(input.clip_distances < vec4<f32>(0.0))) {
895 return vec4<f32>(0.0);
896 }
897
898 let shadow = b_shadows[input.shadow_id];
899 let half_size = shadow.bounds.size / 2.0;
900 let center = shadow.bounds.origin + half_size;
901 let center_to_point = input.position.xy - center;
902
903 let corner_radius = pick_corner_radius(center_to_point, shadow.corner_radii);
904
905 // The signal is only non-zero in a limited range, so don't waste samples
906 let low = center_to_point.y - half_size.y;
907 let high = center_to_point.y + half_size.y;
908 let start = clamp(-3.0 * shadow.blur_radius, low, high);
909 let end = clamp(3.0 * shadow.blur_radius, low, high);
910
911 // Accumulate samples (we can get away with surprisingly few samples)
912 let step = (end - start) / 4.0;
913 var y = start + step * 0.5;
914 var alpha = 0.0;
915 for (var i = 0; i < 4; i += 1) {
916 let blur = blur_along_x(center_to_point.x, center_to_point.y - y,
917 shadow.blur_radius, corner_radius, half_size);
918 alpha += blur * gaussian(y, shadow.blur_radius) * step;
919 y += step;
920 }
921
922 return blend_color(input.color, alpha);
923}
924
925// --- path rasterization --- //
926
927struct PathRasterizationVertex {
928 xy_position: vec2<f32>,
929 st_position: vec2<f32>,
930 color: Background,
931 bounds: Bounds,
932}
933
934var<storage, read> b_path_vertices: array<PathRasterizationVertex>;
935
936struct PathRasterizationVarying {
937 @builtin(position) position: vec4<f32>,
938 @location(0) st_position: vec2<f32>,
939 @location(1) vertex_id: u32,
940 //TODO: use `clip_distance` once Naga supports it
941 @location(3) clip_distances: vec4<f32>,
942}
943
944@vertex
945fn vs_path_rasterization(@builtin(vertex_index) vertex_id: u32) -> PathRasterizationVarying {
946 let v = b_path_vertices[vertex_id];
947
948 var out = PathRasterizationVarying();
949 out.position = to_device_position_impl(v.xy_position);
950 out.st_position = v.st_position;
951 out.vertex_id = vertex_id;
952 out.clip_distances = distance_from_clip_rect_impl(v.xy_position, v.bounds);
953 return out;
954}
955
956@fragment
957fn fs_path_rasterization(input: PathRasterizationVarying) -> @location(0) vec4<f32> {
958 let dx = dpdx(input.st_position);
959 let dy = dpdy(input.st_position);
960 if (any(input.clip_distances < vec4<f32>(0.0))) {
961 return vec4<f32>(0.0);
962 }
963
964 let v = b_path_vertices[input.vertex_id];
965 let background = v.color;
966 let bounds = v.bounds;
967
968 var alpha: f32;
969 if (length(vec2<f32>(dx.x, dy.x)) < 0.001) {
970 // If the gradient is too small, return a solid color.
971 alpha = 1.0;
972 } else {
973 let gradient = 2.0 * input.st_position.xx * vec2<f32>(dx.x, dy.x) - vec2<f32>(dx.y, dy.y);
974 let f = input.st_position.x * input.st_position.x - input.st_position.y;
975 let distance = f / length(gradient);
976 alpha = saturate(0.5 - distance);
977 }
978 let gradient_color = prepare_gradient_color(
979 background.tag,
980 background.color_space,
981 background.solid,
982 background.colors,
983 );
984 let color = gradient_color(background, input.position.xy, bounds,
985 gradient_color.solid, gradient_color.color0, gradient_color.color1);
986 return vec4<f32>(color.rgb * color.a * alpha, color.a * alpha);
987}
988
989// --- paths --- //
990
991struct PathSprite {
992 bounds: Bounds,
993}
994var<storage, read> b_path_sprites: array<PathSprite>;
995
996struct PathVarying {
997 @builtin(position) position: vec4<f32>,
998 @location(0) texture_coords: vec2<f32>,
999}
1000
1001@vertex
1002fn vs_path(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PathVarying {
1003 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
1004 let sprite = b_path_sprites[instance_id];
1005 // Don't apply content mask because it was already accounted for when rasterizing the path.
1006 let device_position = to_device_position(unit_vertex, sprite.bounds);
1007 // For screen-space intermediate texture, convert screen position to texture coordinates
1008 let screen_position = sprite.bounds.origin + unit_vertex * sprite.bounds.size;
1009 let texture_coords = screen_position / globals.viewport_size;
1010
1011 var out = PathVarying();
1012 out.position = device_position;
1013 out.texture_coords = texture_coords;
1014
1015 return out;
1016}
1017
1018@fragment
1019fn fs_path(input: PathVarying) -> @location(0) vec4<f32> {
1020 let sample = textureSample(t_sprite, s_sprite, input.texture_coords);
1021 return sample;
1022}
1023
1024// --- underlines --- //
1025
1026struct Underline {
1027 order: u32,
1028 pad: u32,
1029 bounds: Bounds,
1030 content_mask: Bounds,
1031 color: Hsla,
1032 thickness: f32,
1033 wavy: u32,
1034}
1035var<storage, read> b_underlines: array<Underline>;
1036
1037struct UnderlineVarying {
1038 @builtin(position) position: vec4<f32>,
1039 @location(0) @interpolate(flat) color: vec4<f32>,
1040 @location(1) @interpolate(flat) underline_id: u32,
1041 //TODO: use `clip_distance` once Naga supports it
1042 @location(3) clip_distances: vec4<f32>,
1043}
1044
1045@vertex
1046fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> UnderlineVarying {
1047 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
1048 let underline = b_underlines[instance_id];
1049
1050 var out = UnderlineVarying();
1051 out.position = to_device_position(unit_vertex, underline.bounds);
1052 out.color = hsla_to_rgba(underline.color);
1053 out.underline_id = instance_id;
1054 out.clip_distances = distance_from_clip_rect(unit_vertex, underline.bounds, underline.content_mask);
1055 return out;
1056}
1057
1058@fragment
1059fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
1060 const WAVE_FREQUENCY: f32 = 2.0;
1061 const WAVE_HEIGHT_RATIO: f32 = 0.8;
1062
1063 // Alpha clip first, since we don't have `clip_distance`.
1064 if (any(input.clip_distances < vec4<f32>(0.0))) {
1065 return vec4<f32>(0.0);
1066 }
1067
1068 let underline = b_underlines[input.underline_id];
1069 if ((underline.wavy & 0xFFu) == 0u)
1070 {
1071 return blend_color(input.color, input.color.a);
1072 }
1073
1074 let half_thickness = underline.thickness * 0.5;
1075
1076 let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
1077 let frequency = M_PI_F * WAVE_FREQUENCY * underline.thickness / underline.bounds.size.y;
1078 let amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.y;
1079
1080 let sine = sin(st.x * frequency) * amplitude;
1081 let dSine = cos(st.x * frequency) * amplitude * frequency;
1082 let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);
1083 let distance_in_pixels = distance * underline.bounds.size.y;
1084 let distance_from_top_border = distance_in_pixels - half_thickness;
1085 let distance_from_bottom_border = distance_in_pixels + half_thickness;
1086 let alpha = saturate(0.5 - max(-distance_from_bottom_border, distance_from_top_border));
1087 return blend_color(input.color, alpha * input.color.a);
1088}
1089
1090// --- monochrome sprites --- //
1091
1092struct MonochromeSprite {
1093 order: u32,
1094 pad: u32,
1095 bounds: Bounds,
1096 content_mask: Bounds,
1097 color: Hsla,
1098 tile: AtlasTile,
1099 transformation: TransformationMatrix,
1100}
1101var<storage, read> b_mono_sprites: array<MonochromeSprite>;
1102
1103struct MonoSpriteVarying {
1104 @builtin(position) position: vec4<f32>,
1105 @location(0) tile_position: vec2<f32>,
1106 @location(1) @interpolate(flat) color: vec4<f32>,
1107 @location(3) clip_distances: vec4<f32>,
1108}
1109
1110@vertex
1111fn vs_mono_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> MonoSpriteVarying {
1112 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
1113 let sprite = b_mono_sprites[instance_id];
1114
1115 var out = MonoSpriteVarying();
1116 out.position = to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation);
1117
1118 out.tile_position = to_tile_position(unit_vertex, sprite.tile);
1119 out.color = hsla_to_rgba(sprite.color);
1120 out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
1121 return out;
1122}
1123
1124@fragment
1125fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
1126 let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
1127 // Alpha clip after using the derivatives.
1128 if (any(input.clip_distances < vec4<f32>(0.0))) {
1129 return vec4<f32>(0.0);
1130 }
1131 return blend_color(input.color, sample);
1132}
1133
1134// --- polychrome sprites --- //
1135
1136struct PolychromeSprite {
1137 order: u32,
1138 pad: u32,
1139 grayscale: u32,
1140 opacity: f32,
1141 bounds: Bounds,
1142 content_mask: Bounds,
1143 corner_radii: Corners,
1144 tile: AtlasTile,
1145}
1146var<storage, read> b_poly_sprites: array<PolychromeSprite>;
1147
1148struct PolySpriteVarying {
1149 @builtin(position) position: vec4<f32>,
1150 @location(0) tile_position: vec2<f32>,
1151 @location(1) @interpolate(flat) sprite_id: u32,
1152 @location(3) clip_distances: vec4<f32>,
1153}
1154
1155@vertex
1156fn vs_poly_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PolySpriteVarying {
1157 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
1158 let sprite = b_poly_sprites[instance_id];
1159
1160 var out = PolySpriteVarying();
1161 out.position = to_device_position(unit_vertex, sprite.bounds);
1162 out.tile_position = to_tile_position(unit_vertex, sprite.tile);
1163 out.sprite_id = instance_id;
1164 out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
1165 return out;
1166}
1167
1168@fragment
1169fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
1170 let sample = textureSample(t_sprite, s_sprite, input.tile_position);
1171 // Alpha clip after using the derivatives.
1172 if (any(input.clip_distances < vec4<f32>(0.0))) {
1173 return vec4<f32>(0.0);
1174 }
1175
1176 let sprite = b_poly_sprites[input.sprite_id];
1177 let distance = quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
1178
1179 var color = sample;
1180 if ((sprite.grayscale & 0xFFu) != 0u) {
1181 let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
1182 color = vec4<f32>(vec3<f32>(grayscale), sample.a);
1183 }
1184 return blend_color(color, sprite.opacity * saturate(0.5 - distance));
1185}
1186
1187// --- surfaces --- //
1188
1189struct SurfaceParams {
1190 bounds: Bounds,
1191 content_mask: Bounds,
1192}
1193
1194var<uniform> surface_locals: SurfaceParams;
1195var t_y: texture_2d<f32>;
1196var t_cb_cr: texture_2d<f32>;
1197var s_surface: sampler;
1198
1199const ycbcr_to_RGB = mat4x4<f32>(
1200 vec4<f32>( 1.0000f, 1.0000f, 1.0000f, 0.0),
1201 vec4<f32>( 0.0000f, -0.3441f, 1.7720f, 0.0),
1202 vec4<f32>( 1.4020f, -0.7141f, 0.0000f, 0.0),
1203 vec4<f32>(-0.7010f, 0.5291f, -0.8860f, 1.0),
1204);
1205
1206struct SurfaceVarying {
1207 @builtin(position) position: vec4<f32>,
1208 @location(0) texture_position: vec2<f32>,
1209 @location(3) clip_distances: vec4<f32>,
1210}
1211
1212@vertex
1213fn vs_surface(@builtin(vertex_index) vertex_id: u32) -> SurfaceVarying {
1214 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
1215
1216 var out = SurfaceVarying();
1217 out.position = to_device_position(unit_vertex, surface_locals.bounds);
1218 out.texture_position = unit_vertex;
1219 out.clip_distances = distance_from_clip_rect(unit_vertex, surface_locals.bounds, surface_locals.content_mask);
1220 return out;
1221}
1222
1223@fragment
1224fn fs_surface(input: SurfaceVarying) -> @location(0) vec4<f32> {
1225 // Alpha clip after using the derivatives.
1226 if (any(input.clip_distances < vec4<f32>(0.0))) {
1227 return vec4<f32>(0.0);
1228 }
1229
1230 let y_cb_cr = vec4<f32>(
1231 textureSampleLevel(t_y, s_surface, input.texture_position, 0.0).r,
1232 textureSampleLevel(t_cb_cr, s_surface, input.texture_position, 0.0).rg,
1233 1.0);
1234
1235 return ycbcr_to_RGB * y_cb_cr;
1236}