tessera_ui/renderer/compute/
resource.rs

1use std::collections::HashMap;
2
3use uuid::Uuid;
4
5pub type ComputeResource = wgpu::Buffer;
6pub type ComputeResourceRef = uuid::Uuid;
7
8#[derive(Debug)]
9pub struct ComputeResourceManager {
10    resources: HashMap<Uuid, ComputeResource>,
11}
12
13impl Default for ComputeResourceManager {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl ComputeResourceManager {
20    pub fn new() -> Self {
21        Self {
22            resources: HashMap::new(),
23        }
24    }
25
26    /// Clear all resources.
27    pub fn clear(&mut self) {
28        self.resources.clear();
29    }
30
31    /// Move a buffer into the resource manager.
32    pub fn push(&mut self, buffer: wgpu::Buffer) -> ComputeResourceRef {
33        let id = Uuid::new_v4();
34        self.resources.insert(id, buffer);
35        id
36    }
37
38    /// Access a resource in ref by its ID.
39    pub fn get(&self, id: &ComputeResourceRef) -> Option<&ComputeResource> {
40        self.resources.get(id)
41    }
42
43    /// Remove a resource by its ID.
44    pub fn remove(&mut self, id: &ComputeResourceRef) -> Option<ComputeResource> {
45        self.resources.remove(id)
46    }
47
48    /// Check if a resource exists by its ID.
49    pub fn contains(&self, id: &ComputeResourceRef) -> bool {
50        self.resources.contains_key(id)
51    }
52
53    /// Move a resource out of the manager.
54    pub fn take(&mut self, id: &ComputeResourceRef) -> Option<ComputeResource> {
55        self.resources.remove(id)
56    }
57}