use crate::{
render::{GlyphDrawCmd, TargetRect, ViewportUniformParams},
scissor::target_rects_overlap,
};
const MAX_HELD_DRAWS: usize = 256;
fn target_rect_union(a: TargetRect, b: TargetRect) -> TargetRect {
let left = a.0.min(b.0);
let top = a.1.min(b.1);
let right = (a.0 + a.2).max(b.0 + b.2);
let bottom = (a.1 + a.3).max(b.1 + b.3);
(left, top, right - left, bottom - top)
}
const HELD_CELL_SHIFT: u32 = 6;
pub(super) fn held_cells(rect: TargetRect) -> (std::ops::Range<usize>, u64) {
let (x, y, width, height) = rect;
let last_column = (x.saturating_add(width.max(1) - 1) >> HELD_CELL_SHIFT).min(63);
let first_column = (x >> HELD_CELL_SHIFT).min(63);
let columns = last_column - first_column + 1;
let mask = if columns >= 64 {
u64::MAX
} else {
((1_u64 << columns) - 1) << first_column
};
let first_row = (y >> HELD_CELL_SHIFT) as usize;
let last_row = (y.saturating_add(height.max(1) - 1) >> HELD_CELL_SHIFT) as usize;
(first_row..last_row + 1, mask)
}
#[derive(Default)]
struct LaneCells {
union: Option<TargetRect>,
cells: Vec<u64>,
rows: Vec<Vec<TargetRect>>,
}
impl LaneCells {
fn mark(&mut self, rect: TargetRect) {
self.union = Some(
self.union
.map_or(rect, |union| target_rect_union(union, rect)),
);
let (rows, mask) = held_cells(rect);
if self.cells.len() < rows.end {
self.cells.resize(rows.end, 0);
}
if self.rows.len() < rows.end {
self.rows.resize_with(rows.end, Vec::new);
}
for (cells, held) in self.cells[rows.clone()]
.iter_mut()
.zip(&mut self.rows[rows])
{
*cells |= mask;
held.push(rect);
}
}
fn is_empty(&self) -> bool {
self.union.is_none()
}
fn overlaps(&self, rect: TargetRect) -> bool {
if !self
.union
.is_some_and(|union| target_rects_overlap(union, rect))
{
return false;
}
let (rows, mask) = held_cells(rect);
let end = rows.end.min(self.cells.len());
let start = rows.start.min(end);
self.cells[start..end]
.iter()
.zip(&self.rows[start..end])
.any(|(cells, held)| {
cells & mask != 0 && held.iter().any(|held| target_rects_overlap(*held, rect))
})
}
fn clear(&mut self) {
for held in &mut self.rows[..self.cells.len()] {
held.clear();
}
self.cells.clear();
self.union = None;
}
}
pub(super) enum ShapeLane {
Chunk,
Behind(TargetRect),
Flush,
}
#[derive(PartialEq)]
pub(super) enum GlyphLane {
Front,
Behind,
Flush,
}
#[derive(Default)]
pub(crate) struct HeldDraws {
cmds: Option<std::ops::Range<usize>>,
images: Option<(std::ops::Range<usize>, ViewportUniformParams)>,
held: usize,
image_bounds: Vec<TargetRect>,
front: LaneCells,
shapes: LaneCells,
glyphs: LaneCells,
}
impl HeldDraws {
pub(super) fn hold(
&mut self,
cmds: std::ops::Range<usize>,
bounds: impl IntoIterator<Item = TargetRect>,
) {
self.cmds = Some(match self.cmds.take() {
Some(held) => held.start..cmds.end,
None => cmds,
});
for rect in bounds {
self.front.mark(rect);
self.held += 1;
}
}
pub(super) fn hold_image(
&mut self,
cmds: std::ops::Range<usize>,
viewport: ViewportUniformParams,
bounds: TargetRect,
) {
let cmds = match self.images.take() {
Some((held, _)) => held.start..cmds.end,
None => cmds,
};
self.images = Some((cmds, viewport));
self.image_bounds.push(bounds);
self.front.mark(bounds);
self.held += 1;
}
pub(super) fn hold_shapes(&mut self, reach: TargetRect) {
self.shapes.mark(reach);
self.held += 1;
}
pub(super) fn hold_behind(&mut self, cmds: &[GlyphDrawCmd]) {
for cmd in cmds {
self.glyphs.mark(cmd.bounds());
self.held += 1;
}
}
pub(super) fn is_empty(&self) -> bool {
self.front.is_empty() && self.shapes.is_empty() && self.glyphs.is_empty()
}
pub(super) fn overlaps(&self, rect: TargetRect) -> bool {
self.front.overlaps(rect) || self.shapes.overlaps(rect) || self.glyphs.overlaps(rect)
}
pub(super) fn place_shapes(&self, reach: Option<TargetRect>) -> ShapeLane {
let Some(reach) = reach.filter(|reach| self.overlaps(*reach)) else {
return ShapeLane::Chunk;
};
if self.full() || self.glyphs.overlaps(reach) {
ShapeLane::Flush
} else {
ShapeLane::Behind(reach)
}
}
pub(super) fn place_glyphs(&self, cmds: &[GlyphDrawCmd]) -> GlyphLane {
if self.full() {
return GlyphLane::Flush;
}
let behind = cmds.iter().any(|cmd| {
let rect = cmd.bounds();
self.covers_image(rect) || self.shapes.overlaps(rect) || self.glyphs.overlaps(rect)
});
match (behind, cmds.iter().all(GlyphDrawCmd::takes_shared_plain)) {
(false, _) => GlyphLane::Front,
(true, true) => GlyphLane::Behind,
(true, false) => GlyphLane::Flush,
}
}
pub(super) fn overlaps_behind(&self, rect: TargetRect) -> bool {
self.shapes.overlaps(rect) || self.glyphs.overlaps(rect)
}
pub(super) fn covers_image(&self, rect: TargetRect) -> bool {
self.image_bounds
.iter()
.any(|held| target_rects_overlap(*held, rect))
}
pub(super) fn image_viewport(&self) -> Option<ViewportUniformParams> {
self.images.as_ref().map(|(_, viewport)| *viewport)
}
pub(super) fn full(&self) -> bool {
self.held >= MAX_HELD_DRAWS
}
pub(super) fn has_behind(&self) -> bool {
!self.shapes.is_empty() || !self.glyphs.is_empty()
}
pub(super) fn take(&mut self) -> HeldCmds {
let behind = self.has_behind();
self.held = 0;
self.image_bounds.clear();
self.front.clear();
self.shapes.clear();
self.glyphs.clear();
HeldCmds {
glyphs: self.cmds.take(),
images: self.images.take(),
behind,
}
}
}
pub(super) struct HeldCmds {
pub(super) glyphs: Option<std::ops::Range<usize>>,
pub(super) images: Option<(std::ops::Range<usize>, ViewportUniformParams)>,
pub(super) behind: bool,
}