1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::types::{DescriptorPoolCreateFlags, DescriptorTotalCount};

/// Memory exhausted error.
#[derive(Debug)]
pub enum CreatePoolError {
    /// Device memory exhausted.
    OutOfDeviceMemory,

    /// Host memory exhausted.
    OutOfHostMemory,

    /// A descriptor pool creation has failed due to fragmentation.
    Fragmentation,
}

/// Memory exhausted error.
#[derive(Debug)]
pub enum DeviceAllocationError {
    /// Device memory exhausted.
    OutOfDeviceMemory,

    /// Host memory exhausted.
    OutOfHostMemory,

    /// Failed to allocate memory from pool.
    OutOfPoolMemory,

    /// Pool allocation failed due to fragmentation of pool's memory.
    FragmentedPool,
}

/// Abstract device that can create pools of type `P` and allocate sets `S` with layout `L`.
pub trait DescriptorDevice<L, P, S> {
    /// Creates a new descriptor pool.
    ///
    /// # Safety
    ///
    /// Actually safe.
    /// TODO: Remove `unsafe` with next breaking change.
    unsafe fn create_descriptor_pool(
        &self,
        descriptor_count: &DescriptorTotalCount,
        max_sets: u32,
        flags: DescriptorPoolCreateFlags,
    ) -> Result<P, CreatePoolError>;

    /// Destroys descriptor pool.
    ///
    /// # Safety
    ///
    /// Pool must be created from this device.
    /// All descriptor sets allocated from this pool become invalid.
    unsafe fn destroy_descriptor_pool(&self, pool: P);

    /// Allocates descriptor sets.
    ///
    /// # Safety
    ///
    /// Pool must be created from this device.
    unsafe fn alloc_descriptor_sets<'a>(
        &self,
        pool: &mut P,
        layouts: impl ExactSizeIterator<Item = &'a L>,
        sets: &mut impl Extend<S>,
    ) -> Result<(), DeviceAllocationError>
    where
        L: 'a;

    /// Deallocates descriptor sets.
    ///
    /// # Safety
    ///
    /// Sets must be allocated from specified pool and not deallocated before.
    unsafe fn dealloc_descriptor_sets(&self, pool: &mut P, sets: impl Iterator<Item = S>);
}