Skip to main content

concinnity_asset/
debug_hud.rs

1// Developer debug HUD schema.
2
3use crate::{AssetId, de_opt_asset_ref};
4
5/// Requests the developer debug HUD: a set of [TextLabel](#textlabel) chips
6/// with diagnostic readouts, anchored to the top-right of the window and
7/// toggled with F1 (hidden by default).
8///
9/// Each label field, when set, receives one chip: `passes_label` a multi-line
10/// list of the heaviest rendering steps of the last frame, `mouse_label` the
11/// cursor position in window pixels, `camera_label` the live camera pose
12/// (position, yaw, pitch) in the exact form a fixed viewpoint is reproduced
13/// with, and `sys_label` the worker-thread and host-memory budgets (the job
14/// pool's thread count, and the process resident set against the memory
15/// budget). Chips whose stat is unavailable stay blank. The chips stack
16/// vertically from the top-right corner in the order cursor, then camera, then
17/// system, then passes (passes is last because its height varies with the
18/// frame's step count), so their on-screen position is fixed by the engine
19/// rather than the authored coordinates.
20///
21/// The always-on frame-rate and GPU-memory readouts live on the separate
22/// [StatHud](#stathud).
23///
24/// Every rendering world receives a `DebugHud` and its chip labels at build
25/// time when it declares none, so the example below is only needed to restyle
26/// the chips. The HUD only activates in developer contexts: a debug build of
27/// the host binary, or a world launched through `cn debug`; release builds
28/// leave it inert even when declared. Declare an
29/// [EngineDefaults](#enginedefaults) with `"debug_hud": false` to remove it
30/// from the build entirely.
31#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
32#[serde(default)]
33pub struct DebugHud {
34    /// [TextLabel](#textlabel) that receives the per-step GPU-timing chip text.
35    #[serde(deserialize_with = "de_opt_asset_ref")]
36    pub passes_label: Option<AssetId>,
37    /// [TextLabel](#textlabel) that receives the cursor-position chip text.
38    #[serde(deserialize_with = "de_opt_asset_ref")]
39    pub mouse_label: Option<AssetId>,
40    /// [TextLabel](#textlabel) that receives the live camera-pose chip text.
41    #[serde(deserialize_with = "de_opt_asset_ref")]
42    pub camera_label: Option<AssetId>,
43    /// [TextLabel](#textlabel) that receives the thread / memory budget chip text.
44    #[serde(deserialize_with = "de_opt_asset_ref")]
45    pub sys_label: Option<AssetId>,
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn a_blank_hud_claims_no_labels() {
54        // Every chip is opt-in: an unset slot means that readout is suppressed
55        // rather than drawn somewhere arbitrary.
56        let h = DebugHud::default();
57        assert!(h.passes_label.is_none());
58        assert!(h.mouse_label.is_none());
59        assert!(h.camera_label.is_none());
60        assert!(h.sys_label.is_none());
61    }
62
63    #[test]
64    fn each_chip_binds_its_own_label_and_round_trips_through_postcard() {
65        crate::test_support::install_resolvers();
66        let h: DebugHud = serde_json::from_str(
67            r#"{"passes_label":"passes_chip","mouse_label":"","camera_label":"cam","sys_label":6}"#,
68        )
69        .unwrap();
70        assert_eq!(h.passes_label, Some(AssetId(11)));
71        assert_eq!(h.mouse_label, None);
72        assert_eq!(h.camera_label, Some(AssetId(3)));
73        assert_eq!(h.sys_label, Some(AssetId(6)));
74
75        let bytes = postcard::to_allocvec(&h).unwrap();
76        let back: DebugHud = postcard::from_bytes(&bytes).unwrap();
77        assert_eq!(back.passes_label, Some(AssetId(11)));
78        assert_eq!(back.mouse_label, None);
79        assert_eq!(back.sys_label, Some(AssetId(6)));
80    }
81}