gpu_alloc/
error.rs

1use {
2    core::fmt::{self, Display},
3    gpu_alloc_types::{DeviceMapError, OutOfMemory},
4};
5
6/// Enumeration of possible errors that may occur during memory allocation.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum AllocationError {
9    /// Backend reported that device memory has been exhausted.\
10    /// Deallocating device memory from the same heap may increase chance
11    /// that another allocation would succeed.
12    OutOfDeviceMemory,
13
14    /// Backend reported that host memory has been exhausted.\
15    /// Deallocating host memory may increase chance that another allocation would succeed.
16    OutOfHostMemory,
17
18    /// Allocation request cannot be fulfilled as no available memory types allowed
19    /// by `Request.memory_types` mask is compatible with `request.usage`.
20    NoCompatibleMemoryTypes,
21
22    /// Reached limit on allocated memory objects count.\
23    /// Deallocating device memory may increase chance that another allocation would succeed.
24    /// Especially dedicated memory blocks.
25    ///
26    /// If this error is returned when memory heaps are far from exhausted
27    /// `Config` should be tweaked to allocate larger memory objects.
28    TooManyObjects,
29}
30
31impl From<OutOfMemory> for AllocationError {
32    fn from(err: OutOfMemory) -> Self {
33        match err {
34            OutOfMemory::OutOfDeviceMemory => AllocationError::OutOfDeviceMemory,
35            OutOfMemory::OutOfHostMemory => AllocationError::OutOfHostMemory,
36        }
37    }
38}
39
40impl Display for AllocationError {
41    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            AllocationError::OutOfDeviceMemory => fmt.write_str("Device memory exhausted"),
44            AllocationError::OutOfHostMemory => fmt.write_str("Host memory exhausted"),
45            AllocationError::NoCompatibleMemoryTypes => fmt.write_str(
46                "No compatible memory types from requested types support requested usage",
47            ),
48            AllocationError::TooManyObjects => {
49                fmt.write_str("Reached limit on allocated memory objects count")
50            }
51        }
52    }
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for AllocationError {}
57
58/// Enumeration of possible errors that may occur during memory mapping.
59#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
60pub enum MapError {
61    /// Backend reported that device memory has been exhausted.\
62    /// Deallocating device memory from the same heap may increase chance
63    /// that another mapping would succeed.
64    OutOfDeviceMemory,
65
66    /// Backend reported that host memory has been exhausted.\
67    /// Deallocating host memory may increase chance that another mapping would succeed.
68    OutOfHostMemory,
69
70    /// Attempt to map memory block with non-host-visible memory type.\
71    /// Ensure to include `UsageFlags::HOST_ACCESS` into allocation request
72    /// when memory mapping is intended.
73    NonHostVisible,
74
75    /// Map failed for implementation specific reason.\
76    /// For Vulkan backend this includes failed attempt
77    /// to allocate large enough virtual address space.
78    MapFailed,
79
80    /// Mapping failed due to block being already mapped.
81    AlreadyMapped,
82}
83
84impl From<DeviceMapError> for MapError {
85    fn from(err: DeviceMapError) -> Self {
86        match err {
87            DeviceMapError::OutOfDeviceMemory => MapError::OutOfDeviceMemory,
88            DeviceMapError::OutOfHostMemory => MapError::OutOfHostMemory,
89            DeviceMapError::MapFailed => MapError::MapFailed,
90        }
91    }
92}
93
94impl From<OutOfMemory> for MapError {
95    fn from(err: OutOfMemory) -> Self {
96        match err {
97            OutOfMemory::OutOfDeviceMemory => MapError::OutOfDeviceMemory,
98            OutOfMemory::OutOfHostMemory => MapError::OutOfHostMemory,
99        }
100    }
101}
102
103impl Display for MapError {
104    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
105        match self {
106            MapError::OutOfDeviceMemory => fmt.write_str("Device memory exhausted"),
107            MapError::OutOfHostMemory => fmt.write_str("Host memory exhausted"),
108            MapError::MapFailed => fmt.write_str("Failed to map memory object"),
109            MapError::NonHostVisible => fmt.write_str("Impossible to map non-host-visible memory"),
110            MapError::AlreadyMapped => fmt.write_str("Block is already mapped"),
111        }
112    }
113}
114
115#[cfg(feature = "std")]
116impl std::error::Error for MapError {}