bort_vk/
memory_allocator.rs

1//! See [here](https://asawicki.info/news_1740_vulkan_memory_types_on_pc_and_how_to_use_them) for advice
2//! on vulkan memory types on PC.
3
4use crate::{device::Device, AllocatorAccess};
5use ash::prelude::VkResult;
6use bort_vma::AllocatorCreateInfo;
7use std::sync::Arc;
8
9/// so it's easy to find all allocation callback args, just in case I want to use them in the future.
10pub const ALLOCATION_CALLBACK_NONE: Option<&ash::vk::AllocationCallbacks> = None;
11
12// ~~ Memory Allocator ~~
13
14pub struct MemoryAllocator {
15    inner: Arc<bort_vma::Allocator>,
16
17    // dependencies
18    device: Arc<Device>,
19}
20
21impl MemoryAllocator {
22    pub fn new(device: Arc<Device>) -> VkResult<Self> {
23        let api_version_uint = device.instance().max_api_version().as_vk_uint();
24        let allocator_info = AllocatorCreateInfo::new(
25            device.instance().inner(),
26            device.inner(),
27            device.physical_device().handle(),
28        )
29        .vulkan_api_version(api_version_uint);
30
31        unsafe { Self::new_from_create_info(device.clone(), allocator_info) }
32    }
33
34    pub unsafe fn new_from_create_info(
35        device: Arc<Device>,
36        create_info: AllocatorCreateInfo,
37    ) -> VkResult<Self> {
38        let inner = Arc::new(bort_vma::Allocator::new(create_info)?);
39        Ok(Self { inner, device })
40    }
41
42    // Getters
43
44    /// Access the `bort_vma::Allocator` struct that `self` contains. Allows you to access vma allocator
45    /// functions.
46    #[inline]
47    pub fn inner(&self) -> &bort_vma::Allocator {
48        &self.inner
49    }
50
51    /// Needed because of the way `bort_vma::AllocatorPool` is implemented.
52    #[inline]
53    pub(crate) fn inner_arc(&self) -> &Arc<bort_vma::Allocator> {
54        &self.inner
55    }
56}
57
58impl AllocatorAccess for MemoryAllocator {
59    #[inline]
60    fn vma_alloc_ref(&self) -> &dyn bort_vma::Alloc {
61        self.inner.as_ref()
62    }
63
64    #[inline]
65    fn device(&self) -> &Arc<Device> {
66        &self.device
67    }
68}