use crate::SegmentCount;
use super::{
BinHeader, Clip, ClipBbox, ClipBic, ClipElement, DrawBbox, DrawMonoid, Layout, LineSoup, Path,
PathBbox, PathMonoid, PathSegment, Tile,
};
use bytemuck::{Pod, Zeroable};
const TILE_WIDTH: u32 = 16;
const TILE_HEIGHT: u32 = 16;
pub(crate) const PATH_REDUCE_WG: u32 = 256;
const PATH_BBOX_WG: u32 = 256;
const FLATTEN_WG: u32 = 256;
const CLIP_REDUCE_WG: u32 = 256;
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
#[repr(C)]
pub struct BumpAllocators {
pub failed: u32,
pub binning: u32,
pub ptcl: u32,
pub tile: u32,
pub seg_counts: u32,
pub segments: u32,
pub blend: u32,
pub lines: u32,
}
#[derive(Default)]
pub struct BumpAllocatorMemory {
pub total: u32,
pub binning: BufferSize<u32>,
pub ptcl: BufferSize<u32>,
pub tile: BufferSize<Tile>,
pub seg_counts: BufferSize<SegmentCount>,
pub segments: BufferSize<PathSegment>,
pub lines: BufferSize<LineSoup>,
}
impl BumpAllocators {
pub fn memory(&self) -> BumpAllocatorMemory {
let binning = BufferSize::new(self.binning);
let ptcl = BufferSize::new(self.ptcl);
let tile = BufferSize::new(self.tile);
let seg_counts = BufferSize::new(self.seg_counts);
let segments = BufferSize::new(self.segments);
let lines = BufferSize::new(self.lines);
BumpAllocatorMemory {
total: binning.size_in_bytes()
+ ptcl.size_in_bytes()
+ tile.size_in_bytes()
+ seg_counts.size_in_bytes()
+ segments.size_in_bytes()
+ lines.size_in_bytes(),
binning,
ptcl,
tile,
seg_counts,
segments,
lines,
}
}
}
impl std::fmt::Display for BumpAllocatorMemory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"\n \
\tTotal:\t\t\t{} bytes ({:.2} KB | {:.2} MB)\n\
\tBinning\t\t\t{} elements ({} bytes)\n\
\tPTCL\t\t\t{} elements ({} bytes)\n\
\tTile:\t\t\t{} elements ({} bytes)\n\
\tSegment Counts:\t\t{} elements ({} bytes)\n\
\tSegments:\t\t{} elements ({} bytes)\n\
\tLines:\t\t\t{} elements ({} bytes)",
self.total,
self.total as f32 / (1 << 10) as f32,
self.total as f32 / (1 << 20) as f32,
self.binning.len(),
self.binning.size_in_bytes(),
self.ptcl.len(),
self.ptcl.size_in_bytes(),
self.tile.len(),
self.tile.size_in_bytes(),
self.seg_counts.len(),
self.seg_counts.size_in_bytes(),
self.segments.len(),
self.segments.size_in_bytes(),
self.lines.len(),
self.lines.size_in_bytes()
)
}
}
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
#[repr(C)]
pub struct IndirectCount {
pub count_x: u32,
pub count_y: u32,
pub count_z: u32,
pub pad0: u32,
}
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
#[repr(C)]
pub struct ConfigUniform {
pub width_in_tiles: u32,
pub height_in_tiles: u32,
pub target_width: u32,
pub target_height: u32,
pub base_color: u32,
pub layout: Layout,
pub lines_size: u32,
pub binning_size: u32,
pub tiles_size: u32,
pub seg_counts_size: u32,
pub segments_size: u32,
pub blend_size: u32,
pub ptcl_size: u32,
}
#[derive(Default)]
pub struct RenderConfig {
pub gpu: ConfigUniform,
pub workgroup_counts: WorkgroupCounts,
pub buffer_sizes: BufferSizes,
}
impl RenderConfig {
pub fn new(
layout: &Layout,
width: u32,
height: u32,
base_color: &peniko::Color,
budget: &BumpBudget,
) -> Self {
let new_width = width.next_multiple_of(TILE_WIDTH);
let new_height = height.next_multiple_of(TILE_HEIGHT);
let width_in_tiles = new_width / TILE_WIDTH;
let height_in_tiles = new_height / TILE_HEIGHT;
let n_path_tags = layout.path_tags_size();
let workgroup_counts =
WorkgroupCounts::new(layout, width_in_tiles, height_in_tiles, n_path_tags);
let buffer_sizes = BufferSizes::new(
layout,
&workgroup_counts,
width_in_tiles,
height_in_tiles,
budget,
);
Self {
gpu: ConfigUniform {
width_in_tiles,
height_in_tiles,
target_width: width,
target_height: height,
base_color: base_color.premultiply().to_rgba8().to_u32(),
lines_size: buffer_sizes.lines.len(),
binning_size: buffer_sizes.bin_data.len() - layout.bin_data_start,
tiles_size: buffer_sizes.tiles.len(),
seg_counts_size: buffer_sizes.seg_counts.len(),
segments_size: buffer_sizes.segments.len(),
blend_size: buffer_sizes.blend_spill.len(),
ptcl_size: buffer_sizes.ptcl.len(),
layout: *layout,
},
workgroup_counts,
buffer_sizes,
}
}
}
pub type WorkgroupSize = (u32, u32, u32);
#[derive(Copy, Clone, Debug, Default)]
pub struct WorkgroupCounts {
pub use_large_path_scan: bool,
pub path_reduce: WorkgroupSize,
pub path_reduce2: WorkgroupSize,
pub path_scan1: WorkgroupSize,
pub path_scan: WorkgroupSize,
pub bbox_clear: WorkgroupSize,
pub flatten: WorkgroupSize,
pub draw_reduce: WorkgroupSize,
pub draw_leaf: WorkgroupSize,
pub clip_reduce: WorkgroupSize,
pub clip_leaf: WorkgroupSize,
pub binning: WorkgroupSize,
pub tile_alloc: WorkgroupSize,
pub path_count_setup: WorkgroupSize,
pub backdrop: WorkgroupSize,
pub coarse: WorkgroupSize,
pub path_tiling_setup: WorkgroupSize,
pub fine: WorkgroupSize,
}
impl WorkgroupCounts {
pub fn new(
layout: &Layout,
width_in_tiles: u32,
height_in_tiles: u32,
n_path_tags: u32,
) -> Self {
let n_paths = layout.n_paths;
let n_draw_objects = layout.n_draw_objects;
let n_clips = layout.n_clips;
let path_tag_padded = align_up(n_path_tags, 4 * PATH_REDUCE_WG);
let path_tag_wgs = path_tag_padded / (4 * PATH_REDUCE_WG);
let use_large_path_scan = path_tag_wgs > PATH_REDUCE_WG;
let reduced_size = if use_large_path_scan {
align_up(path_tag_wgs, PATH_REDUCE_WG)
} else {
path_tag_wgs
};
let draw_object_wgs = n_draw_objects.div_ceil(PATH_BBOX_WG);
let draw_monoid_wgs = draw_object_wgs.min(PATH_BBOX_WG);
let flatten_wgs = n_path_tags.div_ceil(FLATTEN_WG);
let clip_reduce_wgs = n_clips.saturating_sub(1) / CLIP_REDUCE_WG;
let clip_wgs = n_clips.div_ceil(CLIP_REDUCE_WG);
let path_wgs = n_paths.div_ceil(PATH_BBOX_WG);
let width_in_bins = width_in_tiles.div_ceil(16);
let height_in_bins = height_in_tiles.div_ceil(16);
Self {
use_large_path_scan,
path_reduce: (path_tag_wgs, 1, 1),
path_reduce2: (PATH_REDUCE_WG, 1, 1),
path_scan1: (reduced_size / PATH_REDUCE_WG, 1, 1),
path_scan: (path_tag_wgs, 1, 1),
bbox_clear: (draw_object_wgs, 1, 1),
flatten: (flatten_wgs, 1, 1),
draw_reduce: (draw_monoid_wgs, 1, 1),
draw_leaf: (draw_monoid_wgs, 1, 1),
clip_reduce: (clip_reduce_wgs, 1, 1),
clip_leaf: (clip_wgs, 1, 1),
binning: (draw_object_wgs, 1, 1),
tile_alloc: (path_wgs, 1, 1),
path_count_setup: (1, 1, 1),
backdrop: (path_wgs, 1, 1),
coarse: (width_in_bins, height_in_bins, 1),
path_tiling_setup: (1, 1, 1),
fine: (width_in_tiles, height_in_tiles, 1),
}
}
}
#[derive(Copy, Clone, Eq, Default, Debug)]
pub struct BufferSize<T: Sized> {
len: u32,
_phantom: std::marker::PhantomData<T>,
}
impl<T: Sized> BufferSize<T> {
pub const fn new(len: u32) -> Self {
Self {
len: if len > 0 { len } else { 1 },
_phantom: std::marker::PhantomData,
}
}
pub const fn from_size_in_bytes(size: u32) -> Self {
Self::new(size / size_of::<T>() as u32)
}
#[expect(clippy::len_without_is_empty, reason = "The buffer can never be empty")]
pub const fn len(self) -> u32 {
self.len
}
pub const fn size_in_bytes(self) -> u32 {
size_of::<T>() as u32 * self.len
}
pub const fn aligned_in_bytes(self, alignment: u32) -> u32 {
align_up(self.size_in_bytes(), alignment)
}
}
impl<T: Sized> PartialEq for BufferSize<T> {
fn eq(&self, other: &Self) -> bool {
self.len == other.len
}
}
impl<T: Sized> PartialOrd for BufferSize<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.len.partial_cmp(&other.len)
}
}
#[derive(Copy, Clone, Debug, Default)]
pub struct BufferSizes {
pub path_reduced: BufferSize<PathMonoid>,
pub path_reduced2: BufferSize<PathMonoid>,
pub path_reduced_scan: BufferSize<PathMonoid>,
pub path_monoids: BufferSize<PathMonoid>,
pub path_bboxes: BufferSize<PathBbox>,
pub draw_reduced: BufferSize<DrawMonoid>,
pub draw_monoids: BufferSize<DrawMonoid>,
pub info: BufferSize<u32>,
pub clip_inps: BufferSize<Clip>,
pub clip_els: BufferSize<ClipElement>,
pub clip_bics: BufferSize<ClipBic>,
pub clip_bboxes: BufferSize<ClipBbox>,
pub draw_bboxes: BufferSize<DrawBbox>,
pub bump_alloc: BufferSize<BumpAllocators>,
pub indirect_count: BufferSize<IndirectCount>,
pub bin_headers: BufferSize<BinHeader>,
pub paths: BufferSize<Path>,
pub lines: BufferSize<LineSoup>,
pub bin_data: BufferSize<u32>,
pub tiles: BufferSize<Tile>,
pub seg_counts: BufferSize<SegmentCount>,
pub segments: BufferSize<PathSegment>,
pub blend_spill: BufferSize<u32>,
pub ptcl: BufferSize<u32>,
}
impl BufferSizes {
pub fn new(
layout: &Layout,
workgroups: &WorkgroupCounts,
width_in_tiles: u32,
height_in_tiles: u32,
budget: &BumpBudget,
) -> Self {
let n_paths = layout.n_paths;
let n_draw_objects = layout.n_draw_objects;
let n_clips = layout.n_clips;
let path_tag_wgs = workgroups.path_reduce.0;
let reduced_size = if workgroups.use_large_path_scan {
align_up(path_tag_wgs, PATH_REDUCE_WG)
} else {
path_tag_wgs
};
let path_reduced = BufferSize::new(reduced_size);
let path_reduced2 = BufferSize::new(PATH_REDUCE_WG);
let path_reduced_scan = BufferSize::new(reduced_size);
let path_monoids = BufferSize::new(path_tag_wgs * PATH_REDUCE_WG);
let path_bboxes = BufferSize::new(n_paths);
let binning_wgs = workgroups.binning.0;
let draw_monoid_wgs = workgroups.draw_reduce.0;
let draw_reduced = BufferSize::new(draw_monoid_wgs);
let draw_monoids = BufferSize::new(n_draw_objects);
let info = BufferSize::new(layout.bin_data_start);
let clip_inps = BufferSize::new(n_clips);
let clip_els = BufferSize::new(n_clips);
let clip_bics = BufferSize::new(n_clips / CLIP_REDUCE_WG);
let clip_bboxes = BufferSize::new(n_clips);
let draw_bboxes = BufferSize::new(n_paths);
let bump_alloc = BufferSize::new(1);
let indirect_count = BufferSize::new(1);
let bin_headers = BufferSize::new(binning_wgs * 256);
let n_paths_aligned = align_up(n_paths, 256);
let paths = BufferSize::new(n_paths_aligned);
let n_tiles = width_in_tiles.max(1) * height_in_tiles.max(1);
let floor = BumpBudget::floor_for_scene(n_tiles, layout.path_tags_size());
let bin_data = BufferSize::new(
layout
.bin_data_start
.saturating_add(budget.binning.max(floor.binning))
.min(budget.binding_capacity::<u32>())
.max(layout.bin_data_start.saturating_add(1)),
);
let tiles = BufferSize::new(
budget
.tiles
.max(floor.tiles)
.min(budget.binding_capacity::<Tile>()),
);
let lines = BufferSize::new(
budget
.lines
.max(floor.lines)
.min(budget.binding_capacity::<LineSoup>()),
);
let seg_counts = BufferSize::new(
budget
.seg_counts
.max(floor.seg_counts)
.min(budget.binding_capacity::<SegmentCount>()),
);
let segments = BufferSize::new(
budget
.segments
.max(floor.segments)
.max(seg_counts.len())
.min(budget.binding_capacity::<PathSegment>()),
);
let blend_spill = BufferSize::new(
budget
.blend_spill
.max(floor.blend_spill)
.min(budget.binding_capacity::<u32>()),
);
let ptcl = BufferSize::new(
n_tiles
.saturating_mul(PTCL_INITIAL_ALLOC)
.saturating_add(budget.ptcl.max(floor.ptcl))
.min(budget.binding_capacity::<u32>()),
);
Self {
path_reduced,
path_reduced2,
path_reduced_scan,
path_monoids,
path_bboxes,
draw_reduced,
draw_monoids,
info,
clip_inps,
clip_els,
clip_bics,
clip_bboxes,
draw_bboxes,
bump_alloc,
indirect_count,
lines,
bin_headers,
paths,
bin_data,
tiles,
seg_counts,
segments,
blend_spill,
ptcl,
}
}
}
const CORRUPT_COUNT: u32 = 1 << 31;
const PTCL_INITIAL_ALLOC: u32 = 64;
const FLOOR_LINES_PER_TILE: u32 = 16;
const FLOOR_LINES_MIN: u32 = 10 << 14;
const FLOOR_LINES_PER_PATH_TAG: u32 = 3;
const FLOOR_BINNING: u32 = 1 << 14;
const FLOOR_BLEND_SPILL: u32 = 1 << 14;
const FLOOR_TILES_PER_TILE: u32 = 16;
const FLOOR_TILES_MIN: u32 = 30 << 12;
const FLOOR_SEG_COUNTS_PER_TILE: u32 = 8;
const FLOOR_SEG_COUNTS_MIN: u32 = 10 << 14;
const FLOOR_PTCL_PER_TILE: u32 = 320;
const FLOOR_PTCL_MIN: u32 = 5 << 19;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct BumpBudget {
pub lines: u32,
pub binning: u32,
pub tiles: u32,
pub seg_counts: u32,
pub segments: u32,
pub blend_spill: u32,
pub ptcl: u32,
pub max_binding_bytes: u32,
}
impl BumpBudget {
pub const UPSTREAM: Self = Self {
lines: 1 << 21,
binning: 1 << 18,
tiles: 1 << 21,
seg_counts: 1 << 21,
segments: 1 << 21,
blend_spill: 1 << 20,
ptcl: 1 << 23,
max_binding_bytes: u32::MAX,
};
pub const INITIAL: Self = Self {
lines: 0,
binning: 0,
tiles: 0,
seg_counts: 0,
segments: 0,
blend_spill: 0,
ptcl: 0,
max_binding_bytes: u32::MAX,
};
pub fn floor_for_viewport(n_tiles: u32) -> Self {
Self::floor_for_scene(n_tiles, 0)
}
pub fn floor_for_scene(n_tiles: u32, n_path_tags: u32) -> Self {
Self {
lines: n_tiles
.saturating_mul(FLOOR_LINES_PER_TILE)
.max(FLOOR_LINES_MIN)
.max(n_path_tags.saturating_mul(FLOOR_LINES_PER_PATH_TAG)),
binning: FLOOR_BINNING,
tiles: n_tiles
.saturating_mul(FLOOR_TILES_PER_TILE)
.max(FLOOR_TILES_MIN),
seg_counts: n_tiles
.saturating_mul(FLOOR_SEG_COUNTS_PER_TILE)
.max(FLOOR_SEG_COUNTS_MIN),
segments: n_tiles
.saturating_mul(FLOOR_SEG_COUNTS_PER_TILE)
.max(FLOOR_SEG_COUNTS_MIN),
blend_spill: FLOOR_BLEND_SPILL,
ptcl: n_tiles
.saturating_mul(FLOOR_PTCL_PER_TILE)
.max(FLOOR_PTCL_MIN),
max_binding_bytes: u32::MAX,
}
}
#[must_use]
pub fn with_max_binding_bytes(self, bytes: u32) -> Self {
let mut budget = Self {
max_binding_bytes: bytes,
..self
};
budget.clamp_to_binding();
budget
}
pub fn binding_capacity<T>(&self) -> u32 {
self.max_binding_bytes / size_of::<T>() as u32
}
fn clamp_to_binding(&mut self) {
self.lines = self.lines.min(self.binding_capacity::<LineSoup>());
self.binning = self.binning.min(self.binding_capacity::<u32>());
self.tiles = self.tiles.min(self.binding_capacity::<Tile>());
self.seg_counts = self.seg_counts.min(self.binding_capacity::<SegmentCount>());
self.segments = self.segments.min(self.binding_capacity::<PathSegment>());
self.blend_spill = self.blend_spill.min(self.binding_capacity::<u32>());
self.ptcl = self.ptcl.min(self.binding_capacity::<u32>());
}
pub const CEILING: Self = Self {
lines: Self::UPSTREAM.lines << 2,
binning: Self::UPSTREAM.binning << 2,
tiles: Self::UPSTREAM.tiles << 2,
seg_counts: Self::UPSTREAM.seg_counts << 2,
segments: Self::UPSTREAM.segments << 2,
blend_spill: Self::UPSTREAM.blend_spill << 2,
ptcl: Self::UPSTREAM.ptcl << 2,
max_binding_bytes: u32::MAX,
};
pub fn observe(&mut self, used: &BumpAllocators) -> bool {
self.observe_within(used, &Self::CEILING)
}
pub fn observe_within(&mut self, used: &BumpAllocators, ceiling: &Self) -> bool {
let before = *self;
let grow = |field: &mut u32, used: u32, ceiling: u32| {
if used < CORRUPT_COUNT {
*field = (*field).max(used.saturating_mul(2).min(ceiling));
}
};
grow(&mut self.lines, used.lines, ceiling.lines);
grow(&mut self.binning, used.binning, ceiling.binning);
grow(&mut self.tiles, used.tile, ceiling.tiles);
grow(&mut self.seg_counts, used.seg_counts, ceiling.seg_counts);
grow(
&mut self.segments,
used.segments.max(used.seg_counts),
ceiling.segments,
);
grow(&mut self.blend_spill, used.blend, ceiling.blend_spill);
grow(&mut self.ptcl, used.ptcl, ceiling.ptcl);
self.clamp_to_binding();
*self != before
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "each field is scaled down from a u32 by a factor below one"
)]
pub fn fit_total_bytes(&mut self, max_bytes: u64) {
let size = self.size_in_bytes();
if size <= max_bytes {
return;
}
let scale = max_bytes as f64 / size as f64;
for field in [
&mut self.lines,
&mut self.binning,
&mut self.tiles,
&mut self.seg_counts,
&mut self.segments,
&mut self.blend_spill,
&mut self.ptcl,
] {
*field = (f64::from(*field) * scale) as u32;
}
}
pub fn ceiling_for_viewport(n_tiles: u32) -> Self {
let floor = Self::floor_for_viewport(n_tiles);
let scaled =
|ceiling: u32, floor: u32| ceiling.max(floor.saturating_mul(CEILING_OVER_FLOOR));
Self {
lines: scaled(Self::CEILING.lines, floor.lines),
binning: scaled(Self::CEILING.binning, floor.binning),
tiles: scaled(Self::CEILING.tiles, floor.tiles),
seg_counts: scaled(Self::CEILING.seg_counts, floor.seg_counts),
segments: scaled(Self::CEILING.segments, floor.segments),
blend_spill: Self::CEILING.blend_spill.max(
n_tiles.saturating_mul(BLEND_SPILL_WORDS_PER_LEVEL * CEILING_BLEND_SPILL_LEVELS),
),
ptcl: scaled(Self::CEILING.ptcl, floor.ptcl),
max_binding_bytes: u32::MAX,
}
}
pub fn size_in_bytes(&self) -> u64 {
u64::from(self.lines) * size_of::<LineSoup>() as u64
+ u64::from(self.binning) * 4
+ u64::from(self.tiles) * size_of::<Tile>() as u64
+ u64::from(self.seg_counts) * size_of::<SegmentCount>() as u64
+ u64::from(self.segments) * size_of::<PathSegment>() as u64
+ u64::from(self.blend_spill) * 4
+ u64::from(self.ptcl) * 4
}
}
impl Default for BumpBudget {
fn default() -> Self {
Self::INITIAL
}
}
const CEILING_OVER_FLOOR: u32 = 4;
const BLEND_SPILL_WORDS_PER_LEVEL: u32 = TILE_WIDTH * TILE_HEIGHT;
const CEILING_BLEND_SPILL_LEVELS: u32 = 4;
const fn align_up(len: u32, alignment: u32) -> u32 {
len + (len.wrapping_neg() & (alignment - 1))
}
#[cfg(test)]
mod tests {
use super::{
BLEND_SPILL_WORDS_PER_LEVEL, BufferSizes, BumpAllocators, BumpBudget, CORRUPT_COUNT,
PTCL_INITIAL_ALLOC, RenderConfig,
};
use crate::{Layout, LineSoup, WorkgroupCounts};
#[test]
fn blend_spill_can_grow_to_cover_a_spilled_5k_desktop() {
let n_tiles = 5120_u32.div_ceil(16) * 2880_u32.div_ceil(16);
let used = BumpAllocators {
failed: 0x10,
blend: n_tiles * BLEND_SPILL_WORDS_PER_LEVEL,
..BumpAllocators::default()
};
let mut budget = BumpBudget::INITIAL;
budget.observe_within(&used, &BumpBudget::ceiling_for_viewport(n_tiles));
assert!(
budget.blend_spill >= used.blend,
"grew to {} blend words, the frame needs {}: it would overflow on every retry",
budget.blend_spill,
used.blend,
);
assert_eq!(
BumpBudget::ceiling_for_viewport(1),
BumpBudget::CEILING,
"a one-tile viewport keeps the flat ceiling"
);
}
#[test]
fn segments_cover_every_count_a_frame_may_make() {
let (width, height) = (480_u32, 320_u32);
let n_tiles = width.div_ceil(16) * height.div_ceil(16);
let used = BumpAllocators {
failed: 0x8,
lines: 3_310_659,
binning: 642,
tile: 82_223,
seg_counts: 3_602_928,
..BumpAllocators::default()
};
let mut budget = BumpBudget::INITIAL;
budget.observe_within(&used, &BumpBudget::ceiling_for_viewport(n_tiles));
assert!(budget.segments >= used.seg_counts);
let lopsided = BumpBudget {
seg_counts: 7_205_856,
..BumpBudget::INITIAL
};
let s = sizes(width, height, &lopsided);
assert!(
s.segments.len() >= s.seg_counts.len(),
"{} segments for {} counts",
s.segments.len(),
s.seg_counts.len(),
);
}
fn sizes(width: u32, height: u32, budget: &BumpBudget) -> BufferSizes {
let layout = Layout {
bin_data_start: 4096,
..Layout::default()
};
let (w_tiles, h_tiles) = (width.div_ceil(16), height.div_ceil(16));
let wgs = WorkgroupCounts::new(&layout, w_tiles, h_tiles, 0);
BufferSizes::new(&layout, &wgs, w_tiles, h_tiles, budget)
}
#[test]
fn ptcl_has_room_to_allocate_from_at_every_resolution() {
for (w, h) in [(800, 600), (1920, 1080), (2560, 1440), (3840, 2160)] {
let s = sizes(w, h, &BumpBudget::INITIAL);
let structural = w.div_ceil(16) * h.div_ceil(16) * PTCL_INITIAL_ALLOC;
let dynamic = s.ptcl.len() - structural;
assert!(
dynamic >= structural / 2,
"{w}x{h}: {dynamic} words to allocate from, under a floor of {structural}",
);
}
}
#[test]
fn ptcl_floor_covers_a_clipped_foreign_surface_composite_first_frame() {
let width_in_tiles = 1600_u32.div_ceil(16);
let height_in_tiles = 900_u32.div_ceil(16);
let n_tiles = width_in_tiles * height_in_tiles;
let measured_ptcl_words = 1_085_696;
let floor = BumpBudget::floor_for_viewport(n_tiles);
assert!(
floor.ptcl >= measured_ptcl_words,
"floor of {} ptcl words does not cover the {measured_ptcl_words} a real clipped \
client composite asked for at {n_tiles} tiles -- the first frame a client \
presents would overflow ptcl and render with its pixels silently missing",
floor.ptcl,
);
}
#[test]
fn ptcl_floor_keeps_dense_late_shell_chrome_stable() {
let n_tiles = 1600u32.div_ceil(16) * 900u32.div_ceil(16);
let floor = BumpBudget::floor_for_viewport(n_tiles);
assert!(
floor.ptcl >= 2_621_440,
"floor of {} ptcl words is below the measured 2.5M stability threshold for late shell chrome",
floor.ptcl,
);
}
#[test]
fn lines_floor_scales_with_the_viewport_it_has_to_cover() {
let n_tiles = 3840u32.div_ceil(16) * 2160u32.div_ceil(16);
let measured_lines = 105_450;
let floor = BumpBudget::floor_for_viewport(n_tiles);
assert!(
floor.lines >= measured_lines,
"floor of {} lines does not cover the {measured_lines} a subtree cache at \
scale 2 asked for at {n_tiles} tiles",
floor.lines,
);
let small = BumpBudget::floor_for_viewport(320u32.div_ceil(16) * 240u32.div_ceil(16));
assert!(
floor.lines > small.lines,
"a 4K desktop reserves {} lines and a 320x240 window {} -- the floor is not \
scaling with the viewport",
floor.lines,
small.lines,
);
}
#[test]
fn floors_cover_first_frame_of_complete_offscreen_app_capture() {
let desktop = BumpBudget::floor_for_viewport(1600u32.div_ceil(16) * 900u32.div_ceil(16));
assert!(desktop.lines >= 137_812);
assert!(desktop.tiles >= 102_091);
let complete_app =
BumpBudget::floor_for_viewport(1310u32.div_ceil(16) * 1188u32.div_ceil(16));
assert!(complete_app.lines >= 126_771);
assert!(complete_app.seg_counts >= 144_180);
assert!(complete_app.segments >= 144_180);
}
#[test]
fn lines_floor_covers_dashed_path_expansion_on_the_first_frame() {
let n_tiles = 440_u32.div_ceil(16) * 80_u32.div_ceil(16);
let encoded_path_tags = 503_706;
let measured_lines = 1_054_848;
let floor = BumpBudget::floor_for_scene(n_tiles, encoded_path_tags);
assert!(
floor.lines >= measured_lines,
"floor of {} lines does not cover the {measured_lines} emitted by a dashed path",
floor.lines,
);
assert!(
floor.lines < BumpBudget::UPSTREAM.lines,
"scene-derived floor {} fell back to upstream's fixed reservation",
floor.lines,
);
}
#[test]
fn ptcl_reserves_the_structural_floor_on_top_of_the_heuristic_one() {
for (w, h) in [(800, 600), (1920, 1080), (3840, 2160)] {
let s = sizes(w, h, &BumpBudget::INITIAL);
let n_tiles = w.div_ceil(16) * h.div_ceil(16);
let structural = n_tiles * PTCL_INITIAL_ALLOC;
let heuristic_floor = BumpBudget::floor_for_viewport(n_tiles).ptcl;
assert!(
s.ptcl.len() >= structural + heuristic_floor,
"{w}x{h}: reserved {}, expected the structural floor {structural} \
plus the heuristic one {heuristic_floor} = {}",
s.ptcl.len(),
structural + heuristic_floor,
);
}
}
#[test]
fn bin_data_always_clears_the_info_block() {
let layout = Layout {
bin_data_start: 1 << 20,
..Layout::default()
};
let wgs = WorkgroupCounts::new(&layout, 120, 68, 0);
let s = BufferSizes::new(&layout, &wgs, 120, 68, &BumpBudget::INITIAL);
assert!(
s.bin_data.len() > layout.bin_data_start,
"bin_data {} does not clear bin_data_start {}",
s.bin_data.len(),
layout.bin_data_start,
);
let cfg = RenderConfig::new(
&layout,
1920,
1080,
&peniko::Color::BLACK,
&BumpBudget::INITIAL,
);
assert!(
cfg.gpu.binning_size < s.bin_data.len(),
"binning_size {} underflowed",
cfg.gpu.binning_size,
);
}
#[test]
fn growth_covers_an_overflowed_frame() {
let mut budget = BumpBudget::INITIAL;
let overflowed = BumpAllocators {
failed: 0x2,
binning: 4_000,
ptcl: 700_000,
tile: 500_000,
seg_counts: 90_000,
segments: 90_000,
blend: 0,
lines: 40_000,
};
assert!(budget.observe(&overflowed), "should have grown");
assert!(budget.tiles >= overflowed.tile);
assert!(budget.ptcl >= overflowed.ptcl);
assert!(budget.lines >= overflowed.lines);
assert!(
!budget.observe(&overflowed),
"a repeat of the same frame should not grow again"
);
}
#[test]
fn growth_stops_at_the_ceiling() {
let mut budget = BumpBudget::INITIAL;
let most = CORRUPT_COUNT - 1;
let huge = BumpAllocators {
failed: 0x1f,
binning: most,
ptcl: most,
tile: most,
seg_counts: most,
segments: most,
blend: most,
lines: most,
};
budget.observe(&huge);
assert_eq!(budget.tiles, BumpBudget::CEILING.tiles);
assert_eq!(budget.ptcl, BumpBudget::CEILING.ptcl);
let quiet = BumpAllocators::default();
assert!(
!budget.observe(&quiet),
"a quiet frame must not shrink the reservation mid-session"
);
}
const WEBGPU_DEFAULT_BINDING: u32 = 128 << 20;
fn assert_bump_buffers_bindable(s: &BufferSizes, limit: u32) {
for (name, bytes) in [
("lines", s.lines.size_in_bytes()),
("bin_data", s.bin_data.size_in_bytes()),
("tiles", s.tiles.size_in_bytes()),
("seg_counts", s.seg_counts.size_in_bytes()),
("segments", s.segments.size_in_bytes()),
("blend_spill", s.blend_spill.size_in_bytes()),
("ptcl", s.ptcl.size_in_bytes()),
] {
assert!(bytes <= limit, "{name} is {bytes} bytes, past {limit}");
}
}
#[test]
fn a_wrapped_counter_does_not_grow_the_budget() {
let mut budget = BumpBudget::INITIAL.with_max_binding_bytes(WEBGPU_DEFAULT_BINDING);
let wrapped = BumpAllocators {
failed: 0x10,
segments: 2_373_742_623,
blend: 4_293_529_600,
lines: 38_037,
..BumpAllocators::default()
};
assert!(wrapped.segments >= CORRUPT_COUNT && wrapped.blend >= CORRUPT_COUNT);
assert!(
budget.observe(&wrapped),
"the plausible lines count still grows"
);
assert_eq!(budget.lines, 2 * 38_037);
assert_eq!(budget.segments, BumpBudget::INITIAL.segments);
assert_eq!(budget.blend_spill, BumpBudget::INITIAL.blend_spill);
}
#[test]
fn growth_never_passes_a_total_memory_limit() {
let mut budget = BumpBudget::INITIAL.with_max_binding_bytes(WEBGPU_DEFAULT_BINDING);
let huge = BumpAllocators {
lines: u32::MAX / 2,
binning: u32::MAX / 2,
tile: u32::MAX / 2,
seg_counts: u32::MAX / 2,
segments: u32::MAX / 2,
blend: u32::MAX / 2,
ptcl: u32::MAX / 2,
..Default::default()
};
budget.observe(&huge);
assert!(
budget.size_in_bytes() > 512 << 20,
"{}",
budget.size_in_bytes()
);
budget.fit_total_bytes(128 << 20);
assert!(
budget.size_in_bytes() <= 128 << 20,
"{}",
budget.size_in_bytes()
);
assert!(budget.ptcl > 0 && budget.lines > 0);
let fitted = budget;
budget.fit_total_bytes(128 << 20);
assert_eq!(budget, fitted, "a budget within the limit is left alone");
}
#[test]
fn growth_never_passes_the_device_binding_limit() {
let mut budget = BumpBudget::INITIAL.with_max_binding_bytes(WEBGPU_DEFAULT_BINDING);
let crowded = BumpAllocators {
failed: 0x1f,
lines: 4 << 20,
segments: 4 << 20,
..BumpAllocators::default()
};
assert!(budget.observe(&crowded));
assert_eq!(
budget.lines,
WEBGPU_DEFAULT_BINDING / size_of::<LineSoup>() as u32
);
assert!(!budget.observe(&crowded), "a saturated budget is stable");
for (w, h) in [(1920, 1080), (3840, 2160), (7680, 4320)] {
assert_bump_buffers_bindable(&sizes(w, h, &budget), WEBGPU_DEFAULT_BINDING);
}
let mut unlimited = BumpBudget::INITIAL;
unlimited.observe(&crowded);
assert_eq!(unlimited.lines, BumpBudget::CEILING.lines);
}
#[test]
fn scene_floors_never_pass_the_device_binding_limit() {
let layout = Layout {
bin_data_start: 4096,
..Layout::default()
};
let budget = BumpBudget::CEILING.with_max_binding_bytes(WEBGPU_DEFAULT_BINDING);
let (w_tiles, h_tiles) = (7680_u32.div_ceil(16), 4320_u32.div_ceil(16));
let wgs = WorkgroupCounts::new(&layout, w_tiles, h_tiles, 0);
let mut s = BufferSizes::new(&layout, &wgs, w_tiles, h_tiles, &budget);
assert_bump_buffers_bindable(&s, WEBGPU_DEFAULT_BINDING);
let dense = Layout {
path_tag_base: 0,
path_data_base: 64 << 20,
..layout
};
s = BufferSizes::new(&dense, &wgs, w_tiles, h_tiles, &budget);
assert!(dense.path_tags_size() >= 16 << 20);
assert_bump_buffers_bindable(&s, WEBGPU_DEFAULT_BINDING);
}
#[test]
fn a_desktop_viewport_reserves_far_less_than_upstream() {
let ours = sizes(1920, 1080, &BumpBudget::INITIAL);
let theirs = sizes(1920, 1080, &BumpBudget::UPSTREAM);
let bytes = |s: &BufferSizes| -> u64 {
u64::from(s.lines.size_in_bytes())
+ u64::from(s.bin_data.size_in_bytes())
+ u64::from(s.tiles.size_in_bytes())
+ u64::from(s.seg_counts.size_in_bytes())
+ u64::from(s.segments.size_in_bytes())
+ u64::from(s.blend_spill.size_in_bytes())
+ u64::from(s.ptcl.size_in_bytes())
};
let (ours, theirs) = (bytes(&ours), bytes(&theirs));
assert!(
ours * 7 < theirs,
"reserved {ours} bytes against upstream's {theirs}; expected at least a sevenfold reduction",
);
}
}