const M_PI_F: f32 = 3.1415926;
struct Globals {
viewport_size: vec2<f32>,
premultiplied_alpha: u32,
pad: u32,
}
struct Bounds {
origin: vec2<f32>,
size: vec2<f32>,
}
struct Hsla {
h: f32,
s: f32,
l: f32,
a: f32,
}
struct Underline {
order: u32,
pad: u32,
bounds: Bounds,
content_mask: Bounds,
color: Hsla,
thickness: f32,
wavy: u32,
}
struct UnderlineVarying {
@builtin(position) position: vec4<f32>,
@location(0) @interpolate(flat) color: vec4<f32>,
@location(1) @interpolate(flat) underline_id: u32,
@location(3) clip_distances: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(1) @binding(0) var<storage, read> b_underlines: array<Underline>;
fn to_device_position_impl(position: vec2<f32>) -> vec4<f32> {
let device_position = position / globals.viewport_size * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
return vec4<f32>(device_position, 0.0, 1.0);
}
fn to_device_position(unit_vertex: vec2<f32>, bounds: Bounds) -> vec4<f32> {
let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
return to_device_position_impl(position);
}
fn hsla_to_rgba(hsla: Hsla) -> vec4<f32> {
let h = hsla.h * 6.0;
let s = hsla.s;
let l = hsla.l;
let a = hsla.a;
let c = (1.0 - abs(2.0 * l - 1.0)) * s;
let x = c * (1.0 - abs(h % 2.0 - 1.0));
let m = l - c / 2.0;
var color = vec3<f32>(m);
if (h >= 0.0 && h < 1.0) {
color.r += c;
color.g += x;
} else if (h >= 1.0 && h < 2.0) {
color.r += x;
color.g += c;
} else if (h >= 2.0 && h < 3.0) {
color.g += c;
color.b += x;
} else if (h >= 3.0 && h < 4.0) {
color.g += x;
color.b += c;
} else if (h >= 4.0 && h < 5.0) {
color.r += x;
color.b += c;
} else {
color.r += c;
color.b += x;
}
return vec4<f32>(color, a);
}
fn distance_from_clip_rect_impl(position: vec2<f32>, clip_bounds: Bounds) -> vec4<f32> {
let tl = position - clip_bounds.origin;
let br = clip_bounds.origin + clip_bounds.size - position;
return vec4<f32>(tl.x, br.x, tl.y, br.y);
}
fn distance_from_clip_rect(unit_vertex: vec2<f32>, bounds: Bounds, clip_bounds: Bounds) -> vec4<f32> {
let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
return distance_from_clip_rect_impl(position, clip_bounds);
}
fn blend_color(color: vec4<f32>, alpha_factor: f32) -> vec4<f32> {
let alpha = color.a * alpha_factor;
let multiplier = select(1.0, alpha, globals.premultiplied_alpha != 0u);
return vec4<f32>(color.rgb * multiplier, alpha);
}
@vertex
fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> UnderlineVarying {
let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
let underline = b_underlines[instance_id];
var out = UnderlineVarying();
out.position = to_device_position(unit_vertex, underline.bounds);
out.color = hsla_to_rgba(underline.color);
out.underline_id = instance_id;
out.clip_distances = distance_from_clip_rect(unit_vertex, underline.bounds, underline.content_mask);
return out;
}
@fragment
fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
const WAVE_FREQUENCY: f32 = 2.0;
const WAVE_HEIGHT_RATIO: f32 = 0.8;
if (any(input.clip_distances < vec4<f32>(0.0))) {
return vec4<f32>(0.0);
}
let underline = b_underlines[input.underline_id];
if ((underline.wavy & 0xFFu) == 0u) {
return blend_color(input.color, input.color.a);
}
let half_thickness = underline.thickness * 0.5;
let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
let frequency = M_PI_F * WAVE_FREQUENCY * underline.thickness / underline.bounds.size.y;
let amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.y;
let sine = sin(st.x * frequency) * amplitude;
let dSine = cos(st.x * frequency) * amplitude * frequency;
let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);
let distance_in_pixels = distance * underline.bounds.size.y;
let distance_from_top_border = distance_in_pixels - half_thickness;
let distance_from_bottom_border = distance_in_pixels + half_thickness;
let alpha = saturate(0.5 - max(-distance_from_bottom_border, distance_from_top_border));
return blend_color(input.color, alpha * input.color.a);
}