use std::ops::{Bound, RangeBounds};
use std::time::{Duration, Instant};
use crate::error::Error;
use crate::renderer::{Info, Renderer};
pub struct StripFrame {
pub label: String,
pub width: u32,
pub height: u32,
pub pixels: Vec<u8>,
pub info: Info,
pub time: Duration,
}
pub struct Strip {
pub frames: Vec<StripFrame>,
}
pub type Step<'a, A> = (&'a str, &'a mut dyn FnMut(&mut A));
pub fn strip<A>(
app: &mut A,
renderer: fn(&mut A) -> &mut Renderer,
render: &mut dyn FnMut(&mut A),
steps: &mut [Step<'_, A>],
) -> Result<Strip, Error> {
let auto_reset = renderer(app).info().auto_reset;
renderer(app).info_mut().auto_reset = false;
let mut frames = Vec::with_capacity(steps.len() + 1);
let mut result = capture(app, renderer, render, "").map(|frame| frames.push(frame));
for (label, step) in steps.iter_mut() {
if result.is_err() {
break;
}
step(app);
result = capture(app, renderer, render, label).map(|frame| frames.push(frame));
}
renderer(app).info_mut().auto_reset = auto_reset;
result?;
Ok(Strip { frames })
}
fn capture<A>(
app: &mut A,
renderer: fn(&mut A) -> &mut Renderer,
render: &mut dyn FnMut(&mut A),
label: &str,
) -> Result<StripFrame, Error> {
renderer(app).info_mut().reset();
let started = Instant::now();
render(app);
renderer(app)
.device()
.poll(wgpu::PollType::wait_indefinitely())
.map_err(|e| Error::Readback {
reason: e.to_string(),
})?;
let time = started.elapsed();
let info = renderer(app).info().clone();
let (width, height, pixels) = renderer(app).read_canvas_pixels()?;
Ok(StripFrame {
label: label.to_string(),
width,
height,
pixels,
info,
time,
})
}
impl Strip {
pub fn times(&self) -> Vec<Duration> {
self.frames.iter().map(|frame| frame.time).collect()
}
pub fn assert_steady(&self, range: impl RangeBounds<usize>) {
self.assert_steady_uploading(range, 0);
}
pub fn assert_steady_uploading(&self, range: impl RangeBounds<usize>, textures: u64) {
let start = match range.start_bound() {
Bound::Included(index) => *index,
Bound::Excluded(index) => index + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(index) => index + 1,
Bound::Excluded(index) => *index,
Bound::Unbounded => self.frames.len(),
};
assert!(
end <= self.frames.len(),
"three-rs: the strip has {} frames, not {end}",
self.frames.len()
);
for (index, frame) in self.frames.iter().enumerate().take(end).skip(start) {
let build = &frame.info.build;
assert_eq!(
(
build.total() - build.textures_uploaded,
build.textures_uploaded
),
(0, textures),
"frame {index} ({}) built {build:?}; a steady frame must build \
nothing beyond {textures} texture upload(s)",
frame.label,
);
}
}
pub fn write_png(&self, path: &str) {
assert!(!self.frames.is_empty(), "three-rs: the strip has no frames");
let width: u32 = self.frames.iter().map(|frame| frame.width).sum();
let tallest = self
.frames
.iter()
.map(|frame| frame.height)
.max()
.expect("three-rs: the strip has frames");
let height = tallest + GUTTER;
let mut canvas = vec![0u8; (width * height * 4) as usize];
for pixel in canvas.as_chunks_mut::<4>().0.iter_mut() {
*pixel = BACKGROUND;
}
let mut x = 0;
for (index, frame) in self.frames.iter().enumerate() {
blit(&mut canvas, width, x, 0, frame);
gutter(&mut canvas, width, x, tallest, frame.width, index, frame);
x += frame.width;
}
super::write_png(path, width, height, &canvas);
}
}
fn blit(canvas: &mut [u8], canvas_width: u32, x: u32, y: u32, frame: &StripFrame) {
for row in 0..frame.height {
let from = (row * frame.width * 4) as usize;
let to = ((y + row) * canvas_width + x) as usize * 4;
let bytes = (frame.width * 4) as usize;
canvas[to..to + bytes].copy_from_slice(&frame.pixels[from..from + bytes]);
}
}
fn gutter(
canvas: &mut [u8],
canvas_width: u32,
x: u32,
y: u32,
cell_width: u32,
index: usize,
frame: &StripFrame,
) {
let label = if frame.label.is_empty() {
format!("#{index}")
} else {
format!("#{index} {}", frame.label)
};
let render = &frame.info.render;
let build = &frame.info.build;
let lines = [
label,
format!(
"CALLS {} TRI {} LINES {}",
render.calls, render.triangles, render.lines
),
format!(
"PROG {} PIPE {} GEO {} BUF {} TEX {}",
build.programs_compiled,
build.pipelines_built,
build.geometries_uploaded,
build.buffers_written,
build.textures_uploaded,
),
];
let colours = [LABEL, TEXT, if build.total() == 0 { TEXT } else { BUILT }];
let room = (cell_width.saturating_sub(PAD * 2) / ADVANCE) as usize;
for (line, (text, colour)) in lines.iter().zip(colours).enumerate() {
let text: String = text.chars().take(room).collect();
draw_text(
canvas,
canvas_width,
x + PAD,
y + PAD + line as u32 * LINE,
&text,
colour,
);
}
}
const GUTTER: u32 = PAD * 2 + LINE * 3;
const PAD: u32 = 4;
const LINE: u32 = GLYPH_HEIGHT * SCALE + 4;
const SCALE: u32 = 2;
const GLYPH_WIDTH: u32 = 3;
const GLYPH_HEIGHT: u32 = 5;
const ADVANCE: u32 = (GLYPH_WIDTH + 1) * SCALE;
const BACKGROUND: [u8; 4] = [24, 24, 24, 255];
const LABEL: [u8; 4] = [255, 255, 255, 255];
const TEXT: [u8; 4] = [170, 170, 170, 255];
const BUILT: [u8; 4] = [255, 176, 64, 255];
fn draw_text(canvas: &mut [u8], canvas_width: u32, x: u32, y: u32, text: &str, colour: [u8; 4]) {
for (index, character) in text.chars().enumerate() {
let rows = glyph(character);
let origin = x + index as u32 * ADVANCE;
for (row, bits) in rows.iter().enumerate() {
for column in 0..GLYPH_WIDTH {
if bits & (1 << (GLYPH_WIDTH - 1 - column)) == 0 {
continue;
}
for dy in 0..SCALE {
for dx in 0..SCALE {
let px = origin + column * SCALE + dx;
let py = y + row as u32 * SCALE + dy;
let at = ((py * canvas_width + px) * 4) as usize;
if px < canvas_width && at + 4 <= canvas.len() {
canvas[at..at + 4].copy_from_slice(&colour);
}
}
}
}
}
}
}
fn glyph(character: char) -> [u8; GLYPH_HEIGHT as usize] {
match character.to_ascii_uppercase() {
' ' => [0b000, 0b000, 0b000, 0b000, 0b000],
'A' => [0b111, 0b101, 0b111, 0b101, 0b101],
'B' => [0b110, 0b101, 0b110, 0b101, 0b110],
'C' => [0b111, 0b100, 0b100, 0b100, 0b111],
'D' => [0b110, 0b101, 0b101, 0b101, 0b110],
'E' => [0b111, 0b100, 0b110, 0b100, 0b111],
'F' => [0b111, 0b100, 0b110, 0b100, 0b100],
'G' => [0b111, 0b100, 0b101, 0b101, 0b111],
'H' => [0b101, 0b101, 0b111, 0b101, 0b101],
'I' => [0b111, 0b010, 0b010, 0b010, 0b111],
'J' => [0b001, 0b001, 0b001, 0b101, 0b111],
'K' => [0b101, 0b101, 0b110, 0b101, 0b101],
'L' => [0b100, 0b100, 0b100, 0b100, 0b111],
'M' => [0b111, 0b111, 0b101, 0b101, 0b101],
'N' => [0b110, 0b101, 0b101, 0b101, 0b101],
'O' => [0b111, 0b101, 0b101, 0b101, 0b111],
'P' => [0b111, 0b101, 0b111, 0b100, 0b100],
'Q' => [0b111, 0b101, 0b101, 0b111, 0b011],
'R' => [0b111, 0b101, 0b111, 0b110, 0b101],
'S' => [0b111, 0b100, 0b111, 0b001, 0b111],
'T' => [0b111, 0b010, 0b010, 0b010, 0b010],
'U' => [0b101, 0b101, 0b101, 0b101, 0b111],
'V' => [0b101, 0b101, 0b101, 0b101, 0b010],
'W' => [0b101, 0b101, 0b101, 0b111, 0b111],
'X' => [0b101, 0b101, 0b010, 0b101, 0b101],
'Y' => [0b101, 0b101, 0b010, 0b010, 0b010],
'Z' => [0b111, 0b001, 0b010, 0b100, 0b111],
'0' => [0b111, 0b101, 0b101, 0b101, 0b111],
'1' => [0b010, 0b110, 0b010, 0b010, 0b111],
'2' => [0b111, 0b001, 0b111, 0b100, 0b111],
'3' => [0b111, 0b001, 0b111, 0b001, 0b111],
'4' => [0b101, 0b101, 0b111, 0b001, 0b001],
'5' => [0b111, 0b100, 0b111, 0b001, 0b111],
'6' => [0b111, 0b100, 0b111, 0b101, 0b111],
'7' => [0b111, 0b001, 0b001, 0b001, 0b001],
'8' => [0b111, 0b101, 0b111, 0b101, 0b111],
'9' => [0b111, 0b101, 0b111, 0b001, 0b111],
'#' => [0b101, 0b111, 0b101, 0b111, 0b101],
'.' => [0b000, 0b000, 0b000, 0b000, 0b010],
',' => [0b000, 0b000, 0b000, 0b010, 0b100],
':' => [0b000, 0b010, 0b000, 0b010, 0b000],
'-' => [0b000, 0b000, 0b111, 0b000, 0b000],
'+' => [0b000, 0b010, 0b111, 0b010, 0b000],
'=' => [0b000, 0b111, 0b000, 0b111, 0b000],
'/' => [0b001, 0b001, 0b010, 0b100, 0b100],
'(' => [0b001, 0b010, 0b010, 0b010, 0b001],
')' => [0b100, 0b010, 0b010, 0b010, 0b100],
_ => [0b111, 0b001, 0b011, 0b000, 0b010],
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_glyph_fits_its_cell() {
const { assert!(GLYPH_WIDTH * SCALE < ADVANCE) };
for row in glyph('W') {
assert_eq!(row & !0b111, 0, "a glyph row is three bits wide");
}
}
#[test]
fn the_gutter_holds_its_three_lines() {
assert_eq!(GUTTER, PAD * 2 + LINE * 3);
const { assert!(GUTTER > GLYPH_HEIGHT * SCALE * 3) };
}
#[test]
fn an_unknown_character_is_a_question_mark() {
assert_eq!(glyph('~'), glyph('\u{1f600}'));
assert_ne!(glyph('~'), glyph(' '));
assert_eq!(glyph('a'), glyph('A'));
}
}