egui_ash/
allocator.rs

1use std::ffi::c_void;
2use std::ptr::NonNull;
3
4use anyhow::Result;
5use ash::vk::*;
6
7/// Represents a memory allocation.
8pub trait Allocation: Send + Sync {
9    /// Returns the vk::DeviceMemory object that is backing this allocation.
10    unsafe fn memory(&self) -> DeviceMemory;
11
12    /// Returns the offset of the allocation on the vk::DeviceMemory. When binding the memory to a buffer or image, this offset needs to be supplied as well.
13    fn offset(&self) -> u64;
14
15    /// Returns the size of the allocation
16    fn size(&self) -> u64;
17
18    /// Returns a valid mapped pointer if the memory is host visible, otherwise it will return None. The pointer already points to the exact memory region of the suballocation, so no offset needs to be applied.
19    fn mapped_ptr(&self) -> Option<NonNull<c_void>>;
20}
21
22/// Represents a memory location for [`AllocationCreateInfo`].
23pub trait MemoryLocation: Clone + Send + Sync {
24    fn gpu_only() -> Self;
25    fn cpu_to_gpu() -> Self;
26    fn gpu_to_cpu() -> Self;
27}
28
29/// allocation create info for [`Allocator::allocate`].
30pub trait AllocationCreateInfo {
31    type MemoryLocation: MemoryLocation;
32    fn new(
33        name: Option<&'static str>,
34        requirements: MemoryRequirements,
35        location: Self::MemoryLocation,
36        linear: bool,
37    ) -> Self;
38}
39
40/// trait for GPU memory allocator.
41pub trait Allocator: Clone + Send + Sync {
42    type Allocation: Allocation;
43    type AllocationCreateInfo: AllocationCreateInfo;
44
45    /// Allocate a new memory region.
46    fn allocate(&self, desc: Self::AllocationCreateInfo) -> Result<Self::Allocation>;
47
48    /// Free a memory region.
49    fn free(&self, allocation: Self::Allocation) -> Result<()>;
50}