remove old define_device_owned_handle macro_rules

This commit is contained in:
janis 2026-10-24 18:44:11 +02:00
parent dd02ea23cf
commit 69039ba3c1
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
3 changed files with 86 additions and 94 deletions

View file

@ -1,8 +1,7 @@
use std::sync::{Arc, atomic::AtomicU8}; use std::sync::{Arc, atomic::AtomicU8};
use crate::{ use crate::{
define_device_owned_handle, device::{DeviceObject, DeviceOwned},
device::DeviceOwned,
images::{Image, QueueOwnership}, images::{Image, QueueOwnership},
pipeline::{Pipeline, PipelineLayout}, pipeline::{Pipeline, PipelineLayout},
sync::{self, FenceFuture}, sync::{self, FenceFuture},
@ -174,15 +173,30 @@ impl CommandBufferState2 {
} }
} }
define_device_owned_handle! { #[derive(Debug)]
#[derive(Debug)] pub struct SingleUseCommand {
pub SingleUseCommand(vk::CommandBuffer) { inner: DeviceObject<vk::CommandBuffer>,
pool: Arc<SingleUseCommandPool>, pool: Arc<SingleUseCommandPool>,
state: CommandBufferState2, state: CommandBufferState2,
} => |this| unsafe { }
this.pool
impl DeviceOwned<vk::CommandBuffer> for SingleUseCommand {
fn device(&self) -> &Device {
self.inner.device()
}
fn handle(&self) -> vk::CommandBuffer {
*self.inner
}
}
impl Drop for SingleUseCommand {
fn drop(&mut self) {
unsafe {
self.pool
.pool .pool
.with_locked(|&pool| this.device().dev().free_command_buffers(pool, &[this.handle()])) .with_locked(|&pool| self.device().dev().free_command_buffers(pool, &[self.handle()]))
}
} }
} }
@ -208,7 +222,15 @@ impl SingleUseCommand {
buffer buffer
}; };
Self::construct(device, buffer, None, pool, CommandBufferState2::recording()) Ok(Self {
inner: DeviceObject::new_debug_named(
device,
buffer,
None::<std::borrow::Cow<'static, str>>,
),
pool,
state: CommandBufferState2::recording(),
})
} }
pub fn state(&self) -> CommandBufferState { pub fn state(&self) -> CommandBufferState {
@ -642,8 +664,7 @@ mod command_pools {
use thread_local::ThreadLocal; use thread_local::ThreadLocal;
use crate::{ use crate::{
define_device_owned_handle, device::{Device, DeviceObject, DeviceOwned},
device::{Device, DeviceOwned},
queue::Queue, queue::Queue,
sync, sync,
util::MutexExt, util::MutexExt,
@ -659,13 +680,19 @@ mod command_pools {
InvalidQueueSubmission, InvalidQueueSubmission,
} }
define_device_owned_handle! { pub struct CommandPool {
#[derive()] inner: DeviceObject<vk::CommandPool>,
pub CommandPool(vk::CommandPool) { family: u32,
family: u32, free_list: Mutex<Cell<(Vec<vk::CommandBufferLevel>, Vec<vk::CommandBuffer>)>>,
free_list: Mutex<Cell<(Vec<vk::CommandBufferLevel>, Vec<vk::CommandBuffer>)>>, }
} => |this| unsafe {
this.device().dev().destroy_command_pool(this.handle(), None); impl DeviceOwned<vk::CommandPool> for CommandPool {
fn device(&self) -> &Device {
self.inner.device()
}
fn handle(&self) -> vk::CommandPool {
*self.inner
} }
} }
@ -684,7 +711,11 @@ mod command_pools {
let pool = unsafe { dev.dev().create_command_pool(&pool_info, None)? }; let pool = unsafe { dev.dev().create_command_pool(&pool_info, None)? };
Self::construct(dev, pool, name, family, Mutex::new(Default::default())) Ok(Self {
inner: DeviceObject::new_debug_named(dev, pool, name),
family,
free_list: Mutex::new(Default::default()),
})
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -761,13 +792,11 @@ mod command_pools {
.begin_command_buffer(handle, &begin_info)?; .begin_command_buffer(handle, &begin_info)?;
} }
Ok(CommandBuffer::construct( Ok(CommandBuffer {
self.device().clone(), inner: DeviceObject::new_debug_named(self.device().clone(), handle, name),
handle, pool: self.clone(),
name,
self.clone(),
level, level,
)?) })
} }
pub fn alloc_primary( pub fn alloc_primary(
@ -861,14 +890,31 @@ mod command_pools {
} }
} }
define_device_owned_handle! { #[derive(Debug)]
#[derive(Debug)] pub struct CommandBuffer {
pub CommandBuffer(vk::CommandBuffer) { inner: DeviceObject<vk::CommandBuffer>,
pool: Arc<CommandPool>, pool: Arc<CommandPool>,
#[allow(unused)] #[allow(unused)]
level: vk::CommandBufferLevel, level: vk::CommandBufferLevel,
} => |this| unsafe { }
this.device().dev().free_command_buffers(this.pool.handle(), &[this.handle()]);
impl DeviceOwned<vk::CommandBuffer> for CommandBuffer {
fn device(&self) -> &Device {
self.inner.device()
}
fn handle(&self) -> vk::CommandBuffer {
*self.inner
}
}
impl Drop for CommandBuffer {
fn drop(&mut self) {
unsafe {
self.device()
.dev()
.free_command_buffers(self.pool.handle(), &[self.handle()]);
}
} }
} }

