euv_core/game/surface/struct.rs
1use super::*;
2
3/// Shared GPU texture/render-target handle composited directly with the EUV HUD on the same device without CPU readback.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
5pub struct GpuTextureHandle {
6 /// Backend-opaque GPU texture or image view identifier (`0` indicates invalid/unbound).
7 #[get(type(copy))]
8 pub(crate) texture_id: u64,
9 /// Owning GPU logical device identifier verifying same-device compositing compatibility.
10 #[get(type(copy))]
11 pub(crate) device_id: u64,
12 /// Monotonic resource generation rejecting stale handles after resize or device loss.
13 #[get(type(copy))]
14 pub(crate) generation: u64,
15 /// Texture width in physical pixels.
16 #[get(type(copy))]
17 pub(crate) width: u32,
18 /// Texture height in physical pixels.
19 #[get(type(copy))]
20 pub(crate) height: u32,
21 /// Pixel format of the GPU render target.
22 #[get(type(copy))]
23 pub(crate) format: GpuTextureFormat,
24 /// Timeline semaphore or fence value signaling completion of 3D/2D GPU rendering into this texture.
25 #[get(type(copy))]
26 pub(crate) fence_value: u64,
27 /// Whether UV `(0, 0)` corresponds to the top-left corner (`false` for bottom-left OpenGL/WebGL targets).
28 #[get(type(copy))]
29 pub(crate) origin_top_left: bool,
30}
31
32/// Descriptor emitted onto `GameRenderContext` binding a GPU render target into the single-present HUD compositor.
33#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
34pub struct GpuCompositeDescriptor {
35 /// Destination bounds in root logical coordinates.
36 #[get(type(copy))]
37 pub(crate) rect: Rect,
38 /// Shared same-device GPU texture handle rendered by the game engine.
39 #[get(type(copy))]
40 pub(crate) handle: GpuTextureHandle,
41 /// Layer ordering index relative to overlaid EUV HUD elements.
42 #[get(type(copy))]
43 pub(crate) z_layer: i32,
44 /// Whether premultiplied alpha blending is enabled when compositing over underlying UI.
45 #[get(type(copy))]
46 pub(crate) preserve_alpha: bool,
47}
48
49/// Stateful controller coordinating same-device GPU viewport compositing and independent world vs HUD dirty tracking.
50#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
51pub struct GpuCompositeTarget {
52 /// Currently attached persistent GPU texture handle.
53 #[get(type(copy))]
54 pub(crate) handle: GpuTextureHandle,
55 /// Active viewport destination rectangle in root logical coordinates.
56 #[get(type(copy))]
57 pub(crate) viewport_rect: Rect,
58 /// Monotonic revision counter bumped only when 2D/3D game world content changes.
59 #[get(type(copy))]
60 pub(crate) world_revision: u64,
61 /// Last presented game world revision.
62 #[get(type(copy))]
63 pub(crate) presented_world_revision: u64,
64 /// Monotonic revision counter bumped only when overlaid EUV HUD content changes.
65 #[get(type(copy))]
66 pub(crate) hud_revision: u64,
67 /// Last presented HUD revision.
68 #[get(type(copy))]
69 pub(crate) presented_hud_revision: u64,
70 /// Highest GPU fence submitted for this target.
71 #[get(type(copy))]
72 pub(crate) submitted_fence: u64,
73 /// Highest GPU fence confirmed complete by the device queue.
74 #[get(type(copy))]
75 pub(crate) completed_fence: u64,
76 /// Number of frames composited directly on GPU without CPU pixel readback.
77 #[get(type(copy))]
78 pub(crate) zero_readback_presents: u64,
79 /// Number of HUD-only composite frames that reused the existing 3D/2D world target without re-rendering the world.
80 #[get(type(copy))]
81 pub(crate) hud_only_reuses: u64,
82}
83
84/// Single reusable RGBA8 backing slot inside a bounded `RenderBufferPool`.
85#[derive(Clone, Data, Debug, Default, PartialEq)]
86pub struct PooledRenderBufferSlot {
87 /// Persistent shared pixel buffer reused across frames without per-frame heap allocation or blit memcpy.
88 #[get(skip)]
89 #[get_mut(skip)]
90 #[set(skip)]
91 pub(crate) pixels: Option<Arc<Vec<u8>>>,
92 /// Current logical frame width stored in `pixels`.
93 #[get(type(copy))]
94 pub(crate) width: u32,
95 /// Current logical frame height stored in `pixels`.
96 #[get(type(copy))]
97 pub(crate) height: u32,
98 /// Fence value guarding reclamation while this slot is in flight with a compositor/uploader.
99 #[get(type(copy))]
100 pub(crate) fence_id: u64,
101 /// Whether this slot is currently leased or awaiting fence completion.
102 #[get(type(copy))]
103 pub(crate) in_flight: bool,
104 /// Whether the pixel buffer is currently checked out inside a live `PooledFrameLease`.
105 #[get(type(copy))]
106 pub(crate) checked_out: bool,
107 /// Generation counter incremented each time this slot is acquired.
108 #[get(type(copy))]
109 pub(crate) generation: u64,
110}
111
112/// Observable allocation, reuse, and backpressure counters for `RenderBufferPool`.
113#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
114pub struct RenderBufferPoolStats {
115 /// Maximum bounded slot capacity.
116 #[get(type(copy))]
117 pub(crate) capacity: usize,
118 /// Slots currently leased or awaiting fence retirement.
119 #[get(type(copy))]
120 pub(crate) in_flight_slots: usize,
121 /// Free slots immediately available for zero-allocation acquisition.
122 #[get(type(copy))]
123 pub(crate) free_slots: usize,
124 /// Total acquisitions satisfied without growing backing heap capacity.
125 #[get(type(copy))]
126 pub(crate) reuse_hits: u64,
127 /// Total backing buffer capacity growths (expected to plateau after warm-up).
128 #[get(type(copy))]
129 pub(crate) backing_allocations: u64,
130 /// Acquisitions rejected due to all bounded slots being in flight (backpressure).
131 #[get(type(copy))]
132 pub(crate) backpressure_rejections: u64,
133}
134
135/// Internal mutable state shared between `RenderBufferPool` and active `PooledFrameLease` handles.
136#[derive(Clone, Data, Debug, Default)]
137pub(crate) struct RenderBufferPoolState {
138 /// Fixed-length ring of reusable pixel slots.
139 pub(crate) slots: Vec<PooledRenderBufferSlot>,
140 /// Last retired compositor/GPU fence value.
141 #[get(type(copy))]
142 pub(crate) completed_fence: u64,
143 /// Total zero-allocation reuse hits.
144 #[get(type(copy))]
145 pub(crate) reuse_hits: u64,
146 /// Total backing buffer capacity allocations.
147 #[get(type(copy))]
148 pub(crate) backing_allocations: u64,
149 /// Total rejections when every bounded slot was in flight.
150 #[get(type(copy))]
151 pub(crate) backpressure_rejections: u64,
152}
153
154/// Zero-allocation bounded CPU RGBA8 render output buffer pool with fence-gated reclamation.
155#[derive(Clone, CustomDebug, Data)]
156pub struct RenderBufferPool {
157 /// Shared bounded pool state coordinating slot leases and fence retirement.
158 #[debug(skip)]
159 #[get(pub(crate))]
160 #[get_mut(pub(crate))]
161 #[set(pub(crate))]
162 pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
163}
164
165/// Zero-copy shared reference handle to a rendered RGBA8 frame buffer backed by `Arc<Vec<u8>>` for direct display-list submission.
166#[derive(Clone, Data, Debug, Default, PartialEq)]
167pub struct PooledFrameRef {
168 /// Rendered frame width in pixels.
169 #[get(type(copy))]
170 pub(crate) width: u32,
171 /// Rendered frame height in pixels.
172 #[get(type(copy))]
173 pub(crate) height: u32,
174 /// Assigned submission fence value.
175 #[get(type(copy))]
176 pub(crate) fence_id: u64,
177 /// Slot acquisition generation.
178 #[get(type(copy))]
179 pub(crate) generation: u64,
180 /// Reference-counted shared RGBA8 pixel bytes (`0` byte copy on clone or blit).
181 #[get(skip)]
182 #[get_mut(skip)]
183 #[set(skip)]
184 pub(crate) pixels: Arc<Vec<u8>>,
185}
186
187/// RAII lease holding exclusive mutable access to a pooled RGBA8 buffer slot, returning it on drop or fence commit.
188#[derive(CustomDebug, Data)]
189pub struct PooledFrameLease {
190 /// Owning pool state receiving the pixel buffer back on release or drop.
191 #[debug(skip)]
192 #[get(skip)]
193 #[get_mut(skip)]
194 #[set(skip)]
195 pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
196 /// Bound slot index inside `RenderBufferPoolState::slots`.
197 #[get(type(copy))]
198 pub(crate) slot_index: usize,
199 /// Rendered frame width in pixels.
200 #[get(type(copy))]
201 pub(crate) width: u32,
202 /// Rendered frame height in pixels.
203 #[get(type(copy))]
204 pub(crate) height: u32,
205 /// Assigned submission fence value.
206 #[get(type(copy))]
207 pub(crate) fence_id: u64,
208 /// Slot acquisition generation.
209 #[get(type(copy))]
210 pub(crate) generation: u64,
211 /// Checked-out shared pixel storage buffer swapped back into the pool slot upon release.
212 #[get(skip)]
213 #[get_mut(skip)]
214 #[set(skip)]
215 pub(crate) pixels: Option<Arc<Vec<u8>>>,
216 /// Whether this lease has already been returned to the pool.
217 #[get(type(copy))]
218 pub(crate) released: bool,
219}