#![cfg_attr(not(any(feature = "wgpu", feature = "dx12")), allow(dead_code))]
use crate::{
app::glyph_engine::GLYPH_ATLAS_PADDING,
layout::{quad::ClipRect, render_list::GlyphCmd},
};
const TRANSPARENT_EDGE: f32 = (GLYPH_ATLAS_PADDING - 1) as f32;
const IDENTITY: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
pub(crate) fn same_clip(a: ClipRect, b: ClipRect) -> bool {
a == b
}
fn rounded(clip: ClipRect) -> bool {
clip.is_rounded()
}
fn texel_for_pixel(glyph: &GlyphCmd) -> bool {
let whole = |value: f32| value == (value as i32) as f32;
(glyph.shadow_sigma <= 0.0)
& whole(glyph.x)
& whole(glyph.y)
& (glyph.atlas_max[0] - glyph.atlas_min[0] == glyph.width)
& (glyph.atlas_max[1] - glyph.atlas_min[1] == glyph.height)
}
fn scissor_spares(glyph: &GlyphCmd, scissor: [f32; 4]) -> bool {
let reach = if texel_for_pixel(glyph) {
-TRANSPARENT_EDGE
} else if glyph.shadow_sigma > 0.0 {
(glyph.shadow_sigma * 2.0).ceil() + 1.0
} else {
0.0
};
glyph.transform == IDENTITY
&& glyph.x - reach >= scissor[0]
&& glyph.y - reach >= scissor[1]
&& glyph.x + glyph.width + reach <= scissor[2]
&& glyph.y + glyph.height + reach <= scissor[3]
}
fn clip_spares(glyphs: &[GlyphCmd], scissor: [f32; 4]) -> bool {
let mut plain = true;
let [mut left, mut top] = [f32::INFINITY; 2];
let [mut right, mut bottom] = [f32::NEG_INFINITY; 2];
for glyph in glyphs {
plain &= texel_for_pixel(glyph) & (glyph.transform == IDENTITY);
left = left.min(glyph.x);
top = top.min(glyph.y);
right = right.max(glyph.x + glyph.width);
bottom = bottom.max(glyph.y + glyph.height);
}
if plain {
return left + TRANSPARENT_EDGE >= scissor[0]
&& top + TRANSPARENT_EDGE >= scissor[1]
&& right - TRANSPARENT_EDGE <= scissor[2]
&& bottom - TRANSPARENT_EDGE <= scissor[3];
}
glyphs.iter().all(|glyph| scissor_spares(glyph, scissor))
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct GlyphRun {
pub start: usize,
pub count: usize,
pub spared: bool,
}
pub(crate) fn glyph_runs(
glyphs: &[GlyphCmd],
mut window_scissor: impl FnMut(ClipRect) -> Option<[f32; 4]>,
mut run: impl FnMut(usize, GlyphRun),
) {
let mut start = 0;
while start < glyphs.len() {
let order = glyphs[start].order;
let clip = glyphs[start].clip;
let mut end = start + 1;
while end < glyphs.len() && glyphs[end].order == order && same_clip(glyphs[end].clip, clip) {
end += 1;
}
let spared = !clip.active
|| (!rounded(clip) && window_scissor(clip).is_some_and(|scissor| clip_spares(&glyphs[start..end], scissor)));
run(
order,
GlyphRun {
start,
count: end - start,
spared,
},
);
start = end;
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct GlyphBatch {
pub count: usize,
pub joined: usize,
pub unclipped: bool,
}
pub(crate) fn batch_glyph_runs(
glyphs: &[GlyphCmd],
first: GlyphRun,
following: impl IntoIterator<Item = Option<GlyphRun>>,
in_window: bool,
) -> GlyphBatch {
let clip = glyphs[first.start].clip;
let unclipped = in_window && first.spared;
let mut batch = GlyphBatch {
count: first.count,
joined: 0,
unclipped,
};
for run in following {
let Some(next) = run else {
break;
};
let joins = next.start == first.start + batch.count
&& if unclipped {
next.spared
} else {
same_clip(glyphs[next.start].clip, clip)
};
if !joins {
break;
}
batch.count += next.count;
batch.joined += 1;
}
batch
}
#[cfg(test)]
mod tests {
use super::{GlyphBatch, GlyphRun, batch_glyph_runs, clip_spares, glyph_runs, scissor_spares};
use crate::{
layout::{quad::ClipRect, render_list::GlyphCmd},
node::border::BorderRadius,
};
fn glyph(order: usize, x: f32, clip: ClipRect) -> GlyphCmd {
GlyphCmd {
order,
x,
y: 10.0,
width: 8.0,
height: 8.0,
color: [1.0; 4],
atlas_min: [0.0; 2],
atlas_max: [8.0; 2],
transform: [1.0, 0.0, 0.0, 1.0],
transform_origin: [0.0; 2],
sharpness: 1.0,
color_glyph: false,
shadow_sigma: 0.0,
clip,
}
}
fn clip(width: f32) -> ClipRect {
ClipRect::rect(0.0, 0.0, width, 100.0)
}
fn glyphs() -> Vec<GlyphCmd> {
[(1, 100.0), (1, 100.0), (2, 100.0), (2, 100.0), (3, 50.0), (3, 50.0)]
.into_iter()
.enumerate()
.map(|(index, (order, width))| glyph(order, 10.0 + 10.0 * (index % 2) as f32, clip(width)))
.collect()
}
fn run(start: usize, spared: bool) -> GlyphRun {
GlyphRun {
start,
count: 2,
spared,
}
}
fn batch(count: usize, joined: usize, unclipped: bool) -> GlyphBatch {
GlyphBatch {
count,
joined,
unclipped,
}
}
fn scissor(clip: ClipRect) -> Option<[f32; 4]> {
Some([clip.x, clip.y, clip.x + clip.width, clip.y + clip.height])
}
fn runs(glyphs: &[GlyphCmd]) -> Vec<(usize, GlyphRun)> {
let mut runs = Vec::new();
glyph_runs(glyphs, scissor, |order, run| runs.push((order, run)));
runs
}
#[test]
fn glyphs_split_into_runs_by_order_and_clip() {
assert_eq!(
runs(&glyphs()),
[(1, run(0, true)), (2, run(2, true)), (3, run(4, true))]
);
assert_eq!(runs(&[]), []);
let mut glyphs = glyphs();
for glyph in &mut glyphs[..4] {
glyph.order = 7;
}
glyphs[2].clip = clip(60.0);
glyphs[3].clip = clip(60.0);
assert_eq!(runs(&glyphs), [(7, run(0, true)), (7, run(2, true)), (3, run(4, true))]);
}
#[test]
fn a_run_is_spared_when_its_clip_cuts_none_of_its_glyphs() {
let mut glyphs = glyphs();
glyphs[4].clip = clip(25.0);
glyphs[5].clip = clip(25.0);
assert_eq!(runs(&glyphs)[2], (3, run(4, false)));
let rounded = ClipRect::rounded_rect(
0.0,
0.0,
100.0,
100.0,
Some(BorderRadius {
top_left: 4.0,
top_right: 0.0,
bottom_right: 0.0,
bottom_left: 0.0,
}),
);
let mut glyphs = self::glyphs();
glyphs[0].clip = rounded;
glyphs[1].clip = rounded;
assert_eq!(runs(&glyphs)[0], (1, run(0, false)));
let mut none_left = Vec::new();
glyph_runs(&self::glyphs(), |_| None, |_, run| none_left.push(run.spared));
assert_eq!(none_left, [false, false, false]);
}
#[test]
fn a_run_without_a_clip_is_spared_whatever_its_glyphs() {
let mut glyphs = glyphs();
for glyph in &mut glyphs[..2] {
glyph.clip = ClipRect::default();
glyph.transform = [0.0, 1.0, -1.0, 0.0];
}
assert_eq!(runs(&glyphs)[0], (1, run(0, true)));
}
#[test]
fn runs_that_follow_under_the_same_clip_join_the_draw() {
let following = [Some(run(2, false))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, false), following, true),
batch(4, 1, false)
);
}
#[test]
fn a_run_under_another_clip_ends_a_clipped_draw() {
let following = [Some(run(2, false)), Some(run(4, false))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, false), following, true),
batch(4, 1, false)
);
let following = [Some(run(4, false))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(2, false), following, true),
batch(2, 0, false)
);
}
#[test]
fn runs_their_clips_spare_join_whatever_the_clips() {
let following = [Some(run(2, true)), Some(run(4, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, true), following, true),
batch(6, 2, true)
);
}
#[test]
fn in_a_layer_spared_runs_keep_their_clips() {
let following = [Some(run(2, true)), Some(run(4, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, true), following, false),
batch(4, 1, false)
);
}
#[test]
fn a_run_its_clip_cuts_ends_an_unclipped_draw() {
let following = [Some(run(2, false)), Some(run(4, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, true), following, true),
batch(2, 0, true)
);
let following = [Some(run(4, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(2, false), following, true),
batch(2, 0, false)
);
}
#[test]
fn another_draw_between_two_runs_ends_the_draw() {
let following = [None, Some(run(2, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, true), following, true),
batch(2, 0, true)
);
let following = [None, Some(run(2, false))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, false), following, true),
batch(2, 0, false)
);
}
#[test]
fn a_run_that_does_not_continue_the_instances_ends_the_draw() {
let following = [Some(run(4, true)), Some(run(2, true))];
assert_eq!(
batch_glyph_runs(&glyphs(), run(0, true), following, true),
batch(2, 0, true)
);
}
#[test]
fn a_run_alone_is_its_own_draw() {
assert_eq!(batch_glyph_runs(&glyphs(), run(4, false), [], true), batch(2, 0, false));
}
#[test]
fn a_scissor_spares_a_glyph_inside_it_and_cuts_one_across_its_edges() {
let mut glyph = glyphs()[0].clone();
glyph.x += 0.5;
assert!(scissor_spares(&glyph, [10.5, 10.0, 18.5, 18.0]));
assert!(!scissor_spares(&glyph, [11.0, 10.0, 18.5, 18.0]));
assert!(!scissor_spares(&glyph, [10.5, 11.0, 18.5, 18.0]));
assert!(!scissor_spares(&glyph, [10.5, 10.0, 18.0, 18.0]));
assert!(!scissor_spares(&glyph, [10.5, 10.0, 18.5, 17.0]));
}
#[test]
fn the_transparent_edge_of_a_glyph_drawn_texel_for_pixel_may_be_cut() {
let glyph = glyphs()[0].clone();
assert!(scissor_spares(&glyph, [11.0, 11.0, 17.0, 17.0]));
assert!(!scissor_spares(&glyph, [12.0, 11.0, 17.0, 17.0]));
assert!(!scissor_spares(&glyph, [11.0, 12.0, 17.0, 17.0]));
assert!(!scissor_spares(&glyph, [11.0, 11.0, 16.0, 17.0]));
assert!(!scissor_spares(&glyph, [11.0, 11.0, 17.0, 16.0]));
let mut scaled = glyph;
scaled.atlas_max = [16.0, 16.0];
assert!(scissor_spares(&scaled, [10.0, 10.0, 18.0, 18.0]));
assert!(!scissor_spares(&scaled, [11.0, 11.0, 17.0, 17.0]));
}
#[test]
fn a_run_is_cut_when_any_of_its_glyphs_is() {
let plain = &glyphs()[..2];
assert!(clip_spares(plain, [11.0, 11.0, 27.0, 17.0]));
assert!(!clip_spares(plain, [12.0, 11.0, 27.0, 17.0]));
assert!(!clip_spares(plain, [11.0, 11.0, 26.0, 17.0]));
let mut mixed = plain.to_vec();
mixed[1].x += 0.5;
assert!(clip_spares(&mixed, [11.0, 10.0, 28.5, 18.0]));
assert!(!clip_spares(&mixed, [11.0, 10.0, 28.0, 18.0]));
assert!(!clip_spares(&mixed, [12.0, 10.0, 28.5, 18.0]));
assert!(!clip_spares(&mixed, [11.0, 11.0, 28.5, 18.0]));
}
#[test]
fn a_shadow_needs_room_for_its_blur_and_a_transformed_glyph_is_never_spared() {
let mut shadow = glyphs()[0].clone();
shadow.shadow_sigma = 1.5;
assert!(scissor_spares(&shadow, [6.0, 6.0, 22.0, 22.0]));
assert!(!scissor_spares(&shadow, [7.0, 6.0, 22.0, 22.0]));
let mut turned = glyphs()[0].clone();
turned.transform = [0.0, 1.0, -1.0, 0.0];
assert!(!scissor_spares(&turned, [0.0, 0.0, 1000.0, 1000.0]));
let mut unplaced = glyphs()[0].clone();
unplaced.x = f32::NAN;
assert!(!scissor_spares(&unplaced, [0.0, 0.0, 1000.0, 1000.0]));
}
}