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) text_cache_len: usize,
60    pub(crate) clear: wgpu::Color,
61}
62
63pub(crate) struct FrameSceneStorage {
64    pub(crate) root: LayerScene,
65    pub(crate) overlay: Option<LayerScene>,
66}
67
68/// Present-stage timestamps for one consumed packet, in nanoseconds on the
69/// clock the producer injected at runtime start (`0` = stage did not run or
70/// no clock was injected). Carried back in `RenderReturns` so the
71/// producer's frame telemetry keeps recording acquire/render/present phases
72/// after those stages move to the present thread. Plain integers so the
73/// packet types stay clock-library-free on every target.
74#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
75pub struct PresentTimings {
76    pub after_acquire_ns: i64,
77    pub after_render_ns: i64,
78    pub after_present_ns: i64,
79}
80
81#[derive(Default)]
82pub(crate) struct RenderReturns {
83    pub(crate) scene: Option<FrameSceneStorage>,
84    pub(crate) frame_id: u64,
85    pub(crate) outcome: PresentOutcome,
86    #[cfg(not(target_arch = "wasm32"))]
87    pub(crate) timings: PresentTimings,
88}
89
90const _: () = {
91    const fn assert_send<T: Send>() {}
92    assert_send::<FramePacket>();
93    assert_send::<FrameSceneStorage>();
94    assert_send::<LayerScene>();
95    assert_send::<ChildLayer>();
96    assert_send::<CompositorScene>();
97    assert_send::<RunDraw>();
98    assert_send::<ImageDraw>();
99    assert_send::<TextDraw>();
100    assert_send::<ShadowDraw>();
101    assert_send::<DrawOp>();
102    assert_send::<EffectLayer>();
103    assert_send::<BackdropLayer>();
104    assert_send::<RenderReturns>();
105    assert_send::<CancelReason>();
106    assert_send::<PresentOutcome>();
107    assert_send::<PresentTimings>();
108};