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