Skip to main content

dear_imgui_wgpu/
observation.rs

1//! Detached metadata describing observed renderer work, without GPU-completion claims.
2
3use dear_imgui_rs::{ContextId, NativeWindowTarget, render::SnapshotEpoch};
4
5use crate::WgpuDrawDependencies;
6
7/// The UI scope in which renderer work was observed.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct WgpuFrameScope {
10    /// Context that produced the draw data.
11    pub context_id: ContextId,
12    /// Managed renderer epoch; absent only for explicitly legacy draw data.
13    pub epoch: Option<SnapshotEpoch>,
14    /// Dear ImGui native frame counter when the draw data was captured.
15    pub frame_count: usize,
16}
17
18/// Why a target did not produce Dear ImGui draw commands during this attempt.
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20#[non_exhaustive]
21pub enum WgpuFrameSkipReason {
22    /// The framebuffer has at least one zero dimension.
23    ZeroSize,
24    /// No valid draw data was available.
25    NoDrawData,
26    /// Every command was empty or clipped away.
27    NoDrawCommands,
28    /// Surface acquisition timed out.
29    Timeout,
30    /// The outdated surface was reconfigured for a later attempt.
31    Outdated,
32    /// The lost surface was recreated for a later attempt.
33    Lost,
34    /// The compositor reported the surface as occluded.
35    Occluded,
36    /// The native allocation was no longer owned by the captured platform owner.
37    RetiredTarget,
38    /// A previous recorded or pending frame still owns this surface acquisition.
39    TargetBusy,
40}
41
42/// Successful recording metadata returned for an application-owned render pass.
43///
44/// The render pass does not reveal its native window or queue. This value therefore proves only
45/// which commands were recorded; it cannot establish submission, presentation, or GPU completion.
46#[derive(Clone, Debug, Eq, PartialEq)]
47pub struct WgpuDrawRecording {
48    /// UI frame associated with this recording attempt.
49    pub scope: WgpuFrameScope,
50    /// External view mappings captured during command preparation.
51    pub dependencies: WgpuDrawDependencies,
52    /// Whether Dear ImGui commands were successfully recorded into the supplied render pass.
53    pub recorded: bool,
54    /// Reason for an attempt with no Dear ImGui commands.
55    pub skip_reason: Option<WgpuFrameSkipReason>,
56}
57
58impl WgpuDrawRecording {
59    pub(crate) fn skipped(scope: WgpuFrameScope, reason: WgpuFrameSkipReason) -> Self {
60        Self {
61            scope,
62            dependencies: WgpuDrawDependencies::default(),
63            recorded: false,
64            skip_reason: Some(reason),
65        }
66    }
67}
68
69/// Highest operation actually observed for one native target in one attempt.
70///
71/// Submission is a host queue operation. PresentationRequested means the backend called the
72/// presentation API. Neither stage proves GPU completion, compositor acceptance, scanout, or
73/// visible pixels. A submitted clear-only pass can have no external texture dependencies.
74#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
75pub enum WgpuFrameStage {
76    /// No successful recording was observed.
77    Unrecorded,
78    /// Commands were recorded successfully.
79    Recorded,
80    /// The target's command buffer was passed to Queue::submit.
81    Submitted,
82    /// The target's retained surface frame was passed to the presentation API.
83    PresentationRequested,
84}
85
86/// Result of one target attempt, independent of the highest operation already observed.
87#[derive(Clone, Debug, Eq, PartialEq)]
88#[non_exhaustive]
89pub enum WgpuFrameOutcome {
90    /// The backend completed the operations represented by `stage`.
91    Observed,
92    /// Drawing or acquisition was skipped for a stated reason.
93    Skipped(WgpuFrameSkipReason),
94    /// Recorded or submitted work was explicitly abandoned without requesting presentation.
95    Discarded,
96    /// An operation failed; earlier recorded/submitted stages remain valid evidence.
97    Failed {
98        /// Operation being attempted when the error was observed.
99        operation: &'static str,
100        /// Owned diagnostic text, without retaining native resources or errors.
101        message: String,
102    },
103}
104
105/// Immutable-by-ownership association between native target, frame, dependencies and observed work.
106///
107/// Save this value directly in editor history. Resolve live windows separately by `instance_id`;
108/// the stored viewport routing ID must never be reinterpreted through a newer routing table.
109#[derive(Clone, Debug, Eq, PartialEq)]
110pub struct WgpuViewportObservation {
111    /// Native registration and routing association captured for this attempt.
112    pub target: NativeWindowTarget,
113    /// UI frame associated with the observation.
114    pub scope: WgpuFrameScope,
115    /// External mappings used by successfully prepared commands.
116    pub dependencies: WgpuDrawDependencies,
117    /// Highest completed operation observed by this backend.
118    pub stage: WgpuFrameStage,
119    /// Skip or failure information, retaining any earlier successful stage.
120    pub outcome: WgpuFrameOutcome,
121}
122
123impl WgpuViewportObservation {
124    #[cfg(any(feature = "multi-viewport-winit", feature = "multi-viewport-sdl3"))]
125    pub(crate) fn from_recording(target: NativeWindowTarget, recording: WgpuDrawRecording) -> Self {
126        Self {
127            target,
128            scope: recording.scope,
129            dependencies: recording.dependencies,
130            stage: if recording.recorded {
131                WgpuFrameStage::Recorded
132            } else {
133                WgpuFrameStage::Unrecorded
134            },
135            outcome: recording
136                .skip_reason
137                .map_or(WgpuFrameOutcome::Observed, WgpuFrameOutcome::Skipped),
138        }
139    }
140}