wgpu_core/track/blas.rs
1use crate::{
2 resource::{Blas, Trackable},
3 track::metadata::ResourceMetadata,
4};
5use alloc::sync::Arc;
6
7/// A tracker that holds tracks BLASes.
8///
9/// This is mostly a safe shell around [`ResourceMetadata`]
10#[derive(Debug)]
11pub(crate) struct BlasTracker {
12 metadata: ResourceMetadata<Arc<Blas>>,
13 size: usize,
14}
15
16impl BlasTracker {
17 pub fn new() -> Self {
18 Self {
19 metadata: ResourceMetadata::new(),
20 size: 0,
21 }
22 }
23
24 /// Inserts a single resource into the resource tracker.
25 ///
26 /// Returns a reference to the newly inserted resource.
27 /// (This allows avoiding a clone/reference count increase in many cases.)
28 pub fn insert_single(&mut self, resource: Arc<Blas>) -> &Arc<Blas> {
29 let index = resource.tracker_index().as_usize();
30 self.allow_index(index);
31
32 unsafe {
33 // # SAFETY: we just allowed this resource, which makes the metadata object larger if
34 // it's not in bounds
35 self.metadata.insert(index, resource)
36 }
37 }
38
39 /// Sets the size of all the vectors inside the tracker.
40 ///
41 /// Must be called with the highest possible Texture ID before
42 /// all unsafe functions are called.
43 pub fn set_size(&mut self, size: usize) {
44 self.size = size;
45 self.metadata.set_size(size)
46 }
47
48 /// Extend the vectors to let the given index be valid.
49 fn allow_index(&mut self, index: usize) {
50 if index >= self.size {
51 self.set_size(index + 1);
52 }
53 }
54
55 /// Returns true if the tracker owns the given texture.
56 pub fn contains(&self, blas: &Blas) -> bool {
57 self.metadata.contains(blas.tracker_index().as_usize())
58 }
59}