Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bench/layer-atlas/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# A page of many layers the size of the frame draws every one of them in every frame, and a page of more than Vello's
# image atlas holds fails its render. A layer with a perspective transform (or a filter) is an image the size of its
# box in that atlas, new in each frame; the atlas kept the last two frames' images, so with ten such layers or more
# the frame's last ones found no room and were left out, with no error: fit.tsx's 18 drew 10 from its second frame
# on. The atlas now lets go of what the frame doesn't use and packs the frame's images again before it leaves one
# out, and a frame with an image left out fails the render naming it (over.tsx's 40 rendered, 12 of them missing).
# Each layer holds one white 160x100 tile on black, so a frame's mean luma counts its tiles. Exits 1 on a failure.
# bench/layer-atlas/check.sh (RAEA_BIN: the raea to run, default target/release/raea)
cd "$(dirname "$0")/../.." || exit 2
BIN=${RAEA_BIN:-$PWD/target/release/raea}
export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0
OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-layer-atlas.XXXXXX")
trap 'rm -rf "$OUT"' EXIT
status=0
# a video's frames short of N tiles, from each frame's mean luma (limited range: black 16, white 235)
short() { # MP4 N
ffmpeg -loglevel error -i "$1" -vf "signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=-" -f null - |
awk -F= -v n="$2" '/YAVG/ { f++; if (($2 - 16) / 219 * 1920 * 1080 / 16000 < n - 0.5) bad++ } END { print bad + 0, f + 0 }'
}

"$BIN" render bench/layer-atlas/fit.tsx -o "$OUT/fit.mp4" > "$OUT/fit.log" 2>&1 || { echo "FAIL: the video of 18 layers failed"; tail -3 "$OUT/fit.log"; exit 1; }
read -r bad frames < <(short "$OUT/fit.mp4" 18)
if [ "$frames" = 48 ] && [ "$bad" = 0 ]; then echo "ok a video of 18 layers: 18 tiles in each of its 48 frames"
else echo "FAIL a video of 18 layers: $bad of $frames frames are short of 18 tiles"; status=1; fi

mkdir -p "$OUT/stills"
"$BIN" render bench/layer-atlas/fit.tsx --at 0,1,2,3,4,5 -o "$OUT/stills" > "$OUT/stills.log" 2>&1 || { echo "FAIL: the stills of 18 layers failed"; tail -3 "$OUT/stills.log"; exit 1; }
bad=0
for f in "$OUT"/stills/frame-*.png; do
tiles=$(magick "$f" -alpha off -colorspace gray -format "%[fx:int(mean*1920*1080/16000+0.5)]" info:)
[ "$tiles" = 18 ] || { echo "FAIL $(basename "$f"): $tiles tiles of 18"; bad=1; status=1; }
done
[ $bad = 0 ] && echo "ok six stills of 18 layers in one call: 18 tiles in each"

if "$BIN" render bench/layer-atlas/over.tsx -o "$OUT/over.mp4" > "$OUT/over.log" 2>&1; then
read -r bad frames < <(short "$OUT/over.mp4" 40)
echo "FAIL a video of 40 layers rendered, $bad of its $frames frames short of 40 tiles"; status=1
elif grep -q "frame 0: .*image atlas" "$OUT/over.log"; then echo "ok a video of 40 layers fails, naming the frame and the atlas"
else echo "FAIL a video of 40 layers failed without naming the frame and the atlas:"; tail -3 "$OUT/over.log"; status=1; fi

[ $status = 0 ] && echo "PASS a page's layers are all drawn, or its render fails"
exit $status
5 changes: 5 additions & 0 deletions bench/layer-atlas/fit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 18 layers the size of the frame: Vello's image atlas has room for 28 (bench/layer-atlas/check.sh)
import { registerRoot } from "raea";
import { planes } from "./planes";

registerRoot(planes(18));
5 changes: 5 additions & 0 deletions bench/layer-atlas/over.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 40 layers the size of the frame: more than Vello's image atlas holds at once (bench/layer-atlas/check.sh)
import { registerRoot } from "raea";
import { planes } from "./planes";

