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
use {
    super::{
        driver::{CommandBuffer, ComputePipeline, Device},
        ptr::SharedPointerKind,
        HashPool, Lease,
    },
    ash::vk,
    std::{
        error::Error,
        fmt::{Debug, Display, Formatter},
        thread::panicking,
    },
};

pub type CommandFn<P> = Box<dyn FnOnce(&Device<P>, &CommandBuffer<P>)>;

pub fn execute<C, P>(
    cmd_buf: C,
    func: impl FnOnce(&Device<P>, &CommandBuffer<P>) + 'static,
) -> CommandChain<C, P>
where
    C: AsRef<CommandBuffer<P>>,
    P: SharedPointerKind,
{
    CommandChain {
        cmd_buf,
        funcs: vec![Box::new(func)],
    }
}

#[derive(Debug)]
pub struct ExecutionError;

impl Display for ExecutionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Error for ExecutionError {}

impl From<vk::Result> for ExecutionError {
    fn from(_: vk::Result) -> Self {
        Self
    }
}

pub struct CommandChain<C, P>
where
    C: AsRef<CommandBuffer<P>>,
    P: SharedPointerKind,
{
    cmd_buf: C,
    funcs: Vec<CommandFn<P>>,
}

impl<C, P> CommandChain<C, P>
where
    C: AsRef<CommandBuffer<P>>,
    P: SharedPointerKind,
{
    pub fn new(cmd_buf: C) -> Self {
        Self {
            cmd_buf,
            funcs: vec![],
        }
    }

    pub fn push_execute(
        mut self,
        func: impl FnOnce(&Device<P>, &CommandBuffer<P>) + 'static,
    ) -> Self {
        self.funcs.push(Box::new(func));
        self
    }

    /// Pushes "something" into a pile that won't be dropped until this command buffer submission
    /// actually finishes executing. Useful for keeping lifetimes in sync and handling undesired
    /// leases.
    pub fn push_shared_ref(mut self, shared_ref: impl Debug + 'static) -> Self {
        CommandBuffer::push_fenced_drop(self.cmd_buf.as_ref(), shared_ref);
        self
    }

    pub fn submit(mut self) -> Result<(), ExecutionError> {
        self.submit_mut()
    }

    fn submit_mut(&mut self) -> Result<(), ExecutionError> {
        use std::slice::from_ref;

        let cmd_buf = self.cmd_buf.as_ref();
        let device = &cmd_buf.device;

        unsafe {
            Device::wait_for_fence(device, &cmd_buf.fence).map_err(|_| ExecutionError)?;

            device
                .reset_command_pool(cmd_buf.pool, vk::CommandPoolResetFlags::RELEASE_RESOURCES)
                .map_err(|_| ExecutionError)?;
            device
                .begin_command_buffer(
                    **cmd_buf,
                    &vk::CommandBufferBeginInfo::builder()
                        .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT),
                )
                .map_err(|_| ExecutionError)?;

            for func in self.funcs.drain(..) {
                func(device, cmd_buf);
            }

            device
                .end_command_buffer(**cmd_buf)
                .map_err(|_| ExecutionError)?;
            device
                .reset_fences(from_ref(&cmd_buf.fence))
                .map_err(|_| ExecutionError)?;
            device
                .queue_submit(
                    *device.queue,
                    from_ref(&vk::SubmitInfo::builder().command_buffers(from_ref(&*cmd_buf))),
                    cmd_buf.fence,
                )
                .map_err(|_| ExecutionError)?;
        }

        Ok(())
    }
}

impl<C, P> Drop for CommandChain<C, P>
where
    C: AsRef<CommandBuffer<P>>,
    P: SharedPointerKind,
{
    fn drop(&mut self) {
        if panicking() {
            return;
        }

        // Submit here if they did not ask while we were all happy and un-dropped
        if !self.funcs.is_empty() {
            self.submit_mut().unwrap(); // Call submit manually to handle errors
        }
    }
}

impl<C, P> From<C> for CommandChain<C, P>
where
    C: AsRef<CommandBuffer<P>>,
    P: SharedPointerKind,
{
    fn from(cmd_buf: C) -> Self {
        Self {
            cmd_buf,
            funcs: vec![],
        }
    }
}