molgfx-core 0.1.2

The semantic scene graph: columnar tables, GPU record layouts, the borrowed coordinate seam.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Fixed-capacity translation from compact GPU picks to global dataset rows.

use crate::{ChunkId, ChunkSpan, DatasetId, EntityKind, LocalRow, LogicalRow, PickingError};

/// Global, collision-free provenance of one picked entity.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct GlobalPickIdentity {
    dataset: DatasetId,
    chunk: ChunkId,
    row: LogicalRow,
    kind: EntityKind,
}

impl GlobalPickIdentity {
    /// Creates an identity from a resolved resident page and source row.
    #[must_use]
    pub const fn new(
        dataset: DatasetId,
        chunk: ChunkId,
        row: LogicalRow,
        kind: EntityKind,
    ) -> Self {
        Self {
            dataset,
            chunk,
            row,
            kind,
        }
    }

    /// Dataset containing the picked entity.
    #[must_use]
    pub const fn dataset(self) -> DatasetId {
        self.dataset
    }

    /// Chunk that supplied the resident row.
    #[must_use]
    pub const fn chunk(self) -> ChunkId {
        self.chunk
    }

    /// Stable row in the complete logical dataset.
    #[must_use]
    pub const fn row(self) -> LogicalRow {
        self.row
    }

    /// Entity namespace used to resolve cold provenance without collisions.
    #[must_use]
    pub const fn kind(self) -> EntityKind {
        self.kind
    }
}

/// Provenance shared by every local row in one resident picking page.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PickPageDescriptor {
    dataset: DatasetId,
    chunk: ChunkId,
    rows: ChunkSpan,
    kind: EntityKind,
}

impl PickPageDescriptor {
    /// Creates a page mapping for one entity namespace and chunk span.
    #[must_use]
    pub const fn new(
        dataset: DatasetId,
        chunk: ChunkId,
        rows: ChunkSpan,
        kind: EntityKind,
    ) -> Self {
        Self {
            dataset,
            chunk,
            rows,
            kind,
        }
    }

    /// Dataset containing this page.
    #[must_use]
    pub const fn dataset(self) -> DatasetId {
        self.dataset
    }

    /// Stable chunk represented by this page.
    #[must_use]
    pub const fn chunk(self) -> ChunkId {
        self.chunk
    }

    /// Logical rows represented by chunk-local indices.
    #[must_use]
    pub const fn rows(self) -> ChunkSpan {
        self.rows
    }

    /// Collision-free entity namespace for this page.
    #[must_use]
    pub const fn kind(self) -> EntityKind {
        self.kind
    }
}

/// Dense index of a page in the bounded resident working set.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct ResidentPage(u32);

impl ResidentPage {
    /// Returns the compact value written to the GPU picking attachment.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }
}

/// Monotonic generation of one reusable resident page slot.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct PickGeneration(u64);

impl PickGeneration {
    /// Returns the host-side generation value.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Reservation retained by upload and draw submission until completion.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PickPageTicket {
    page: ResidentPage,
    generation: PickGeneration,
}

impl PickPageTicket {
    /// Resident page encoded by this ticket.
    #[must_use]
    pub const fn page(self) -> ResidentPage {
        self.page
    }

    /// Generation that makes delayed work safe to reject.
    #[must_use]
    pub const fn generation(self) -> PickGeneration {
        self.generation
    }
}

/// Eight-byte token emitted by the GPU: resident page plus chunk-local row.
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuPickToken {
    page: u32,
    local_row: u32,
}

impl GpuPickToken {
    /// Sentinel used to clear a picking attachment.
    pub const NONE: Self = Self {
        page: u32::MAX,
        local_row: u32::MAX,
    };

    /// Creates the exact two-word value written by a picking attachment.
    #[must_use]
    pub const fn new(resident_page: u32, local_row: u32) -> Self {
        Self {
            page: resident_page,
            local_row,
        }
    }

    /// Resident page encoded by the GPU.
    #[must_use]
    pub const fn page(self) -> Option<ResidentPage> {
        if self.page == u32::MAX {
            None
        } else {
            Some(ResidentPage(self.page))
        }
    }