registerRoot(planes(40));
27 changes: 27 additions & 0 deletions bench/layer-atlas/planes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// N layers the size of the frame, each holding one white tile in a cell of its own, on black: a frame's mean luma
// counts the tiles drawn. An element with a perspective transform is drawn apart and placed as an image the size of
// its box, new in every frame, in Vello's image atlas. The perspective here is slight (the far corner moves under
// 3 px) and changes with the frame, so each frame is painted anew, the layers land a pixel or two apart in size, and
// no tile leaves its cell. bench/layer-atlas/check.sh
import { AbsoluteFill, Composition, useCurrentFrame } from "raea";

const W = 1920, H = 1080, COLS = 8;

function Planes({ n }: { n: number }) {
const f = useCurrentFrame();
return (
<AbsoluteFill style={{ background: "#000" }}>
{Array.from({ length: n }, (_, i) => {
// matrix3d's m14 and m24: w = 1 + a x + b y
const a = (1e-7 * (1 + ((i + f) % 5))).toFixed(9), b = (1e-7 * (1 + ((2 * i + f) % 3))).toFixed(9);
return (
<div key={i} style={{ position: "absolute", left: 0, top: 0, width: W, height: H, transformOrigin: "0 0", transform: `matrix3d(1,0,0,${a},0,1,0,${b},0,0,1,0,0,0,0,1)` }}>
<div style={{ position: "absolute", left: 40 + (i % COLS) * 230, top: 40 + Math.floor(i / COLS) * 200, width: 160, height: 100, background: "#fff" }} />
</div>
);
})}
</AbsoluteFill>
);
}

