π gpu-allocator
[dependencies]
gpu-allocator = "0.7.0"
This crate provides a fully written in Rust memory allocator for Vulkan, and will provide one for DirectX 12 in the future.
Setting up the Vulkan memory allocator
use gpu_allocator::*;
let mut allocator = VulkanAllocator::new(&VulkanAllocatorCreateDesc {
instance,
device,
physical_device,
debug_settings: Default::default(),
buffer_device_address: true, // Ideally, check the BufferDeviceAddressFeatures struct.
});
Simple Vulkan allocation example
use gpu_allocator::*;
// Setup vulkan info
let vk_info = vk::BufferCreateInfo::builder()
.size(512)
.usage(vk::BufferUsageFlags::STORAGE_BUFFER);
let buffer = unsafe { device.create_buffer(&vk_info, None) }.unwrap();
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) };
let allocation = allocator
.allocate(&AllocationCreateDesc {
name: "Example allocation",
requirements,
location: MemoryLocation::CpuToGpu,
linear: true, // Buffers are always linear
}).unwrap();
// Bind memory to the buffer
unsafe { device.bind_buffer_memory(buffer, allocation.memory(), allocation.offset()).unwrap() };
// Cleanup
allocator.free(allocation).unwrap();
unsafe { device.destroy_buffer(buffer, None) };
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Alternative libraries
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.