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
326
327
328
329
330
331
332
333
334
335
336
337
//! Common functions for `Allocator` and `AllocatorPool`
use crate::ffi;
use crate::Allocation;
use crate::AllocationCreateInfo;
use crate::Allocator;
use crate::AllocatorPool;
use crate::PoolHandle;
use ash::{prelude::VkResult, vk};
pub trait Alloc {
fn allocator(&self) -> &Allocator;
fn pool(&self) -> PoolHandle;
/// Helps to find memory type index, given memory type bits and allocation info.
///
/// This algorithm tries to find a memory type that:
///
/// - Is allowed by memory type bits.
/// - Contains all the flags from `allocation_info.required_flags`.
/// - Matches intended usage.
/// - Has as many flags from `allocation_info.preferred_flags` as possible.
///
/// Returns ash::vk::Result::ERROR_FEATURE_NOT_PRESENT if not found. Receiving such a result
/// from this function or any other allocating function probably means that your
/// device doesn't support any memory type with requested features for the specific
/// type of resource you want to use it for. Please check parameters of your
/// resource, like image layout (OPTIMAL versus LINEAR) or mip level count.
unsafe fn find_memory_type_index(
&self,
memory_type_bits: u32,
allocation_info: &AllocationCreateInfo,
) -> VkResult<u32> {
let mut memory_type_index: u32 = 0;
let mut allocation_info: ffi::VmaAllocationCreateInfo = allocation_info.into();
allocation_info.pool = self.pool().0;
ffi::vmaFindMemoryTypeIndex(
self.allocator().internal,
memory_type_bits,
&allocation_info,
&mut memory_type_index,
)
.result()?;
Ok(memory_type_index)
}
/// Helps to find memory type index, given buffer info and allocation info.
///
/// It can be useful e.g. to determine value to be used as `AllocatorPoolCreateInfo::memory_type_index`.
/// It internally creates a temporary, dummy buffer that never has memory bound.
/// It is just a convenience function, equivalent to calling:
///
/// - `ash::vk::Device::create_buffer`
/// - `ash::vk::Device::get_buffer_memory_requirements`
/// - `Allocator::find_memory_type_index`
/// - `ash::vk::Device::destroy_buffer`
unsafe fn find_memory_type_index_for_buffer_info(
&self,
buffer_info: &ash::vk::BufferCreateInfo,
allocation_info: &AllocationCreateInfo,
) -> VkResult<u32> {
let mut allocation_info: ffi::VmaAllocationCreateInfo = allocation_info.into();
allocation_info.pool = self.pool().0;
let mut memory_type_index: u32 = 0;
ffi::vmaFindMemoryTypeIndexForBufferInfo(
self.allocator().internal,
buffer_info,
&allocation_info,
&mut memory_type_index,
)
.result()?;
Ok(memory_type_index)
}
/// Helps to find memory type index, given image info and allocation info.
///
/// It can be useful e.g. to determine value to be used as `AllocatorPoolCreateInfo::memory_type_index`.
/// It internally creates a temporary, dummy image that never has memory bound.
/// It is just a convenience function, equivalent to calling:
///
/// - `ash::vk::Device::create_image`
/// - `ash::vk::Device::get_image_memory_requirements`
/// - `Allocator::find_memory_type_index`
/// - `ash::vk::Device::destroy_image`
unsafe fn find_memory_type_index_for_image_info(
&self,
image_info: ash::vk::ImageCreateInfo,
allocation_info: &AllocationCreateInfo,
) -> VkResult<u32> {
let mut allocation_info: ffi::VmaAllocationCreateInfo = allocation_info.into();
allocation_info.pool = self.pool().0;
let mut memory_type_index: u32 = 0;
ffi::vmaFindMemoryTypeIndexForImageInfo(
self.allocator().internal,
&image_info,
&allocation_info,
&mut memory_type_index,
)
.result()?;
Ok(memory_type_index)
}
/// General purpose memory allocation.
///
/// You should free the memory using `Allocator::free_memory` or 'Allocator::free_memory_pages'.
///
/// It is recommended to use `Allocator::allocate_memory_for_buffer`, `Allocator::allocate_memory_for_image`,
/// `Allocator::create_buffer`, `Allocator::create_image` instead whenever possible.
unsafe fn allocate_memory(
&self,
memory_requirements: &ash::vk::MemoryRequirements,
create_info: &AllocationCreateInfo,
) -> VkResult<Allocation> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
ffi::vmaAllocateMemory(
self.allocator().internal,
memory_requirements,
&create_info,
&mut allocation,
std::ptr::null_mut(),
)
.result()?;
Ok(Allocation(allocation))
}
/// General purpose memory allocation for multiple allocation objects at once.
///
/// You should free the memory using `Allocator::free_memory` or `Allocator::free_memory_pages`.
///
/// Word "pages" is just a suggestion to use this function to allocate pieces of memory needed for sparse binding.
/// It is just a general purpose allocation function able to make multiple allocations at once.
/// It may be internally optimized to be more efficient than calling `Allocator::allocate_memory` `allocations.len()` times.
///
/// All allocations are made using same parameters. All of them are created out of the same memory pool and type.
unsafe fn allocate_memory_pages(
&self,
memory_requirements: &ash::vk::MemoryRequirements,
create_info: &AllocationCreateInfo,
allocation_count: usize,
) -> VkResult<Vec<Allocation>> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut allocations: Vec<ffi::VmaAllocation> = vec![std::mem::zeroed(); allocation_count];
ffi::vmaAllocateMemoryPages(
self.allocator().internal,
memory_requirements,
&create_info,
allocation_count,
allocations.as_mut_ptr(),
std::ptr::null_mut(),
)
.result()?;
let allocations: Vec<Allocation> = allocations
.into_iter()
.map(|alloc| Allocation(alloc))
.collect();
Ok(allocations)
}
/// Buffer specialized memory allocation.
///
/// You should free the memory using `Allocator::free_memory` or 'Allocator::free_memory_pages'.
unsafe fn allocate_memory_for_buffer(
&self,
buffer: ash::vk::Buffer,
create_info: &AllocationCreateInfo,
) -> VkResult<Allocation> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
let mut allocation_info: ffi::VmaAllocationInfo = std::mem::zeroed();
ffi::vmaAllocateMemoryForBuffer(
self.allocator().internal,
buffer,
&create_info,
&mut allocation,
&mut allocation_info,
)
.result()?;
Ok(Allocation(allocation))
}
/// Image specialized memory allocation.
///
/// You should free the memory using `Allocator::free_memory` or 'Allocator::free_memory_pages'.
unsafe fn allocate_memory_for_image(
&self,
image: ash::vk::Image,
create_info: &AllocationCreateInfo,
) -> VkResult<Allocation> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
ffi::vmaAllocateMemoryForImage(
self.allocator().internal,
image,
&create_info,
&mut allocation,
std::ptr::null_mut(),
)
.result()?;
Ok(Allocation(allocation))
}
/// This function automatically creates a buffer, allocates appropriate memory
/// for it, and binds the buffer with the memory.
///
/// If the function succeeded, you must destroy both buffer and allocation when you
/// no longer need them using either convenience function `Allocator::destroy_buffer` or
/// separately, using `ash::Device::destroy_buffer` and `Allocator::free_memory`.
///
/// If `AllocatorCreateFlags::KHR_DEDICATED_ALLOCATION` flag was used,
/// VK_KHR_dedicated_allocation extension is used internally to query driver whether
/// it requires or prefers the new buffer to have dedicated allocation. If yes,
/// and if dedicated allocation is possible (AllocationCreateInfo::pool is null
/// and `AllocationCreateFlags::NEVER_ALLOCATE` is not used), it creates dedicated
/// allocation for this buffer, just like when using `AllocationCreateFlags::DEDICATED_MEMORY`.
unsafe fn create_buffer(
&self,
buffer_info: &ash::vk::BufferCreateInfo,
create_info: &AllocationCreateInfo,
) -> VkResult<(ash::vk::Buffer, Allocation)> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut buffer = vk::Buffer::null();
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
ffi::vmaCreateBuffer(
self.allocator().internal,
&*buffer_info,
&create_info,
&mut buffer,
&mut allocation,
std::ptr::null_mut(),
)
.result()?;
Ok((buffer, Allocation(allocation)))
}
/// brief Creates a buffer with additional minimum alignment.
///
/// Similar to vmaCreateBuffer() but provides additional parameter `minAlignment` which allows to specify custom,
/// minimum alignment to be used when placing the buffer inside a larger memory block, which may be needed e.g.
/// for interop with OpenGL.
unsafe fn create_buffer_with_alignment(
&self,
buffer_info: &ash::vk::BufferCreateInfo,
create_info: &AllocationCreateInfo,
min_alignment: vk::DeviceSize,
) -> VkResult<(ash::vk::Buffer, Allocation)> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut buffer = vk::Buffer::null();
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
ffi::vmaCreateBufferWithAlignment(
self.allocator().internal,
&*buffer_info,
&create_info,
min_alignment,
&mut buffer,
&mut allocation,
std::ptr::null_mut(),
)
.result()?;
Ok((buffer, Allocation(allocation)))
}
/// This function automatically creates an image, allocates appropriate memory
/// for it, and binds the image with the memory.
///
/// If the function succeeded, you must destroy both image and allocation when you
/// no longer need them using either convenience function `Allocator::destroy_image` or
/// separately, using `ash::Device::destroy_image` and `Allocator::free_memory`.
///
/// If `AllocatorCreateFlags::KHR_DEDICATED_ALLOCATION` flag was used,
/// `VK_KHR_dedicated_allocation extension` is used internally to query driver whether
/// it requires or prefers the new image to have dedicated allocation. If yes,
/// and if dedicated allocation is possible (AllocationCreateInfo::pool is null
/// and `AllocationCreateFlags::NEVER_ALLOCATE` is not used), it creates dedicated
/// allocation for this image, just like when using `AllocationCreateFlags::DEDICATED_MEMORY`.
///
/// If `VK_ERROR_VALIDAITON_FAILED_EXT` is returned, VMA may have encountered a problem
/// that is not caught by the validation layers. One example is if you try to create a 0x0
/// image, a panic will occur and `VK_ERROR_VALIDAITON_FAILED_EXT` is thrown.
unsafe fn create_image(
&self,
image_info: &ash::vk::ImageCreateInfo,
create_info: &AllocationCreateInfo,
) -> VkResult<(ash::vk::Image, Allocation)> {
let mut create_info: ffi::VmaAllocationCreateInfo = create_info.into();
create_info.pool = self.pool().0;
let mut image = vk::Image::null();
let mut allocation: ffi::VmaAllocation = std::mem::zeroed();
ffi::vmaCreateImage(
self.allocator().internal,
&*image_info,
&create_info,
&mut image,
&mut allocation,
std::ptr::null_mut(),
)
.result()?;
Ok((image, Allocation(allocation)))
}
}
impl Alloc for AllocatorPool {
fn allocator(&self) -> &Allocator {
self.allocator.as_ref()
}
fn pool(&self) -> PoolHandle {
self.pool
}
}
impl Alloc for Allocator {
fn allocator(&self) -> &Allocator {
self
}
fn pool(&self) -> PoolHandle {
PoolHandle(std::ptr::null_mut())
}
}