export const planes = (n: number) => () => <Composition id="planes" component={() => <Planes n={n} />} width={W} height={H} fps={24} durationInFrames={48} />;
13 changes: 13 additions & 0 deletions gpu/device/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ pub fn on_settle(settle: fn()) {
/// skipped too (#23).
pub fn fed_checked<T>(f: i64, first: T, wait: fn(), redraw_full: fn(bool), paint: impl FnOnce() -> Result<T, String>) -> Result<T, String> {
wait();
// a frame raea-page refused (`refuse`) fails the render naming it, as a still's does (`black_checked`): the fed
// render had no one to read the refusal, encoded the frame as it came and went on
let unrefused = || refused().map_or(Ok(()), |why| Err(format!("frame {f}: {why}")));
unrefused()?;
if take_skipped().is_none() {
return Ok(first);
}
Expand All @@ -170,6 +174,7 @@ pub fn fed_checked<T>(f: i64, first: T, wait: fn(), redraw_full: fn(bool), paint
redraw_full(false);
let again = again?;
wait();
unrefused()?;
match take_skipped() {
Some(why) => Err(format!("frame {f}: {why}")),
None => Ok(again),
Expand Down Expand Up @@ -293,6 +298,14 @@ mod tests {
})
.unwrap_err();
assert_eq!(e, "frame 7: a page (1920x1080) wasn't drawn");
// the fed render's frame, which no read back checks: a refused one fails the render by its number too, and
// the frame after it isn't refused with it
fn drawn() {}
assert_eq!(fed_checked(8, 1, drawn, full, || panic!("no draw skipped")).unwrap(), 1);
refuse("a page (1920x1080) was drawn without 2 of its images".into());
let e = fed_checked(9, 1, drawn, full, || panic!("no draw skipped")).unwrap_err();
assert_eq!(e, "frame 9: a page (1920x1080) was drawn without 2 of its images");
assert_eq!(fed_checked(10, 1, drawn, full, || panic!("no draw skipped")).unwrap(), 1);
}

#[test]
Expand Down
12 changes: 12 additions & 0 deletions systems/page/src/skipped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
//! counted (raea_gpu_device::check::skipped_draw), and the frame's check (`skip_checked`) draws the frames since again
//! at those sizes, or refuses one skipped at them too, so the render fails naming it.
//! RAEA_PAGE_DRAWN_CHECK=0 draws without the check (for measuring what it costs; a skipped draw goes unseen again).
//! And whether it drew every image: one that finds no room in Vello's image atlas is drawn at no size, again with no
//! error. A layer with a filter or a perspective transform is such an image, the size of its box, so a page of many
//! lost its nearest ones. The resolve counts them (vendor/vello_encoding `take_images_left_out`) and the frame is
//! refused (raea_gpu_device::check::refuse), so the render fails naming it.

use std::sync::atomic::{AtomicBool, Ordering};

Expand Down Expand Up @@ -52,7 +56,15 @@ pub fn render(renderer: &mut vello::Renderer, device: &wgpu::Device, queue: &wgp
if check {
queue.write_texture(texel, &SENTINEL, layout, one);
}
// (the count is this draw's: a resolve that only measures the scene, RAEA_VELLO_BUMP_USE's, counts its own before it)
vello::low_level::vello_encoding::take_images_left_out();
renderer.render_to_texture(device, queue, scene, &target.create_view(&Default::default()), params).map_err(|e| format!("vello, {what}: {e}"))?;
let left_out = vello::low_level::vello_encoding::take_images_left_out();
if left_out > 0 {
let why = format!("{what} ({}x{}) was drawn without {left_out} of its images: Vello's image atlas (8192 px a side) has no room for them. Each element with a filter or a perspective transform is an image the size of its box, so fewer of them, or smaller boxes, draw it", params.width, params.height);
bevy::log::error!("raea-page: {why}");
raea_gpu_device::check::refuse(why);
}
if !check {
return Ok(());
}
Expand Down
96 changes: 96 additions & 0 deletions vendor/vello_encoding/src/image_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub(crate) struct ImageCache {
map: HashMap<u64, ResidentImage>,
/// Images that must be uploaded in the current resolve pass, with atlas locations.
images: Vec<(ImageData, u32, u32)>,
/// raea: whether the current resolve has packed the atlas again (`repack_with`: once a resolve)
repacked_in_resolve: bool,
}

impl Default for ImageCache {
Expand All @@ -70,13 +72,15 @@ impl ImageCache {
evicted_in_resolve: 0,
map: HashMap::default(),
images: Vec::default(),
repacked_in_resolve: false,
}
}

pub(crate) fn begin_resolve(&mut self) {
self.generation = self.generation.wrapping_add(1);
self.evicted_in_resolve = 0;
self.images.clear();
self.repacked_in_resolve = false;
}

pub(crate) fn restart_resolve_pass(&mut self) {
Expand Down Expand Up @@ -187,6 +191,54 @@ impl ImageCache {
self.evicted_in_resolve != 0
}

/// raea: with the atlas at its largest and no room for an image, the resident images the current resolve hasn't
/// used are let go, the ones unused longest first (one generation a call; false when none is left). Upstream stops
/// at `evict_stale_entries`, which keeps what the last two resolves used: an embedder whose images are new in
/// every frame (raea-page's layers, each a placeholder the size of its box) fills the atlas with the last two
/// frames' and draws the frame without its last images.
pub(crate) fn evict_unused(&mut self) -> bool {
// once packed again (`repack_with`), what's resident is the resolve's own images, the ones its pass hasn't
// reached yet among them
if self.repacked_in_resolve {
return false;
}
let generation = self.generation;
let Some(oldest) = self.map.values().map(|resident| resident.last_used_generation).filter(|used| *used != generation).min() else {
return false;
};
for (_id, resident) in self.map.extract_if(|_, resident| resident.last_used_generation == oldest) {
self.atlas.deallocate(resident.alloc_id);
self.evicted_in_resolve += 1;
}
true
}

/// raea: the atlas emptied and packed with `wanted` alone (the current resolve's images), the tallest first, once
/// a resolve (false after that). What `evict_unused` leaves lies where the images it let go had put it, and
/// images met in paint order cut the room up: 18 images near a 1080p frame's size, a few px apart, didn't all
/// find a place in an atlas with the area for 28. Tallest first, a guillotine packs them in rows. The caller
/// starts its pass again, as after `bump_size`: every image is uploaded again.
pub(crate) fn repack_with<'a>(&mut self, wanted: impl Iterator<Item = &'a ImageData>) -> bool {
if std::mem::replace(&mut self.repacked_in_resolve, true) {
return false;
}
let mut wanted: Vec<&ImageData> = wanted.collect();
wanted.sort_by_key(|image| (std::cmp::Reverse(image.height), std::cmp::Reverse(image.width), image.data.id()));
wanted.dedup_by_key(|image| image.data.id());
self.evicted_in_resolve += self.map.keys().filter(|id| !wanted.iter().any(|image| image.data.id() == **id)).count();
self.atlas = AtlasAllocator::new(self.atlas.size());
self.map.clear();
// not yet used by this pass: `get_or_insert` uploads each as it meets it
let unused = self.generation.wrapping_sub(1);
for image in wanted {
let Some(alloc) = self.atlas.allocate(size2(image.width as _, image.height as _)) else { continue };
let (x, y) = (alloc.rectangle.min.x as u32, alloc.rectangle.min.y as u32);
self.map.insert(image.data.id(), ResidentImage { image: image.clone(), alloc_id: alloc.id, x, y, dirty: true, last_used_generation: unused });
}
self.images.clear();
true
}

fn repack_to_size(&mut self, size: i32) -> bool {
let mut atlas = AtlasAllocator::new(size2(size, size));
let mut entries: Vec<_> = self.map.iter().collect();
Expand Down Expand Up @@ -348,6 +400,50 @@ mod tests {
assert!(!cache.evict_stale_entries());
}

/// raea: at the atlas's largest size, what the resolve hasn't used goes the longest unused first, and what it has
/// used stays
#[test]
fn unused_entries_are_let_go_longest_unused_first() {
let mut cache = ImageCache::new_with_sizes(32, 32);
let (old, last, this) = (image(1, 8, 8), image(2, 8, 8), image(3, 8, 8));
for image in [&old, &last, &this] {
cache.begin_resolve();
assert!(cache.get_or_insert(image).is_some());
}
let resident = |cache: &ImageCache| [&old, &last, &this].map(|image| cache.map.contains_key(&image.data.id()));
assert!(cache.evict_unused());
assert_eq!(resident(&cache), [false, true, true]);
assert!(cache.evict_unused());
assert_eq!(resident(&cache), [false, false, true]);
assert!(!cache.evict_unused());
assert_eq!((resident(&cache), cache.evicted_in_resolve), ([false, false, true], 2));
}

/// raea: a crowded atlas packed again holds the wanted images alone, each to be uploaded as the pass meets it, and
/// is packed once a resolve
#[test]
fn a_crowded_atlas_is_packed_again_with_the_wanted_images() {
let mut cache = ImageCache::new_with_sizes(16, 16);
let (before, a, b) = (image(1, 10, 10), image(2, 16, 6), image(3, 16, 10));
cache.begin_resolve();
assert!(cache.get_or_insert(&before).is_some());
cache.finish_resolve();
cache.begin_resolve();
// twice wanted: one place
assert!(cache.repack_with([&a, &b, &a].into_iter()));
assert!(!cache.map.contains_key(&before.data.id()));
assert_eq!((cache.map.len(), cache.evicted_in_resolve, cache.images.len()), (2, 1, 0));
// what it packed is the resolve's own, though the pass hasn't met it yet: none of it is let go
assert!(!cache.evict_unused());
assert_eq!(cache.map.len(), 2);
// the taller first, at the top
assert_eq!((cache.get_or_insert(&b), cache.get_or_insert(&a)), (Some((0, 0)), Some((0, 10))));
assert_eq!(cache.images.len(), 2);
assert!(!cache.repack_with([&a, &b].into_iter()));
cache.begin_resolve();
assert!(cache.repack_with([&a].into_iter()));
}

#[test]
fn failed_repack_leaves_existing_residency_unchanged() {
let mut cache = ImageCache::new_with_sizes(16, 16);
Expand Down
2 changes: 1 addition & 1 deletion vendor/vello_encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub use path::{
PathTag, SegmentCount, Style, Tile,
};
pub use ramp_cache::Ramps;
pub use resolve::{Layout, Patch, Resolver, resolve_solid_paths_only};
pub use resolve::{Layout, Patch, Resolver, resolve_solid_paths_only, take_images_left_out};

#[cfg(feature = "bump_estimate")]
pub use estimate::BumpEstimator;
Expand Down
Loading
Loading