pebble-engine 0.10.3

A modular, ECS-style graphics/app framework for Rust.
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
use slotmap::{Key as _, SecondaryMap, SlotMap, new_key_type};
use std::collections::HashMap;

new_key_type! {
    /// Untyped slot-map key for an asset entry.
    ///
    /// Prefer the typed [`Handle<T>`](crate::assets::handle::Handle) over this
    /// in most code. `RawAssetHandle` is used internally by the storage and
    /// sync systems.
    pub struct RawAssetHandle;
}

/// Storage for raw CPU-side assets of type `T`.
///
/// Assets are inserted by name and looked up by either name or
/// [`RawAssetHandle`]. When an asset is inserted or updated its handle is
/// pushed onto the *dirty queue*, which the sync system drains each tick to
/// upload changed assets to the GPU.
pub struct Assets<T: 'static + Send + Sync> {
    storage: SlotMap<RawAssetHandle, T>,
    handles: HashMap<String, RawAssetHandle>,
    queue: Vec<RawAssetHandle>,
    removed: Vec<RawAssetHandle>,
}

impl<T: 'static + Send + Sync> Assets<T> {
    pub fn new() -> Self {
        Self {
            storage: SlotMap::with_key(),
            handles: HashMap::new(),
            queue: Vec::new(),
            removed: Vec::new(),
        }
    }

    /// Insert `asset` under `name`, returning its handle.
    ///
    /// If an asset with the same name already exists, its data is replaced
    /// **in-place**: the same slot-map entry (and therefore the same handle)
    /// is reused, so any code that already holds a [`Handle`](crate::assets::handle::Handle)
    /// or [`RawAssetHandle`] for this asset continues to work correctly.
    /// The handle is re-queued so the sync system re-uploads the new data.
    pub fn insert(&mut self, name: &str, asset: T) -> RawAssetHandle {
        if let Some(&existing) = self.handles.get(name) {
            // Replace data in-place so existing handles stay valid.
            if let Some(slot) = self.storage.get_mut(existing) {
                *slot = asset;
                // Only queue once — don't duplicate if already dirty.
                if !self.queue.contains(&existing) {
                    self.queue.push(existing);
                }
                tracing::debug!(
                    "Assets<{}>: replaced data for {:?} ({name}) in-place",
                    std::any::type_name::<T>(),
                    existing
                );
                return existing;
            }
        }

        let handle = self.storage.insert(asset);
        self.handles.insert(name.to_string(), handle);
        self.queue.push(handle);
        handle
    }

    /// Look up an asset by its raw handle.
    pub fn get(&self, handle: RawAssetHandle) -> Option<&T> {
        if handle.is_null() {
            tracing::warn!(
                "Assets<{}>: get() called with a null/default handle — \
                 did you forget to insert the asset and store the returned handle?",
                std::any::type_name::<T>()
            );
            return None;
        }
        let result = self.storage.get(handle);
        if result.is_none() {
            tracing::warn!(
                "Assets<{}>: get() called with a stale handle {:?} — \
                 the asset was likely removed or replaced since this handle was obtained",
                std::any::type_name::<T>(),
                handle
            );
        }
        result
    }

    /// Mutably look up an asset by its raw handle.
    pub fn get_mut(&mut self, handle: RawAssetHandle) -> Option<&mut T> {
        if handle.is_null() {
            tracing::warn!(
                "Assets<{}>: get_mut() called with a null/default handle — \
                 did you forget to insert the asset and store the returned handle?",
                std::any::type_name::<T>()
            );
            return None;
        }
        let result = self.storage.get_mut(handle);
        if result.is_none() {
            tracing::warn!(
                "Assets<{}>: get_mut() called with a stale handle {:?} — \
                 the asset was likely removed or replaced since this handle was obtained",
                std::any::type_name::<T>(),
                handle
            );
        }
        result
    }

    /// Look up an asset by its name.
    pub fn get_by_name(&self, name: &str) -> Option<&T> {
        let result = self.handles
            .get(name)
            .and_then(|&handle| self.storage.get(handle));
        if result.is_none() {
            tracing::debug!(
                "Assets<{}>: get_by_name({:?}) found no asset — \
                 the name may not have been inserted yet",
                std::any::type_name::<T>(),
                name
            );
        }
        result
    }

    /// Mutably look up an asset by its name.
    pub fn get_mut_by_name(&mut self, name: &str) -> Option<&mut T> {
        let handle = self.handles.get(name).copied();
        if handle.is_none() {
            tracing::debug!(
                "Assets<{}>: get_mut_by_name({:?}) found no asset — \
                 the name may not have been inserted yet",
                std::any::type_name::<T>(),
                name
            );
            return None;
        }
        self.storage.get_mut(handle.unwrap())
    }

    /// Look up an asset handle by its name.
    pub fn get_handle_by_name(&self, name: &str) -> Option<RawAssetHandle> {
        self.handles.get(name).copied()
    }

    /// Drain and return all handles currently in the dirty queue.
    ///
    /// Called by the asset sync system each tick.
    pub fn take_dirty(&mut self) -> Vec<RawAssetHandle> {
        std::mem::take(&mut self.queue)
    }

    /// Remove an asset by handle, returning the value if it existed.
    pub fn remove(&mut self, handle: RawAssetHandle) -> Option<T> {
        if handle.is_null() {
            tracing::warn!(
                "Assets<{}>: remove() called with a null/default handle — no-op",
                std::any::type_name::<T>()
            );
            return None;
        }
        let value = self.storage.remove(handle)?;

        self.handles.retain(|_, h| *h != handle);
        self.queue.retain(|h| *h != handle);
        self.removed.push(handle);

        Some(value)
    }

    /// Remove an asset by name, returning the value if it existed.
    pub fn remove_by_name(&mut self, name: &str) -> Option<T> {
        let handle = self.handles.remove(name)?;
        self.queue.retain(|h| *h != handle);
        self.removed.push(handle);
        self.storage.remove(handle)
    }

    /// Drain and return all handles removed since the last call.
    ///
    /// Called by the asset sync system each tick to evict stale processed assets.
    pub fn take_removed(&mut self) -> Vec<RawAssetHandle> {
        std::mem::take(&mut self.removed)
    }

    /// Returns `true` if the dirty queue is empty.
    pub fn dirty_is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    /// Returns the number of handles currently in the dirty queue.
    pub fn dirty_len(&self) -> usize {
        self.queue.len()
    }

    /// Push `handles` back onto the dirty queue so they are retried next tick.
    pub fn requeue(&mut self, handles: Vec<RawAssetHandle>) {
        self.queue.extend(handles);
    }

    /// Replace the data for an existing asset by handle, re-queuing it for
    /// upload. Returns `false` if the handle is null or not present.
    ///
    /// This is the handle-based counterpart to [`insert`](Self::insert): use it
    /// when you already have the handle and don't need to go through a name
    /// lookup. The handle stays valid — only the underlying data changes.
    pub fn replace(&mut self, handle: RawAssetHandle, asset: T) -> bool {
        if handle.is_null() {
            tracing::warn!(
                "Assets<{}>: replace() called with a null/default handle — no-op",
                std::any::type_name::<T>()
            );
            return false;
        }
        let Some(slot) = self.storage.get_mut(handle) else {
            tracing::warn!(
                "Assets<{}>: replace() called with a stale handle {:?} — no-op",
                std::any::type_name::<T>(),
                handle
            );
            return false;
        };
        *slot = asset;
        if !self.queue.contains(&handle) {
            self.queue.push(handle);
        }
        tracing::debug!(
            "Assets<{}>: replaced data for {:?}{} via handle",
            std::any::type_name::<T>(),
            handle,
            self.name_for_handle(handle)
                .map(|n| format!(" ({n})"))
                .unwrap_or_default()
        );
        true
    }

    /// Mark a single asset as dirty so the sync system re-uploads it next tick,
    /// even though its source data has not changed.
    ///
    /// Use this when a resource the asset *depends on* has been recreated (e.g.
    /// a texture that a material instance's bind group references was resized),
    /// requiring the processed asset to be rebuilt with the new version.
    ///
    /// Does nothing if `handle` is null or not present in this store.
    pub fn mark_dirty(&mut self, handle: RawAssetHandle) {
        if handle.is_null() {
            tracing::warn!(
                "Assets<{}>: mark_dirty() called with a null/default handle — no-op",
                std::any::type_name::<T>()
            );
            return;
        }
        if self.storage.contains_key(handle) && !self.queue.contains(&handle) {
            self.queue.push(handle);
        }
    }

    /// Iterate over all assets by handle.
    pub fn iter(&self) -> impl Iterator<Item = (RawAssetHandle, &T)> {
        self.storage.iter()
    }

    /// Mutably iterate over all assets by handle.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (RawAssetHandle, &mut T)> {
        self.storage.iter_mut()
    }

    /// Iterate over `(name, handle)` pairs for every named asset.
    pub fn names(&self) -> impl Iterator<Item = (&str, RawAssetHandle)> {
        self.handles
            .iter()
            .map(|(name, &handle)| (name.as_str(), handle))
    }

    pub fn name_for_handle(&self, handle: RawAssetHandle) -> Option<&str> {
        self.handles
            .iter()
            .find(|(_, h)| **h == handle)
            .map(|(name, _)| name.as_str())
    }
}

