Skip to main content

cranpose_render_wgpu/
frame_packet.rs

1use crate::{
2    collect::{ChildLayer, LayerScene},
3    scene::{
4        BackdropLayer, CompositorScene, DrawOp, EffectLayer, ImageDraw, RunDraw, ShadowDraw,
5        TextDraw,
6    },
7};
8
9/// Why the present stage refused a packet without drawing it. Each reason
10/// names the expectation the packet no longer matches; the frame is not an
11/// error, its scene travels back through `RenderReturns` for recycling.
12/// `pub` (not `pub(crate)`) because the cancellation-protocol tests in
13/// `tests/` observe outcomes through `#[doc(hidden)]` hooks; the module is
14/// private, so the only public path is the hidden re-export in `lib.rs`.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum CancelReason {
17    /// The packet was built against a different `GpuRenderer` instance.
18    RendererEpoch,
19    /// The packet was built against a different surface configuration.
20    SurfaceEpoch,
21    /// The packet was lowered for a different surface size.
22    Viewport,
23    /// The present stage had no usable surface to draw the packet on: the
24    /// surface was dropped, or acquire failed past its one reconfigure
25    /// retry. Producer state (epochs, viewport) still matched, only the
26    /// swapchain was missing.
27    SurfaceUnavailable,
28    /// The device reported an uncaptured error (validation/OOM/internal)
29    /// since the previous frame: nothing of this packet is encoded on the
30    /// suspect device. One cancel per poisoning; the gate clears as it
31    /// fires, so the next packet renders (see `DeviceErrorSentry`).
32    DeviceError,
33}
34
35/// What the present stage did with a packet. `NotRun` is the `Default` so a
36/// draw that never happened can never be reported `Presented`.
37/// `pub` like [`CancelReason`], for the same hidden test re-export.
38#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
39pub enum PresentOutcome {
40    /// No packet was consumed (the default of an untouched returns value).
41    #[default]
42    NotRun,
43    /// The packet was drawn and presented.
44    Presented,
45    /// The packet was refused before any encoding; the scene returned whole.
46    Cancelled(CancelReason),
47}
48
49pub(crate) struct FramePacket {
50    pub(crate) frame_id: u64,
51    pub(crate) viewport: (u32, u32),
52    pub(crate) renderer_epoch: u64,
53    pub(crate) surface_epoch: u64,
54    pub(crate) root_scale: f32,
55    pub(crate) root: LayerScene,
56    pub(crate) overlay: Option<LayerScene>,
57    pub(crate) text_cache_len: usize,
58    pub(crate) clear: wgpu::Color,
59}
60
61pub(crate) struct FrameSceneStorage {
62    pub(crate) root: LayerScene,
63    pub(crate) overlay: Option<LayerScene>,
64}
65
66/// Present-stage timestamps for one consumed packet, in nanoseconds on the
67/// clock the producer injected at runtime start (`0` = stage did not run or
68/// no clock was injected). Carried back in `RenderReturns` so the
69/// producer's frame telemetry keeps recording acquire/render/present phases
70/// after those stages move to the present thread. Plain integers so the
71/// packet types stay clock-library-free on every target.
72#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
73pub struct PresentTimings {
74    pub after_acquire_ns: i64,
75    pub after_render_ns: i64,
76    pub after_present_ns: i64,
77}
78
79#[derive(Default)]
80pub(crate) struct RenderReturns {
81    pub(crate) scene: Option<FrameSceneStorage>,
82    pub(crate) frame_id: u64,
83    pub(crate) outcome: PresentOutcome,
84    #[cfg(not(target_arch = "wasm32"))]
85    pub(crate) timings: PresentTimings,
86}
87
88const _: () = {
89    const fn assert_send<T: Send>() {}
90    assert_send::<FramePacket>();
91    assert_send::<FrameSceneStorage>();
92    assert_send::<LayerScene>();
93    assert_send::<ChildLayer>();
94    assert_send::<CompositorScene>();
95    assert_send::<RunDraw>();
96    assert_send::<ImageDraw>();
97    assert_send::<TextDraw>();
98    assert_send::<ShadowDraw>();
99    assert_send::<DrawOp>();
100    assert_send::<EffectLayer>();
101    assert_send::<BackdropLayer>();
102    assert_send::<RenderReturns>();
103    assert_send::<CancelReason>();
104    assert_send::<PresentOutcome>();
105    assert_send::<PresentTimings>();
106};