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