Skip to content

Commit

Permalink
Add virtual API to VMA wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMayes committed Aug 23, 2024
1 parent 1477690 commit 318f719
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/vma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod allocator;
mod enums;
mod flags;
mod pool;
mod r#virtual;

/// Raw bindings to VMA.
pub mod vma;
Expand All @@ -21,3 +22,4 @@ pub use self::allocator::*;
pub use self::enums::*;
pub use self::flags::*;
pub use self::pool::*;
pub use self::r#virtual::*;
94 changes: 94 additions & 0 deletions ext/vma/src/virtual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: Apache-2.0

use core::mem;

use vulkanalia::prelude::v1_0::*;
use vulkanalia::ResultExt;

use crate::flags::{VirtualAllocationCreateFlags, VirtualBlockCreateFlags};
use crate::vma::*;

#[derive(Copy, Clone, Debug)]
pub struct VirtualBlockOptions {
pub size: vk::DeviceSize,
pub flags: VirtualBlockCreateFlags,
}

#[repr(transparent)]
#[derive(Debug)]
pub struct VirtualBlock(pub(crate) VmaVirtualBlock);

impl VirtualBlock {
pub fn new(options: &VirtualBlockOptions) -> VkResult<Self> {
let info = VmaVirtualBlockCreateInfo {
size: options.size,
flags: options.flags.bits(),
pAllocationCallbacks: core::ptr::null(),
};

unsafe {
let mut block: VmaVirtualBlock = mem::zeroed();
vmaCreateVirtualBlock(&info, &mut block).result()?;
Ok(Self(block))
}
}

pub fn allocate(
&self,
options: &VirtualAllocationOptions,
) -> VkResult<(VirtualAllocation, vk::DeviceSize)> {
let info = VmaVirtualAllocationCreateInfo {
size: options.size,
alignment: options.alignment,
flags: options.flags.bits(),
pUserData: core::ptr::null_mut(),
};

unsafe {
let mut allocation: VmaVirtualAllocation = mem::zeroed();
let mut offset = 0;
vmaVirtualAllocate(self.0, &info, &mut allocation, &mut offset).result()?;
Ok((VirtualAllocation(allocation), offset))
}
}

pub fn free(&self, allocation: VirtualAllocation) {
unsafe { vmaVirtualFree(self.0, allocation.0) };
}

pub fn clear(&self) {
unsafe { vmaClearVirtualBlock(self.0) };
}

pub fn get_allocation_info(&self, allocation: VirtualAllocation) -> VmaVirtualAllocationInfo {
unsafe {
let mut info: VmaVirtualAllocationInfo = mem::zeroed();
vmaGetVirtualAllocationInfo(self.0, allocation.0, &mut info);
info
}
}
}

impl Drop for VirtualBlock {
fn drop(&mut self) {
unsafe { vmaDestroyVirtualBlock(self.0) };
self.0 = core::ptr::null_mut();
}
}

unsafe impl Send for VirtualBlock {}
unsafe impl Sync for VirtualBlock {}

#[derive(Copy, Clone, Debug)]
pub struct VirtualAllocationOptions {
pub size: vk::DeviceSize,
pub alignment: vk::DeviceSize,
pub flags: VirtualAllocationCreateFlags,
}

#[repr(transparent)]
#[derive(Debug)]
pub struct VirtualAllocation(pub(crate) VmaVirtualAllocation);

unsafe impl Send for VirtualAllocation {}
unsafe impl Sync for VirtualAllocation {}

0 comments on commit 318f719

Please sign in to comment.