concinnity_asset/stat_hud.rs
1// Stats HUD schema.
2
3use crate::{AssetId, de_opt_asset_ref};
4
5/// Requests the default on-screen stats HUD. Drives a set of
6/// [TextLabel](#textlabel) chips with live engine stats, refreshed on a fixed
7/// interval.
8///
9/// Each label field, when set, receives one chip: `fps_label` the averaged
10/// frame rate, `vram_label` the GPU-memory use, `ram_label` the host process
11/// memory (resident set size, against the memory budget when known), `ev_label`
12/// the auto-exposure value, and `edr_label` the HDR headroom multiplier. Chips
13/// whose stat is unavailable stay blank. The frame-rate and GPU-memory chips
14/// are shown or hidden from the in-game video settings ("Display performance
15/// stats"); the host-memory, exposure, and HDR chips show whenever their
16/// reading is available.
17///
18/// The chips are packed into a tight strip anchored at the top-left of the
19/// window, left to right in the order fps, vram, ram, ev, edr; a blank chip
20/// reserves no width, so hidden readouts leave no gap. Their on-screen position
21/// is fixed by the engine rather than the authored coordinates.
22///
23/// Developer-facing readouts (per-pass GPU timings, cursor position, live
24/// camera pose) live on the separate [DebugHud](#debughud), toggled with F1.
25///
26/// A world that declares a [MainMenu](#mainmenu) receives a `StatHud`, its
27/// chip labels, and their font at build time when it declares none (the
28/// menu's performance-stats toggles drive the chips), so the example below is
29/// only needed to restyle the chips or run a HUD without a menu. Declare an
30/// [EngineDefaults](#enginedefaults) with `"hud": false` to remove the
31/// injection entirely.
32#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
33#[serde(default)]
34pub struct StatHud {
35 /// [TextLabel](#textlabel) that receives the frame-rate chip text.
36 #[serde(deserialize_with = "de_opt_asset_ref")]
37 pub fps_label: Option<AssetId>,
38 /// [TextLabel](#textlabel) that receives the GPU-memory chip text.
39 #[serde(deserialize_with = "de_opt_asset_ref")]
40 pub vram_label: Option<AssetId>,
41 /// [TextLabel](#textlabel) that receives the host-memory (RSS) chip text.
42 #[serde(deserialize_with = "de_opt_asset_ref")]
43 pub ram_label: Option<AssetId>,
44 /// [TextLabel](#textlabel) that receives the auto-exposure chip text.
45 #[serde(deserialize_with = "de_opt_asset_ref")]
46 pub ev_label: Option<AssetId>,
47 /// [TextLabel](#textlabel) that receives the HDR-headroom chip text.
48 #[serde(deserialize_with = "de_opt_asset_ref")]
49 pub edr_label: Option<AssetId>,
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn a_blank_hud_claims_no_labels() {
58 // Each chip is opt-in, so an unset slot suppresses that readout instead
59 // of drawing it somewhere arbitrary.
60 let h = StatHud::default();
61 assert!(h.fps_label.is_none());
62 assert!(h.vram_label.is_none());
63 assert!(h.ram_label.is_none());
64 assert!(h.ev_label.is_none());
65 assert!(h.edr_label.is_none());
66 }
67
68 #[test]
69 fn each_chip_binds_its_own_label_and_round_trips_through_postcard() {
70 crate::test_support::install_resolvers();
71 let h: StatHud = serde_json::from_str(
72 r#"{"fps_label":"fps_chip","vram_label":"vram","ram_label":"","ev_label":3,
73 "edr_label":"edr_chip"}"#,
74 )
75 .unwrap();
76 assert_eq!(h.fps_label, Some(AssetId(8)));
77 assert_eq!(h.vram_label, Some(AssetId(4)));
78 assert_eq!(h.ram_label, None);
79 assert_eq!(h.ev_label, Some(AssetId(3)));
80 assert_eq!(h.edr_label, Some(AssetId(8)));
81
82 let bytes = postcard::to_allocvec(&h).unwrap();
83 let back: StatHud = postcard::from_bytes(&bytes).unwrap();
84 assert_eq!(back.fps_label, Some(AssetId(8)));
85 assert_eq!(back.ram_label, None);
86 assert_eq!(back.ev_label, Some(AssetId(3)));
87 }
88}