From 0fb4a7a437cef9e50efad8098ccfb3a8096be633 Mon Sep 17 00:00:00 2001 From: Rua Date: Thu, 15 Feb 2024 16:48:07 +0100 Subject: [PATCH] Use `vkGetDeviceQueue2` on Vulkan 1.1 and above (#2466) --- vulkano/src/device/mod.rs | 31 +++++-------- vulkano/src/device/queue.rs | 92 ++++++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/vulkano/src/device/mod.rs b/vulkano/src/device/mod.rs index a28e9c79..ccd4557c 100644 --- a/vulkano/src/device/mod.rs +++ b/vulkano/src/device/mod.rs @@ -96,8 +96,8 @@ //! //! TODO: write -use self::physical::PhysicalDevice; pub(crate) use self::properties::PropertiesFfi; +use self::{physical::PhysicalDevice, queue::DeviceQueueInfo}; pub use self::{ properties::Properties, queue::{Queue, QueueFamilyProperties, QueueFlags, QueueGuard}, @@ -477,12 +477,6 @@ impl Device { .map_or(ptr::null(), |func| func as _) }); - struct QueueToGet { - flags: QueueCreateFlags, - queue_family_index: u32, - id: u32, - } - let mut active_queue_family_indices: SmallVec<[_; 2]> = SmallVec::with_capacity(queue_create_infos.len()); let mut queues_to_get: SmallVec<[_; 2]> = SmallVec::with_capacity(queue_create_infos.len()); @@ -496,10 +490,13 @@ impl Device { } = queue_create_info; active_queue_family_indices.push(queue_family_index); - queues_to_get.extend((0..queues.len() as u32).map(move |id| QueueToGet { - flags, - queue_family_index, - id, + queues_to_get.extend((0..queues.len() as u32).map(move |queue_index| { + DeviceQueueInfo { + flags, + queue_family_index, + queue_index, + ..Default::default() + } })); } @@ -536,15 +533,9 @@ impl Device { let queues_iter = { let device = device.clone(); - queues_to_get.into_iter().map(move |queue_to_get| unsafe { - let QueueToGet { - flags, - queue_family_index, - id, - } = queue_to_get; - - Queue::new(device.clone(), flags, queue_family_index, id) - }) + queues_to_get + .into_iter() + .map(move |queue_info| unsafe { Queue::new(device.clone(), queue_info) }) }; (device, queues_iter) diff --git a/vulkano/src/device/queue.rs b/vulkano/src/device/queue.rs index fd2fb59f..fb01499b 100644 --- a/vulkano/src/device/queue.rs +++ b/vulkano/src/device/queue.rs @@ -30,31 +30,45 @@ pub struct Queue { flags: QueueCreateFlags, queue_family_index: u32, - id: u32, // id within family + queue_index: u32, // index within family state: Mutex, } impl Queue { - pub(super) unsafe fn new( - device: Arc, - flags: QueueCreateFlags, - queue_family_index: u32, - id: u32, - ) -> Arc { - let handle = { - let fns = device.fns(); - let mut output = MaybeUninit::uninit(); - (fns.v1_0.get_device_queue)( - device.handle(), - queue_family_index, - id, - output.as_mut_ptr(), - ); - output.assume_init() + pub(super) unsafe fn new(device: Arc, queue_info: DeviceQueueInfo) -> Arc { + let &DeviceQueueInfo { + flags, + queue_family_index, + queue_index, + _ne: _, + } = &queue_info; + + let queue_info_vk = ash::vk::DeviceQueueInfo2 { + flags: flags.into(), + queue_family_index, + queue_index, + ..Default::default() }; - Self::from_handle(device, handle, flags, queue_family_index, id) + let fns = device.fns(); + let mut output = MaybeUninit::uninit(); + + if device.api_version() >= Version::V1_1 { + (fns.v1_1.get_device_queue2)(device.handle(), &queue_info_vk, output.as_mut_ptr()); + } else { + debug_assert!(queue_info_vk.flags.is_empty()); + debug_assert!(queue_info_vk.p_next.is_null()); + (fns.v1_0.get_device_queue)( + device.handle(), + queue_info_vk.queue_family_index, + queue_info_vk.queue_index, + output.as_mut_ptr(), + ); + } + + let handle = output.assume_init(); + Self::from_handle(device, handle, queue_info) } // TODO: Make public @@ -62,16 +76,21 @@ impl Queue { pub(super) unsafe fn from_handle( device: Arc, handle: ash::vk::Queue, - flags: QueueCreateFlags, - queue_family_index: u32, - id: u32, + queue_info: DeviceQueueInfo, ) -> Arc { + let DeviceQueueInfo { + flags, + queue_family_index, + queue_index, + _ne: _, + } = queue_info; + Arc::new(Queue { handle, device: InstanceOwnedDebugWrapper(device), flags, queue_family_index, - id, + queue_index, state: Mutex::new(Default::default()), }) } @@ -96,8 +115,8 @@ impl Queue { /// Returns the index of this queue within its queue family. #[inline] - pub fn id_within_family(&self) -> u32 { - self.id + pub fn queue_index(&self) -> u32 { + self.queue_index } /// Locks the queue and then calls the provided closure, providing it with an object that @@ -140,7 +159,7 @@ unsafe impl DeviceOwned for Queue { impl PartialEq for Queue { #[inline] fn eq(&self, other: &Self) -> bool { - self.id == other.id + self.queue_index == other.queue_index && self.queue_family_index == other.queue_family_index && self.device == other.device } @@ -150,12 +169,33 @@ impl Eq for Queue {} impl Hash for Queue { fn hash(&self, state: &mut H) { - self.id.hash(state); + self.queue_index.hash(state); self.queue_family_index.hash(state); self.device.hash(state); } } +/// Parameters to retrieve a [`Queue`] from the device. +#[derive(Clone, Debug)] +pub(super) struct DeviceQueueInfo { + pub(super) flags: QueueCreateFlags, + pub(super) queue_family_index: u32, + pub(super) queue_index: u32, + pub(super) _ne: crate::NonExhaustive, +} + +impl Default for DeviceQueueInfo { + #[inline] + fn default() -> Self { + Self { + flags: QueueCreateFlags::empty(), + queue_family_index: 0, + queue_index: 0, + _ne: crate::NonExhaustive(()), + } + } +} + pub struct QueueGuard<'a> { queue: &'a Arc, _state: MutexGuard<'a, QueueState>,