rendy_memory/
memory.rs

1// use std::fmt;
2
3/// Memory object wrapper.
4/// Contains size and properties of the memory.
5#[derive(Debug)]
6pub struct Memory<B: gfx_hal::Backend> {
7    raw: B::Memory,
8    size: u64,
9    properties: gfx_hal::memory::Properties,
10    relevant: relevant::Relevant,
11}
12
13impl<B> Memory<B>
14where
15    B: gfx_hal::Backend,
16{
17    /// Get memory properties.
18    pub fn properties(&self) -> gfx_hal::memory::Properties {
19        self.properties
20    }
21
22    /// Get memory size.
23    pub fn size(&self) -> u64 {
24        self.size
25    }
26
27    /// Get raw memory.
28    pub fn raw(&self) -> &B::Memory {
29        &self.raw
30    }
31
32    /// Unwrap raw memory.
33    pub fn into_raw(self) -> B::Memory {
34        self.relevant.dispose();
35        self.raw
36    }
37
38    /// Create memory from raw object.
39    ///
40    /// # Safety
41    ///
42    /// TODO:
43    pub unsafe fn from_raw(
44        raw: B::Memory,
45        size: u64,
46        properties: gfx_hal::memory::Properties,
47    ) -> Self {
48        Memory {
49            properties,
50            raw,
51            size,
52            relevant: relevant::Relevant,
53        }
54    }
55
56    /// Check if this memory is host-visible and can be mapped.
57    /// `memory.host_visible()` is equivalent to `memory.properties().contains(Properties::CPU_VISIBLE)`
58    pub fn host_visible(&self) -> bool {
59        self.properties
60            .contains(gfx_hal::memory::Properties::CPU_VISIBLE)
61    }
62
63    /// Check if this memory is host-coherent and doesn't require invalidating or flushing.
64    /// `memory.host_coherent()` is equivalent to `memory.properties().contains(Properties::COHERENT)`
65    pub fn host_coherent(&self) -> bool {
66        self.properties
67            .contains(gfx_hal::memory::Properties::COHERENT)
68    }
69}
70
71// pub(crate) fn memory_ptr_fmt(
72//     memory: &*const Memory,
73//     fmt: &mut fmt::Formatter<'_>,
74// ) -> Result<(), fmt::Error> {
75//     unsafe {
76//         if fmt.alternate() {
77//             write!(fmt, "*const {:#?}", **memory)
78//         } else {
79//             write!(fmt, "*const {:?}", **memory)
80//         }
81//     }
82// }