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 pixel buffer reused across frames without per-frame heap allocation.
88 pub(crate) pixels: Vec<u8>,
89 /// Current logical frame width stored in `pixels`.
90 #[get(type(copy))]
91 pub(crate) width: u32,
92 /// Current logical frame height stored in `pixels`.
93 #[get(type(copy))]
94 pub(crate) height: u32,
95 /// Fence value guarding reclamation while this slot is in flight with a compositor/uploader.
96 #[get(type(copy))]
97 pub(crate) fence_id: u64,
98 /// Whether this slot is currently leased or awaiting fence completion.
99 #[get(type(copy))]
100 pub(crate) in_flight: bool,
101 /// Whether the pixel buffer is currently checked out inside a live `PooledFrameLease`.
102 #[get(type(copy))]
103 pub(crate) checked_out: bool,
104 /// Generation counter incremented each time this slot is acquired.
105 #[get(type(copy))]
106 pub(crate) generation: u64,
107}
108
109/// Observable allocation, reuse, and backpressure counters for `RenderBufferPool`.
110#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
111pub struct RenderBufferPoolStats {
112 /// Maximum bounded slot capacity.
113 #[get(type(copy))]
114 pub(crate) capacity: usize,
115 /// Slots currently leased or awaiting fence retirement.
116 #[get(type(copy))]
117 pub(crate) in_flight_slots: usize,
118 /// Free slots immediately available for zero-allocation acquisition.
119 #[get(type(copy))]
120 pub(crate) free_slots: usize,
121 /// Total acquisitions satisfied without growing backing heap capacity.
122 #[get(type(copy))]
123 pub(crate) reuse_hits: u64,
124 /// Total backing buffer capacity growths (expected to plateau after warm-up).
125 #[get(type(copy))]
126 pub(crate) backing_allocations: u64,
127 /// Acquisitions rejected due to all bounded slots being in flight (backpressure).
128 #[get(type(copy))]
129 pub(crate) backpressure_rejections: u64,
130}
131
132/// Internal mutable state shared between `RenderBufferPool` and active `PooledFrameLease` handles.
133#[derive(Clone, Data, Debug, Default)]
134pub(crate) struct RenderBufferPoolState {
135 /// Fixed-length ring of reusable pixel slots.
136 pub(crate) slots: Vec<PooledRenderBufferSlot>,
137 /// Last retired compositor/GPU fence value.
138 #[get(type(copy))]
139 pub(crate) completed_fence: u64,
140 /// Total zero-allocation reuse hits.
141 #[get(type(copy))]
142 pub(crate) reuse_hits: u64,
143 /// Total backing buffer capacity allocations.
144 #[get(type(copy))]
145 pub(crate) backing_allocations: u64,
146 /// Total rejections when every bounded slot was in flight.
147 #[get(type(copy))]
148 pub(crate) backpressure_rejections: u64,
149}
150
151/// Zero-allocation bounded CPU RGBA8 render output buffer pool with fence-gated reclamation.
152#[derive(Clone, CustomDebug, Data)]
153pub struct RenderBufferPool {
154 /// Shared bounded pool state coordinating slot leases and fence retirement.
155 #[debug(skip)]
156 #[get(pub(crate))]
157 #[get_mut(pub(crate))]
158 #[set(pub(crate))]
159 pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
160}
161
162/// RAII lease holding exclusive mutable access to a pooled RGBA8 buffer slot, returning it on drop or fence commit.
163#[derive(CustomDebug, Data)]
164pub struct PooledFrameLease {
165 /// Owning pool state receiving the pixel buffer back on release or drop.
166 #[debug(skip)]
167 #[get(skip)]
168 #[get_mut(skip)]
169 #[set(skip)]
170 pub(crate) state: Rc<RefCell<RenderBufferPoolState>>,
171 /// Bound slot index inside `RenderBufferPoolState::slots`.
172 #[get(type(copy))]
173 pub(crate) slot_index: usize,
174 /// Rendered frame width in pixels.
175 #[get(type(copy))]
176 pub(crate) width: u32,
177 /// Rendered frame height in pixels.
178 #[get(type(copy))]
179 pub(crate) height: u32,
180 /// Assigned submission fence value.
181 #[get(type(copy))]
182 pub(crate) fence_id: u64,
183 /// Slot acquisition generation.
184 #[get(type(copy))]
185 pub(crate) generation: u64,
186 /// Checked-out pixel storage buffer swapped back into the pool slot upon release.
187 pub(crate) pixels: Vec<u8>,
188 /// Whether this lease has already been returned to the pool.
189 #[get(type(copy))]
190 pub(crate) released: bool,
191}