use super::*;
use crcbl_hal::null::NullInstance;
use crcbl_hal::{DeviceDesc, Features, Instance, QueueKind};
fn open() -> (Box<dyn Device>, QueueHandle) {
let instance = NullInstance::gpu_driven();
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::GPU_DRIVEN,
optional_features: Features::PUSH_CONSTANTS,
compatible_surface: None,
})
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
(device, queue)
}
fn open_recorded() -> (crcbl_hal::null::Recorder, Box<dyn Device>, QueueHandle) {
let recorder = crcbl_hal::null::Recorder::new();
let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::GPU_DRIVEN,
optional_features: Features::PUSH_CONSTANTS,
compatible_surface: None,
})
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
(recorder, device, queue)
}
fn uploaded(recorder: &crcbl_hal::null::Recorder, renderer: &UiRenderer) -> (usize, usize) {
use crcbl_hal::null::Event;
let vertices = renderer.vertex_buffers[renderer.frame];
let indices = renderer.index_buffers[renderer.frame];
let mut written = (0, 0);
for event in recorder.events() {
if let Event::BufferWritten {
buffer,
offset,
len,
} = event
{
assert_eq!(offset, 0, "a ring is written from its start");
if buffer == vertices {
written.0 += len;
} else if buffer == indices {
written.1 += len;
}
}
}
written
}
#[test]
fn ui_renderer_builds_and_leaks_nothing() {
let (recorder, device, queue) = open_recorded();
let before = recorder.total_live_objects();
let renderer = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb)
.expect("the null backend accepts everything");
assert!(
recorder.total_live_objects() > before,
"a renderer that created nothing would also leak nothing"
);
renderer.destroy(device.as_ref());
assert_eq!(
recorder.total_live_objects(),
before,
"destroy must give back every object new took"
);
recorder.assert_valid();
}
#[test]
fn the_glyph_atlas_is_still_an_r8_upload_at_the_same_pitch() {
use crcbl_hal::null::{Command, Event};
use crcbl_hal::{Extent3d, Offset3d, ResourceState};
let recorder = crcbl_hal::null::Recorder::new();
let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::GPU_DRIVEN,
optional_features: Features::PUSH_CONSTANTS,
compatible_surface: None,
})
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
let (atlas_w, atlas_h, atlas_pixels) = FontAtlas::built_in().glyph_bitmap();
assert_eq!((atlas_w, atlas_h), (768, 13));
assert_eq!(atlas_pixels.len(), 768 * 13);
assert_eq!(
768 % device.caps().limits.optimal_buffer_copy_offset_alignment,
0,
"the pitch below is the unpadded width only because the row is already aligned"
);
let renderer = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb)
.expect("the null backend accepts everything");
let written = recorder
.events()
.into_iter()
.find_map(|event| match event {
Event::BufferWritten { len, .. } => Some(len),
_ => None,
})
.expect("the atlas staging buffer is written before any frame buffer");
assert_eq!(
written,
768 * 13,
"one byte per texel: the same call with an Rgba8Unorm atlas would write four times this"
);
let commands = recorder.commands();
let copy = commands
.iter()
.find_map(|command| match command {
Command::CopyBufferToImage(copy) => Some(*copy),
_ => None,
})
.expect("the atlas is uploaded with one buffer-to-image copy");
assert_eq!(
copy.buffer_row_length, 768,
"R8 is one byte per texel, so the texel pitch equals the byte pitch"
);
assert_eq!(copy.buffer_image_height, atlas_h);
assert_eq!(copy.image_extent, Extent3d::d2(atlas_w, atlas_h));
assert_eq!(copy.image_offset, Offset3d { x: 0, y: 0, z: 0 });
let transitions: Vec<_> = commands
.iter()
.filter_map(|command| match command {
Command::Barrier { images, .. } => Some(images.clone()),
_ => None,
})
.flatten()
.map(|barrier| (barrier.from, barrier.to))
.collect();
assert_eq!(
transitions,
[
(ResourceState::Undefined, ResourceState::TransferDst),
(ResourceState::TransferDst, ResourceState::ShaderRead),
(ResourceState::Undefined, ResourceState::TransferDst),
(ResourceState::TransferDst, ResourceState::ShaderRead),
(ResourceState::Undefined, ResourceState::TransferDst),
(ResourceState::TransferDst, ResourceState::ShaderRead),
(ResourceState::Undefined, ResourceState::TransferDst),
(ResourceState::TransferDst, ResourceState::ShaderRead),
],
"the two atlases, the transparent 1x1 set 1 binds for untextured draws and \
the glyph pages are the only barriers the UI renderer's construction records"
);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn an_empty_draw_list_uploads_no_bytes_and_leaves_the_counts_at_zero() {
let (recorder, device, queue) = open_recorded();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
recorder.clear();
let dl = DrawList::new();
renderer
.begin_frame(device.as_ref(), &dl, &atlas, 1.0)
.expect("empty draw list upload should succeed");
assert_eq!(uploaded(&recorder, &renderer), (0, 0));
assert_eq!(renderer.last_vertex_count[renderer.frame], 0);
assert_eq!(renderer.last_index_count[renderer.frame], 0);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn a_rect_and_a_line_of_text_each_upload_exactly_the_geometry_they_tessellate_to() {
let atlas = FontAtlas::built_in();
let mut rect = DrawList::new();
rect.rect(
glam::Vec2::new(10.0, 20.0),
glam::Vec2::new(110.0, 120.0),
[1.0, 0.0, 0.0, 1.0],
);
let mut text = DrawList::new();
text.text(
glam::Vec2::new(10.0, 10.0),
"Hello",
[1.0, 1.0, 1.0, 1.0],
14.0,
);
let mut sizes = Vec::new();
for (what, dl) in [("a rect", &rect), ("a line of text", &text)] {
let (recorder, device, queue) = open_recorded();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
recorder.clear();
renderer
.begin_frame(device.as_ref(), dl, &atlas, 1.0)
.expect("upload should succeed");
let (vertices, indices) = dl.to_triangles(Some(&atlas), None, 1.0);
let expected = (
vertices.len() * std::mem::size_of::<Vertex2d>(),
indices.len() * std::mem::size_of::<u32>(),
);
assert!(
expected.0 > 0 && expected.1 > 0,
"{what} tessellates to nothing, so this case asserts nothing"
);
assert_eq!(uploaded(&recorder, &renderer), expected, "{what}");
assert_eq!(renderer.last_vertex_count[renderer.frame], vertices.len());
assert_eq!(renderer.last_index_count[renderer.frame], indices.len());
sizes.push(expected);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
assert_eq!(sizes.len(), 2, "both primitives were measured");
assert_ne!(
sizes[0], sizes[1],
"one quad and five glyphs are not the same geometry"
);
}
#[test]
fn a_steady_state_frame_recreates_nothing() {
let recorder = crcbl_hal::null::Recorder::new();
let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::GPU_DRIVEN,
optional_features: Features::PUSH_CONSTANTS,
compatible_surface: None,
})
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut dl = DrawList::new();
dl.text(
glam::Vec2::new(10.0, 10.0),
"steady",
[1.0, 1.0, 1.0, 1.0],
14.0,
);
for _ in 0..FRAMES_IN_FLIGHT {
renderer
.begin_frame(device.as_ref(), &dl, &atlas, 1.0)
.expect("upload");
}
let buffers = renderer.vertex_buffers.clone();
let groups = renderer.frame_groups.clone();
let settled = recorder.total_live_objects();
for _ in 0..8 {
renderer
.begin_frame(device.as_ref(), &dl, &atlas, 1.0)
.expect("upload");
}
assert_eq!(
recorder.total_live_objects(),
settled,
"an unchanged draw list must not allocate"
);
assert_eq!(
renderer.vertex_buffers, buffers,
"the vertex ring must be reused, not reallocated"
);
assert_eq!(
renderer.frame_groups, groups,
"the frame bind group only changes when its vertex buffer does"
);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn the_counters_are_the_one_draw_and_the_indices_it_covers() {
let (device, queue) = open();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut one_rect = DrawList::new();
one_rect.rect(
glam::Vec2::ZERO,
glam::Vec2::new(10.0, 10.0),
[1.0, 1.0, 1.0, 1.0],
);
renderer
.begin_frame(device.as_ref(), &one_rect, &atlas, 1.0)
.expect("upload");
let counters = renderer.counters();
assert_eq!(counters.draws, 1, "one `draw_indexed` for the whole list");
assert_eq!(counters.instances, 1);
assert_eq!(counters.drawn, Some(1));
assert_eq!(counters.triangles, Some(2), "a quad is two triangles");
let mut with_text = one_rect.clone();
with_text.text(
glam::Vec2::new(4.0, 4.0),
"counters",
[1.0, 1.0, 1.0, 1.0],
14.0,
);
renderer
.begin_frame(device.as_ref(), &with_text, &atlas, 1.0)
.expect("upload");
let richer = renderer.counters();
assert_eq!(
richer.draws, 1,
"still one draw, however much is in the list"
);
let (_, indices) = with_text.to_triangles(Some(&atlas), None, 1.0);
assert_eq!(richer.triangles, Some(indices.len() as u64 / 3));
assert!(
richer.triangles > counters.triangles,
"a longer list must move the counter: {:?} against {:?}",
richer.triangles,
counters.triangles,
);
renderer
.begin_frame(device.as_ref(), &DrawList::new(), &atlas, 1.0)
.expect("upload");
assert_eq!(
renderer.counters(),
crate::counters::FrameCounters::default()
);
renderer.destroy(device.as_ref());
}
#[test]
fn a_bigger_draw_list_grows_the_ring_once() {
let (device, queue) = open();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut small = DrawList::new();
small.rect(
glam::Vec2::ZERO,
glam::Vec2::new(10.0, 10.0),
[1.0, 1.0, 1.0, 1.0],
);
renderer
.begin_frame(device.as_ref(), &small, &atlas, 1.0)
.expect("upload");
let before = renderer.vertex_capacity[renderer.frame];
let mut big = DrawList::new();
for index in 0..512 {
let x = index as f32;
big.rect(
glam::Vec2::new(x, 0.0),
glam::Vec2::new(x + 1.0, 1.0),
[1.0, 1.0, 1.0, 1.0],
);
}
for _ in 0..FRAMES_IN_FLIGHT {
renderer
.begin_frame(device.as_ref(), &big, &atlas, 1.0)
.expect("upload");
}
assert!(
renderer.vertex_capacity[renderer.frame] > before,
"the ring must grow when the frame no longer fits"
);
renderer.destroy(device.as_ref());
}
fn open_portable() -> (Box<dyn Device>, QueueHandle) {
let instance = NullInstance::portable();
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::COMPUTE,
optional_features: Features::empty(),
compatible_surface: None,
})
.expect("the tier B null adapter opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
(device, queue)
}
#[test]
fn push_constants_or_not_the_renderer_is_the_same() {
for (device, queue) in [open(), open_portable()] {
let renderer = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb)
.expect("neither device is refused");
assert_eq!(
renderer.constant_buffers.len(),
FRAMES_IN_FLIGHT,
"one constants buffer per frame in flight, or two frames share one"
);
renderer.destroy(device.as_ref());
}
}
#[test]
fn the_portable_renderer_leaks_nothing() {
let recorder = crcbl_hal::null::Recorder::new();
let instance = NullInstance::portable().with_recorder(recorder.clone());
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: adapter.id,
required_features: Features::COMPUTE,
optional_features: Features::empty(),
compatible_surface: None,
})
.expect("the portable null adapter opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
let before = recorder.total_live_objects();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut dl = DrawList::new();
dl.text(glam::Vec2::new(4.0, 4.0), "score", [1.0; 4], 14.0);
for _ in 0..FRAMES_IN_FLIGHT * 2 {
renderer
.begin_frame(device.as_ref(), &dl, &atlas, 1.0)
.expect("upload");
}
renderer.destroy(device.as_ref());
assert_eq!(recorder.total_live_objects(), before);
recorder.assert_valid();
}
fn pause_menu() -> crcbl_ui::menu::Menu {
use crcbl_ui::menu::{Menu, MenuItem};
Menu::new(
"PAUSED",
vec![
MenuItem::new(1, "RESUME", "ESC"),
MenuItem::new(2, "QUIT", ""),
],
)
}
const TEST_ART_SIDE: u32 = 8;
fn test_skin(images: &mut ImageAtlas) -> crcbl_ui::menu::MenuSkin {
use crcbl_ui::menu::MenuSkin;
use crcbl_ui::{ButtonSkin, NineSliceImage, SkinInsets};
let pixels = vec![0xff; (TEST_ART_SIDE * TEST_ART_SIDE * 4) as usize];
let mut register = || {
images
.register(TEST_ART_SIDE, TEST_ART_SIDE, &pixels)
.expect("a square fits an empty page")
};
let insets = SkinInsets::new(2.0, 2.0, 2.0, 2.0);
let mut sliced = || NineSliceImage {
image: register(),
insets,
};
let panel = sliced();
let buttons = ButtonSkin {
idle: sliced(),
hovered: sliced(),
pressed: sliced(),
};
MenuSkin {
panel,
buttons,
scrim: register(),
}
}
fn with_test_skin(
device: &dyn Device,
queue: QueueHandle,
) -> (UiRenderer, crcbl_ui::menu::MenuSkin) {
let mut images = ImageAtlas::new();
let skin = test_skin(&mut images);
let renderer =
UiRenderer::with_images(device, queue, Format::Bgra8UnormSrgb, images).expect("built");
(renderer, skin)
}
fn paused_list(skin: &crcbl_ui::menu::MenuSkin, extent: (u32, u32), atlas: &FontAtlas) -> DrawList {
let mut list = DrawList::new();
list.rect(
glam::Vec2::new(0.0, 0.0),
glam::Vec2::new(64.0, 8.0),
[1.0; 4],
);
list.begin_overlay();
let panel = pause_menu();
panel.render(&mut list, &panel.layout(extent, atlas), skin);
list
}
#[test]
fn a_paused_frame_draws_both_halves_of_one_upload_with_the_menu_art_above_the_cut() {
use crate::graph::{CompiledPass, RenderGraph};
use crate::transient::{TransientImageDesc, TransientPool};
use crcbl_hal::null::{Command, Event};
use crcbl_hal::{CommandEncoderDesc, Format as HalFormat, ImageUsage};
const EXTENT: (u32, u32) = (128, 96);
let (recorder, device, queue) = open_recorded();
let mut pool = TransientPool::new();
let (mut ui, skin) = with_test_skin(device.as_ref(), queue);
let atlas = FontAtlas::built_in();
let list = paused_list(&skin, EXTENT, &atlas);
ui.begin_frame(device.as_ref(), &list, &atlas, 1.0)
.expect("upload");
let total = ui.last_index_count[ui.frame] as u32;
let split = ui.last_overlay_index[ui.frame] as u32;
assert!(
split > 0 && split < total,
"the fixture must put geometry on both sides of the cut: {split} of {total}"
);
let triangles = list.to_triangles_split(Some(&atlas), None, 1.0);
let image_vertices: Vec<u32> = triangles.indices[..]
.iter()
.copied()
.filter(|&index| {
triangles.vertices[index as usize].primitive()
== Some(crcbl_ui::draw_list::Primitive::Image)
})
.collect();
assert!(!image_vertices.is_empty(), "the menu drew no art");
assert!(
triangles.indices[..split as usize]
.iter()
.all(|&index| !image_vertices.contains(&index)),
"menu art landed below the cut"
);
recorder.clear();
let mut graph = RenderGraph::new(queue);
let target = graph.create_image(
"target",
TransientImageDesc::new(
EXTENT,
HalFormat::Bgra8UnormSrgb,
ImageUsage::COLOR_ATTACHMENT,
),
);
ui.add_passes(&mut graph, target, EXTENT);
let compiled = graph.compile(&pool).expect("a legal frame");
let labels: Vec<&str> = compiled.passes().iter().map(CompiledPass::label).collect();
assert_eq!(
labels,
["ui-composite", "ui-overlay"],
"the HUD half, then the overlay half, and nothing between them"
);
let mut encoder = device.create_command_encoder(&CommandEncoderDesc {
label: Some("paused frame"),
queue,
});
compiled
.execute(device.as_ref(), &mut pool, encoder.as_mut(), None)
.expect("the graph executed");
let commands = encoder.finish().expect("recording succeeded");
let drawn: Vec<std::ops::Range<u32>> = recorder
.commands()
.into_iter()
.filter_map(|command| match command {
Command::DrawIndexed { indices, .. } => Some(indices),
_ => None,
})
.collect();
assert_eq!(
drawn,
vec![0..split, split..total],
"the two halves must partition the frame's one index buffer"
);
let writes = recorder
.events()
.into_iter()
.filter(|event| matches!(event, Event::BufferWritten { .. }))
.count();
assert_eq!(
writes, 2,
"one viewport-constants write per pass and no second geometry \
upload: the tessellation happened before the frame"
);
let counters = ui.counters();
assert_eq!(counters.draws, 2, "one draw per half");
assert_eq!(counters.drawn, Some(2));
assert_eq!(
counters.triangles,
Some(u64::from(total) / 3),
"and every triangle in the buffer is in one half or the other"
);
device.destroy_command_buffer(commands);
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
}
#[test]
fn an_uncut_draw_list_is_still_one_ui_composite_pass() {
use crate::graph::{CompiledPass, RenderGraph};
use crate::transient::{TransientImageDesc, TransientPool};
use crcbl_hal::{Format as HalFormat, ImageUsage};
const EXTENT: (u32, u32) = (128, 96);
let (device, queue) = open();
let mut pool = TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut list = DrawList::new();
list.text(glam::Vec2::new(4.0, 4.0), "SCORE", [1.0; 4], 14.0);
ui.begin_frame(device.as_ref(), &list, &atlas, 1.0)
.expect("upload");
let mut graph = RenderGraph::new(queue);
let target = graph.create_image(
"target",
TransientImageDesc::new(
EXTENT,
HalFormat::Bgra8UnormSrgb,
ImageUsage::COLOR_ATTACHMENT,
),
);
ui.add_passes(&mut graph, target, EXTENT);
let compiled = graph.compile(&pool).expect("a legal frame");
let labels: Vec<&str> = compiled.passes().iter().map(CompiledPass::label).collect();
assert_eq!(labels, ["ui-composite"]);
assert_eq!(ui.counters().draws, 1, "one half, one draw");
drop(compiled);
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
}
#[test]
fn growing_the_ring_keeps_the_constants_bound() {
let (device, queue) = open_portable();
let mut renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let mut big = DrawList::new();
for index in 0..512 {
let x = index as f32;
big.rect(
glam::Vec2::new(x, 0.0),
glam::Vec2::new(x + 1.0, 1.0),
[1.0; 4],
);
}
for _ in 0..FRAMES_IN_FLIGHT {
renderer
.begin_frame(device.as_ref(), &big, &atlas, 1.0)
.expect("upload");
}
assert!(renderer.vertex_capacity[renderer.frame] > INITIAL_RING_BYTES);
assert_eq!(
renderer.constant_buffers.len(),
FRAMES_IN_FLIGHT,
"the rebuilt group still names a constants buffer per frame"
);
renderer.destroy(device.as_ref());
}
fn image_copies(commands: &[crcbl_hal::null::Command]) -> Vec<crcbl_hal::BufferImageCopy> {
commands
.iter()
.filter_map(|command| match command {
crcbl_hal::null::Command::CopyBufferToImage(copy) => Some(*copy),
_ => None,
})
.collect()
}
fn record_frame(
ui: &mut UiRenderer,
device: &dyn Device,
queue: QueueHandle,
pool: &mut crate::transient::TransientPool,
list: &DrawList,
) -> Vec<String> {
use crate::graph::{CompiledPass, RenderGraph};
use crate::transient::TransientImageDesc;
use crcbl_hal::{CommandEncoderDesc, ImageUsage};
const EXTENT: (u32, u32) = (64, 48);
ui.begin_frame(device, list, &FontAtlas::built_in(), 1.0)
.expect("upload");
let mut graph = RenderGraph::new(queue);
let target = graph.create_image(
"target",
TransientImageDesc::new(EXTENT, Format::Bgra8UnormSrgb, ImageUsage::COLOR_ATTACHMENT),
);
ui.add_passes(&mut graph, target, EXTENT);
let compiled = graph.compile(pool).expect("a legal frame");
let labels = compiled
.passes()
.iter()
.map(|pass| CompiledPass::label(pass).to_owned())
.collect();
let mut encoder = device.create_command_encoder(&CommandEncoderDesc {
label: Some("ui frame"),
queue,
});
compiled
.execute(device, pool, encoder.as_mut(), None)
.expect("the graph executed");
let commands = encoder.finish().expect("recording succeeded");
device.destroy_command_buffer(commands);
labels
}
fn one_rect() -> DrawList {
let mut list = DrawList::new();
list.rect(glam::Vec2::ZERO, glam::Vec2::splat(8.0), [1.0; 4]);
list
}
fn buffer_writes(recorder: &crcbl_hal::null::Recorder) -> Vec<(BufferHandle, usize)> {
recorder
.events()
.into_iter()
.filter_map(|event| match event {
crcbl_hal::null::Event::BufferWritten { buffer, len, .. } => Some((buffer, len)),
_ => None,
})
.collect()
}
fn test_art_rect() -> crcbl_ui::image::TexelRect {
let mut images = ImageAtlas::new();
test_skin(&mut images);
images
.dirty()
.expect("registering the test art dirtied the page")
}
fn cleared_before_copied(
commands: &[crcbl_hal::null::Command],
buffer: BufferHandle,
bytes: u64,
) -> bool {
use crcbl_hal::null::Command;
let cleared = commands.iter().position(|command| {
matches!(command, Command::ClearBuffer { buffer: cleared, offset: 0, size }
if *cleared == buffer && *size >= bytes)
});
let copied = commands.iter().position(
|command| matches!(command, Command::CopyBufferToImage(copy) if copy.buffer == buffer),
);
matches!((cleared, copied), (Some(cleared), Some(copied)) if cleared < copied)
}
#[test]
fn the_image_atlas_page_is_zeroed_on_the_gpu_and_only_the_art_is_staged() {
let (recorder, device, queue) = open_recorded();
let (renderer, _) = with_test_skin(device.as_ref(), queue);
let commands = recorder.commands();
let copies: Vec<_> = image_copies(&commands)
.into_iter()
.filter(|copy| copy.image == renderer.image_page.image)
.collect();
let rect = test_art_rect();
let [.., art] = copies[..] else {
panic!("expected the zeroed page and then the art, got {copies:?}");
};
let whole = copies[0];
let page_bytes = u64::from(PAGE_SIZE) * u64::from(PAGE_SIZE) * 4;
assert!(
cleared_before_copied(&commands, whole.buffer, page_bytes),
"the zero copies must read a buffer zeroed first, in {commands:?}"
);
let mut writes = vec![0u8; PAGE_SIZE as usize * PAGE_SIZE as usize];
for copy in &copies {
assert!(
copy.buffer == whole.buffer || copy.buffer == art.buffer,
"a copy from neither the zeroed buffer nor the art: {copy:?}"
);
let Offset3d { x, y, .. } = copy.image_offset;
for row in 0..copy.image_extent.height {
let start = (y as u32 + row) * PAGE_SIZE + x as u32;
for texel in start..start + copy.image_extent.width {
writes[texel as usize] += 1;
}
}
}
assert!(
writes.iter().all(|&count| count == 1),
"a page texel was written twice or never: {copies:?}"
);
assert_ne!(art.buffer, whole.buffer, "the art is staged, not zeroed");
assert_eq!(art.image_extent, Extent3d::d2(rect.width, rect.height));
assert_eq!(
(art.image_offset.x, art.image_offset.y),
(rect.x as i32, rect.y as i32)
);
let writes = buffer_writes(&recorder);
assert!(
writes.iter().all(|(buffer, _)| *buffer != whole.buffer),
"the zeroed buffer is never written from the host: {writes:?}"
);
assert_eq!(
writes
.iter()
.filter(|(buffer, _)| *buffer == art.buffer)
.map(|(_, len)| *len)
.sum::<usize>(),
rect.width as usize * rect.height as usize * 4,
"the art's staging is its own rectangle at four bytes a texel"
);
assert_eq!(renderer.images().dirty(), None, "the page owes nothing");
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn construction_writes_only_the_font_bitmap_and_the_registered_art() {
let (recorder, device, queue) = open_recorded();
let (renderer, _) = with_test_skin(device.as_ref(), queue);
let (_, _, font) = FontAtlas::built_in().glyph_bitmap();
let rect = test_art_rect();
let art = rect.width as usize * rect.height as usize * 4;
let writes = buffer_writes(&recorder);
assert_eq!(
writes.iter().map(|(_, len)| *len).sum::<usize>(),
font.len() + art,
"construction wrote {writes:?}; the font bitmap is {} bytes and the art {art}",
font.len()
);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn construction_with_no_art_writes_only_the_font_bitmap() {
let (recorder, device, queue) = open_recorded();
let renderer = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let (_, _, font) = FontAtlas::built_in().glyph_bitmap();
let writes = buffer_writes(&recorder);
assert_eq!(
writes.iter().map(|(_, len)| *len).sum::<usize>(),
font.len(),
"construction wrote {writes:?}; the font bitmap is {} bytes",
font.len()
);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn a_frame_with_no_new_image_uploads_nothing() {
let (recorder, device, queue) = open_recorded();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
recorder.clear();
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &one_rect());
assert_eq!(labels, ["ui-composite"]);
assert!(image_copies(&recorder.commands()).is_empty());
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn a_registered_image_is_copied_once_as_its_own_rectangle_inside_the_graph() {
use crcbl_hal::null::Command;
let (recorder, device, queue) = open_recorded();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let image = ui
.images_mut()
.register(5, 3, &[200; 5 * 3 * 4])
.expect("fits an empty page");
let dirty = ui
.images()
.dirty()
.expect("the registration dirtied the page");
recorder.clear();
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &one_rect());
assert_eq!(
labels,
["ui-images", "ui-composite"],
"the copy comes before the draw that samples it"
);
let commands = recorder.commands();
let copies = image_copies(&commands);
assert_eq!(copies.len(), 1, "{copies:?}");
let copy = copies[0];
assert_eq!(copy.image, ui.image_page.image);
assert_eq!(
(copy.image_offset.x, copy.image_offset.y),
(dirty.x as i32, dirty.y as i32)
);
assert_eq!(copy.image_extent, Extent3d::d2(dirty.width, dirty.height));
assert!(
dirty.x <= image.x
&& dirty.y <= image.y
&& image.x + image.width <= dirty.x + dirty.width
&& image.y + image.height <= dirty.y + dirty.height,
"the copied rectangle {dirty:?} does not cover the image {image:?}"
);
let page_transitions: Vec<_> = commands
.iter()
.filter_map(|command| match command {
Command::Barrier { images, .. } => Some(images.clone()),
_ => None,
})
.flatten()
.filter(|barrier| barrier.image == ui.image_page.image)
.map(|barrier| (barrier.from, barrier.to))
.collect();
assert_eq!(
page_transitions,
[
(ResourceState::ShaderRead, ResourceState::TransferDst),
(ResourceState::TransferDst, ResourceState::ShaderRead),
]
);
let returned = commands
.iter()
.position(|command| match command {
Command::Barrier { images, .. } => images.iter().any(|barrier| {
barrier.image == ui.image_page.image && barrier.to == ResourceState::ShaderRead
}),
_ => false,
})
.expect("the page is returned to ShaderRead");
let drawn = commands
.iter()
.position(|command| matches!(command, Command::DrawIndexed { .. }))
.expect("the rect is drawn");
assert!(
returned < drawn,
"the page went back to ShaderRead at command {returned}, after the draw at {drawn}"
);
recorder.clear();
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &one_rect());
assert_eq!(labels, ["ui-composite"], "uploaded once, not every frame");
assert!(image_copies(&recorder.commands()).is_empty());
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
recorder.assert_valid();
}
#[test]
fn an_upload_that_was_never_recorded_is_staged_again() {
let (device, queue) = open();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
ui.images_mut()
.register(4, 4, &[9; 4 * 4 * 4])
.expect("fits");
let atlas = FontAtlas::built_in();
ui.begin_frame(device.as_ref(), &one_rect(), &atlas, 1.0)
.expect("upload");
let first = ui.image_upload.expect("staged").rect;
ui.begin_frame(device.as_ref(), &one_rect(), &atlas, 1.0)
.expect("upload");
let again = ui.image_upload.expect("staged again").rect;
assert_eq!(first, again);
ui.destroy(device.as_ref());
}
#[cfg(feature = "parsed-font")]
fn glyph_run(text: &str) -> DrawList {
use crcbl_ui::font::Font;
use crcbl_ui::font::layout::TextLayout;
let font = Font::sans();
let layout = TextLayout::new(font, text, 20.0, 24.0, None);
let mut list = DrawList::new();
list.glyphs(
glam::Vec2::splat(4.0),
font,
20.0,
[1.0; 4],
layout.glyphs(),
);
list
}
#[test]
fn the_glyph_pages_are_every_layer_uploaded_empty_at_start_up() {
let (recorder, device, queue) = open_recorded();
let renderer = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let commands = recorder.commands();
let copies: Vec<_> = image_copies(&commands)
.into_iter()
.filter(|copy| copy.image == renderer.glyph_pages.image)
.collect();
let layers: Vec<u32> = copies
.iter()
.map(|copy| {
assert_eq!(
copy.image_extent,
Extent3d::d2(GLYPH_PAGE_SIZE, GLYPH_PAGE_SIZE)
);
assert_eq!(copy.image_subresource.layer_count, 1);
copy.image_subresource.base_layer
})
.collect();
assert_eq!(layers, (0..GLYPH_MAX_PAGES as u32).collect::<Vec<_>>());
let page_bytes = u64::from(GLYPH_PAGE_SIZE) * u64::from(GLYPH_PAGE_SIZE);
let writes = buffer_writes(&recorder);
for copy in &copies {
assert!(
cleared_before_copied(&commands, copy.buffer, page_bytes),
"layer {} must be copied from a buffer zeroed first, in {commands:?}",
copy.image_subresource.base_layer
);
assert!(
writes.iter().all(|(buffer, _)| *buffer != copy.buffer),
"the zeroed buffer is never written from the host: {writes:?}"
);
}
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
#[cfg(feature = "parsed-font")]
#[test]
fn rasterised_glyphs_are_copied_into_their_page_once_ahead_of_the_draw() {
use crcbl_hal::null::Command;
let (recorder, device, queue) = open_recorded();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
recorder.clear();
let list = glyph_run("Kerned AVATAR");
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &list);
assert_eq!(labels, ["ui-glyphs", "ui-composite"]);
assert!(ui.glyphs().stats().rasterized > 0);
let commands = recorder.commands();
let copies: Vec<_> = image_copies(&commands)
.into_iter()
.filter(|copy| copy.image == ui.glyph_pages.image)
.collect();
assert_eq!(copies.len(), 1, "one page, one rectangle: {copies:?}");
let copy = copies[0];
assert_eq!(copy.image_subresource.base_layer, 0);
let (vertices, _) = list.to_triangles(None, None, 1.0);
assert!(vertices.is_empty(), "the run needs the glyph atlas to draw");
let triangles = ui.last_index_count[ui.frame] / 6;
assert!(triangles >= 11, "{triangles} glyph quads");
let page = GLYPH_PAGE_SIZE as f32;
let mut atlas = crcbl_ui::font::atlas::GlyphAtlas::new(GLYPH_PAGE_SIZE, GLYPH_MAX_PAGES, 1000);
atlas.begin_frame();
let (vertices, _) = list.to_triangles(None, Some(&mut atlas), 1.0);
for vertex in vertices {
let texel = vertex.uv * page;
assert!(
texel.x >= copy.image_offset.x as f32
&& texel.y >= copy.image_offset.y as f32
&& texel.x <= (copy.image_offset.x as u32 + copy.image_extent.width) as f32
&& texel.y <= (copy.image_offset.y as u32 + copy.image_extent.height) as f32,
"a glyph samples {texel} outside the copied {copy:?}"
);
}
let returned = commands
.iter()
.position(|command| match command {
Command::Barrier { images, .. } => images.iter().any(|barrier| {
barrier.image == ui.glyph_pages.image && barrier.to == ResourceState::ShaderRead
}),
_ => false,
})
.expect("the pages are returned to ShaderRead");
let drawn = commands
.iter()
.position(|command| matches!(command, Command::DrawIndexed { .. }))
.expect("the glyphs are drawn");
assert!(returned < drawn);
recorder.clear();
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &list);
assert_eq!(
labels,
["ui-composite"],
"cached glyphs were uploaded again"
);
assert_eq!(ui.glyphs().stats().rasterized, 0);
assert!(image_copies(&recorder.commands()).is_empty());
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
recorder.assert_valid();
}
#[cfg(feature = "parsed-font")]
#[test]
fn a_colour_glyph_is_drawn_as_an_image_staged_in_its_own_frame() {
use crcbl_ui::font::Font;
use crcbl_ui::font::layout::TextLayout;
let (recorder, device, queue) = open_recorded();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
recorder.clear();
let emoji = Font::parse(include_bytes!(
"../../../crcbl-ui/tests/fixtures/emoji/cbdt.ttf"
))
.expect("the fixture parses");
let layout = TextLayout::new(&emoji, "\u{1F600}", 16.0, 20.0, None);
let mut list = DrawList::new();
list.glyphs(
glam::Vec2::splat(4.0),
&emoji,
16.0,
[1.0; 4],
layout.glyphs(),
);
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &list);
assert_eq!(labels, ["ui-images", "ui-composite"]);
assert_eq!(ui.last_index_count[ui.frame], 6, "one quad");
let pictures = ui.images().images();
assert_eq!(pictures.len(), 1, "the emoji's picture");
let picture = pictures[0];
assert_eq!((picture.width, picture.height), (16, 16));
let copies: Vec<_> = image_copies(&recorder.commands())
.into_iter()
.filter(|copy| copy.image == ui.image_page.image)
.collect();
assert_eq!(copies.len(), 1, "{copies:?}");
let (x, y) = (
copies[0].image_offset.x as u32,
copies[0].image_offset.y as u32,
);
let extent = copies[0].image_extent;
assert!(
x <= picture.x
&& y <= picture.y
&& picture.x + picture.width <= x + extent.width
&& picture.y + picture.height <= y + extent.height,
"{:?} does not cover {picture:?}",
copies[0]
);
recorder.clear();
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &list);
assert_eq!(labels, ["ui-composite"]);
assert_eq!(ui.last_index_count[ui.frame], 6);
assert_eq!(ui.images().images().len(), 1);
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
recorder.assert_valid();
}
#[cfg(feature = "parsed-font")]
#[test]
fn a_glyph_upload_that_was_never_recorded_is_staged_again() {
let (device, queue) = open();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let atlas = FontAtlas::built_in();
let list = glyph_run("again");
ui.begin_frame(device.as_ref(), &list, &atlas, 1.0)
.expect("upload");
let first: Vec<TexelRect> = ui.glyph_uploads.iter().map(|upload| upload.rect).collect();
assert_eq!(first.len(), 1);
ui.begin_frame(device.as_ref(), &list, &atlas, 1.0)
.expect("upload");
let again: Vec<TexelRect> = ui.glyph_uploads.iter().map(|upload| upload.rect).collect();
assert_eq!(first, again);
ui.destroy(device.as_ref());
}
#[cfg(feature = "parsed-font")]
#[test]
fn glyph_uploads_leak_nothing() {
let (recorder, device, queue) = open_recorded();
let before = recorder.total_live_objects();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
for (round, text) in ["abc", "def", "ghi", "jkl"].into_iter().enumerate() {
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &glyph_run(text));
assert_eq!(labels[0], "ui-glyphs", "round {round}");
}
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
assert_eq!(recorder.total_live_objects(), before);
recorder.assert_valid();
}
#[test]
fn image_uploads_leak_nothing() {
let (recorder, device, queue) = open_recorded();
let before = recorder.total_live_objects();
let mut pool = crate::transient::TransientPool::new();
let mut ui = UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
for round in 0..(FRAMES_IN_FLIGHT * 2) {
ui.images_mut()
.register(3, 3, &[round as u8; 3 * 3 * 4])
.expect("fits");
let labels = record_frame(&mut ui, device.as_ref(), queue, &mut pool, &one_rect());
assert_eq!(labels[0], "ui-images", "round {round}");
}
ui.destroy(device.as_ref());
pool.destroy(device.as_ref());
assert_eq!(recorder.total_live_objects(), before);
recorder.assert_valid();
}