Vulkan memory management as extension to nobs-vk.
Buffer and image creation in vulkan is tricky in comparison to e.g. OpenGL, because
1. We have to create the buffer/image and then later bind it to avkDeviceMemory
that has to be created separately.
2. Creating a single vkDeviceMemory
allocation for every buffer/image is bad practice, in fact it is encouraged to bind resources that are used together on the same allocation.
3. Another layer of difficulty is introduced with memory types, since not all resources (should) share the same memory properties - which is different for each Driver/Vendor
nobs-vkmem provides convenient and accessible methods for creating buffers and images and binding them to physical memory. This dramatically reduces boiler plate code, while still offers the user considerable control over how resources are bound to memory.
Find a complete documentation of this library at docs.rs.
```rust // create an allocator with default page size // (128MiB for device local / 8MB for host visible memory) let mut allocator = vkmem::Allocator::new(physicaldevicehandle, device_handle);
// declare handles let mut bufub = vk::NULLHANDLE; let mut img = vk::NULLHANDLE; let mut bb = vk::NULLHANDLE;
// configure
vkmem::Buffer::new(&mut bufub)
.size(std::mem::sizeof::
// Mapped gives a convenient view on the memory { let mapped = allocator.getmapped(bufub).unwrap(); let ubb = Ub { a: 123, b: 4, c: 5 }; mapped.hosttodevice(&ubb); // going out of scope automatically unmapps... } { let mapped = allocator.getmapped(bufub).unwrap(); let mut ubb = Ub { a: 0, b: 0, c: 0 }; mapped.devicetohost(&mut ubb); println!("{:?}", ubb); }
// we can print stats in a yaml format for the currently allocated pages println!("{}", allocator.print_stats());
// buffers and images can be destroyed // destroymany() should be preferred over destroy(), // because this will rearrange and merge allocated blocks into the free list again allocator.destroymany(&[buf_ub, img]);
// destroying does NOT free memory // if we want to free memory we can do this only, if a whole page is not used any more // in this case we can free the memory again allocator.free_unused(); ```
Feel encouraged to contribute, in any way you can think!