vulkanalia_vma/
virtual.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use core::mem;
4
5use vulkanalia::ResultExt;
6use vulkanalia::prelude::v1_0::*;
7
8use crate::flags::{VirtualAllocationCreateFlags, VirtualBlockCreateFlags};
9use crate::vma::*;
10
11#[derive(Copy, Clone, Debug)]
12pub struct VirtualBlockOptions {
13    pub size: vk::DeviceSize,
14    pub flags: VirtualBlockCreateFlags,
15}
16
17#[repr(transparent)]
18#[derive(Debug)]
19pub struct VirtualBlock(pub(crate) VmaVirtualBlock);
20
21impl VirtualBlock {
22    pub fn new(options: &VirtualBlockOptions) -> VkResult<Self> {
23        let info = VmaVirtualBlockCreateInfo {
24            size: options.size,
25            flags: options.flags.bits(),
26            pAllocationCallbacks: core::ptr::null(),
27        };
28
29        unsafe {
30            let mut block: VmaVirtualBlock = mem::zeroed();
31            vmaCreateVirtualBlock(&info, &mut block).result()?;
32            Ok(Self(block))
33        }
34    }
35
36    pub fn allocate(
37        &self,
38        options: &VirtualAllocationOptions,
39    ) -> VkResult<(VirtualAllocation, vk::DeviceSize)> {
40        let info = VmaVirtualAllocationCreateInfo {
41            size: options.size,
42            alignment: options.alignment,
43            flags: options.flags.bits(),
44            pUserData: core::ptr::null_mut(),
45        };
46
47        unsafe {
48            let mut allocation: VmaVirtualAllocation = mem::zeroed();
49            let mut offset = 0;
50            vmaVirtualAllocate(self.0, &info, &mut allocation, &mut offset).result()?;
51            Ok((VirtualAllocation(allocation), offset))
52        }
53    }
54
55    pub fn free(&self, allocation: VirtualAllocation) {
56        unsafe { vmaVirtualFree(self.0, allocation.0) };
57    }
58
59    pub fn clear(&self) {
60        unsafe { vmaClearVirtualBlock(self.0) };
61    }
62
63    pub fn get_allocation_info(&self, allocation: VirtualAllocation) -> VmaVirtualAllocationInfo {
64        unsafe {
65            let mut info: VmaVirtualAllocationInfo = mem::zeroed();
66            vmaGetVirtualAllocationInfo(self.0, allocation.0, &mut info);
67            info
68        }
69    }
70}
71
72impl Drop for VirtualBlock {
73    fn drop(&mut self) {
74        unsafe { vmaDestroyVirtualBlock(self.0) };
75        self.0 = core::ptr::null_mut();
76    }
77}
78
79unsafe impl Send for VirtualBlock {}
80unsafe impl Sync for VirtualBlock {}
81
82#[derive(Copy, Clone, Debug)]
83pub struct VirtualAllocationOptions {
84    pub size: vk::DeviceSize,
85    pub alignment: vk::DeviceSize,
86    pub flags: VirtualAllocationCreateFlags,
87}
88
89#[repr(transparent)]
90#[derive(Debug)]
91pub struct VirtualAllocation(pub(crate) VmaVirtualAllocation);
92
93unsafe impl Send for VirtualAllocation {}
94unsafe impl Sync for VirtualAllocation {}