    /// Chunk-local row encoded by the GPU.
    #[must_use]
    pub const fn local_row(self) -> LocalRow {
        LocalRow::new(self.local_row)
    }
}

/// GPU token paired with the page generation captured at draw submission.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PickReadback {
    token: GpuPickToken,
    generation: PickGeneration,
}

impl PickReadback {
    /// Pairs GPU bytes with the generation retained by the submission.
    #[must_use]
    pub const fn new(token: GpuPickToken, generation: PickGeneration) -> Self {
        Self { token, generation }
    }

    /// Compact bytes returned by the GPU.
    #[must_use]
    pub const fn token(self) -> GpuPickToken {
        self.token
    }

    /// Page generation captured before command submission.
    #[must_use]
    pub const fn generation(self) -> PickGeneration {
        self.generation
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum PageState {
    Vacant,
    Pending(PickPageDescriptor),
    Resident(PickPageDescriptor),
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct PageSlot {
    generation: PickGeneration,
    state: PageState,
}

impl Default for PageSlot {
    fn default() -> Self {
        Self {
            generation: PickGeneration(0),
            state: PageState::Vacant,
        }
    }
}

/// Fixed-capacity page table whose memory is proportional to the working set.
#[derive(Debug)]
pub struct PagedPickResolver {
    slots: Box<[PageSlot]>,
    resident: u32,
}

impl PagedPickResolver {
    /// Allocates the complete resolver once; later transitions do not allocate.
    ///
    /// # Errors
    ///
    /// Returns a typed capacity or allocation error.
    pub fn new(capacity: u32) -> Result<Self, PickingError> {
        if capacity == 0 {
            return Err(PickingError::EmptyCapacity);
        }
        if capacity == u32::MAX {
            return Err(PickingError::CapacityTooLarge);
        }
        let capacity = usize::try_from(capacity).map_err(|_| PickingError::CapacityTooLarge)?;
        let mut slots = Vec::new();
        slots
            .try_reserve_exact(capacity)
            .map_err(|_| PickingError::AllocationFailed)?;
        slots.resize(capacity, PageSlot::default());
        Ok(Self {
            slots: slots.into_boxed_slice(),
            resident: 0,
        })
    }

    /// Maximum number of requested and resident pages.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.slots.len()
    }

    /// Number of pages currently ready for picking.
    #[must_use]
    pub const fn resident_len(&self) -> u32 {
        self.resident
    }

    /// Reserves a vacant slot for an asynchronous page upload.
    ///
    /// # Errors
    ///
    /// Rejects duplicate provenance, a full working set or generation overflow.
    pub fn request(
        &mut self,
        descriptor: PickPageDescriptor,
    ) -> Result<PickPageTicket, PickingError> {
        if self.slots.iter().any(|slot| match slot.state {
            PageState::Pending(value) | PageState::Resident(value) => {
                same_namespace(value, descriptor)
            }
            PageState::Vacant => false,
        }) {
            return Err(PickingError::DuplicatePage);
        }
        let Some((index, slot)) = self
            .slots
            .iter_mut()
            .enumerate()
            .find(|(_, slot)| slot.state == PageState::Vacant)
        else {
            return Err(PickingError::WorkingSetFull);
        };
        let Some(generation) = slot.generation.0.checked_add(1) else {
            return Err(PickingError::GenerationExhausted);
        };
        let page = u32::try_from(index).map_err(|_| PickingError::CapacityTooLarge)?;
        slot.generation = PickGeneration(generation);
        slot.state = PageState::Pending(descriptor);
        Ok(PickPageTicket {
            page: ResidentPage(page),
            generation: slot.generation,
        })
    }

    /// Marks one requested page ready after its upload completes.
    ///
    /// # Errors
    ///
    /// Rejects stale completions and pages not awaiting an upload.
    pub fn complete(&mut self, ticket: PickPageTicket) -> Result<(), PickingError> {
        let slot = self.slot_mut(ticket)?;
        let PageState::Pending(descriptor) = slot.state else {
            return Err(PickingError::PageNotPending);
        };
        slot.state = PageState::Resident(descriptor);
        self.resident = self
            .resident
            .checked_add(1)
            .ok_or(PickingError::CapacityTooLarge)?;
        Ok(())
    }

    /// Cancels pending work or evicts a resident page.
    ///
    /// # Errors
    ///
    /// Rejects tickets for another page generation.
    pub fn release(&mut self, ticket: PickPageTicket) -> Result<(), PickingError> {
        let was_resident = {
            let slot = self.slot_mut(ticket)?;
            let was_resident = matches!(slot.state, PageState::Resident(_));
            slot.state = PageState::Vacant;
            was_resident
        };
        if was_resident {
            self.resident = self
                .resident
                .checked_sub(1)
                .ok_or(PickingError::PageNotResident)?;
        }
        Ok(())
    }

    /// Creates GPU bytes for a resident page and validates its local row.
    ///
    /// # Errors
    ///
    /// Rejects stale tickets, non-resident pages and rows outside the chunk.
    pub fn token(
        &self,
        ticket: PickPageTicket,
        local_row: LocalRow,
    ) -> Result<GpuPickToken, PickingError> {
        let descriptor = self.resident_descriptor(ticket.page, ticket.generation)?;
        validate_local(ticket.page, local_row, descriptor.rows())?;
        Ok(GpuPickToken {
            page: ticket.page.get(),
            local_row: local_row.get(),
        })
    }

    /// Resolves a generation-stamped GPU readback to global provenance.
    ///
    /// # Errors
    ///
    /// Rejects empty, stale, non-resident or out-of-span tokens.
    pub fn resolve(&self, readback: PickReadback) -> Result<GlobalPickIdentity, PickingError> {
        let Some(page) = readback.token.page() else {
            return Err(PickingError::EmptyToken);
        };
        let descriptor = self.resident_descriptor(page, readback.generation)?;
        let local = readback.token.local_row();
        validate_local(page, local, descriptor.rows())?;
        let row = descriptor
            .rows()
            .logical_row(descriptor.chunk(), local)
            .map_err(|_| local_error(page, local, descriptor.rows()))?;
        Ok(GlobalPickIdentity {
            dataset: descriptor.dataset(),
            chunk: descriptor.chunk(),
            row,
            kind: descriptor.kind(),
        })
    }

    fn resident_descriptor(
        &self,
        page: ResidentPage,
        generation: PickGeneration,
    ) -> Result<PickPageDescriptor, PickingError> {
        let slot = self.slot(page)?;
        if slot.generation != generation {
            return Err(PickingError::StaleGeneration);
        }
        match slot.state {
            PageState::Resident(descriptor) => Ok(descriptor),
            PageState::Vacant | PageState::Pending(_) => Err(PickingError::PageNotResident),
        }
    }

    fn slot(&self, page: ResidentPage) -> Result<&PageSlot, PickingError> {
        let index = usize::try_from(page.get()).map_err(|_| page_error(page))?;
        self.slots.get(index).ok_or_else(|| page_error(page))
    }

    fn slot_mut(&mut self, ticket: PickPageTicket) -> Result<&mut PageSlot, PickingError> {
        let index = usize::try_from(ticket.page.get()).map_err(|_| page_error(ticket.page))?;
        let slot = self
            .slots
            .get_mut(index)
            .ok_or_else(|| page_error(ticket.page))?;
        if slot.generation != ticket.generation {
            return Err(PickingError::StaleGeneration);
        }
        Ok(slot)
    }
}

fn validate_local(page: ResidentPage, row: LocalRow, span: ChunkSpan) -> Result<(), PickingError> {
    if row.get() >= span.row_count() {
        return Err(local_error(page, row, span));
    }
    Ok(())
}

fn local_error(page: ResidentPage, row: LocalRow, span: ChunkSpan) -> PickingError {
    PickingError::LocalRowOutsidePage {
        page: page.get(),
        row: row.get(),
        row_count: span.row_count(),
    }
}

fn page_error(page: ResidentPage) -> PickingError {
    PickingError::PageOutOfRange { page: page.get() }
}

fn same_namespace(left: PickPageDescriptor, right: PickPageDescriptor) -> bool {
    left.dataset() == right.dataset()
        && left.chunk() == right.chunk()
        && left.kind() == right.kind()
}

#[cfg(test)]
#[path = "picking_tests.rs"]
mod tests;