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