use crcbl_ui::debug::{DebugModule, DebugSection};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FrameCounters {
pub draws: u64,
pub instances: u64,
pub drawn: Option<u64>,
pub triangles: Option<u64>,
pub clusters: Option<u64>,
pub cull_frame: Option<u64>,
}
impl Default for FrameCounters {
fn default() -> Self {
Self {
draws: 0,
instances: 0,
drawn: Some(0),
triangles: Some(0),
clusters: Some(0),
cull_frame: None,
}
}
}
impl FrameCounters {
#[must_use]
pub const fn plus(self, other: Self) -> Self {
Self {
draws: self.draws + other.draws,
instances: self.instances + other.instances,
drawn: match (self.drawn, other.drawn) {
(Some(mine), Some(theirs)) => Some(mine + theirs),
_ => None,
},
triangles: match (self.triangles, other.triangles) {
(Some(mine), Some(theirs)) => Some(mine + theirs),
_ => None,
},
clusters: match (self.clusters, other.clusters) {
(Some(mine), Some(theirs)) => Some(mine + theirs),
_ => None,
},
cull_frame: match (self.cull_frame, other.cull_frame) {
(Some(mine), Some(theirs)) if theirs < mine => Some(theirs),
(Some(mine), _) => Some(mine),
(None, theirs) => theirs,
},
}
}
}
pub const INDIRECT: &str = "indirect";
pub const UNKNOWN: &str = "unknown";
impl DebugModule for FrameCounters {
fn debug_section(&self, out: &mut DebugSection) {
out.set_title("counters");
out.row("draws recorded", format_args!("{}", self.draws));
out.row("instances submitted", format_args!("{}", self.instances));
write_known(out, "instances drawn", self.drawn, INDIRECT);
write_known(out, "triangles drawn", self.triangles, INDIRECT);
write_known(out, "clusters drawn", self.clusters, UNKNOWN);
if let Some(frame) = self.cull_frame {
out.row("cull frame", format_args!("{frame}"));
}
}
}
fn write_known(out: &mut DebugSection, label: &str, value: Option<u64>, absent: &str) {
match value {
Some(value) => out.row(label, format_args!("{value}")),
None => out.row_str(label, absent),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rows(counters: &FrameCounters) -> Vec<(String, String)> {
let mut section = DebugSection::default();
counters.debug_section(&mut section);
assert_eq!(section.title(), "counters");
section
.rows()
.iter()
.map(|row| (row.label.clone(), row.value.clone()))
.collect()
}
fn known(draws: u64, instances: u64, triangles: u64) -> FrameCounters {
FrameCounters {
draws,
instances,
drawn: Some(instances),
triangles: Some(triangles),
clusters: Some(0),
cull_frame: None,
}
}
#[test]
fn the_row_renders_the_counters_it_was_given() {
assert_eq!(
rows(&known(3, 412, 824)),
vec![
("draws recorded".to_owned(), "3".to_owned()),
("instances submitted".to_owned(), "412".to_owned()),
("instances drawn".to_owned(), "412".to_owned()),
("triangles drawn".to_owned(), "824".to_owned()),
("clusters drawn".to_owned(), "0".to_owned()),
]
);
assert_eq!(
rows(&known(5, 9, 18)),
vec![
("draws recorded".to_owned(), "5".to_owned()),
("instances submitted".to_owned(), "9".to_owned()),
("instances drawn".to_owned(), "9".to_owned()),
("triangles drawn".to_owned(), "18".to_owned()),
("clusters drawn".to_owned(), "0".to_owned()),
]
);
}
#[test]
fn the_cull_frame_row_appears_only_when_a_counter_came_off_the_gpu() {
let labels: Vec<String> = rows(&known(3, 412, 824))
.into_iter()
.map(|(label, _)| label)
.collect();
assert!(
!labels.contains(&"cull frame".to_owned()),
"nothing here came off the GPU: {labels:?}",
);
let latent = FrameCounters {
draws: 15,
instances: 4097,
drawn: Some(2043),
triangles: None,
clusters: Some(881),
cull_frame: Some(37),
};
assert_eq!(
rows(&latent),
vec![
("draws recorded".to_owned(), "15".to_owned()),
("instances submitted".to_owned(), "4097".to_owned()),
("instances drawn".to_owned(), "2043".to_owned()),
("triangles drawn".to_owned(), INDIRECT.to_owned()),
("clusters drawn".to_owned(), "881".to_owned()),
("cull frame".to_owned(), "37".to_owned()),
]
);
}
#[test]
fn adding_keeps_the_oldest_frame_either_side_drew_on() {
let sprites = known(2, 30, 60);
let latent = FrameCounters {
cull_frame: Some(37),
..known(1, 5, 10)
};
let older = FrameCounters {
cull_frame: Some(31),
..known(1, 5, 10)
};
assert_eq!(sprites.plus(latent).cull_frame, Some(37));
assert_eq!(latent.plus(sprites).cull_frame, Some(37));
assert_eq!(latent.plus(older).cull_frame, Some(31));
assert_eq!(older.plus(latent).cull_frame, Some(31));
}
#[test]
fn an_unknown_counter_is_a_word_and_not_a_zero() {
let indirect = FrameCounters {
draws: 4,
instances: 7,
drawn: None,
triangles: None,
clusters: None,
cull_frame: None,
};
assert_eq!(
rows(&indirect),
vec![
("draws recorded".to_owned(), "4".to_owned()),
("instances submitted".to_owned(), "7".to_owned()),
("instances drawn".to_owned(), INDIRECT.to_owned()),
("triangles drawn".to_owned(), INDIRECT.to_owned()),
("clusters drawn".to_owned(), UNKNOWN.to_owned()),
]
);
assert_ne!(
INDIRECT, UNKNOWN,
"the two absences are different claims and must read differently",
);
assert_eq!(
rows(&known(0, 0, 0))[2],
("instances drawn".to_owned(), "0".to_owned())
);
}
#[test]
fn submitted_and_drawn_are_not_the_same_field() {
let culled = FrameCounters {
draws: 1,
instances: 900,
drawn: Some(120),
triangles: Some(240),
clusters: None,
cull_frame: Some(9),
};
let rendered = rows(&culled);
assert_eq!(
rendered[1],
("instances submitted".to_owned(), "900".to_owned())
);
assert_eq!(
rendered[2],
("instances drawn".to_owned(), "120".to_owned())
);
}
#[test]
fn adding_sums_what_is_known_and_gives_up_on_what_is_not() {
let sprites = known(3, 400, 800);
let ui = known(1, 1, 26);
assert_eq!(
sprites.plus(ui),
FrameCounters {
draws: 4,
instances: 401,
drawn: Some(401),
triangles: Some(826),
clusters: Some(0),
cull_frame: None,
}
);
assert_eq!(sprites.plus(ui), ui.plus(sprites));
let indirect = FrameCounters {
draws: 15,
instances: 6,
drawn: None,
triangles: None,
clusters: None,
cull_frame: None,
};
let total = sprites.plus(indirect);
assert_eq!(
total.draws, 18,
"the draws are still known: the CPU records them"
);
assert_eq!(total.instances, 406);
assert_eq!(
total.drawn, None,
"400 of an unknown number is not a total of instances drawn"
);
assert_eq!(total.triangles, None);
assert_eq!(total.clusters, None);
}
#[test]
fn the_empty_value_is_zero_and_known_and_is_the_identity_of_plus() {
let nothing = FrameCounters::default();
assert_eq!(nothing.draws, 0);
assert_eq!(nothing.instances, 0);
assert_eq!(nothing.drawn, Some(0));
assert_eq!(nothing.triangles, Some(0));
assert_eq!(nothing.clusters, Some(0));
assert_eq!(nothing.cull_frame, None);
assert_eq!(nothing.plus(nothing), nothing);
let sprites = known(2, 30, 60);
assert_eq!(nothing.plus(sprites), sprites);
assert_eq!(sprites.plus(nothing), sprites);
}
#[test]
fn the_sections_labels_are_its_own() {
let labels: Vec<String> = rows(&FrameCounters {
cull_frame: Some(1),
..FrameCounters::default()
})
.into_iter()
.map(|(label, _)| label)
.collect();
assert_eq!(
labels,
[
"draws recorded",
"instances submitted",
"instances drawn",
"triangles drawn",
"clusters drawn",
"cull frame",
]
);
}
}