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
use std::sync::Arc;

use erupt::vk;
#[cfg(feature = "tracing")]
use tracing1::error;

use crate::context::Context;
use crate::{
    AscheError, ComputeCommandBuffer, ComputeCommandPool, Fence, GraphicsCommandBuffer,
    GraphicsCommandPool, Result, TransferCommandBuffer, TransferCommandPool,
};

macro_rules! impl_queue {
    (
        #[doc = $doc:expr]
        $queue_name:ident => $pool_name:ident, $buffer_name:ident
    ) => {
        #[doc = $doc]
        #[derive(Debug)]
        pub struct $queue_name {
            /// The queue family index of this queue.
            pub family_index: u32,
            pub(crate) raw: vk::Queue,
            command_pool_counter: u64,
            context: Arc<Context>,
        }

        impl Drop for $queue_name {
            fn drop(&mut self) {
                unsafe {
                    self.context.device.queue_wait_idle(self.raw).unwrap();
                };
            }
        }

        impl $queue_name {
            pub(crate) fn new(context: Arc<Context>, family_index: u32, queue: vk::Queue) -> Self {
                Self {
                    raw: queue,
                    family_index,
                    command_pool_counter: 0,
                    context,
                }
            }

            /// Creates a new command pool. Pools are not cached and are owned by the caller.
            pub fn create_command_pool(&mut self) -> Result<$pool_name> {
                let counter = self.command_pool_counter;
                let command_pool =
                    $pool_name::new(self.context.clone(), self.family_index, counter)?;

                self.command_pool_counter += 1;

                Ok(command_pool)
            }

            /// Submits a command buffer to a queue.
            #[doc = "[Vulkan Manual Page](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkQueueSubmit2KHR.html)"]
            pub fn submit(&mut self, command_buffer: &$buffer_name, fence: Option<&Fence>) -> Result<()> {
                let command_buffer_infos = [vk::CommandBufferSubmitInfoKHRBuilder::new()
                    .command_buffer(command_buffer.raw)
                    .device_mask(1)];

                let fence = fence.map(|fence| fence.raw);

                let wait_semaphore_infos: Vec<vk::SemaphoreSubmitInfoKHRBuilder> =
                if let Some(wait_semaphore) = command_buffer.wait_semaphore.as_ref() {
                    vec![wait_semaphore.into()]
                } else {
                    vec![]
                };

                let signal_semaphore_infos: Vec<vk::SemaphoreSubmitInfoKHRBuilder> =
                if let Some(signal_semaphore) = command_buffer.signal_semaphore.as_ref() {
                    vec![signal_semaphore.into()]
                } else {
                    vec![]
                };

                let mut submit_info = vk::SubmitInfo2KHRBuilder::new().command_buffer_infos(&command_buffer_infos);

                submit_info = if signal_semaphore_infos.len() > 0 {
                    submit_info.signal_semaphore_infos(signal_semaphore_infos.as_slice())
                } else {
                    submit_info
                };

                submit_info = if wait_semaphore_infos.len() > 0 {
                    submit_info.wait_semaphore_infos(wait_semaphore_infos.as_slice())
                } else {
                    submit_info
                };

                unsafe {
                    self.context
                        .device
                        .queue_submit2_khr(self.raw, &[submit_info], fence)
                }
                .map_err(|err| {
                    #[cfg(feature = "tracing")]
                    error!("Unable to queue and submit a command buffer: {}", err);
                    AscheError::VkResult(err)
                })?;

                Ok(())
            }


            /// Submit command buffers to a queue.
            #[doc = "[Vulkan Manual Page](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkQueueSubmit2KHR.html)"]
            pub fn submit_all(&mut self, command_buffer: &[$buffer_name], fence: Option<&Fence>) -> Result<()> {
                let command_buffer_infos = command_buffer.iter().map(|cb| {
                    vk::CommandBufferSubmitInfoKHRBuilder::new()
                        .command_buffer(cb.raw)
                        .device_mask(1)
                })
                .collect::<Vec<vk::CommandBufferSubmitInfoKHRBuilder>>();

                let mut wait_semaphore_infos: Vec<vk::SemaphoreSubmitInfoKHRBuilder> = Vec::with_capacity(4);
                let mut signal_semaphore_infos: Vec<vk::SemaphoreSubmitInfoKHRBuilder> = Vec::with_capacity(4);

                 for cb in command_buffer.iter() {
                    if let Some(wait_semaphore) = cb.wait_semaphore.as_ref() {
                        let wait_semaphore: vk::SemaphoreSubmitInfoKHRBuilder = wait_semaphore.into();
                        wait_semaphore_infos.push(wait_semaphore);
                    } else {
                        return Err(AscheError::MissingWaitSemaphore);
                    };
                    if let Some(signal_semaphore) = cb.signal_semaphore.as_ref() {
                        let signal_semaphore: vk::SemaphoreSubmitInfoKHRBuilder = signal_semaphore.into();
                        signal_semaphore_infos.push(signal_semaphore);
                    } else {
                        return Err(AscheError::MissingSignalSemaphore);
                    };
                };

                let submit_infos = command_buffer.iter().enumerate().map(|(id, _)| {
                    vk::SubmitInfo2KHRBuilder::new()
                        .command_buffer_infos(&command_buffer_infos[id..id + 1])
                        .wait_semaphore_infos(&wait_semaphore_infos[id..id + 1])
                        .signal_semaphore_infos(&signal_semaphore_infos[id..id + 1])
                })
                .collect::<Vec<vk::SubmitInfo2KHRBuilder>>();

                let fence = fence.map(|fence| fence.raw);

                unsafe {
                    self.context
                        .device
                        .queue_submit2_khr(self.raw, &submit_infos, fence)
                }
                .map_err(|err| {
                    #[cfg(feature = "tracing")]
                    error!("Unable to queue and submit command buffers: {}", err);
                    AscheError::VkResult(err)
                })?;

                Ok(())
            }
        }
    };
}

impl_queue!(
    #[doc = "A queue for compute operations."]
    ComputeQueue => ComputeCommandPool, ComputeCommandBuffer
);

impl_queue!(
    #[doc = "A queue for graphics operations."]
    GraphicsQueue => GraphicsCommandPool, GraphicsCommandBuffer
);

impl_queue!(
    #[doc = "A queue for transfer operations."]
    TransferQueue => TransferCommandPool, TransferCommandBuffer
);