View file

@ -25,7 +25,7 @@ pub mod util;
pub use renderer_device::{device, instance, queue, sync}; pub use renderer_device::{device, instance, queue, sync};
pub use renderer_device::{ pub use renderer_device::{
compatible_extension_properties, define_device_owned_handle, extension_intersection, compatible_extension_properties, extension_intersection,
get_physical_device_features, get_physical_device_properties, make_extension, get_physical_device_features, get_physical_device_properties, make_extension,
DebugUtils, DebugUtilsCreateInfo, Device, DeviceOwned, DeviceRef, Error, DebugUtils, DebugUtilsCreateInfo, Device, DeviceOwned, DeviceRef, Error,
ExtendsDeviceFeatures2Debug, ExtendsDeviceProperties2Debug, Extension, Instance, ExtendsDeviceFeatures2Debug, ExtendsDeviceProperties2Debug, Extension, Instance,

View file

@ -897,63 +897,6 @@ impl<T: Pooled> Pool<T> {
} }
} }
// Macro for helping create and destroy Vulkan objects which are owned by a device.
#[macro_export]
macro_rules! define_device_owned_handle {
($(#[$attr:meta])*
$ty_vis:vis $ty:ident($handle:ty) {
$($(#[$field_attr:meta])* $field_vis:vis $field_name:ident : $field_ty:ty),*
$(,)?
} $(=> |$this:ident| $dtor:stmt)?) => {
$(#[$attr])*
$ty_vis struct $ty {
inner: $crate::device::DeviceObject<$handle>,
$(
$(#[$field_attr])*
$field_vis $field_name: $field_ty,
)*
}
impl $crate::device::DeviceOwned<$handle> for $ty {
fn device(&self) -> &$crate::device::Device {
self.inner.device()
}
fn handle(&self) -> $handle {
*self.inner
}
}
impl $ty {
#[allow(clippy::too_many_arguments, reason = "This function is generated by a macro")]
fn construct(
device: $crate::device::Device,
handle: $handle,
name: Option<::std::borrow::Cow<'static, str>>,
$($field_name: $field_ty,)*
) -> ::ash::prelude::VkResult<Self> {
Ok(Self {
inner: $crate::device::DeviceObject::new_debug_named(
device,
handle,
name,
),
$($field_name,)*
})
}
}
$(
impl Drop for $ty {
fn drop(&mut self) {
#[allow(unused_mut)]
let mut $this = self;
$dtor
}
}
)?
};
}
pub mod asdf { pub mod asdf {
use std::{ use std::{
mem::ManuallyDrop, mem::ManuallyDrop,
@ -1129,11 +1072,14 @@ pub mod asdf {
} }
impl traits::ExternallyManagedObject<Device> for vk::CommandBuffer { impl traits::ExternallyManagedObject<Device> for vk::CommandBuffer {
// Command buffers cannot be destroyed individually; their outer wrappers free them in Drop.
unsafe fn destroy(self, _: &Device) {} unsafe fn destroy(self, _: &Device) {}
} }
impl traits::ExternallyManagedObject<Device> for vk::CommandPool { impl traits::ExternallyManagedObject<Device> for vk::CommandPool {
unsafe fn destroy(self, _: &Device) {} unsafe fn destroy(self, device: &Device) {
unsafe { device.raw.destroy_command_pool(self, None) };
}
} }
impl<T> crate::device::DeviceOwned<T> for DeviceObject<T> impl<T> crate::device::DeviceOwned<T> for DeviceObject<T>