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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::sync::Arc;
use std::marker::PhantomData;
use smallvec::SmallVec;
use vks;
use ::{ VdResult, Device, Handle, CommandPoolCreateInfo, CommandPoolCreateFlags,
    CommandBufferAllocateInfo, CommandBufferHandle, CommandBufferLevel, CommandBuffer};


#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct CommandPoolHandle(pub(crate) vks::VkCommandPool);

impl CommandPoolHandle {
    #[inline(always)]
    pub fn to_raw(&self) -> vks::VkCommandPool {
        self.0
    }
}

unsafe impl Handle for CommandPoolHandle {
    type Target = CommandPoolHandle;

    #[inline(always)]
    fn handle(&self) -> Self::Target {
        *self
    }
}


#[derive(Debug)]
struct Inner {
    handle: CommandPoolHandle,
    device: Device,
}

impl Drop for Inner {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_command_pool(self.handle, None);
        }
    }
}


/// A command pool.
///
///
/// ### Destruction
/// 
/// Dropping this `CommandPool` will cause `Device::destroy_command_pool` to be called, 
/// automatically releasing any resources associated with it.
///
#[derive(Debug, Clone)]
pub struct CommandPool {
    inner: Arc<Inner>,
}

impl CommandPool {
    /// Returns a new `CommandPoolBuilder`.
    pub fn builder<'b>() -> CommandPoolBuilder<'b> {
        CommandPoolBuilder::new()
    }

    /// Allocates command buffers from an existing command pool.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkAllocateCommandBuffers.html
    //
    fn allocate_command_buffer_handles(&self, level: CommandBufferLevel, count: u32)
            -> VdResult<SmallVec<[CommandBufferHandle; 16]>> {
        let alloc_info = CommandBufferAllocateInfo::builder()
            .command_pool(self.handle())
            .level(level)
            .command_buffer_count(count)
            .build();

        // let mut command_buffers: SmallVec<[CommandBufferHandle; 16]> = SmallVec::new();
        // command_buffers.reserve_exact(count as usize);
        // unsafe {
        //     command_buffers.set_len(count as usize);
        //     ::check(self.inner.device.proc_addr_loader().vk.vkAllocateCommandBuffers(
        //         self.inner.device.handle().0, alloc_info.as_raw(),
        //         command_buffers.as_mut_ptr() as *mut vks::VkCommandBuffer));
        // }
        // Ok(command_buffers)
        unsafe { self.inner.device.allocate_command_buffers(&alloc_info) }
    }

    /// Allocates command buffers from an existing command pool.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkAllocateCommandBuffers.html
    //
    pub fn allocate_command_buffers(&self, level: CommandBufferLevel, count: u32)
            -> VdResult<SmallVec<[CommandBuffer; 16]>> {
        self.allocate_command_buffer_handles(level, count)?.iter().map(|&hndl| {
            CommandBuffer::from_parts(self.clone(), hndl)
        }).collect::<Result<SmallVec<_>, _>>()
    }

    /// Allocates a command buffer from an existing command pool.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkAllocateCommandBuffers.html
    //
    pub fn allocate_command_buffer(&self, level: CommandBufferLevel) -> VdResult<CommandBuffer> {
        self.allocate_command_buffers(level, 1).map(|mut cbs| cbs.remove(0))
    }

    /// Returns this object's handle.
    pub fn handle(&self) -> CommandPoolHandle {
        self.inner.handle
    }

    /// Returns a reference to the associated device.
    pub fn device(&self) -> &Device {
        &self.inner.device
    }
}

unsafe impl<'h> Handle for &'h CommandPool {
    type Target = CommandPoolHandle;

    #[inline(always)]
    fn handle(&self) -> Self::Target {
        self.inner.handle
    }
}


/// A builder for `CommandPool`.
#[derive(Debug, Clone)]
pub struct CommandPoolBuilder<'b> {
    create_info: CommandPoolCreateInfo<'b>,
    _p: PhantomData<&'b ()>,
}

impl<'b> CommandPoolBuilder<'b> {
    /// Returns a new render pass builder.
    pub fn new() -> CommandPoolBuilder<'b> {
        CommandPoolBuilder {
            create_info: CommandPoolCreateInfo::default(),
            _p: PhantomData,
        }
    }

    /// Specifies the usage behavior for the pool and command buffers
    /// allocated from it.
    pub fn flags<'s>(&'s mut self, flags: CommandPoolCreateFlags)
            -> &'s mut CommandPoolBuilder<'b> {
        self.create_info.set_flags(flags);
        self
    }

    /// Specifies a queue family.
    ///
    /// All command buffers allocated from this command pool must be submitted
    /// on queues from the same queue family.
    pub fn queue_family_index<'s>(&'s mut self, queue_family_index: u32)
            -> &'s mut CommandPoolBuilder<'b> {
        self.create_info.set_queue_family_index(queue_family_index);
        self
    }

    /// Creates and returns a new `CommandPool`
    pub fn build(&self, device: Device) -> VdResult<CommandPool> {
        let handle = unsafe { device.create_command_pool(&self.create_info, None)? };

        Ok(CommandPool {
            inner: Arc::new(Inner {
                handle,
                device,
            })
        })
    }
}