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