1struct GlobalParams {
2 viewport_size: vec2<f32>,
3 pad: vec2<u32>,
4}
5
6var<uniform> globals: GlobalParams;
7var t_sprite: texture_2d<f32>;
8var s_sprite: sampler;
9
10const M_PI_F: f32 = 3.1415926;
11const GRAYSCALE_FACTORS: vec3<f32> = vec3<f32>(0.2126, 0.7152, 0.0722);
12
13struct Bounds {
14 origin: vec2<f32>,
15 size: vec2<f32>,
16}
17struct Corners {
18 top_left: f32,
19 top_right: f32,
20 bottom_right: f32,
21 bottom_left: f32,
22}
23struct Edges {
24 top: f32,
25 right: f32,
26 bottom: f32,
27 left: f32,
28}
29struct Hsla {
30 h: f32,
31 s: f32,
32 l: f32,
33 a: f32,
34}
35
36struct AtlasTextureId {
37 index: u32,
38 kind: u32,
39}
40
41struct AtlasBounds {
42 origin: vec2<i32>,
43 size: vec2<i32>,
44}
45struct AtlasTile {
46 texture_id: AtlasTextureId,
47 tile_id: u32,
48 padding: u32,
49 bounds: AtlasBounds,
50}
51
52fn to_device_position_impl(position: vec2<f32>) -> vec4<f32> {
53 let device_position = position / globals.viewport_size * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
54 return vec4<f32>(device_position, 0.0, 1.0);
55}
56
57fn to_device_position(unit_vertex: vec2<f32>, bounds: Bounds) -> vec4<f32> {
58 let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
59 return to_device_position_impl(position);
60}
61
62fn to_tile_position(unit_vertex: vec2<f32>, tile: AtlasTile) -> vec2<f32> {
63 let atlas_size = vec2<f32>(textureDimensions(t_sprite, 0));
64 return (vec2<f32>(tile.bounds.origin) + unit_vertex * vec2<f32>(tile.bounds.size)) / atlas_size;
65}
66
67fn distance_from_clip_rect_impl(position: vec2<f32>, clip_bounds: Bounds) -> vec4<f32> {
68 let tl = position - clip_bounds.origin;
69 let br = clip_bounds.origin + clip_bounds.size - position;
70 return vec4<f32>(tl.x, br.x, tl.y, br.y);
71}
72
73fn distance_from_clip_rect(unit_vertex: vec2<f32>, bounds: Bounds, clip_bounds: Bounds) -> vec4<f32> {
74 let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
75 return distance_from_clip_rect_impl(position, clip_bounds);
76}
77
78fn hsla_to_rgba(hsla: Hsla) -> vec4<f32> {
79 let h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
80 let s = hsla.s;
81 let l = hsla.l;
82 let a = hsla.a;
83
84 let c = (1.0 - abs(2.0 * l - 1.0)) * s;
85 let x = c * (1.0 - abs(h % 2.0 - 1.0));
86 let m = l - c / 2.0;
87
88 var color = vec4<f32>(m, m, m, a);
89
90 if (h >= 0.0 && h < 1.0) {
91 color.r += c;
92 color.g += x;
93 } else if (h >= 1.0 && h < 2.0) {
94 color.r += x;
95 color.g += c;
96 } else if (h >= 2.0 && h < 3.0) {
97 color.g += c;
98 color.b += x;
99 } else if (h >= 3.0 && h < 4.0) {
100 color.g += x;
101 color.b += c;
102 } else if (h >= 4.0 && h < 5.0) {
103 color.r += x;
104 color.b += c;
105 } else {
106 color.r += c;
107 color.b += x;
108 }
109
110 return color;
111}
112
113fn over(below: vec4<f32>, above: vec4<f32>) -> vec4<f32> {
114 let alpha = above.a + below.a * (1.0 - above.a);
115 let color = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
116 return vec4<f32>(color, alpha);
117}
118
119// A standard gaussian function, used for weighting samples
120fn gaussian(x: f32, sigma: f32) -> f32{
121 return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * M_PI_F) * sigma);
122}
123
124// This approximates the error function, needed for the gaussian integral
125fn erf(v: vec2<f32>) -> vec2<f32> {
126 let s = sign(v);
127 let a = abs(v);
128 let r1 = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a;
129 let r2 = r1 * r1;
130 return s - s / (r2 * r2);
131}
132
133fn blur_along_x(x: f32, y: f32, sigma: f32, corner: f32, half_size: vec2<f32>) -> f32 {
134 let delta = min(half_size.y - corner - abs(y), 0.0);
135 let curved = half_size.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
136 let integral = 0.5 + 0.5 * erf((x + vec2<f32>(-curved, curved)) * (sqrt(0.5) / sigma));
137 return integral.y - integral.x;
138}
139
140fn pick_corner_radius(point: vec2<f32>, radii: Corners) -> f32 {
141 if (point.x < 0.0) {
142 if (point.y < 0.0) {
143 return radii.top_left;
144 } else {
145 return radii.bottom_left;
146 }
147 } else {
148 if (point.y < 0.0) {
149 return radii.top_right;
150 } else {
151 return radii.bottom_right;
152 }
153 }
154}
155
156fn quad_sdf(point: vec2<f32>, bounds: Bounds, corner_radii: Corners) -> f32 {
157 let half_size = bounds.size / 2.0;
158 let center = bounds.origin + half_size;
159 let center_to_point = point - center;
160 let corner_radius = pick_corner_radius(center_to_point, corner_radii);
161 let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
162 return length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
163 min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
164 corner_radius;
165}
166
167// --- quads --- //
168
169struct Quad {
170 order: u32,
171 pad: u32,
172 bounds: Bounds,
173 content_mask: Bounds,
174 background: Hsla,
175 border_color: Hsla,
176 corner_radii: Corners,
177 border_widths: Edges,
178}
179var<storage, read> b_quads: array<Quad>;
180
181struct QuadVarying {
182 @builtin(position) position: vec4<f32>,
183 @location(0) @interpolate(flat) background_color: vec4<f32>,
184 @location(1) @interpolate(flat) border_color: vec4<f32>,
185 @location(2) @interpolate(flat) quad_id: u32,
186 //TODO: use `clip_distance` once Naga supports it
187 @location(3) clip_distances: vec4<f32>,
188}
189
190@vertex
191fn vs_quad(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> QuadVarying {
192 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
193 let quad = b_quads[instance_id];
194
195 var out = QuadVarying();
196 out.position = to_device_position(unit_vertex, quad.bounds);
197 out.background_color = hsla_to_rgba(quad.background);
198 out.border_color = hsla_to_rgba(quad.border_color);
199 out.quad_id = instance_id;
200 out.clip_distances = distance_from_clip_rect(unit_vertex, quad.bounds, quad.content_mask);
201 return out;
202}
203
204@fragment
205fn fs_quad(input: QuadVarying) -> @location(0) vec4<f32> {
206 // Alpha clip first, since we don't have `clip_distance`.
207 if (any(input.clip_distances < vec4<f32>(0.0))) {
208 return vec4<f32>(0.0);
209 }
210
211 let quad = b_quads[input.quad_id];
212 // Fast path when the quad is not rounded and doesn't have any border.
213 if (quad.corner_radii.top_left == 0.0 && quad.corner_radii.bottom_left == 0.0 &&
214 quad.corner_radii.top_right == 0.0 &&
215 quad.corner_radii.bottom_right == 0.0 && quad.border_widths.top == 0.0 &&
216 quad.border_widths.left == 0.0 && quad.border_widths.right == 0.0 &&
217 quad.border_widths.bottom == 0.0) {
218 return input.background_color;
219 }
220
221 let half_size = quad.bounds.size / 2.0;
222 let center = quad.bounds.origin + half_size;
223 let center_to_point = input.position.xy - center;
224
225 let corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
226
227 let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
228 let distance =
229 length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
230 min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
231 corner_radius;
232
233 let vertical_border = select(quad.border_widths.left, quad.border_widths.right, center_to_point.x > 0.0);
234 let horizontal_border = select(quad.border_widths.top, quad.border_widths.bottom, center_to_point.y > 0.0);
235 let inset_size = half_size - corner_radius - vec2<f32>(vertical_border, horizontal_border);
236 let point_to_inset_corner = abs(center_to_point) - inset_size;
237
238 var border_width = 0.0;
239 if (point_to_inset_corner.x < 0.0 && point_to_inset_corner.y < 0.0) {
240 border_width = 0.0;
241 } else if (point_to_inset_corner.y > point_to_inset_corner.x) {
242 border_width = horizontal_border;
243 } else {
244 border_width = vertical_border;
245 }
246
247 var color = input.background_color;
248 if (border_width > 0.0) {
249 let inset_distance = distance + border_width;
250 // Blend the border on top of the background and then linearly interpolate
251 // between the two as we slide inside the background.
252 let blended_border = over(input.background_color, input.border_color);
253 color = mix(blended_border, input.background_color,
254 saturate(0.5 - inset_distance));
255 }
256
257 return color * vec4<f32>(1.0, 1.0, 1.0, saturate(0.5 - distance));
258}
259
260// --- shadows --- //
261
262struct Shadow {
263 order: u32,
264 blur_radius: f32,
265 bounds: Bounds,
266 corner_radii: Corners,
267 content_mask: Bounds,
268 color: Hsla,
269}
270var<storage, read> b_shadows: array<Shadow>;
271
272struct ShadowVarying {
273 @builtin(position) position: vec4<f32>,
274 @location(0) @interpolate(flat) color: vec4<f32>,
275 @location(1) @interpolate(flat) shadow_id: u32,
276 //TODO: use `clip_distance` once Naga supports it
277 @location(3) clip_distances: vec4<f32>,
278}
279
280@vertex
281fn vs_shadow(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> ShadowVarying {
282 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
283 var shadow = b_shadows[instance_id];
284
285 let margin = 3.0 * shadow.blur_radius;
286 // Set the bounds of the shadow and adjust its size based on the shadow's
287 // spread radius to achieve the spreading effect
288 shadow.bounds.origin -= vec2<f32>(margin);
289 shadow.bounds.size += 2.0 * vec2<f32>(margin);
290
291 var out = ShadowVarying();
292 out.position = to_device_position(unit_vertex, shadow.bounds);
293 out.color = hsla_to_rgba(shadow.color);
294 out.shadow_id = instance_id;
295 out.clip_distances = distance_from_clip_rect(unit_vertex, shadow.bounds, shadow.content_mask);
296 return out;
297}
298
299@fragment
300fn fs_shadow(input: ShadowVarying) -> @location(0) vec4<f32> {
301 // Alpha clip first, since we don't have `clip_distance`.
302 if (any(input.clip_distances < vec4<f32>(0.0))) {
303 return vec4<f32>(0.0);
304 }
305
306 let shadow = b_shadows[input.shadow_id];
307 let half_size = shadow.bounds.size / 2.0;
308 let center = shadow.bounds.origin + half_size;
309 let center_to_point = input.position.xy - center;
310
311 let corner_radius = pick_corner_radius(center_to_point, shadow.corner_radii);
312
313 // The signal is only non-zero in a limited range, so don't waste samples
314 let low = center_to_point.y - half_size.y;
315 let high = center_to_point.y + half_size.y;
316 let start = clamp(-3.0 * shadow.blur_radius, low, high);
317 let end = clamp(3.0 * shadow.blur_radius, low, high);
318
319 // Accumulate samples (we can get away with surprisingly few samples)
320 let step = (end - start) / 4.0;
321 var y = start + step * 0.5;
322 var alpha = 0.0;
323 for (var i = 0; i < 4; i += 1) {
324 let blur = blur_along_x(center_to_point.x, center_to_point.y - y,
325 shadow.blur_radius, corner_radius, half_size);
326 alpha += blur * gaussian(y, shadow.blur_radius) * step;
327 y += step;
328 }
329
330 return input.color * vec4<f32>(1.0, 1.0, 1.0, alpha);
331}
332
333// --- path rasterization --- //
334
335struct PathVertex {
336 xy_position: vec2<f32>,
337 st_position: vec2<f32>,
338 content_mask: Bounds,
339}
340var<storage, read> b_path_vertices: array<PathVertex>;
341
342struct PathRasterizationVarying {
343 @builtin(position) position: vec4<f32>,
344 @location(0) st_position: vec2<f32>,
345 //TODO: use `clip_distance` once Naga supports it
346 @location(3) clip_distances: vec4<f32>,
347}
348
349@vertex
350fn vs_path_rasterization(@builtin(vertex_index) vertex_id: u32) -> PathRasterizationVarying {
351 let v = b_path_vertices[vertex_id];
352
353 var out = PathRasterizationVarying();
354 out.position = to_device_position_impl(v.xy_position);
355 out.st_position = v.st_position;
356 out.clip_distances = distance_from_clip_rect_impl(v.xy_position, v.content_mask);
357 return out;
358}
359
360@fragment
361fn fs_path_rasterization(input: PathRasterizationVarying) -> @location(0) f32 {
362 let dx = dpdx(input.st_position);
363 let dy = dpdy(input.st_position);
364 if (any(input.clip_distances < vec4<f32>(0.0))) {
365 return 0.0;
366 }
367
368 let gradient = 2.0 * input.st_position.xx * vec2<f32>(dx.x, dy.x) - vec2<f32>(dx.y, dy.y);
369 let f = input.st_position.x * input.st_position.x - input.st_position.y;
370 let distance = f / length(gradient);
371 return saturate(0.5 - distance);
372}
373
374// --- paths --- //
375
376struct PathSprite {
377 bounds: Bounds,
378 color: Hsla,
379 tile: AtlasTile,
380}
381var<storage, read> b_path_sprites: array<PathSprite>;
382
383struct PathVarying {
384 @builtin(position) position: vec4<f32>,
385 @location(0) tile_position: vec2<f32>,
386 @location(1) color: vec4<f32>,
387}
388
389@vertex
390fn vs_path(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PathVarying {
391 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
392 let sprite = b_path_sprites[instance_id];
393 // Don't apply content mask because it was already accounted for when rasterizing the path.
394
395 var out = PathVarying();
396 out.position = to_device_position(unit_vertex, sprite.bounds);
397 out.tile_position = to_tile_position(unit_vertex, sprite.tile);
398 out.color = hsla_to_rgba(sprite.color);
399 return out;
400}
401
402@fragment
403fn fs_path(input: PathVarying) -> @location(0) vec4<f32> {
404 let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
405 let mask = 1.0 - abs(1.0 - sample % 2.0);
406 return input.color * mask;
407}
408
409// --- underlines --- //
410
411struct Underline {
412 order: u32,
413 pad: u32,
414 bounds: Bounds,
415 content_mask: Bounds,
416 color: Hsla,
417 thickness: f32,
418 wavy: u32,
419}
420var<storage, read> b_underlines: array<Underline>;
421
422struct UnderlineVarying {
423 @builtin(position) position: vec4<f32>,
424 @location(0) @interpolate(flat) color: vec4<f32>,
425 @location(1) @interpolate(flat) underline_id: u32,
426 //TODO: use `clip_distance` once Naga supports it
427 @location(3) clip_distances: vec4<f32>,
428}
429
430@vertex
431fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> UnderlineVarying {
432 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
433 let underline = b_underlines[instance_id];
434
435 var out = UnderlineVarying();
436 out.position = to_device_position(unit_vertex, underline.bounds);
437 out.color = hsla_to_rgba(underline.color);
438 out.underline_id = instance_id;
439 out.clip_distances = distance_from_clip_rect(unit_vertex, underline.bounds, underline.content_mask);
440 return out;
441}
442
443@fragment
444fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
445 // Alpha clip first, since we don't have `clip_distance`.
446 if (any(input.clip_distances < vec4<f32>(0.0))) {
447 return vec4<f32>(0.0);
448 }
449
450 let underline = b_underlines[input.underline_id];
451 if ((underline.wavy & 0xFFu) == 0u)
452 {
453 return vec4<f32>(0.0);
454 }
455
456 let half_thickness = underline.thickness * 0.5;
457 let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
458 let frequency = M_PI_F * 3.0 * underline.thickness / 8.0;
459 let amplitude = 1.0 / (2.0 * underline.thickness);
460 let sine = sin(st.x * frequency) * amplitude;
461 let dSine = cos(st.x * frequency) * amplitude * frequency;
462 let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);
463 let distance_in_pixels = distance * underline.bounds.size.y;
464 let distance_from_top_border = distance_in_pixels - half_thickness;
465 let distance_from_bottom_border = distance_in_pixels + half_thickness;
466 let alpha = saturate(0.5 - max(-distance_from_bottom_border, distance_from_top_border));
467 return input.color * vec4<f32>(1.0, 1.0, 1.0, alpha);
468}
469
470// --- monochrome sprites --- //
471
472struct MonochromeSprite {
473 order: u32,
474 pad: u32,
475 bounds: Bounds,
476 content_mask: Bounds,
477 color: Hsla,
478 tile: AtlasTile,
479}
480var<storage, read> b_mono_sprites: array<MonochromeSprite>;
481
482struct MonoSpriteVarying {
483 @builtin(position) position: vec4<f32>,
484 @location(0) tile_position: vec2<f32>,
485 @location(1) @interpolate(flat) color: vec4<f32>,
486 @location(3) clip_distances: vec4<f32>,
487}
488
489@vertex
490fn vs_mono_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> MonoSpriteVarying {
491 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
492 let sprite = b_mono_sprites[instance_id];
493
494 var out = MonoSpriteVarying();
495 out.position = to_device_position(unit_vertex, sprite.bounds);
496 out.tile_position = to_tile_position(unit_vertex, sprite.tile);
497 out.color = hsla_to_rgba(sprite.color);
498 out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
499 return out;
500}
501
502@fragment
503fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
504 let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
505 // Alpha clip after using the derivatives.
506 if (any(input.clip_distances < vec4<f32>(0.0))) {
507 return vec4<f32>(0.0);
508 }
509 return input.color * vec4<f32>(1.0, 1.0, 1.0, sample);
510}
511
512// --- polychrome sprites --- //
513
514struct PolychromeSprite {
515 order: u32,
516 grayscale: u32,
517 bounds: Bounds,
518 content_mask: Bounds,
519 corner_radii: Corners,
520 tile: AtlasTile,
521}
522var<storage, read> b_poly_sprites: array<PolychromeSprite>;
523
524struct PolySpriteVarying {
525 @builtin(position) position: vec4<f32>,
526 @location(0) tile_position: vec2<f32>,
527 @location(1) @interpolate(flat) sprite_id: u32,
528 @location(3) clip_distances: vec4<f32>,
529}
530
531@vertex
532fn vs_poly_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PolySpriteVarying {
533 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
534 let sprite = b_poly_sprites[instance_id];
535
536 var out = PolySpriteVarying();
537 out.position = to_device_position(unit_vertex, sprite.bounds);
538 out.tile_position = to_tile_position(unit_vertex, sprite.tile);
539 out.sprite_id = instance_id;
540 out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
541 return out;
542}
543
544@fragment
545fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
546 let sample = textureSample(t_sprite, s_sprite, input.tile_position);
547 // Alpha clip after using the derivatives.
548 if (any(input.clip_distances < vec4<f32>(0.0))) {
549 return vec4<f32>(0.0);
550 }
551
552 let sprite = b_poly_sprites[input.sprite_id];
553 let distance = quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
554
555 var color = sample;
556 if ((sprite.grayscale & 0xFFu) != 0u) {
557 let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
558 color = vec4<f32>(vec3<f32>(grayscale), sample.a);
559 }
560 color.a *= saturate(0.5 - distance);
561 return color;
562}
563
564// --- surfaces --- //
565
566struct SurfaceParams {
567 bounds: Bounds,
568 content_mask: Bounds,
569}
570
571var<uniform> surface_locals: SurfaceParams;
572var t_y: texture_2d<f32>;
573var t_cb_cr: texture_2d<f32>;
574var s_surface: sampler;
575
576const ycbcr_to_RGB = mat4x4<f32>(
577 vec4<f32>( 1.0000f, 1.0000f, 1.0000f, 0.0),
578 vec4<f32>( 0.0000f, -0.3441f, 1.7720f, 0.0),
579 vec4<f32>( 1.4020f, -0.7141f, 0.0000f, 0.0),
580 vec4<f32>(-0.7010f, 0.5291f, -0.8860f, 1.0),
581);
582
583struct SurfaceVarying {
584 @builtin(position) position: vec4<f32>,
585 @location(0) texture_position: vec2<f32>,
586 @location(3) clip_distances: vec4<f32>,
587}
588
589@vertex
590fn vs_surface(@builtin(vertex_index) vertex_id: u32) -> SurfaceVarying {
591 let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
592
593 var out = SurfaceVarying();
594 out.position = to_device_position(unit_vertex, surface_locals.bounds);
595 out.texture_position = unit_vertex;
596 out.clip_distances = distance_from_clip_rect(unit_vertex, surface_locals.bounds, surface_locals.content_mask);
597 return out;
598}
599
600@fragment
601fn fs_surface(input: SurfaceVarying) -> @location(0) vec4<f32> {
602 // Alpha clip after using the derivatives.
603 if (any(input.clip_distances < vec4<f32>(0.0))) {
604 return vec4<f32>(0.0);
605 }
606
607 let y_cb_cr = vec4<f32>(
608 textureSampleLevel(t_y, s_surface, input.texture_position, 0.0).r,
609 textureSampleLevel(t_cb_cr, s_surface, input.texture_position, 0.0).rg,
610 1.0);
611
612 return ycbcr_to_RGB * y_cb_cr;
613}