shaders.wgsl

  1struct Globals {
  2    viewport_size: vec2<f32>,
  3    pad: vec2<u32>,
  4}
  5
  6var<uniform> globals: Globals;
  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 ViewId {
 14    lo: u32,
 15    hi: u32,
 16}
 17
 18struct Bounds {
 19    origin: vec2<f32>,
 20    size: vec2<f32>,
 21}
 22struct Corners {
 23    top_left: f32,
 24    top_right: f32,
 25    bottom_right: f32,
 26    bottom_left: f32,
 27}
 28struct Edges {
 29    top: f32,
 30    right: f32,
 31    bottom: f32,
 32    left: f32,
 33}
 34struct Hsla {
 35    h: f32,
 36    s: f32,
 37    l: f32,
 38    a: f32,
 39}
 40
 41struct AtlasTextureId {
 42    index: u32,
 43    kind: u32,
 44}
 45
 46struct AtlasBounds {
 47    origin: vec2<i32>,
 48    size: vec2<i32>,
 49}
 50struct AtlasTile {
 51    texture_id: AtlasTextureId,
 52    tile_id: u32,
 53    padding: u32,
 54    bounds: AtlasBounds,
 55}
 56
 57fn to_device_position_impl(position: vec2<f32>) -> vec4<f32> {
 58    let device_position = position / globals.viewport_size * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
 59    return vec4<f32>(device_position, 0.0, 1.0);
 60}
 61
 62fn to_device_position(unit_vertex: vec2<f32>, bounds: Bounds) -> vec4<f32> {
 63    let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
 64    return to_device_position_impl(position);
 65}
 66
 67fn to_tile_position(unit_vertex: vec2<f32>, tile: AtlasTile) -> vec2<f32> {
 68  let atlas_size = vec2<f32>(textureDimensions(t_sprite, 0));
 69  return (vec2<f32>(tile.bounds.origin) + unit_vertex * vec2<f32>(tile.bounds.size)) / atlas_size;
 70}
 71
 72fn distance_from_clip_rect_impl(position: vec2<f32>, clip_bounds: Bounds) -> vec4<f32> {
 73    let tl = position - clip_bounds.origin;
 74    let br = clip_bounds.origin + clip_bounds.size - position;
 75    return vec4<f32>(tl.x, br.x, tl.y, br.y);
 76}
 77
 78fn distance_from_clip_rect(unit_vertex: vec2<f32>, bounds: Bounds, clip_bounds: Bounds) -> vec4<f32> {
 79    let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
 80    return distance_from_clip_rect_impl(position, clip_bounds);
 81}
 82
 83fn hsla_to_rgba(hsla: Hsla) -> vec4<f32> {
 84    let h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
 85    let s = hsla.s;
 86    let l = hsla.l;
 87    let a = hsla.a;
 88
 89    let c = (1.0 - abs(2.0 * l - 1.0)) * s;
 90    let x = c * (1.0 - abs(h % 2.0 - 1.0));
 91    let m = l - c / 2.0;
 92
 93    var color = vec4<f32>(m, m, m, a);
 94
 95    if (h >= 0.0 && h < 1.0) {
 96        color.r += c;
 97        color.g += x;
 98    } else if (h >= 1.0 && h < 2.0) {
 99        color.r += x;
100        color.g += c;
101    } else if (h >= 2.0 && h < 3.0) {
102        color.g += c;
103        color.b += x;
104    } else if (h >= 3.0 && h < 4.0) {
105        color.g += x;
106        color.b += c;
107    } else if (h >= 4.0 && h < 5.0) {
108        color.r += x;
109        color.b += c;
110    } else {
111        color.r += c;
112        color.b += x;
113    }
114
115    return color;
116}
117
118fn over(below: vec4<f32>, above: vec4<f32>) -> vec4<f32> {
119    let alpha = above.a + below.a * (1.0 - above.a);
120    let color = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
121    return vec4<f32>(color, alpha);
122}
123
124// A standard gaussian function, used for weighting samples
125fn gaussian(x: f32, sigma: f32) -> f32{
126    return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * M_PI_F) * sigma);
127}
128
129// This approximates the error function, needed for the gaussian integral
130fn erf(v: vec2<f32>) -> vec2<f32> {
131    let s = sign(v);
132    let a = abs(v);
133    let r1 = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a;
134    let r2 = r1 * r1;
135    return s - s / (r2 * r2);
136}
137
138fn blur_along_x(x: f32, y: f32, sigma: f32, corner: f32, half_size: vec2<f32>) -> f32 {
139  let delta = min(half_size.y - corner - abs(y), 0.0);
140  let curved = half_size.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
141  let integral = 0.5 + 0.5 * erf((x + vec2<f32>(-curved, curved)) * (sqrt(0.5) / sigma));
142  return integral.y - integral.x;
143}
144
145fn pick_corner_radius(point: vec2<f32>, radii: Corners) -> f32 {
146    if (point.x < 0.0) {
147        if (point.y < 0.0) {
148            return radii.top_left;
149        } else {
150            return radii.bottom_left;
151        }
152    } else {
153        if (point.y < 0.0) {
154            return radii.top_right;
155        } else {
156            return radii.bottom_right;
157        }
158    }
159}
160
161fn quad_sdf(point: vec2<f32>, bounds: Bounds, corner_radii: Corners) -> f32 {
162    let half_size = bounds.size / 2.0;
163    let center = bounds.origin + half_size;
164    let center_to_point = point - center;
165    let corner_radius = pick_corner_radius(center_to_point, corner_radii);
166    let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
167    return length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
168        min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
169        corner_radius;
170}
171
172// --- quads --- //
173
174struct Quad {
175    view_id: ViewId,
176    layer_id: u32,
177    order: u32,
178    bounds: Bounds,
179    content_mask: Bounds,
180    background: Hsla,
181    border_color: Hsla,
182    corner_radii: Corners,
183    border_widths: Edges,
184}
185var<storage, read> b_quads: array<Quad>;
186
187struct QuadVarying {
188    @builtin(position) position: vec4<f32>,
189    @location(0) @interpolate(flat) background_color: vec4<f32>,
190    @location(1) @interpolate(flat) border_color: vec4<f32>,
191    @location(2) @interpolate(flat) quad_id: u32,
192    //TODO: use `clip_distance` once Naga supports it
193    @location(3) clip_distances: vec4<f32>,
194}
195
196@vertex
197fn vs_quad(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> QuadVarying {
198    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
199    let quad = b_quads[instance_id];
200
201    var out = QuadVarying();
202    out.position = to_device_position(unit_vertex, quad.bounds);
203    out.background_color = hsla_to_rgba(quad.background);
204    out.border_color = hsla_to_rgba(quad.border_color);
205    out.quad_id = instance_id;
206    out.clip_distances = distance_from_clip_rect(unit_vertex, quad.bounds, quad.content_mask);
207    return out;
208}
209
210@fragment
211fn fs_quad(input: QuadVarying) -> @location(0) vec4<f32> {
212    // Alpha clip first, since we don't have `clip_distance`.
213    if (any(input.clip_distances < vec4<f32>(0.0))) {
214        return vec4<f32>(0.0);
215    }
216
217    let quad = b_quads[input.quad_id];
218    let half_size = quad.bounds.size / 2.0;
219    let center = quad.bounds.origin + half_size;
220    let center_to_point = input.position.xy - center;
221
222    let corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
223
224    let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
225    let distance =
226      length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
227      min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
228      corner_radius;
229
230    let vertical_border = select(quad.border_widths.left, quad.border_widths.right, center_to_point.x > 0.0);
231    let horizontal_border = select(quad.border_widths.top, quad.border_widths.bottom, center_to_point.y > 0.0);
232    let inset_size = half_size - corner_radius - vec2<f32>(vertical_border, horizontal_border);
233    let point_to_inset_corner = abs(center_to_point) - inset_size;
234
235    var border_width = 0.0;
236    if (point_to_inset_corner.x < 0.0 && point_to_inset_corner.y < 0.0) {
237        border_width = 0.0;
238    } else if (point_to_inset_corner.y > point_to_inset_corner.x) {
239        border_width = horizontal_border;
240    } else {
241        border_width = vertical_border;
242    }
243
244    var color = input.background_color;
245    if (border_width > 0.0) {
246        let inset_distance = distance + border_width;
247        // Blend the border on top of the background and then linearly interpolate
248        // between the two as we slide inside the background.
249        let blended_border = over(input.background_color, input.border_color);
250        color = mix(blended_border, input.background_color,
251                    saturate(0.5 - inset_distance));
252    }
253
254    return color * vec4<f32>(1.0, 1.0, 1.0, saturate(0.5 - distance));
255}
256
257// --- shadows --- //
258
259struct Shadow {
260    view_id: ViewId,
261    layer_id: u32,
262    order: u32,
263    bounds: Bounds,
264    corner_radii: Corners,
265    content_mask: Bounds,
266    color: Hsla,
267    blur_radius: f32,
268    pad: u32,
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    view_id: ViewId,
413    layer_id: u32,
414    order: u32,
415    bounds: Bounds,
416    content_mask: Bounds,
417    color: Hsla,
418    thickness: f32,
419    wavy: u32,
420}
421var<storage, read> b_underlines: array<Underline>;
422
423struct UnderlineVarying {
424    @builtin(position) position: vec4<f32>,
425    @location(0) @interpolate(flat) color: vec4<f32>,
426    @location(1) @interpolate(flat) underline_id: u32,
427    //TODO: use `clip_distance` once Naga supports it
428    @location(3) clip_distances: vec4<f32>,
429}
430
431@vertex
432fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> UnderlineVarying {
433    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
434    let underline = b_underlines[instance_id];
435
436    var out = UnderlineVarying();
437    out.position = to_device_position(unit_vertex, underline.bounds);
438    out.color = hsla_to_rgba(underline.color);
439    out.underline_id = instance_id;
440    out.clip_distances = distance_from_clip_rect(unit_vertex, underline.bounds, underline.content_mask);
441    return out;
442}
443
444@fragment
445fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
446    // Alpha clip first, since we don't have `clip_distance`.
447    if (any(input.clip_distances < vec4<f32>(0.0))) {
448        return vec4<f32>(0.0);
449    }
450
451    let underline = b_underlines[input.underline_id];
452    if ((underline.wavy & 0xFFu) == 0u)
453    {
454        return vec4<f32>(0.0);
455    }
456
457    let half_thickness = underline.thickness * 0.5;
458    let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
459    let frequency = M_PI_F * 3.0 * underline.thickness / 8.0;
460    let amplitude = 1.0 / (2.0 * underline.thickness);
461    let sine = sin(st.x * frequency) * amplitude;
462    let dSine = cos(st.x * frequency) * amplitude * frequency;
463    let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);
464    let distance_in_pixels = distance * underline.bounds.size.y;
465    let distance_from_top_border = distance_in_pixels - half_thickness;
466    let distance_from_bottom_border = distance_in_pixels + half_thickness;
467    let alpha = saturate(0.5 - max(-distance_from_bottom_border, distance_from_top_border));
468    return input.color * vec4<f32>(1.0, 1.0, 1.0, alpha);
469}
470
471// --- monochrome sprites --- //
472
473struct MonochromeSprite {
474    view_id: ViewId,
475    layer_id: u32,
476    order: u32,
477    bounds: Bounds,
478    content_mask: Bounds,
479    color: Hsla,
480    tile: AtlasTile,
481}
482var<storage, read> b_mono_sprites: array<MonochromeSprite>;
483
484struct MonoSpriteVarying {
485    @builtin(position) position: vec4<f32>,
486    @location(0) tile_position: vec2<f32>,
487    @location(1) @interpolate(flat) color: vec4<f32>,
488    @location(3) clip_distances: vec4<f32>,
489}
490
491@vertex
492fn vs_mono_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> MonoSpriteVarying {
493    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
494    let sprite = b_mono_sprites[instance_id];
495
496    var out = MonoSpriteVarying();
497    out.position = to_device_position(unit_vertex, sprite.bounds);
498    out.tile_position = to_tile_position(unit_vertex, sprite.tile);
499    out.color = hsla_to_rgba(sprite.color);
500    out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
501    return out;
502}
503
504@fragment
505fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
506    let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
507    // Alpha clip after using the derivatives.
508    if (any(input.clip_distances < vec4<f32>(0.0))) {
509        return vec4<f32>(0.0);
510    }
511    return input.color * vec4<f32>(1.0, 1.0, 1.0, sample);
512}
513
514// --- polychrome sprites --- //
515
516struct PolychromeSprite {
517    view_id: ViewId,
518    layer_id: u32,
519    order: u32,
520    bounds: Bounds,
521    content_mask: Bounds,
522    corner_radii: Corners,
523    tile: AtlasTile,
524    grayscale: u32,
525    pad: u32,
526}
527var<storage, read> b_poly_sprites: array<PolychromeSprite>;
528
529struct PolySpriteVarying {
530    @builtin(position) position: vec4<f32>,
531    @location(0) tile_position: vec2<f32>,
532    @location(1) @interpolate(flat) sprite_id: u32,
533    @location(3) clip_distances: vec4<f32>,
534}
535
536@vertex
537fn vs_poly_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PolySpriteVarying {
538    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
539    let sprite = b_poly_sprites[instance_id];
540
541    var out = PolySpriteVarying();
542    out.position = to_device_position(unit_vertex, sprite.bounds);
543    out.tile_position = to_tile_position(unit_vertex, sprite.tile);
544    out.sprite_id = instance_id;
545    out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
546    return out;
547}
548
549@fragment
550fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
551    let sample = textureSample(t_sprite, s_sprite, input.tile_position);
552    // Alpha clip after using the derivatives.
553    if (any(input.clip_distances < vec4<f32>(0.0))) {
554        return vec4<f32>(0.0);
555    }
556
557    let sprite = b_poly_sprites[input.sprite_id];
558    let distance = quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
559
560    var color = sample;
561    if ((sprite.grayscale & 0xFFu) != 0u) {
562        let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
563        color = vec4<f32>(vec3<f32>(grayscale), sample.a);
564    }
565    color.a *= saturate(0.5 - distance);
566    return color;;
567}
568
569// --- surface sprites --- //