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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
pub use self::host_visible::StdHostVisibleMemoryTypePool;
pub use self::host_visible::StdHostVisibleMemoryTypePoolAlloc;
pub use self::non_host_visible::StdNonHostVisibleMemoryTypePool;
pub use self::non_host_visible::StdNonHostVisibleMemoryTypePoolAlloc;
pub use self::pool::StdMemoryPool;
pub use self::pool::StdMemoryPoolAlloc;
use crate::device::physical::MemoryType;
use crate::device::{Device, DeviceOwned};
use crate::memory::DedicatedAlloc;
use crate::memory::DeviceMemory;
use crate::memory::DeviceMemoryAllocError;
use crate::memory::MappedDeviceMemory;
use crate::memory::MemoryRequirements;
use crate::DeviceSize;
use std::sync::Arc;
mod host_visible;
mod non_host_visible;
mod pool;
const MAX_POOL_ALLOC: DeviceSize = 256 * 1024 * 1024;
fn choose_allocation_memory_type<'s, F>(
device: &'s Arc<Device>,
requirements: &MemoryRequirements,
mut filter: F,
map: MappingRequirement,
) -> MemoryType<'s>
where
F: FnMut(MemoryType) -> AllocFromRequirementsFilter,
{
let mem_ty = {
let mut filter = |ty: MemoryType| {
if map == MappingRequirement::Map && !ty.is_host_visible() {
return AllocFromRequirementsFilter::Forbidden;
}
filter(ty)
};
let first_loop = device
.physical_device()
.memory_types()
.map(|t| (t, AllocFromRequirementsFilter::Preferred));
let second_loop = device
.physical_device()
.memory_types()
.map(|t| (t, AllocFromRequirementsFilter::Allowed));
first_loop
.chain(second_loop)
.filter(|&(t, _)| (requirements.memory_type_bits & (1 << t.id())) != 0)
.filter(|&(t, rq)| filter(t) == rq)
.next()
.expect("Couldn't find a memory type to allocate from")
.0
};
mem_ty
}
#[cfg(any(
target_os = "linux",
target_os = "dragonflybsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
pub(crate) fn alloc_dedicated_with_exportable_fd<F>(
device: Arc<Device>,
requirements: &MemoryRequirements,
layout: AllocLayout,
map: MappingRequirement,
dedicated: DedicatedAlloc,
filter: F,
) -> Result<PotentialDedicatedAllocation<StdMemoryPoolAlloc>, DeviceMemoryAllocError>
where
F: FnMut(MemoryType) -> AllocFromRequirementsFilter,
{
assert!(device.enabled_extensions().khr_external_memory_fd);
assert!(device.enabled_extensions().khr_external_memory);
let mem_ty = choose_allocation_memory_type(&device, requirements, filter, map);
match map {
MappingRequirement::Map => {
let mem = DeviceMemory::dedicated_alloc_and_map_with_exportable_fd(
device.clone(),
mem_ty,
requirements.size,
dedicated,
)?;
Ok(PotentialDedicatedAllocation::DedicatedMapped(mem))
}
MappingRequirement::DoNotMap => {
let mem = DeviceMemory::dedicated_alloc_with_exportable_fd(
device.clone(),
mem_ty,
requirements.size,
dedicated,
)?;
Ok(PotentialDedicatedAllocation::Dedicated(mem))
}
}
}
pub unsafe trait MemoryPool: DeviceOwned {
type Alloc: MemoryPoolAlloc;
fn alloc_generic(
&self,
ty: MemoryType,
size: DeviceSize,
alignment: DeviceSize,
layout: AllocLayout,
map: MappingRequirement,
) -> Result<Self::Alloc, DeviceMemoryAllocError>;
fn alloc_from_requirements<F>(
&self,
requirements: &MemoryRequirements,
layout: AllocLayout,
map: MappingRequirement,
dedicated: DedicatedAlloc,
filter: F,
) -> Result<PotentialDedicatedAllocation<Self::Alloc>, DeviceMemoryAllocError>
where
F: FnMut(MemoryType) -> AllocFromRequirementsFilter,
{
let mem_ty = choose_allocation_memory_type(self.device(), requirements, filter, map);
if !requirements.prefer_dedicated && requirements.size <= MAX_POOL_ALLOC {
let alloc = self.alloc_generic(
mem_ty,
requirements.size,
requirements.alignment,
layout,
map,
)?;
return Ok(alloc.into());
}
if let DedicatedAlloc::None = dedicated {
let alloc = self.alloc_generic(
mem_ty,
requirements.size,
requirements.alignment,
layout,
map,
)?;
return Ok(alloc.into());
}
match map {
MappingRequirement::Map => {
let mem = DeviceMemory::dedicated_alloc_and_map(
self.device().clone(),
mem_ty,
requirements.size,
dedicated,
)?;
Ok(PotentialDedicatedAllocation::DedicatedMapped(mem))
}
MappingRequirement::DoNotMap => {
let mem = DeviceMemory::dedicated_alloc(
self.device().clone(),
mem_ty,
requirements.size,
dedicated,
)?;
Ok(PotentialDedicatedAllocation::Dedicated(mem))
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AllocFromRequirementsFilter {
Preferred,
Allowed,
Forbidden,
}
pub unsafe trait MemoryPoolAlloc: Send + Sync {
fn mapped_memory(&self) -> Option<&MappedDeviceMemory>;
fn memory(&self) -> &DeviceMemory;
fn offset(&self) -> DeviceSize;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MappingRequirement {
Map,
DoNotMap,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum AllocLayout {
Linear,
Optimal,
}
#[derive(Debug)]
pub enum PotentialDedicatedAllocation<A> {
Generic(A),
Dedicated(DeviceMemory),
DedicatedMapped(MappedDeviceMemory),
}
unsafe impl<A> MemoryPoolAlloc for PotentialDedicatedAllocation<A>
where
A: MemoryPoolAlloc,
{
#[inline]
fn mapped_memory(&self) -> Option<&MappedDeviceMemory> {
match *self {
PotentialDedicatedAllocation::Generic(ref alloc) => alloc.mapped_memory(),
PotentialDedicatedAllocation::Dedicated(_) => None,
PotentialDedicatedAllocation::DedicatedMapped(ref mem) => Some(mem),
}
}
#[inline]
fn memory(&self) -> &DeviceMemory {
match *self {
PotentialDedicatedAllocation::Generic(ref alloc) => alloc.memory(),
PotentialDedicatedAllocation::Dedicated(ref mem) => mem,
PotentialDedicatedAllocation::DedicatedMapped(ref mem) => mem.as_ref(),
}
}
#[inline]
fn offset(&self) -> DeviceSize {
match *self {
PotentialDedicatedAllocation::Generic(ref alloc) => alloc.offset(),
PotentialDedicatedAllocation::Dedicated(_) => 0,
PotentialDedicatedAllocation::DedicatedMapped(_) => 0,
}
}
}
impl<A> From<A> for PotentialDedicatedAllocation<A> {
#[inline]
fn from(alloc: A) -> PotentialDedicatedAllocation<A> {
PotentialDedicatedAllocation::Generic(alloc)
}
}