Skip to main content

euv_core/render/compute/
struct.rs

1use super::*;
2
3/// Exact 16-byte `#[repr(C)]` GPU indirect draw argument buffer matching `MTLDrawPrimitivesIndirectArguments` and WGSL/Vulkan indirect draw.
4#[repr(C)]
5#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
6pub struct GpuDrawIndirectArgs {
7    /// Vertex count per instance (`6` for two-triangle UI quad).
8    #[get(type(copy))]
9    pub(crate) vertex_count: u32,
10    /// Compacted visible primitive instance count atomically populated by `euv_layout_cull_and_emit`.
11    #[get(type(copy))]
12    pub(crate) instance_count: u32,
13    /// First vertex offset (`0`).
14    #[get(type(copy))]
15    pub(crate) vertex_start: u32,
16    /// Base instance offset (`0`).
17    #[get(type(copy))]
18    pub(crate) base_instance: u32,
19}
20
21/// Flat 64-byte `#[repr(C)]` per-level uniform payload bound at buffer slot 5 across all 4 compute kernels.
22#[repr(C)]
23#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
24pub struct GpuLevelUniform {
25    /// Starting BFS index of the active tree depth level.
26    #[get(type(copy))]
27    pub(crate) level_start: u32,
28    /// Node count in the active tree depth level.
29    #[get(type(copy))]
30    pub(crate) level_count: u32,
31    /// Total node count across all levels.
32    #[get(type(copy))]
33    pub(crate) node_count: u32,
34    /// Optional pass flags bitmask.
35    #[get(type(copy))]
36    pub(crate) pass_flags: u32,
37    /// Physical viewport dimensions `[width, height]`.
38    #[get(type(copy))]
39    pub(crate) viewport_size: [f32; 2],
40    /// Simulation frame delta time in seconds for Spring physics layout.
41    #[get(type(copy))]
42    pub(crate) dt_secs: f32,
43    /// Reserved alignment padding.
44    #[get(type(copy))]
45    pub(crate) reserved: u32,
46    /// Root boundary rectangle `[x, y, width, height]` constraining subtree roots.
47    #[get(type(copy))]
48    pub(crate) root_boundary_rect: [f32; 4],
49    /// Query pointer coordinate `[x, y]` for `euv_layout_hit_test`.
50    #[get(type(copy))]
51    pub(crate) pick_point: [f32; 2],
52    /// Trailing 16-byte alignment padding.
53    #[get(type(copy))]
54    pub(crate) pad_tail: [f32; 2],
55}
56
57/// Flat BFS scene descriptor submitted to `GpuComputeLayoutEngine`.
58#[derive(Clone, Data, Debug, Default, PartialEq)]
59pub struct GpuComputeSceneInput {
60    /// Contiguous BFS-ordered layout node inputs.
61    pub(crate) nodes: Vec<GpuLayoutNodeInput>,
62    /// Contiguous BFS-ordered visual style inputs paired 1:1 with `nodes`.
63    pub(crate) visuals: Vec<GpuVisualStyleInput>,
64    /// Inclusive prefix offsets partitioning `nodes` by BFS depth (`depth_count + 1` entries).
65    pub(crate) level_offsets: Vec<u32>,
66    /// Physical viewport `[width, height]`.
67    #[get(type(copy))]
68    pub(crate) viewport_size: [f32; 2],
69    /// Root boundary rectangle `[x, y, width, height]`.
70    #[get(type(copy))]
71    pub(crate) root_boundary_rect: [f32; 4],
72    /// Frame time step in seconds for Spring mode.
73    #[get(type(copy))]
74    pub(crate) dt_secs: f32,
75    /// Optional pointer hit-test coordinate `[x, y]`.
76    #[get(skip)]
77    #[get_mut(skip)]
78    #[set(skip)]
79    pub(crate) pick_point: Option<[f32; 2]>,
80    /// Clear color for optional indirect render pass.
81    #[get(type(copy))]
82    pub(crate) clear_color: Color,
83}
84
85/// Complete telemetry and output snapshot returned by one `GpuComputeLayoutEngine` execution.
86#[derive(Clone, Data, Debug, Default, PartialEq)]
87pub struct GpuComputeDispatchResult {
88    /// Backend that executed the compute layout and optional indirect draw pass.
89    #[get(type(copy))]
90    pub(crate) backend_kind: GpuComputeBackendKind,
91    /// Resolved per-node physical rects and content boxes.
92    pub(crate) outputs: Vec<GpuLayoutNodeOutput>,
93    /// Compacted visible 64-byte `GpuPrimitiveInstance` stream emitted by `euv_layout_cull_and_emit`.
94    pub(crate) primitives: Vec<GpuPrimitiveInstance>,
95    /// Indirect draw arguments written by the GPU compute compaction pass.
96    #[get(type(copy))]
97    pub(crate) indirect_args: GpuDrawIndirectArgs,
98    /// Hit-tested BFS node index (`GPU_HIT_TEST_MISS` when missed or skipped).
99    #[get(type(copy))]
100    pub(crate) hit_node_bfs: u32,
101    /// Measured GPU or reference compute duration in nanoseconds.
102    #[get(type(copy))]
103    pub(crate) gpu_compute_ns: u64,
104    /// Measured GPU indirect render duration in nanoseconds (`0` when compute-only).
105    #[get(type(copy))]
106    pub(crate) gpu_render_ns: u64,
107    /// Number of indirect GPU draw calls executed.
108    #[get(type(copy))]
109    pub(crate) draw_calls: usize,
110    /// Number of non-zero pixels verified in offscreen GPU render target readback (`0` when not read back).
111    #[get(type(copy))]
112    pub(crate) offscreen_non_zero_pixels: usize,
113    /// Total upload bytes copied to GPU buffers during this dispatch.
114    #[get(type(copy))]
115    pub(crate) upload_bytes: usize,
116}
117
118/// Process-wide cached Apple Metal compute pipeline states for all 4 layout compute kernels.
119#[cfg(any(target_os = "macos", target_os = "ios"))]
120#[derive(Clone, Copy, Debug, Default)]
121pub(crate) struct MetalComputePipelineCache {
122    /// Retained `id<MTLDevice>` handle.
123    pub(crate) device: usize,
124    /// Retained `id<MTLCommandQueue>` handle.
125    pub(crate) queue: usize,
126    /// Retained `id<MTLComputePipelineState>` for `euv_layout_measure_bottom_up`.
127    pub(crate) measure_pso: usize,
128    /// Retained `id<MTLComputePipelineState>` for `euv_layout_measure_flat_leaves`.
129    pub(crate) measure_leaves_pso: usize,
130    /// Retained `id<MTLComputePipelineState>` for `euv_layout_solve_top_down`.
131    pub(crate) solve_pso: usize,
132    /// Retained `id<MTLComputePipelineState>` for `euv_layout_solve_flat_children`.
133    pub(crate) solve_flat_pso: usize,
134    /// Retained `id<MTLComputePipelineState>` for `euv_layout_cull_and_emit`.
135    pub(crate) cull_emit_pso: usize,
136    /// Retained `id<MTLComputePipelineState>` for `euv_layout_hit_test`.
137    pub(crate) hit_test_pso: usize,
138    /// Retained UI uber-shader `MTLRenderPipelineState` for indirect draw execution.
139    pub(crate) ui_render_pso: usize,
140    /// Retained 1x1 fallback R8 texture handle.
141    pub(crate) dummy_r8_tex: usize,
142    /// Retained 1x1 fallback RGBA8 texture handle.
143    pub(crate) dummy_rgba8_tex: usize,
144    /// True when the Metal device uses unified CPU/GPU shared memory.
145    pub(crate) has_unified_memory: bool,
146}
147
148/// Cached Objective-C selector pointers for Metal compute, indirect draw, and offscreen texture readback.
149#[cfg(any(target_os = "macos", target_os = "ios"))]
150#[derive(Clone, Copy, Debug, Default)]
151pub(crate) struct MetalComputeSelectors {
152    /// Selector `newComputePipelineStateWithFunction:error:`.
153    pub(crate) new_compute_pso: usize,
154    /// Selector `computeCommandEncoder`.
155    pub(crate) compute_encoder: usize,
156    /// Selector `setComputePipelineState:`.
157    pub(crate) set_compute_pso: usize,
158    /// Selector `setBuffer:offset:atIndex:`.
159    pub(crate) set_compute_buffer: usize,
160    /// Selector `setBytes:length:atIndex:`.
161    pub(crate) set_compute_bytes: usize,
162    /// Selector `dispatchThreadgroups:threadsPerThreadgroup:`.
163    pub(crate) dispatch_threadgroups: usize,
164    /// Selector `drawPrimitives:indirectBuffer:indirectBufferOffset:`.
165    pub(crate) draw_primitives_indirect: usize,
166    /// Selector `getBytes:bytesPerRow:fromRegion:mipmapLevel:`.
167    pub(crate) get_bytes_region: usize,
168    /// Selector `setStorageMode:`.
169    pub(crate) set_storage_mode: usize,
170    /// Selector `hasUnifiedMemory`.
171    pub(crate) has_unified_memory: usize,
172    /// Selector `blitCommandEncoder`.
173    pub(crate) blit_encoder: usize,
174    /// Selector `synchronizeResource:`.
175    pub(crate) sync_resource: usize,
176    /// Selector `copyFromBuffer:sourceOffset:toBuffer:destinationOffset:size:`.
177    pub(crate) copy_from_buffer: usize,
178}
179
180/// Persistent GPU compute layout and zero-readback indirect draw engine supporting Apple Metal hardware and bit-exact CPU parity.
181#[derive(Debug, Getter)]
182pub struct GpuComputeLayoutEngine {
183    /// Active compute backend kind (`MetalHardware` on physical Apple GPUs, else `CpuReferenceParity`).
184    #[get(type(copy))]
185    pub(crate) backend_kind: GpuComputeBackendKind,
186    /// Retained native Metal device handle (`0` on CPU reference parity).
187    #[get(type(copy))]
188    pub(crate) device_handle: usize,
189    /// Retained native Metal command queue handle (`0` on CPU reference parity).
190    #[get(type(copy))]
191    pub(crate) queue_handle: usize,
192    /// Retained persistent input `MTLBuffer` handle.
193    #[get(type(copy))]
194    pub(crate) input_buffer_handle: usize,
195    /// Mapped CPU pointer for `input_buffer_handle`.
196    #[get(type(copy))]
197    pub(crate) input_contents_ptr: usize,
198    /// Retained persistent output `MTLBuffer` handle.
199    #[get(type(copy))]
200    pub(crate) output_buffer_handle: usize,
201    /// Mapped CPU pointer for `output_buffer_handle`.
202    #[get(type(copy))]
203    pub(crate) output_contents_ptr: usize,
204    /// Retained persistent visual style `MTLBuffer` handle.
205    #[get(type(copy))]
206    pub(crate) visual_buffer_handle: usize,
207    /// Mapped CPU pointer for `visual_buffer_handle`.
208    #[get(type(copy))]
209    pub(crate) visual_contents_ptr: usize,
210    /// Retained persistent indirect primitive output `MTLBuffer` handle.
211    #[get(type(copy))]
212    pub(crate) primitive_buffer_handle: usize,
213    /// Mapped CPU pointer for `primitive_buffer_handle`.
214    #[get(type(copy))]
215    pub(crate) primitive_contents_ptr: usize,
216    /// Retained 16-byte `GpuDrawIndirectArgs` `MTLBuffer` handle.
217    #[get(type(copy))]
218    pub(crate) indirect_buffer_handle: usize,
219    /// Mapped CPU pointer for `indirect_buffer_handle`.
220    #[get(type(copy))]
221    pub(crate) indirect_contents_ptr: usize,
222    /// Retained 4-byte hit-test result `MTLBuffer` handle.
223    #[get(type(copy))]
224    pub(crate) hit_buffer_handle: usize,
225    /// Mapped CPU pointer for `hit_buffer_handle`.
226    #[get(type(copy))]
227    pub(crate) hit_contents_ptr: usize,
228    /// Capacity in nodes currently allocated across the persistent GPU compute buffers.
229    #[get(type(copy))]
230    pub(crate) allocated_node_capacity: usize,
231    /// Retained reusable offscreen `BGRA8Unorm` `MTLTexture` handle for hardware render verification.
232    #[get(type(copy))]
233    pub(crate) offscreen_texture_handle: usize,
234    /// Width of `offscreen_texture_handle` in pixels.
235    #[get(type(copy))]
236    pub(crate) offscreen_width: u32,
237    /// Height of `offscreen_texture_handle` in pixels.
238    #[get(type(copy))]
239    pub(crate) offscreen_height: u32,
240    /// Cumulative hardware or reference compute dispatches executed.
241    #[get(type(copy))]
242    pub(crate) total_dispatches: u64,
243    /// Cached uniform grid row height (`> 0.0` when the uploaded flat grid root has uniform child row heights).
244    #[get(type(copy))]
245    pub(crate) cached_uniform_row_h: f32,
246    /// Ping-pong `MTLStorageModeShared` staging buffer handles for `in_flight = 2` dirty-span GPU blits.
247    #[get(type(copy))]
248    pub(crate) upload_staging_handles: [usize; 2],
249    /// Mapped CPU write pointers for `upload_staging_handles`.
250    #[get(type(copy))]
251    pub(crate) upload_staging_ptrs: [usize; 2],
252    /// GPU-private `MTLStorageModePrivate` destination buffer handle receiving coalesced dirty spans.
253    #[get(type(copy))]
254    pub(crate) upload_private_handle: usize,
255    /// Allocated byte capacity of `upload_staging_handles` and `upload_private_handle`.
256    #[get(type(copy))]
257    pub(crate) upload_capacity_bytes: usize,
258}