bort_vk/
memory_access.rs

1use crate::{Device, MemoryAllocation, MemoryError};
2#[cfg(feature = "bytemuck")]
3use bytemuck::{NoUninit, Pod};
4use std::sync::Arc;
5
6// ~~ Allocator Access ~~
7
8/// Unifies different types of vma allocators
9pub trait AllocatorAccess: Send + Sync {
10    fn vma_alloc_ref(&self) -> &dyn bort_vma::Alloc;
11    fn device(&self) -> &Arc<Device>;
12
13    #[inline]
14    fn vma_allocator(&self) -> &bort_vma::Allocator {
15        self.vma_alloc_ref().allocator()
16    }
17}
18
19// ~~ Allocation Access ~~
20
21/// Allows any struct containing a memory allocation to "inherit" the read/write functions
22pub trait AllocationAccess {
23    fn memory_allocation_mut(&mut self) -> &mut MemoryAllocation;
24
25    #[cfg(feature = "bytemuck")]
26    fn write_into_bytes<T>(
27        &mut self,
28        write_data: T,
29        allocation_offset: usize,
30    ) -> Result<(), MemoryError>
31    where
32        T: NoUninit,
33    {
34        self.memory_allocation_mut()
35            .write_into_bytes(write_data, allocation_offset)
36    }
37
38    #[cfg(feature = "bytemuck")]
39    fn write_slice<T>(
40        &mut self,
41        write_data: &[T],
42        allocation_offset: usize,
43    ) -> Result<(), MemoryError>
44    where
45        T: NoUninit,
46    {
47        self.memory_allocation_mut()
48            .write_slice(write_data, allocation_offset)
49    }
50
51    fn write_bytes(
52        &mut self,
53        write_bytes: &[u8],
54        allocation_offset: usize,
55    ) -> Result<(), MemoryError> {
56        self.memory_allocation_mut()
57            .write_bytes(write_bytes, allocation_offset)
58    }
59
60    fn write_struct<T>(
61        &mut self,
62        write_data: T,
63        allocation_offset: usize,
64    ) -> Result<(), MemoryError> {
65        self.memory_allocation_mut()
66            .write_struct(write_data, allocation_offset)
67    }
68
69    fn write_iter<I, T>(
70        &mut self,
71        write_data: I,
72        allocation_offset: usize,
73    ) -> Result<(), MemoryError>
74    where
75        I: IntoIterator<Item = T>,
76        I::IntoIter: ExactSizeIterator,
77    {
78        self.memory_allocation_mut()
79            .write_iter(write_data, allocation_offset)
80    }
81
82    #[cfg(feature = "bytemuck")]
83    fn read_vec<T>(
84        &mut self,
85        element_count: usize,
86        allocation_offset: usize,
87    ) -> Result<Vec<T>, MemoryError>
88    where
89        T: Pod,
90    {
91        self.memory_allocation_mut()
92            .read_vec(element_count, allocation_offset)
93    }
94
95    fn read_struct<T>(&mut self, allocation_offset: usize) -> Result<T, MemoryError> {
96        self.memory_allocation_mut().read_struct(allocation_offset)
97    }
98}