impl<'a, T: 'static + Send + Sync> IntoIterator for &'a Assets<T> {
    type Item = (RawAssetHandle, &'a T);
    type IntoIter = slotmap::basic::Iter<'a, RawAssetHandle, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage.iter()
    }
}

impl<'a, T: 'static + Send + Sync> IntoIterator for &'a mut Assets<T> {
    type Item = (RawAssetHandle, &'a mut T);
    type IntoIter = slotmap::basic::IterMut<'a, RawAssetHandle, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage.iter_mut()
    }
}

/// Storage for backend-processed (GPU) assets indexed by the same
/// [`RawAssetHandle`] as their source in [`Assets`].
///
/// Populated by the asset sync system after a successful [`Asset::upload`](crate::assets::upload::Asset::upload).
pub struct ProcessedAssets<T: 'static + Send + Sync> {
    storage: SecondaryMap<RawAssetHandle, T>,
    pub(crate) names: HashMap<String, RawAssetHandle>,
}

impl<T: 'static + Send + Sync> ProcessedAssets<T> {
    pub fn new() -> Self {
        Self {
            storage: SecondaryMap::new(),
            names: HashMap::new(),
        }
    }

    /// Store a processed asset, returning the previous value if one existed.
    pub fn insert(&mut self, handle: RawAssetHandle, asset: T) -> Option<T> {
        self.storage.insert(handle, asset)
    }

    /// Look up a processed asset by handle.
    pub fn get(&self, handle: RawAssetHandle) -> Option<&T> {
        if handle.is_null() {
            tracing::warn!(
                "ProcessedAssets<{}>: get() called with a null/default handle — \
                 did you forget to insert the source asset and store the returned handle?",
                std::any::type_name::<T>()
            );
            return None;
        }
        let result = self.storage.get(handle);
        if result.is_none() {
            tracing::debug!(
                "ProcessedAssets<{}>: get() for handle {:?} returned nothing — \
                 the asset may still be pending upload or was removed",
                std::any::type_name::<T>(),
                handle
            );
        }
        result
    }

    /// Mutably look up a processed asset by handle.
    pub fn get_mut(&mut self, handle: RawAssetHandle) -> Option<&mut T> {
        if handle.is_null() {
            tracing::warn!(
                "ProcessedAssets<{}>: get_mut() called with a null/default handle — \
                 did you forget to insert the source asset and store the returned handle?",
                std::any::type_name::<T>()
            );
            return None;
        }
        let result = self.storage.get_mut(handle);
        if result.is_none() {
            tracing::debug!(
                "ProcessedAssets<{}>: get_mut() for handle {:?} returned nothing — \
                 the asset may still be pending upload or was removed",
                std::any::type_name::<T>(),
                handle
            );
        }
        result
    }

    /// Remove a processed asset by handle, returning the value if it existed.
    pub fn remove(&mut self, handle: RawAssetHandle) -> Option<T> {
        self.names.retain(|_, h| *h != handle);
        self.storage.remove(handle)
    }

    /// Returns `true` if a processed asset exists for `handle`.
    pub fn contains(&self, handle: RawAssetHandle) -> bool {
        self.storage.contains_key(handle)
    }

    /// Iterate over all processed assets by handle.
    pub fn iter(&self) -> impl Iterator<Item = (RawAssetHandle, &T)> {
        self.storage.iter()
    }

    /// Mutably iterate over all processed assets by handle.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (RawAssetHandle, &mut T)> {
        self.storage.iter_mut()
    }

    pub fn get_by_name(&self, name: &str) -> Option<&T> {
        let handle = self.names.get(name)?;
        self.storage.get(*handle)
    }
}

impl<'a, T: 'static + Send + Sync> IntoIterator for &'a ProcessedAssets<T> {
    type Item = (RawAssetHandle, &'a T);
    type IntoIter = slotmap::secondary::Iter<'a, RawAssetHandle, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage.iter()
    }
}

impl<'a, T: 'static + Send + Sync> IntoIterator for &'a mut ProcessedAssets<T> {
    type Item = (RawAssetHandle, &'a mut T);
    type IntoIter = slotmap::secondary::IterMut<'a, RawAssetHandle, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.storage.iter_mut()
    }
}