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 drew what the surface showed: no image, draw or present.
48 Unchanged,
49 /// The packet was refused before any encoding; the scene returned whole.
50 Cancelled(CancelReason),
51}
52
53pub(crate) struct FramePacket {
54 pub(crate) frame_id: u64,
55 pub(crate) viewport: (u32, u32),
56 pub(crate) renderer_epoch: u64,
57 pub(crate) surface_epoch: u64,
58 pub(crate) root_scale: f32,
59 pub(crate) root: Box<LayerScene>,
60 pub(crate) overlay: Option<Box<LayerScene>>,
61 pub(crate) clear: wgpu::Color,
62 // Whether the app held still, asking for no frame, between the frame
63 // before and this one.
64 pub(crate) held_still: bool,
65 // Whether a pointer was held on the app's controls for this frame.
66 pub(crate) pointer_held: bool,
67 // The scene update the packet was collected from: a frame drawn from the
68 // update the frame before drew shows nothing new of the app's.
69 pub(crate) scene_update: u64,
70}
71
72impl FramePacket {
73 // How the packet's scene moved since the frame that drew `drawn`, the
74 // scene update before.
75 pub(crate) fn scene_motion(&self, drawn: Option<u64>) -> SceneMotion {
76 if self.pointer_held {
77 SceneMotion::Held
78 } else if drawn == Some(self.scene_update) {
79 SceneMotion::Still
80 } else {
81 SceneMotion::Moving
82 }
83 }
84}
85
86// How a frame's scene moved since the frame before, which on WebGL decides
87// whether a paced build runs at its end (see `PipelineCompiler::paced`).
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub(crate) enum SceneMotion {
90 // The frame draws the scene update the frame before drew, with no
91 // pointer held.
92 Still,
93 // The scene changed, with no pointer held.
94 Moving,
95 // A pointer is held on the app's controls.
96 Held,
97}
98
99pub(crate) struct FrameSceneStorage {
100 pub(crate) root: Box<LayerScene>,
101 pub(crate) overlay: Option<Box<LayerScene>>,
102}
103
104/// Present-stage timestamps for one consumed packet, in nanoseconds on the
105/// clock the producer injected at runtime start (`0` = stage did not run or
106/// no clock was injected). Carried back in `RenderReturns` so the
107/// producer's frame telemetry keeps recording acquire/render/present phases
108/// after those stages move to the present thread. Plain integers so the
109/// packet types stay clock-library-free on every target.
110#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
111pub struct PresentTimings {
112 /// When the present stage had acquired the surface texture.
113 pub after_acquire_ns: i64,
114 /// When the present stage had rendered the frame's draws.
115 pub after_render_ns: i64,
116 /// When the present stage had presented the frame to the surface.
117 pub after_present_ns: i64,
118}
119
120#[derive(Default)]
121pub(crate) struct RenderReturns {
122 pub(crate) scene: Option<FrameSceneStorage>,
123 pub(crate) frame_id: u64,
124 pub(crate) outcome: PresentOutcome,
125 #[cfg(not(target_arch = "wasm32"))]
126 pub(crate) timings: PresentTimings,
127}
128
129const _: () = {
130 const fn assert_send<T: Send>() {}
131 assert_send::<FramePacket>();
132 assert_send::<FrameSceneStorage>();
133 assert_send::<LayerScene>();
134 assert_send::<ChildLayer>();
135 assert_send::<CompositorScene>();
136 assert_send::<RunDraw>();
137 assert_send::<ImageDraw>();
138 assert_send::<TextDraw>();
139 assert_send::<ShadowDraw>();
140 assert_send::<DrawOp>();
141 assert_send::<EffectLayer>();
142 assert_send::<BackdropLayer>();
143 assert_send::<RenderReturns>();
144 assert_send::<CancelReason>();
145 assert_send::<PresentOutcome>();
146 assert_send::<PresentTimings>();
147};