1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{AtomSize, Size};

/// Memory object wrapper.
/// Contains size and properties of the memory.
#[derive(Debug)]
pub struct Memory<B: hal::Backend> {
    raw: B::Memory,
    size: Size,
    properties: hal::memory::Properties,
    pub(crate) non_coherent_atom_size: Option<AtomSize>,
}

impl<B: hal::Backend> Memory<B> {
    /// Get memory properties.
    pub fn properties(&self) -> hal::memory::Properties {
        self.properties
    }

    /// Get memory size.
    pub fn size(&self) -> Size {
        self.size
    }

    /// Get raw memory.
    pub fn raw(&self) -> &B::Memory {
        &self.raw
    }

    /// Unwrap raw memory.
    pub fn into_raw(self) -> B::Memory {
        self.raw
    }

    /// Create memory from raw object.
    ///
    /// # Safety
    ///
    /// TODO:
    pub unsafe fn from_raw(
        raw: B::Memory,
        size: Size,
        properties: hal::memory::Properties,
        non_coherent_atom_size: Option<AtomSize>,
    ) -> Self {
        debug_assert_eq!(
            non_coherent_atom_size.is_some(),
            crate::is_non_coherent_visible(properties),
        );
        Memory {
            properties,
            raw,
            size,
            non_coherent_atom_size,
        }
    }

    /// Check if this memory is host-visible and can be mapped.
    /// `Equivalent to `memory.properties().contains(Properties::CPU_VISIBLE)`.
    pub fn is_mappable(&self) -> bool {
        self.properties
            .contains(hal::memory::Properties::CPU_VISIBLE)
    }
}