tessera_ui/renderer/compute/
resource.rs

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