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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
pub mod chunk;
pub mod hybrid;
pub mod passthrough;
pub use self::hybrid::{HybridAllocation, HybridAllocator};
use std::ops::Range;
use gfx_hal::adapter::{MemoryProperties, MemoryTypeId};
use gfx_hal::device::OutOfMemory;
use gfx_hal::memory::{Properties, Requirements};
use gfx_hal::Backend;
use failure::Fail;
#[derive(Debug, Fail)]
pub enum AllocationError {
#[fail(display = "{}", _0)]
OutOfMemory(OutOfMemory),
#[fail(display = "Too many objects allocated")]
TooManyObjects,
#[fail(
display = "No suitable memory type found among {:#b} with flags {:?}",
type_mask, flags
)]
NoSuitableHeap { type_mask: u64, flags: Properties },
}
impl From<gfx_hal::device::AllocationError> for AllocationError {
fn from(v: gfx_hal::device::AllocationError) -> AllocationError {
match v {
gfx_hal::device::AllocationError::OutOfMemory(o) => AllocationError::OutOfMemory(o),
gfx_hal::device::AllocationError::TooManyObjects => AllocationError::TooManyObjects,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Hint {
Permanent,
ShortTerm,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AllocationSpec {
pub hint: Hint,
pub required_flags: Properties,
pub preferred_flags: Properties,
}
impl AllocationSpec {
pub fn new(
hint: Hint,
required_flags: Properties,
preferred_flags: Properties,
) -> AllocationSpec {
AllocationSpec {
hint,
required_flags,
preferred_flags,
}
}
}
pub trait Allocation<B: Backend> {
fn memory(&self) -> &B::Memory;
fn flags(&self) -> Properties;
fn range(&self) -> Range<u64>;
}
pub trait Allocator<B: Backend> {
type Allocation: Allocation<B>;
fn alloc(
&mut self,
device: &B::Device,
req: Requirements,
spec: AllocationSpec,
) -> Result<Self::Allocation, AllocationError>;
fn free(&mut self, device: &B::Device, allocation: Self::Allocation);
}
pub(crate) fn pick_memory_type(
props: &MemoryProperties,
req: Requirements,
spec: AllocationSpec,
) -> Result<MemoryTypeId, AllocationError> {
props
.memory_types
.iter()
.enumerate()
.filter(|&(i, ty)| {
req.size <= props.memory_heaps[ty.heap_index]
&& req.type_mask & (1 << i as u64) != 0
&& spec.required_flags.contains(ty.properties)
})
.max_by_key(|(_, ty)| (ty.properties & spec.preferred_flags).bits().count_ones())
.map(|(i, _)| MemoryTypeId(i))
.ok_or(AllocationError::NoSuitableHeap {
type_mask: req.type_mask,
flags: spec.required_flags,
})
}