Compare commits

..

No commits in common. "294ad3dbbd2988694cb911829f4067ec38238b96" and "6a3297e8cc4eb3ac23e16e61e06b2422ccc2b565" have entirely different histories.

13 changed files with 483 additions and 926 deletions

View file

@ -41,7 +41,6 @@ thread_local = "1.1.8"
ash = "0.38.0" ash = "0.38.0"
ash-window = "0.13.0" ash-window = "0.13.0"
vk-mem = "0.5.0" vk-mem = "0.5.0"
gpu-allocator = { git = "https://github.com/janis-bhm/gpu-allocator", branch = "main" }
vk-sync = "0.1.6" vk-sync = "0.1.6"
arrayvec = "0.7.6" arrayvec = "0.7.6"

View file

@ -23,7 +23,6 @@ tracing = { workspace = true }
ash = { workspace = true } ash = { workspace = true }
ash-window = { workspace = true } ash-window = { workspace = true }
vk-mem = { workspace = true } vk-mem = { workspace = true }
gpu-allocator = { workspace = true }
raw-window-handle = { workspace = true } raw-window-handle = { workspace = true }
egui = { workspace = true , features = ["bytemuck"]} egui = { workspace = true , features = ["bytemuck"]}

View file

@ -5,10 +5,13 @@ use std::{
}; };
use ash::{prelude::VkResult, vk}; use ash::{prelude::VkResult, vk};
use itertools::Itertools;
use vk_mem::Alloc;
use crate::{ use crate::{
define_device_owned_handle,
device::{DeviceOwned, QueueFlags},
Device, Device,
device::{Allocation, DeviceObject, QueueFlags},
}; };
#[derive(Clone)] #[derive(Clone)]
@ -18,7 +21,9 @@ pub struct BufferDesc {
pub size: u64, pub size: u64,
pub usage: vk::BufferUsageFlags, pub usage: vk::BufferUsageFlags,
pub queue_families: QueueFlags, pub queue_families: QueueFlags,
pub mem_location: gpu_allocator::MemoryLocation,
pub mem_usage: vk_mem::MemoryUsage,
pub alloc_flags: vk_mem::AllocationCreateFlags,
} }
impl std::hash::Hash for BufferDesc { impl std::hash::Hash for BufferDesc {
@ -27,7 +32,8 @@ impl std::hash::Hash for BufferDesc {
self.size.hash(state); self.size.hash(state);
self.usage.hash(state); self.usage.hash(state);
self.queue_families.hash(state); self.queue_families.hash(state);
self.mem_location.hash(state); self.mem_usage.hash(state);
self.alloc_flags.bits().hash(state);
} }
} }
@ -39,7 +45,17 @@ impl std::fmt::Debug for BufferDesc {
.field("size", &self.size) .field("size", &self.size)
.field("usage", &self.usage) .field("usage", &self.usage)
.field("queue_families", &self.queue_families) .field("queue_families", &self.queue_families)
.field("mem_location", &self.mem_location) .field("mem_usage", &self.mem_usage)
.field_with("alloc_flags", |f| {
write!(
f,
"{}",
self.alloc_flags
.iter_names()
.map(|(name, _)| name)
.format(" | ")
)
})
.finish() .finish()
} }
} }
@ -53,7 +69,8 @@ impl PartialEq for BufferDesc {
&& self.size == other.size && self.size == other.size
&& self.usage == other.usage && self.usage == other.usage
&& self.queue_families == other.queue_families && self.queue_families == other.queue_families
&& self.mem_location == other.mem_location && self.mem_usage == other.mem_usage
&& self.alloc_flags.bits() == other.alloc_flags.bits()
} }
} }
@ -65,91 +82,109 @@ impl Default for BufferDesc {
size: Default::default(), size: Default::default(),
usage: Default::default(), usage: Default::default(),
queue_families: QueueFlags::empty(), queue_families: QueueFlags::empty(),
mem_location: gpu_allocator::MemoryLocation::Unknown, alloc_flags: vk_mem::AllocationCreateFlags::empty(),
mem_usage: vk_mem::MemoryUsage::Auto,
} }
} }
} }
#[derive(Debug)] define_device_owned_handle! {
pub struct Buffer { #[derive(Debug)]
buffer: DeviceObject<vk::Buffer>, pub Buffer(vk::Buffer) {
desc: BufferDesc, alloc: vk_mem::Allocation,
alloc: Allocation, size: u64,
} => |this| unsafe {
this.device().clone().alloc().destroy_buffer(this.handle(), &mut this.alloc);
}
} }
impl Eq for Buffer {} impl Eq for Buffer {}
impl PartialEq for Buffer { impl PartialEq for Buffer {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
*self.buffer == *other.buffer self.inner == other.inner
} }
} }
impl Buffer { impl Buffer {
pub fn new(device: Device, desc: BufferDesc) -> crate::Result<Self> { pub fn new(device: Device, desc: BufferDesc) -> VkResult<Self> {
let (buffer, requirements) = Self::new_raw(device.clone(), &desc)?; let queue_families = device.queue_families().family_indices(desc.queue_families);
let alloc =
device
.alloc2
.lock()
.allocate(&gpu_allocator::vulkan::AllocationCreateDesc {
name: desc.name.as_deref().unwrap_or_default(),
requirements,
location: desc.mem_location,
linear: true,
allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged,
})?;
Ok(Self {
buffer: DeviceObject::new(buffer, device.clone(), desc.name.clone()),
desc,
alloc: Allocation::Owned(DeviceObject::new_without_name(alloc, device)),
})
}
fn new_raw(
device: Device,
desc: &BufferDesc,
) -> crate::Result<(vk::Buffer, vk::MemoryRequirements)> {
let queue_families = device.queues.family_indices(desc.queue_families);
let sharing_mode = if queue_families.len() > 1 { let sharing_mode = if queue_families.len() > 1 {
vk::SharingMode::CONCURRENT vk::SharingMode::CONCURRENT
} else { } else {
vk::SharingMode::EXCLUSIVE vk::SharingMode::EXCLUSIVE
}; };
let create_info = vk::BufferCreateInfo::default()
let (buffer, allocation) = unsafe {
device.alloc().create_buffer(
&vk::BufferCreateInfo::default()
.size(desc.size) .size(desc.size)
.usage(desc.usage) .usage(desc.usage)
.queue_family_indices(&queue_families) .queue_family_indices(&queue_families)
.sharing_mode(sharing_mode); .sharing_mode(sharing_mode),
&vk_mem::AllocationCreateInfo {
flags: desc.alloc_flags,
usage: desc.mem_usage,
..Default::default()
},
)?
};
let buffer = unsafe { device.dev().create_buffer(&create_info, None)? }; Ok(Self::construct(
let mem_reqs = unsafe { device.dev().get_buffer_memory_requirements(buffer) }; device, buffer, desc.name, allocation, desc.size,
)?)
Ok((buffer, mem_reqs))
} }
pub fn map(&mut self) -> Option<&[u8]> { #[allow(dead_code)]
if let Some(alloc) = self.alloc.allocation() { pub fn map_arc(self: &mut Arc<Self>) -> VkResult<MappedBuffer<'_>> {
alloc.mapped_slice() Arc::get_mut(self).map(Self::map).unwrap()
} else {
None
}
} }
pub fn map_mut(&mut self) -> Option<&mut [u8]> { pub fn map(&mut self) -> VkResult<MappedBuffer<'_>> {
if let Some(alloc) = self.alloc.allocation_mut() { let bytes = unsafe {
alloc.mapped_slice_mut() let data = self.inner.dev().alloc().map_memory(&mut self.alloc)?;
} else { let slice = core::slice::from_raw_parts_mut(data, self.size as usize);
None
}
}
slice
};
Ok(MappedBuffer { inner: self, bytes })
}
pub fn buffer(&self) -> vk::Buffer { pub fn buffer(&self) -> vk::Buffer {
*self.buffer self.handle()
} }
pub fn len(&self) -> u64 { pub fn len(&self) -> u64 {
self.desc.size self.size
}
}
pub struct MappedBuffer<'a> {
bytes: &'a mut [u8],
inner: &'a mut Buffer,
}
impl Drop for MappedBuffer<'_> {
fn drop(&mut self) {
unsafe {
self.inner
.inner
.dev()
.alloc()
.unmap_memory(&mut self.inner.alloc);
}
}
}
impl Deref for MappedBuffer<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.bytes
}
}
impl DerefMut for MappedBuffer<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.bytes
} }
} }

View file

@ -277,7 +277,7 @@ impl SingleUseCommand {
signal: Option<vk::Semaphore>, signal: Option<vk::Semaphore>,
fence: Arc<sync::Fence>, fence: Arc<sync::Fence>,
) -> VkResult<FenceFuture<'a>> { ) -> VkResult<FenceFuture<'a>> {
self.submit_fence(wait, signal, Some(fence.raw()))?; self.submit_fence(wait, signal, Some(fence.fence()))?;
Ok(FenceFuture::new(fence)) Ok(FenceFuture::new(fence))
} }
@ -287,8 +287,8 @@ impl SingleUseCommand {
self, self,
wait: Option<(vk::Semaphore, vk::PipelineStageFlags)>, wait: Option<(vk::Semaphore, vk::PipelineStageFlags)>,
signal: Option<vk::Semaphore>, signal: Option<vk::Semaphore>,
) -> crate::Result<()> { ) -> VkResult<()> {
let fence = Arc::new(sync::Fence::from_pool(&self.device().pools.fences, None)?); let fence = Arc::new(sync::Fence::create(self.device().clone())?);
let future = self.submit_async(wait, signal, fence)?; let future = self.submit_async(wait, signal, fence)?;
future.block()?; future.block()?;
Ok(()) Ok(())

View file

@ -2,8 +2,7 @@ use std::{
borrow::Cow, borrow::Cow,
collections::{BTreeSet, HashMap, HashSet}, collections::{BTreeSet, HashMap, HashSet},
ffi::CStr, ffi::CStr,
mem::ManuallyDrop, ops::Deref,
ops::{Deref, DerefMut},
sync::Arc, sync::Arc,
}; };
@ -12,14 +11,15 @@ use ash::{
prelude::VkResult, prelude::VkResult,
vk::{self, Handle}, vk::{self, Handle},
}; };
use parking_lot::Mutex;
use raw_window_handle::RawDisplayHandle; use raw_window_handle::RawDisplayHandle;
use tinyvec::{ArrayVec, array_vec}; use tinyvec::{ArrayVec, array_vec};
use crate::{ use crate::{
Instance, PhysicalDeviceFeatures, PhysicalDeviceInfo, Result, Instance, PhysicalDeviceFeatures, PhysicalDeviceInfo, Result,
queue::{DeviceQueueInfos, DeviceQueues, Queue}, instance::InstanceInner,
sync::{self, BinarySemaphore, TimelineSemaphore}, queue::Queue,
queue::{DeviceQueueInfos, DeviceQueues},
sync,
}; };
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -108,44 +108,9 @@ struct DeviceExtensions {
pub(crate) mesh_shader: Option<ext::mesh_shader::Device>, pub(crate) mesh_shader: Option<ext::mesh_shader::Device>,
} }
type GpuAllocation = gpu_allocator::vulkan::Allocation; #[allow(unused)]
impl DeviceHandle for GpuAllocation {
unsafe fn destroy(&mut self, device: &Device) {
let mut swapped = GpuAllocation::default();
std::mem::swap(self, &mut swapped);
_ = device.alloc2.lock().free(swapped);
}
}
#[derive(Debug)]
pub(crate) enum Allocation {
Owned(DeviceObject<GpuAllocation>),
Shared(Arc<DeviceObject<GpuAllocation>>),
Unmanaged,
}
impl Allocation {
pub(crate) fn allocation(&self) -> Option<&GpuAllocation> {
match self {
Allocation::Owned(obj) => Some(obj),
Allocation::Shared(arc) => Some(arc.as_ref()),
Allocation::Unmanaged => None,
}
}
pub(crate) fn allocation_mut(&mut self) -> Option<&mut GpuAllocation> {
match self {
Allocation::Owned(obj) => Some(obj),
Allocation::Shared(arc) => Arc::get_mut(arc).map(|alloc| &mut alloc.inner),
Allocation::Unmanaged => None,
}
}
}
pub struct DeviceInner { pub struct DeviceInner {
pub(crate) alloc: vk_mem::Allocator, pub(crate) alloc: vk_mem::Allocator,
pub(crate) alloc2: Mutex<gpu_allocator::vulkan::Allocator>,
pub(crate) raw: ash::Device, pub(crate) raw: ash::Device,
pub(crate) adapter: PhysicalDeviceInfo, pub(crate) adapter: PhysicalDeviceInfo,
pub(crate) instance: Instance, pub(crate) instance: Instance,
@ -422,21 +387,6 @@ impl PhysicalDeviceInfo {
}, },
}; };
let alloc2 =
gpu_allocator::vulkan::Allocator::new(&gpu_allocator::vulkan::AllocatorCreateDesc {
instance: instance.inner.raw.clone(),
device: device.clone(),
physical_device: self.pdev,
debug_settings: Default::default(),
buffer_device_address: false,
allocation_sizes: {
const MB: u64 = 1024 * 1024;
gpu_allocator::AllocationSizes::new(8 * MB, 64 * MB)
.with_max_host_memblock_size(256 * MB)
.with_max_device_memblock_size(256 * MB)
},
})?;
let inner = DeviceInner { let inner = DeviceInner {
raw: device.clone(), raw: device.clone(),
alloc: unsafe { alloc: unsafe {
@ -446,7 +396,6 @@ impl PhysicalDeviceInfo {
self.pdev, self.pdev,
))? ))?
}, },
alloc2: Mutex::new(alloc2),
instance: instance.clone(), instance: instance.clone(),
adapter: self, adapter: self,
queues: device_queues, queues: device_queues,
@ -456,11 +405,7 @@ impl PhysicalDeviceInfo {
_drop: DeviceDrop(device), _drop: DeviceDrop(device),
}; };
let shared = Arc::new(inner); Ok(Device(Arc::new(inner)))
Ok(Device {
pools: Arc::new(DevicePools::new(shared.clone())),
shared,
})
} }
fn required_extensions(&self, requested_extensions: &[Extension<'static>]) -> Vec<*const i8> { fn required_extensions(&self, requested_extensions: &[Extension<'static>]) -> Vec<*const i8> {
@ -489,31 +434,11 @@ impl PhysicalDeviceInfo {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct DevicePools { pub struct Device(Arc<DeviceInner>);
pub(crate) fences: Pool<vk::Fence>,
pub(crate) binary_semaphores: Pool<BinarySemaphore>,
pub(crate) timeline_semaphores: Pool<TimelineSemaphore>,
}
impl DevicePools {
pub fn new(device: Arc<DeviceInner>) -> Self {
Self {
fences: Pool::new(device.clone()),
binary_semaphores: Pool::new(device.clone()),
timeline_semaphores: Pool::new(device),
}
}
}
#[derive(Clone, Debug)]
pub struct Device {
pub(crate) shared: Arc<DeviceInner>,
pub(crate) pools: Arc<DevicePools>,
}
impl PartialEq for Device { impl PartialEq for Device {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared) Arc::ptr_eq(&self.0, &other.0)
} }
} }
@ -523,57 +448,57 @@ impl core::ops::Deref for Device {
type Target = DeviceInner; type Target = DeviceInner;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.shared &self.0
} }
} }
impl DeviceInner { impl Device {
pub fn sync_threadpool(&self) -> &sync::SyncThreadpool { pub fn sync_threadpool(&self) -> &sync::SyncThreadpool {
&self.sync_threadpool &self.0.sync_threadpool
} }
pub fn alloc(&self) -> &vk_mem::Allocator { pub fn alloc(&self) -> &vk_mem::Allocator {
&self.alloc &self.0.alloc
} }
pub fn dev(&self) -> &ash::Device { pub fn dev(&self) -> &ash::Device {
&self.raw &self.0.raw
} }
pub fn instance(&self) -> &Instance { pub fn instance(&self) -> &Instance {
&self.instance &self.0.instance
} }
pub fn queues(&self) -> &DeviceQueues { pub fn queues(&self) -> &DeviceQueues {
&self.queues &self.0.queues
} }
pub fn phy(&self) -> vk::PhysicalDevice { pub fn phy(&self) -> vk::PhysicalDevice {
self.adapter.pdev self.0.adapter.pdev
} }
pub fn features(&self) -> &crate::PhysicalDeviceFeatures { pub fn features(&self) -> &crate::PhysicalDeviceFeatures {
&self.adapter.features &self.0.adapter.features
} }
pub fn properties(&self) -> &crate::PhysicalDeviceProperties { pub fn properties(&self) -> &crate::PhysicalDeviceProperties {
&self.adapter.properties &self.0.adapter.properties
} }
pub fn physical_device(&self) -> &PhysicalDeviceInfo { pub fn physical_device(&self) -> &PhysicalDeviceInfo {
&self.adapter &self.0.adapter
} }
pub fn main_queue(&self) -> &Queue { pub fn main_queue(&self) -> &Queue {
self.queues.graphics() self.0.queues.graphics()
} }
pub fn compute_queue(&self) -> &Queue { pub fn compute_queue(&self) -> &Queue {
self.queues.compute() self.0.queues.compute()
} }
pub fn transfer_queue(&self) -> &Queue { pub fn transfer_queue(&self) -> &Queue {
self.queues.transfer() self.0.queues.transfer()
} }
pub unsafe fn lock_queues(&self) { pub unsafe fn lock_queues(&self) {
unsafe { unsafe {
self.queues.lock(); self.0.queues.lock();
} }
} }
pub unsafe fn unlock_queues(&self) { pub unsafe fn unlock_queues(&self) {
unsafe { unsafe {
self.queues.unlock(); self.0.queues.unlock();
} }
} }
@ -590,7 +515,7 @@ impl DeviceInner {
tracing::warn!("locking all queues and waiting for device to idle"); tracing::warn!("locking all queues and waiting for device to idle");
unsafe { unsafe {
self.lock_queues(); self.lock_queues();
self.raw.device_wait_idle()?; self.dev().device_wait_idle()?;
self.unlock_queues(); self.unlock_queues();
} }
tracing::warn!("finished waiting: unlocking all queues."); tracing::warn!("finished waiting: unlocking all queues.");
@ -615,9 +540,7 @@ impl DeviceInner {
let mut buffer = [0u8; 64]; let mut buffer = [0u8; 64];
let buffer_vec: Vec<u8>; let buffer_vec: Vec<u8>;
let name_bytes = if name.is_empty() { let name_bytes = if name.len() < buffer.len() {
&[]
} else if name.len() < buffer.len() {
buffer[..name.len()].copy_from_slice(name.as_bytes()); buffer[..name.len()].copy_from_slice(name.as_bytes());
&buffer[..] &buffer[..]
} else { } else {
@ -657,8 +580,7 @@ pub struct DeviceOwnedDebugObject<T> {
impl<T: Eq> Eq for DeviceOwnedDebugObject<T> {} impl<T: Eq> Eq for DeviceOwnedDebugObject<T> {}
impl<T: PartialEq> PartialEq for DeviceOwnedDebugObject<T> { impl<T: PartialEq> PartialEq for DeviceOwnedDebugObject<T> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.device.shared, &other.device.shared) std::sync::Arc::ptr_eq(&self.device.0, &other.device.0) && self.object == other.object
&& self.object == other.object
} }
} }
@ -730,20 +652,11 @@ impl<T: DeviceHandle> Deref for DeviceObject<T> {
} }
} }
impl<T: DeviceHandle> DerefMut for DeviceObject<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T: DeviceHandle> DeviceObject<T> { impl<T: DeviceHandle> DeviceObject<T> {
pub fn new(inner: T, device: Device, name: Option<Cow<'static, str>>) -> Self pub fn new(inner: T, device: Device, name: Option<Cow<'static, str>>) -> Self {
where
T: vk::Handle + Clone,
{
unsafe { unsafe {
if let Some(name) = name.as_ref() { if let Some(name) = name.as_ref() {
device.debug_name_object(inner.clone(), &name); device.debug_name_object(inner, &name);
} }
} }
@ -754,14 +667,6 @@ impl<T: DeviceHandle> DeviceObject<T> {
name, name,
} }
} }
pub fn new_without_name(inner: T, device: Device) -> Self {
Self {
inner,
device,
#[cfg(debug_assertions)]
name: None,
}
}
pub fn device(&self) -> &Device { pub fn device(&self) -> &Device {
&self.device &self.device
} }
@ -779,48 +684,46 @@ impl<T: DeviceHandle> DeviceObject<T> {
impl<T: DeviceHandle> Drop for DeviceObject<T> { impl<T: DeviceHandle> Drop for DeviceObject<T> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { self.destroy(&self.device);
self.inner.destroy(&self.device);
}
} }
} }
pub trait DeviceHandle { pub trait DeviceHandle: vk::Handle + Copy {
unsafe fn destroy(&mut self, device: &Device); fn destroy(self, device: &Device);
} }
impl DeviceHandle for vk::Semaphore { impl DeviceHandle for vk::Semaphore {
unsafe fn destroy(&mut self, device: &Device) { fn destroy(self, device: &Device) {
unsafe { unsafe {
device.dev().destroy_semaphore(*self, None); device.dev().destroy_semaphore(self, None);
} }
} }
} }
impl DeviceHandle for vk::Fence { impl DeviceHandle for vk::Fence {
unsafe fn destroy(&mut self, device: &Device) { fn destroy(self, device: &Device) {
unsafe { unsafe {
device.dev().destroy_fence(*self, None); device.dev().destroy_fence(self, None);
} }
} }
} }
impl DeviceHandle for vk::Buffer { impl DeviceHandle for vk::Buffer {
unsafe fn destroy(&mut self, device: &Device) { fn destroy(self, device: &Device) {
unsafe { unsafe {
device.dev().destroy_buffer(*self, None); device.dev().destroy_buffer(self, None);
} }
} }
} }
impl DeviceHandle for vk::SwapchainKHR { impl DeviceHandle for vk::SwapchainKHR {
unsafe fn destroy(&mut self, device: &Device) { fn destroy(self, device: &Device) {
unsafe { unsafe {
device device
.device_extensions .device_extensions
.swapchain .swapchain
.as_ref() .as_ref()
.map(|swapchain| swapchain.destroy_swapchain(*self, None)); .map(|swapchain| swapchain.destroy_swapchain(self, None));
} }
} }
} }
@ -830,100 +733,7 @@ pub trait DeviceOwned<T> {
fn handle(&self) -> T; fn handle(&self) -> T;
} }
pub trait Pooled: Sized { /// Macro for helping create and destroy Vulkan objects which are owned by a device.
fn create_from_pool(pool: &Pool<Self>) -> Result<Self>;
}
pub struct PoolObject<T: Pooled + vk::Handle + Clone> {
pub(crate) inner: ManuallyDrop<T>,
pub(crate) pool: Pool<T>,
#[cfg(debug_assertions)]
pub(crate) name: Option<Cow<'static, str>>,
}
impl<T: Pooled + vk::Handle + Clone> PoolObject<T> {
pub fn name_object(&mut self, name: impl Into<Cow<'static, str>>) {
#[cfg(debug_assertions)]
unsafe {
self.name = Some(name.into());
self.pool
.device
.debug_name_object(T::clone(&self.inner), self.name.as_ref().unwrap());
}
}
pub fn device(&self) -> &Arc<DeviceInner> {
&self.pool.device
}
}
impl<T: Pooled + vk::Handle + Clone> Drop for PoolObject<T> {
fn drop(&mut self) {
let handle = unsafe { ManuallyDrop::take(&mut self.inner) };
#[cfg(debug_assertions)]
if self.name.is_some() {
unsafe { self.pool.device.debug_name_object(handle.clone(), "") };
}
self.pool.push(handle);
}
}
impl<T: Pooled + vk::Handle + Clone> Deref for PoolObject<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Debug, Clone)]
pub struct Pool<T> {
pub(crate) pool: Arc<Mutex<Vec<T>>>,
pub(crate) device: Arc<DeviceInner>,
}
impl<T> Pool<T> {
pub fn push(&self, item: T) {
self.pool.lock().push(item);
}
pub fn new(device: Arc<DeviceInner>) -> Self {
Self {
pool: Arc::new(Mutex::new(Vec::new())),
device,
}
}
pub fn pop(&self) -> Option<T> {
self.pool.lock().pop()
}
}
impl<T: Pooled + vk::Handle + Clone> Pool<T> {
pub fn get(&self) -> Result<PoolObject<T>> {
let item = if let Some(item) = self.pool.lock().pop() {
item
} else {
T::create_from_pool(self)?
};
Ok(PoolObject {
inner: ManuallyDrop::new(item),
pool: self.clone(),
#[cfg(debug_assertions)]
name: None,
})
}
pub fn get_named(&self, name: Option<impl Into<Cow<'static, str>>>) -> Result<PoolObject<T>> {
let mut obj = self.get()?;
if let Some(name) = name {
obj.name_object(name);
}
Ok(obj)
}
}
// Macro for helping create and destroy Vulkan objects which are owned by a device.
#[macro_export] #[macro_export]
macro_rules! define_device_owned_handle { macro_rules! define_device_owned_handle {
($(#[$attr:meta])* ($(#[$attr:meta])*

View file

@ -6,14 +6,12 @@ use std::{
use crate::{ use crate::{
define_device_owned_handle, define_device_owned_handle,
device::{Allocation, DeviceHandle, DeviceObject, DeviceOwned, QueueFlags}, device::{DeviceOwned, QueueFlags},
swapchain::Swapchain,
util::weak_vec::WeakVec,
}; };
use super::Device; use super::Device;
use ash::{prelude::*, vk}; use ash::{prelude::*, vk};
use gpu_allocator::vulkan::{AllocationCreateDesc, AllocationScheme}; use itertools::Itertools;
use parking_lot::Mutex; use parking_lot::Mutex;
use vk_mem::Alloc; use vk_mem::Alloc;
@ -32,7 +30,8 @@ pub struct ImageDesc {
pub queue_families: QueueFlags, pub queue_families: QueueFlags,
pub layout: vk::ImageLayout, pub layout: vk::ImageLayout,
pub mem_location: gpu_allocator::MemoryLocation, pub mem_usage: vk_mem::MemoryUsage,
pub alloc_flags: vk_mem::AllocationCreateFlags,
} }
impl std::hash::Hash for ImageDesc { impl std::hash::Hash for ImageDesc {
@ -48,7 +47,8 @@ impl std::hash::Hash for ImageDesc {
self.usage.hash(state); self.usage.hash(state);
self.queue_families.hash(state); self.queue_families.hash(state);
self.layout.hash(state); self.layout.hash(state);
self.mem_location.hash(state); self.mem_usage.hash(state);
self.alloc_flags.bits().hash(state);
} }
} }
@ -67,7 +67,8 @@ impl PartialEq for ImageDesc {
&& self.usage == other.usage && self.usage == other.usage
&& self.queue_families == other.queue_families && self.queue_families == other.queue_families
&& self.layout == other.layout && self.layout == other.layout
&& self.mem_location == other.mem_location && self.mem_usage == other.mem_usage
&& self.alloc_flags.bits() == other.alloc_flags.bits()
} }
} }
@ -86,7 +87,17 @@ impl<'a> std::fmt::Debug for ImageDesc {
.field("usage", &self.usage) .field("usage", &self.usage)
.field("queue_families", &self.queue_families) .field("queue_families", &self.queue_families)
.field("layout", &self.layout) .field("layout", &self.layout)
.field("mem_location", &self.mem_location) .field("mem_usage", &self.mem_usage)
.field_with("alloc_flags", |f| {
write!(
f,
"{}",
self.alloc_flags
.iter_names()
.map(|(name, _)| name)
.format(" | ")
)
})
.finish() .finish()
} }
} }
@ -106,154 +117,181 @@ impl Default for ImageDesc {
usage: Default::default(), usage: Default::default(),
queue_families: QueueFlags::empty(), queue_families: QueueFlags::empty(),
layout: vk::ImageLayout::UNDEFINED, layout: vk::ImageLayout::UNDEFINED,
mem_location: gpu_allocator::MemoryLocation::Unknown, alloc_flags: vk_mem::AllocationCreateFlags::empty(),
mem_usage: vk_mem::MemoryUsage::Auto,
} }
} }
} }
#[derive(Debug)] define_device_owned_handle! {
enum ImageInner { #[derive(Debug)]
Swapchain(vk::Image, Device), pub Image(vk::Image) {
Allocated(DeviceObject<vk::Image>, Allocation), alloc: Option<vk_mem::Allocation>,
} size: vk::Extent3D,
format: vk::Format,
impl DeviceHandle for vk::Image { views: Mutex<HashMap<ImageViewDesc, vk::ImageView>>,
unsafe fn destroy(&mut self, device: &Device) { aliases: Mutex<HashMap<ImageDesc, Arc<Image>>>,
parent: Option<Weak<Image>>,
is_swapchain_image: bool,
} => |this| if !this.is_swapchain_image {
unsafe { unsafe {
device.dev().destroy_image(*self, None); for &view in this.views.lock().values() {
this.inner.dev().dev().destroy_image_view(view, None);
}
let handle = this.handle();
let dev = this.device().clone();
if let Some(alloc) = this.alloc.as_mut() {
// destroy image handle and allocation
dev.alloc().destroy_image(handle, alloc);
} else {
// destroy image handle
dev.dev().destroy_image(handle, None);
}
} }
} }
} }
impl ImageInner { impl Eq for Image {}
fn image(&self) -> vk::Image { impl PartialEq for Image {
match self { fn eq(&self, other: &Self) -> bool {
Self::Swapchain(image, _) => *image, self.inner == other.inner
Self::Allocated(image, _) => **image,
} }
}
fn device(&self) -> &Device {
match self {
Self::Swapchain(_, device) => device,
Self::Allocated(image, _) => image.device(),
}
}
fn allocation(&self) -> Option<&Allocation> {
match self {
Self::Swapchain(_, _) => None,
Self::Allocated(_, alloc) => Some(alloc),
}
}
fn allocation_mut(&mut self) -> Option<&mut Allocation> {
match self {
Self::Swapchain(_, _) => None,
Self::Allocated(_, alloc) => Some(alloc),
}
}
}
#[derive(Debug)]
pub struct Image {
image: ImageInner,
desc: ImageDesc,
views: Mutex<WeakVec<ImageView>>,
} }
impl Image { impl Image {
pub fn new(device: Device, desc: ImageDesc) -> crate::Result<Self> { pub fn new(device: Device, desc: ImageDesc) -> VkResult<Self> {
let (image, requirements) = Self::new_raw(device.clone(), &desc)?;
let alloc = device.alloc2.lock().allocate(&AllocationCreateDesc {
name: desc.name.as_deref().unwrap_or(""),
requirements,
location: desc.mem_location,
linear: desc.tiling == vk::ImageTiling::LINEAR,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
})?;
Ok(Self {
image: ImageInner::Allocated(
DeviceObject::new(image, device.clone(), desc.name.clone()),
Allocation::Owned(DeviceObject::new_without_name(alloc, device)),
),
desc,
views: Default::default(),
})
}
pub fn new_with_allocation(
device: Device,
allocation: Allocation,
desc: ImageDesc,
) -> crate::Result<Self> {
let (image, requirements) = Self::new_raw(device.clone(), &desc)?;
// validate allocation
let alloc_size = allocation
.allocation()
.map(|alloc| alloc.size())
.unwrap_or(0);
if alloc_size < requirements.size {
tracing::error!(
"allocation size {} is smaller than image memory requirements {}",
alloc_size,
requirements.size
);
return Err(crate::Error::Unspecified);
}
if allocation
.allocation()
.map(|alloc| 1 << alloc.memory_type_index())
.unwrap_or(0)
& requirements.memory_type_bits
== 0
{
return Err(crate::Error::Unspecified);
}
Ok(Self {
image: ImageInner::Allocated(
DeviceObject::new(image, device.clone(), desc.name.clone()),
allocation,
),
desc,
views: Default::default(),
})
}
pub fn from_swapchain_image(image: vk::Image, swapchain: &Swapchain) -> Self {
Self {
image: ImageInner::Swapchain(image, swapchain.swapchain.device().clone()),
desc: ImageDesc {
format: swapchain.config.format,
kind: vk::ImageType::TYPE_2D,
mip_levels: 1,
array_layers: 1,
samples: vk::SampleCountFlags::TYPE_1,
extent: vk::Extent3D {
width: swapchain.config.extent.width,
height: swapchain.config.extent.height,
depth: 1,
},
tiling: vk::ImageTiling::OPTIMAL,
usage: swapchain.config.usage,
queue_families: QueueFlags::PRESENT,
layout: vk::ImageLayout::UNDEFINED,
mem_location: gpu_allocator::MemoryLocation::GpuOnly,
..Default::default()
},
views: Default::default(),
}
}
fn new_raw(
device: Device,
desc: &ImageDesc,
) -> crate::Result<(vk::Image, vk::MemoryRequirements)> {
tracing::trace!("allocate new image with desc={desc:?}"); tracing::trace!("allocate new image with desc={desc:?}");
let ImageDesc { let ImageDesc {
flags, flags,
name,
format,
kind,
mip_levels,
array_layers,
samples,
extent,
tiling,
usage,
queue_families,
layout,
mem_usage,
alloc_flags,
} = desc;
let queue_families = device.queue_families().family_indices(queue_families);
let sharing_mode = if queue_families.len() > 1 {
vk::SharingMode::CONCURRENT
} else {
vk::SharingMode::EXCLUSIVE
};
let info = &vk::ImageCreateInfo::default()
.flags(flags)
.image_type(kind)
.format(format)
.extent(extent)
.samples(samples)
.initial_layout(layout)
.tiling(tiling)
.usage(usage)
.sharing_mode(sharing_mode)
.queue_family_indices(&queue_families)
.array_layers(array_layers)
.mip_levels(mip_levels);
let alloc_info = &vk_mem::AllocationCreateInfo {
usage: mem_usage,
flags: alloc_flags,
..Default::default()
};
let (handle, alloc) = unsafe { device.alloc().create_image(info, alloc_info)? };
Self::construct(
device,
handle,
name,
Some(alloc),
extent,
format,
Mutex::new(HashMap::new()),
Mutex::new(HashMap::new()),
None, // aliased
false,
)
}
pub unsafe fn from_swapchain_image(
device: Device,
image: vk::Image,
name: Option<Cow<'static, str>>,
extent: vk::Extent3D,
format: vk::Format,
) -> Result<Image, vk::Result> {
Self::construct(
device,
image,
name,
None,
extent,
format,
Mutex::new(HashMap::new()),
Mutex::new(HashMap::new()),
None,
true,
)
}
pub fn format(&self) -> vk::Format {
self.format
}
pub fn image(&self) -> vk::Image {
self.handle()
}
pub fn size(&self) -> vk::Extent3D {
self.size
}
pub fn extent_2d(&self) -> vk::Extent2D {
vk::Extent2D {
width: self.size.width,
height: self.size.height,
}
}
pub fn width(&self) -> u32 {
self.size.width
}
pub fn height(&self) -> u32 {
self.size.height
}
pub fn depth(&self) -> u32 {
self.size.depth
}
fn get_parent_or_self(self: &Arc<Self>) -> Arc<Image> {
self.parent
.as_ref()
.map(|weak| weak.upgrade().unwrap())
.unwrap_or_else(|| self.clone())
}
// TODO: figure out how to make this safer
pub fn get_alias(self: &Arc<Self>, desc: ImageDesc) -> VkResult<Arc<Self>> {
unsafe { self.get_parent_or_self().get_alias_inner(desc) }
}
/// # Safety
/// must only be called on the primogenitor of an image.
/// get the primogenitor with [`Self::get_parent_or_self()`]
unsafe fn get_alias_inner(self: Arc<Self>, desc: ImageDesc) -> VkResult<Arc<Image>> {
use std::collections::hash_map::Entry::*;
match self.aliases.lock().entry(desc.clone()) {
Occupied(occupied) => Ok(occupied.get().clone()),
Vacant(vacant) => {
let ImageDesc {
flags,
name,
format, format,
kind, kind,
mip_levels, mip_levels,
@ -267,7 +305,10 @@ impl Image {
.. ..
} = desc; } = desc;
let queue_families = device.queues.family_indices(*queue_families); let queue_families = self
.device()
.queue_families()
.family_indices(queue_families);
let sharing_mode = if queue_families.len() > 1 { let sharing_mode = if queue_families.len() > 1 {
vk::SharingMode::CONCURRENT vk::SharingMode::CONCURRENT
@ -276,87 +317,51 @@ impl Image {
}; };
let info = &vk::ImageCreateInfo::default() let info = &vk::ImageCreateInfo::default()
.flags(*flags) .flags(flags)
.image_type(*kind) .image_type(kind)
.format(*format) .format(format)
.extent(*extent) .extent(extent)
.samples(*samples) .samples(samples)
.initial_layout(*layout) .initial_layout(layout)
.tiling(*tiling) .tiling(tiling)
.usage(*usage) .usage(usage)
.sharing_mode(sharing_mode) .sharing_mode(sharing_mode)
.queue_family_indices(&queue_families) .queue_family_indices(&queue_families)
.array_layers(*array_layers) .array_layers(array_layers)
.mip_levels(*mip_levels); .mip_levels(mip_levels);
// validate let alloc = self
let limits = &device.adapter.properties.core.limits; .alloc
let max_dim = match *kind { .as_ref()
vk::ImageType::TYPE_1D => limits.max_image_dimension1_d, .expect("no alloc associated with image. is this the framebuffer?");
vk::ImageType::TYPE_2D => limits.max_image_dimension2_d,
vk::ImageType::TYPE_3D => limits.max_image_dimension3_d, let image = unsafe {
_ => unreachable!(), let image = self.device().dev().create_image(info, None)?;
let req = self.device().dev().get_image_memory_requirements(image);
if self.device().alloc().get_allocation_info(alloc).size < req.size {
return Err(vk::Result::ERROR_MEMORY_MAP_FAILED);
}
self.device().alloc().bind_image_memory(alloc, image)?;
image
}; };
if extent.width > max_dim || extent.height > max_dim || extent.depth > max_dim { let alias = Self::construct(
tracing::error!( self.device().clone(),
"image extent {extent:?} exceeds device limits (max dimension: {max_dim})" image,
); name,
None,
return Err(crate::Error::ImageTooLarge { extent,
width: extent.width, format,
height: extent.height, Mutex::new(HashMap::new()),
max_size: max_dim, Mutex::new(HashMap::new()),
}); Some(Arc::downgrade(&self)),
} self.is_swapchain_image,
)?;
let image = unsafe { device.raw.create_image(&info, None)? }; Ok(vacant.insert(Arc::new(alias)).clone())
let requirements = unsafe { device.raw.get_image_memory_requirements(image) };
Ok((image, requirements))
}
}
impl Eq for Image {}
impl PartialEq for Image {
fn eq(&self, other: &Self) -> bool {
self.image.image() == other.image.image()
}
}
impl Image {
pub fn format(&self) -> vk::Format {
self.desc.format
}
pub fn image(&self) -> vk::Image {
self.image.image()
}
pub fn size(&self) -> vk::Extent3D {
self.desc.extent
}
pub fn extent_2d(&self) -> vk::Extent2D {
vk::Extent2D {
width: self.desc.extent.width,
height: self.desc.extent.height,
} }
} }
pub fn width(&self) -> u32 {
self.desc.extent.width
}
pub fn height(&self) -> u32 {
self.desc.extent.height
}
pub fn depth(&self) -> u32 {
self.desc.extent.depth
}
pub fn allocation(&self) -> Option<&Allocation> {
self.image.allocation()
}
pub fn allocation_mut(&mut self) -> Option<&mut Allocation> {
self.image.allocation_mut()
} }
/// technically, this ImageView belongs to the image and is managed by it. /// technically, this ImageView belongs to the image and is managed by it.
@ -387,33 +392,11 @@ impl Image {
} }
} }
pub fn create_view(&self, desc: ImageViewDesc) -> crate::Result<ImageView> { pub fn create_view(&self, desc: ImageViewDesc) -> VkResult<ImageView> {
// validate
if !view_kind_compatible(self.desc.kind, desc.kind) {
tracing::error!(
"image view kind {:?} is not compatible with image kind {:?}",
desc.kind,
self.desc.kind
);
return Err(crate::Error::IncompatibleImageViewKind {
image_kind: self.desc.kind,
view_kind: desc.kind,
});
}
if desc.mip_range.0 > self.desc.mip_levels || desc.mip_range.1 > self.desc.mip_levels {
tracing::error!(
"image view mip range {:?} exceeds image mip levels {}",
desc.mip_range,
self.desc.mip_levels
);
return Err(crate::Error::Unspecified);
}
let create_info = vk::ImageViewCreateInfo::default() let create_info = vk::ImageViewCreateInfo::default()
.flags(desc.flags) .flags(desc.flags)
.image(self.image()) .image(self.image())
.view_type(desc.kind) .view_type(vk::ImageViewType::TYPE_2D)
.format(desc.format) .format(desc.format)
.components(desc.components) .components(desc.components)
.subresource_range( .subresource_range(
@ -425,28 +408,16 @@ impl Image {
.layer_count(desc.layer_range.count()), .layer_count(desc.layer_range.count()),
); );
let device = self.image.device(); let view = unsafe { self.device().dev().create_image_view(&create_info, None)? };
let view = unsafe { device.raw.create_image_view(&create_info, None)? };
ImageView::construct(self.device().clone(), view, desc.name) ImageView::construct(self.device().clone(), view, desc.name)
} }
} }
fn view_kind_compatible(image_kind: vk::ImageType, view_kind: vk::ImageViewType) -> bool {
use vk::ImageType as IT;
use vk::ImageViewType as VT;
match (image_kind, view_kind) {
(IT::TYPE_1D, VT::TYPE_1D | VT::TYPE_1D_ARRAY) => true,
(IT::TYPE_2D, VT::TYPE_2D | VT::TYPE_2D_ARRAY | VT::CUBE | VT::CUBE_ARRAY) => true,
(IT::TYPE_3D, VT::TYPE_2D | VT::TYPE_2D_ARRAY | VT::TYPE_3D) => true,
_ => false,
}
}
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct ImageViewDesc { pub struct ImageViewDesc {
pub name: Option<Cow<'static, str>>,
pub flags: vk::ImageViewCreateFlags, pub flags: vk::ImageViewCreateFlags,
pub name: Option<Cow<'static, str>>,
pub kind: vk::ImageViewType, pub kind: vk::ImageViewType,
pub format: vk::Format, pub format: vk::Format,
pub components: vk::ComponentMapping, pub components: vk::ComponentMapping,
@ -554,18 +525,10 @@ impl PartialEq for ImageViewDesc {
} }
} }
#[derive(Debug)] define_device_owned_handle! {
pub struct ImageView { #[derive(Debug)]
view: DeviceObject<vk::ImageView>, pub ImageView(vk::ImageView) {} => |this| unsafe {
desc: ImageViewDesc, this.device().dev().destroy_image_view(this.handle(), None);
image: Arc<Image>,
}
impl DeviceHandle for vk::ImageView {
unsafe fn destroy(&mut self, device: &Device) {
unsafe {
device.dev().destroy_image_view(*self, None);
}
} }
} }

View file

@ -6,12 +6,17 @@
slice_partition_dedup slice_partition_dedup
)] )]
use std::{collections::HashMap, ffi::CStr, fmt::Debug, marker::PhantomData, sync::Arc}; use std::{
cell::OnceCell, collections::HashMap, ffi::CStr, fmt::Debug, marker::PhantomData, sync::Arc,
};
use bitflags::bitflags;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use parking_lot::{Mutex, MutexGuard};
use ash::{ use ash::{
ext, Entry, ext, khr,
prelude::VkResult, prelude::VkResult,
vk::{self, SurfaceCapabilitiesKHR}, vk::{self, SurfaceCapabilitiesKHR},
}; };
@ -27,6 +32,7 @@ pub mod device;
mod egui_pass; mod egui_pass;
mod images; mod images;
pub mod instance; pub mod instance;
mod memory;
mod pipeline; mod pipeline;
pub mod render_graph; pub mod render_graph;
pub mod rendering; pub mod rendering;
@ -95,8 +101,6 @@ pub enum Error {
Utf8Error(#[from] core::str::Utf8Error), Utf8Error(#[from] core::str::Utf8Error),
#[error(transparent)] #[error(transparent)]
NulError(#[from] std::ffi::NulError), NulError(#[from] std::ffi::NulError),
#[error(transparent)]
GpuAllocatorError(#[from] gpu_allocator::AllocationError),
#[error("No Physical Device found.")] #[error("No Physical Device found.")]
NoPhysicalDevice, NoPhysicalDevice,
#[error(transparent)] #[error(transparent)]
@ -111,13 +115,6 @@ pub enum Error {
}, },
#[error("Image dimensions cannot be zero.")] #[error("Image dimensions cannot be zero.")]
ImageZeroSized, ImageZeroSized,
#[error("Incompatible image view kind {view_kind:?} for image kind {image_kind:?}.")]
IncompatibleImageViewKind {
view_kind: vk::ImageViewType,
image_kind: vk::ImageType,
},
#[error("Unspecified Error")]
Unspecified,
} }
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
@ -993,32 +990,13 @@ impl Renderer2 {
&self, &self,
window: RawWindowHandle, window: RawWindowHandle,
extent: vk::Extent2D, extent: vk::Extent2D,
) -> Result<swapchain::Surface> { ) -> Result<swapchain::WindowSurface> {
let surface = unsafe { swapchain::WindowSurface::new(self.device.clone(), extent, window, self.display)
swapchain::Surface::new_from_raw_window_handle(
&self.device.instance,
self.display,
window,
)?
};
surface.configure(
&self.device,
swapchain::SwapchainConfiguration {
present_mode: vk::PresentModeKHR::MAILBOX,
format: vk::Format::R8G8B8A8_UNORM,
color_space: vk::ColorSpaceKHR::SRGB_NONLINEAR,
image_count: 3,
extent,
composite_alpha_mode: vk::CompositeAlphaFlagsKHR::OPAQUE,
usage: vk::ImageUsageFlags::COLOR_ATTACHMENT | vk::ImageUsageFlags::TRANSFER_DST,
},
)?;
Ok(surface)
} }
pub async fn draw_graph<T, F: FnOnce(&mut Renderer2, &mut render_graph::RenderGraph) -> T>( pub async fn draw_graph<T, F: FnOnce(&mut Renderer2, &mut render_graph::RenderGraph) -> T>(
&mut self, &mut self,
surface: &swapchain::Surface, surface: &swapchain::WindowSurface,
cb: F, cb: F,
) -> Result<T> { ) -> Result<T> {
let frame = surface.acquire_image().await?; let frame = surface.acquire_image().await?;
@ -1033,7 +1011,7 @@ impl Renderer2 {
let future = cmds.submit( let future = cmds.submit(
Some((frame.acquire, vk::PipelineStageFlags::TRANSFER)), Some((frame.acquire, vk::PipelineStageFlags::TRANSFER)),
Some(frame.release), Some(frame.release),
Arc::new(sync::Fence::from_pool(&self.device.pools.fences, None)?), Arc::new(sync::Fence::create(self.device.clone())?),
)?; )?;
future.await; future.await;

View file

@ -1 +1,27 @@
#![allow(dead_code)]
use crate::device::Device;
//#[derive(Debug)]
pub struct DeviceMemoryDesc {
pub flags: vk_mem::AllocationCreateFlags,
pub size: u64,
pub align: u64,
pub type_bits: u32,
pub usage: vk_mem::MemoryUsage,
}
#[derive(Debug)]
pub struct DeviceMemory {
device: Device,
alloc: vk_mem::Allocation,
}
impl DeviceMemory {}
impl Drop for DeviceMemory {
fn drop(&mut self) {
unsafe {
self.device.alloc().free_memory(&mut self.alloc);
}
}
}

View file

@ -2,7 +2,6 @@ use bitflags::bitflags;
use parking_lot::Mutex; use parking_lot::Mutex;
use raw_window_handle::RawDisplayHandle; use raw_window_handle::RawDisplayHandle;
use std::{collections::HashMap, ops::Deref, sync::Arc}; use std::{collections::HashMap, ops::Deref, sync::Arc};
use tinyvec::{ArrayVec, array_vec};
use ash::vk; use ash::vk;
@ -134,23 +133,6 @@ impl DeviceQueues {
&self.transfer &self.transfer
} }
pub fn family_indices(&self, flags: crate::device::QueueFlags) -> ArrayVec<[u32; 4]> {
let mut indices = array_vec!([u32; 4]);
use crate::device::QueueFlags as QF;
if flags.intersects(QF::GRAPHICS | QF::PRESENT) {
indices.push(self.graphics.family.index);
}
if flags.contains(QF::ASYNC_COMPUTE) {
indices.push(self.compute.family.index);
}
if flags.contains(QF::TRANSFER) {
indices.push(self.transfer.family.index);
}
let len = indices.partition_dedup().0.len();
_ = indices.drain(len..);
indices
}
pub fn swapchain_family_indices(&self) -> &[u32] { pub fn swapchain_family_indices(&self) -> &[u32] {
core::slice::from_ref(&self.graphics.family.index) core::slice::from_ref(&self.graphics.family.index)
} }

View file

@ -6,17 +6,17 @@ use glam::{f32::Mat4, vec3};
pub use crate::egui_pass::{egui_pass, egui_pre_pass}; pub use crate::egui_pass::{egui_pass, egui_pre_pass};
use crate::{ use crate::{
Result,
buffers::{Buffer, BufferDesc}, buffers::{Buffer, BufferDesc},
commands::{self, traits::CommandBufferExt}, commands::{self, traits::CommandBufferExt},
device::{Device, DeviceOwned}, device::{Device, DeviceOwned},
images::ImageViewDesc, images::ImageViewDesc,
pipeline, pipeline,
render_graph::{ render_graph::{
Access, GraphResourceId, PassDesc, RecordFn, RenderContext, RenderGraph, buffer_barrier, buffer_barrier, Access, GraphResourceId, PassDesc, RecordFn, RenderContext, RenderGraph,
}, },
sync, sync,
util::Rgba8, util::Rgba8,
Result,
}; };
pub struct Wireframe { pub struct Wireframe {
@ -162,11 +162,7 @@ impl Wireframe {
); );
} }
let future = cmd.submit_async( let future = cmd.submit_async(None, None, Arc::new(sync::Fence::create(dev.clone())?))?;
None,
None,
Arc::new(sync::Fence::from_pool(&dev.pools.fences, None)?),
)?;
let (pipeline, layout) = Self::create_pipeline(dev.clone())?; let (pipeline, layout) = Self::create_pipeline(dev.clone())?;

View file

@ -20,7 +20,7 @@ use crate::{
device::{Device, DeviceObject, DeviceOwned}, device::{Device, DeviceObject, DeviceOwned},
images, images,
instance::InstanceInner, instance::InstanceInner,
sync::{self, Fence}, sync,
util::RawMutexGuard, util::RawMutexGuard,
}; };
@ -280,12 +280,12 @@ impl Surface {
pub struct Swapchain { pub struct Swapchain {
// swapchain images, managed by the swapchain and must not be destroyed manually. // swapchain images, managed by the swapchain and must not be destroyed manually.
images: Vec<vk::Image>, images: Vec<vk::Image>,
pub(crate) swapchain: DeviceObject<vk::SwapchainKHR>, swapchain: DeviceObject<vk::SwapchainKHR>,
// this carries the device handle, however the `swapchain` field holds a ref count on the device, so it is safe to hold the pointer in the functor as well. // this carries the device handle, however the `swapchain` field holds a ref count on the device, so it is safe to hold the pointer in the functor as well.
#[debug(skip)] #[debug(skip)]
functor: khr::swapchain::Device, functor: khr::swapchain::Device,
/// current configuration of the swapchain. /// current configuration of the swapchain.
pub(crate) config: SwapchainConfiguration, config: SwapchainConfiguration,
/// the minimum number of images the surface permits. This is used to calculate how many images we can have in-flight at the same time. /// the minimum number of images the surface permits. This is used to calculate how many images we can have in-flight at the same time.
min_image_count: u32, min_image_count: u32,
@ -338,9 +338,9 @@ impl Drop for Swapchain {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
self.release_resources(); self.release_resources();
self.functor.destroy_swapchain(*self.swapchain, None);
} }
// the swapchain itself will be automatically destroyed by the todo!()
// DeviceObject's Drop implementation.
} }
} }
@ -489,7 +489,7 @@ impl Swapchain {
/// suboptimal and should be recreated. /// suboptimal and should be recreated.
fn acquire_image( fn acquire_image(
self: Arc<Self>, self: Arc<Self>,
) -> impl std::future::Future<Output = crate::Result<(SwapchainFrame, bool)>> { ) -> impl std::future::Future<Output = VkResult<(SwapchainFrame, bool)>> {
let frame = self let frame = self
.current_frame .current_frame
.try_update(Ordering::Release, Ordering::Relaxed, |i| { .try_update(Ordering::Release, Ordering::Relaxed, |i| {
@ -500,7 +500,7 @@ impl Swapchain {
tracing::trace!(frame, "acquiring image for frame {frame}"); tracing::trace!(frame, "acquiring image for frame {frame}");
async move { async move {
let fence = Fence::from_pool(&self.swapchain.device().pools.fences, None)?; let fence = self.fences[frame];
let acquire = self.acquire_semaphores[frame]; let acquire = self.acquire_semaphores[frame];
let release = self.release_semaphores[frame]; let release = self.release_semaphores[frame];
@ -510,14 +510,14 @@ impl Swapchain {
move || unsafe { move || unsafe {
this.with_locked(|swapchain| { this.with_locked(|swapchain| {
this.functor this.functor
.acquire_next_image(swapchain, u64::MAX, acquire, fence.raw()) .acquire_next_image(swapchain, u64::MAX, acquire, fence)
}) })
} }
}) })
.await?; .await?;
// wait for image to become available. // wait for image to become available.
fence.into_future().await; sync::FenceFuture::new(fence.clone()).await;
let idx = idx as usize; let idx = idx as usize;
let image = self.images[idx].clone(); let image = self.images[idx].clone();

View file

@ -1,16 +1,10 @@
#[cfg(debug_assertions)]
use std::borrow::Cow;
use std::{ use std::{
future::Future, future::Future,
marker::PhantomData, marker::PhantomData,
mem::ManuallyDrop,
sync::{Arc, atomic::AtomicU32}, sync::{Arc, atomic::AtomicU32},
time::Duration, time::Duration,
}; };
use crate::device::{DeviceObject, DeviceOwned, Pool, PoolObject, Pooled};
use crate::{Result, device::DeviceInner};
use super::Device; use super::Device;
use ash::{prelude::*, vk}; use ash::{prelude::*, vk};
use crossbeam::channel::{Receiver, Sender}; use crossbeam::channel::{Receiver, Sender};
@ -155,273 +149,111 @@ impl SyncThreadpool {
} }
} }
pub enum Fence { pub struct Semaphore {
Dedicated { fence: DeviceObject<vk::Fence> }, device: Device,
Pooled { fence: PoolObject<vk::Fence> }, inner: vk::Semaphore,
} }
impl Pooled for vk::Fence { pub struct Fence {
fn create_from_pool(pool: &Pool<Self>) -> Result<Self> { dev: Device,
let fence = unsafe { fence: vk::Fence,
pool.device
.raw
.create_fence(&vk::FenceCreateInfo::default(), None)?
};
Ok(fence)
}
} }
impl std::fmt::Debug for Fence { impl std::fmt::Debug for Fence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Fence").field("fence", &self.raw()).finish() f.debug_struct("Fence").field("fence", &self.fence).finish()
}
}
impl Drop for Fence {
fn drop(&mut self) {
unsafe {
self.dev.dev().destroy_fence(self.fence, None);
}
} }
} }
impl Fence { impl Fence {
pub fn new_dedicated(device: Device, name: Option<&'static str>) -> Result<Fence> { unsafe fn new(dev: Device, fence: vk::Fence) -> Fence {
let fence = unsafe { Self { dev, fence }
device
.raw
.create_fence(&vk::FenceCreateInfo::default(), None)?
};
Ok(Self::Dedicated {
fence: DeviceObject::new(fence, device, name.map(Into::into)),
})
} }
pub fn from_pool(pool: &Pool<vk::Fence>, name: Option<&'static str>) -> Result<Fence> { pub fn create(dev: Device) -> VkResult<Fence> {
let mut fence = pool.get()?;
#[cfg(debug_assertions)]
if let Some(name) = name {
fence.name_object(name);
}
Ok(Self::Pooled { fence })
}
pub fn raw(&self) -> vk::Fence {
match self {
Fence::Dedicated { fence } => **fence,
Fence::Pooled { fence } => **fence,
}
}
fn device(&self) -> &Arc<DeviceInner> {
match self {
Fence::Dedicated { fence } => &fence.device().shared,
Fence::Pooled { fence } => fence.device(),
}
}
pub fn wait_on(&self, timeout: Option<u64>) -> Result<()> {
unsafe { unsafe {
self.device().raw.wait_for_fences( Ok(Self::new(
core::slice::from_ref(&self.raw()), dev.clone(),
true, dev.dev()
timeout.unwrap_or(u64::MAX), .create_fence(&vk::FenceCreateInfo::default(), None)?,
)? ))
} }
Ok(())
} }
#[allow(dead_code)]
pub fn create_signaled(dev: Device) -> VkResult<Fence> {
unsafe {
Ok(Self::new(
dev.clone(),
dev.dev().create_fence(
&vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED),
None,
)?,
))
}
}
pub fn wait_on(&self, timeout: Option<u64>) -> Result<(), vk::Result> {
use core::slice::from_ref;
unsafe {
self.dev
.dev()
.wait_for_fences(from_ref(&self.fence), true, timeout.unwrap_or(u64::MAX))
}
}
pub fn fence(&self) -> vk::Fence {
self.fence
}
pub fn is_signaled(&self) -> bool { pub fn is_signaled(&self) -> bool {
unsafe { self.dev.dev().get_fence_status(self.fence).unwrap_or(false) }
}
pub fn reset(&self) -> Result<(), vk::Result> {
unsafe { unsafe {
self.device() self.dev
.raw .dev()
.get_fence_status(self.raw()) .reset_fences(core::slice::from_ref(&self.fence))
.unwrap_or(false)
} }
} }
}
pub fn reset(&self) -> Result<()> { impl AsRef<vk::Fence> for Fence {
unsafe { fn as_ref(&self) -> &vk::Fence {
self.device() todo!()
.raw
.reset_fences(core::slice::from_ref(&self.raw()))?
}
Ok(())
}
pub fn into_future<'a>(self) -> FenceFuture<'a> {
FenceFuture::new(Arc::new(self))
} }
} }
#[derive(Debug, Clone, Copy)] #[allow(dead_code)]
enum SemaphoreType { impl Semaphore {
Binary, pub fn new(device: Device) -> VkResult<Self> {
Timeline(u64),
}
pub enum Semaphore {
Dedicated {
semaphore_type: SemaphoreType,
semaphore: DeviceObject<vk::Semaphore>,
},
Pooled {
semaphore_type: SemaphoreType,
semaphore: vk::Semaphore,
device: Device,
#[cfg(debug_assertions)]
name: Option<Cow<'static, str>>,
},
}
#[derive(Debug, Clone)]
pub(crate) struct BinarySemaphore(vk::Semaphore);
#[derive(Debug, Clone)]
pub(crate) struct TimelineSemaphore(vk::Semaphore);
// This is just so that ash can name these semaphore newtypes
impl vk::Handle for BinarySemaphore {
const TYPE: vk::ObjectType = <vk::Semaphore as vk::Handle>::TYPE;
fn as_raw(self) -> u64 {
self.0.as_raw()
}
fn from_raw(_: u64) -> Self {
unimplemented!("BinarySemaphore cannot be created from raw handle")
}
}
impl vk::Handle for TimelineSemaphore {
const TYPE: vk::ObjectType = <vk::Semaphore as vk::Handle>::TYPE;
fn as_raw(self) -> u64 {
self.0.as_raw()
}
fn from_raw(_: u64) -> Self {
unimplemented!("TimelineSemaphore cannot be created from raw handle")
}
}
impl Pooled for BinarySemaphore {
fn create_from_pool(pool: &Pool<Self>) -> Result<Self> {
let mut type_info = let mut type_info =
vk::SemaphoreTypeCreateInfo::default().semaphore_type(vk::SemaphoreType::BINARY); vk::SemaphoreTypeCreateInfo::default().semaphore_type(vk::SemaphoreType::BINARY);
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info); let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
let inner = unsafe { pool.device.raw.create_semaphore(&create_info, None)? }; let inner = unsafe { device.dev().create_semaphore(&create_info, None)? };
Ok(Self(inner))
}
}
impl Pooled for TimelineSemaphore { Ok(Self { device, inner })
fn create_from_pool(pool: &Pool<Self>) -> Result<Self> { }
pub fn new_timeline(device: Device, value: u64) -> VkResult<Self> {
let mut type_info = vk::SemaphoreTypeCreateInfo::default() let mut type_info = vk::SemaphoreTypeCreateInfo::default()
.semaphore_type(vk::SemaphoreType::TIMELINE) .semaphore_type(vk::SemaphoreType::TIMELINE)
.initial_value(0); .initial_value(value);
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info); let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
let inner = unsafe { pool.device.raw.create_semaphore(&create_info, None)? }; let inner = unsafe { device.dev().create_semaphore(&create_info, None)? };
Ok(Self(inner))
Ok(Self { device, inner })
}
pub fn semaphore(&self) -> vk::Semaphore {
self.inner
} }
} }
impl Drop for Semaphore { impl Drop for Semaphore {
fn drop(&mut self) { fn drop(&mut self) {
if let Semaphore::Pooled {
device,
semaphore_type,
semaphore,
name,
} = self
{
#[cfg(debug_assertions)]
if name.is_some() {
// reset the name to avoid confusion in case this semaphore is re-used
unsafe { device.debug_name_object(*semaphore, "") };
}
match semaphore_type {
SemaphoreType::Binary => device
.pools
.binary_semaphores
.push(BinarySemaphore(*semaphore)),
SemaphoreType::Timeline(_) => {
device
.pools
.timeline_semaphores
.push(TimelineSemaphore(*semaphore));
}
}
}
}
}
impl Semaphore {
pub fn new_dedicated(
device: Device,
semaphore_type: SemaphoreType,
name: Option<&'static str>,
) -> Result<Self> {
let mut type_info = vk::SemaphoreTypeCreateInfo::default();
match semaphore_type {
SemaphoreType::Binary => {
type_info = type_info.semaphore_type(vk::SemaphoreType::BINARY);
}
SemaphoreType::Timeline(value) => {
type_info = type_info
.semaphore_type(vk::SemaphoreType::TIMELINE)
.initial_value(value);
}
}
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
let inner = unsafe { device.dev().create_semaphore(&create_info, None)? };
Ok(Self::Dedicated {
semaphore_type,
semaphore: DeviceObject::new(inner, device, name.map(Into::into)),
})
}
pub fn from_pool(
device: Device,
semaphore_type: SemaphoreType,
name: Option<&'static str>,
) -> Result<Self> {
let semaphore = match semaphore_type {
SemaphoreType::Binary => {
if let Some(semaphore) = device.pools.binary_semaphores.pop() {
semaphore.0
} else {
let mut type_info = vk::SemaphoreTypeCreateInfo::default()
.semaphore_type(vk::SemaphoreType::BINARY);
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
unsafe { device.raw.create_semaphore(&create_info, None)? }
}
}
SemaphoreType::Timeline(value) => {
if let Some(semaphore) = device.pools.binary_semaphores.pop() {
semaphore.0
} else {
let mut type_info = vk::SemaphoreTypeCreateInfo::default()
.semaphore_type(vk::SemaphoreType::TIMELINE)
.initial_value(value);
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
unsafe { device.raw.create_semaphore(&create_info, None)? }
}
}
};
#[cfg(debug_assertions)]
if let Some(name) = name {
unsafe { unsafe {
device.debug_name_object(semaphore, name); self.device.dev().destroy_semaphore(self.inner, None);
}
}
Ok(Self::Pooled {
semaphore_type,
semaphore,
device,
#[cfg(debug_assertions)]
name: name.map(Into::into),
})
}
pub fn semaphore(&self) -> vk::Semaphore {
match self {
Semaphore::Dedicated { semaphore, .. } => **semaphore,
Semaphore::Pooled { semaphore, .. } => *semaphore,
} }
} }
} }
@ -433,16 +265,24 @@ pub struct FenceFuture<'a> {
} }
impl FenceFuture<'_> { impl FenceFuture<'_> {
/// # Safety
/// `fence` must not be destroyed while this future is live.
#[allow(dead_code)]
pub unsafe fn from_fence(device: Device, fence: vk::Fence) -> Self {
Self {
fence: Arc::new(unsafe { Fence::new(device, fence) }),
_pd: PhantomData,
}
}
pub fn new(fence: Arc<Fence>) -> Self { pub fn new(fence: Arc<Fence>) -> Self {
Self { Self {
fence, fence,
_pd: PhantomData, _pd: PhantomData,
} }
} }
pub fn block(&self) -> crate::Result<()> { pub fn block(&self) -> VkResult<()> {
self.fence.wait_on(None)?; self.fence.wait_on(None)?;
self.fence.reset()?; self.fence.reset()
Ok(())
} }
} }
@ -459,7 +299,7 @@ impl Future for FenceFuture<'_> {
std::task::Poll::Ready(()) std::task::Poll::Ready(())
} else { } else {
self.fence self.fence
.device() .dev
.sync_threadpool() .sync_threadpool()
.spawn_waiter(self.fence.clone(), cx.waker().clone()); .spawn_waiter(self.fence.clone(), cx.waker().clone());
std::task::Poll::Pending std::task::Poll::Pending

View file

@ -3,77 +3,6 @@ use std::ops::{Deref, DerefMut};
use ash::vk; use ash::vk;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
pub(crate) mod weak_vec {
//! Module containing the [`WeakVec`] API.
use std::{sync::Weak, vec::Vec};
/// An optimized container for `Weak` references of `T` that minimizes reallocations by
/// dropping older elements that no longer have strong references to them.
#[derive(Debug)]
pub(crate) struct WeakVec<T> {
inner: Vec<Weak<T>>,
}
impl<T> Default for WeakVec<T> {
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<T> WeakVec<T> {
pub(crate) fn new() -> Self {
Self { inner: Vec::new() }
}
/// Pushes a new element to this collection.
///
/// If the inner Vec needs to be reallocated, we will first drop older elements that
/// no longer have strong references to them.
pub(crate) fn push(&mut self, value: Weak<T>) {
if self.inner.len() == self.inner.capacity() {
// Iterating backwards has the advantage that we don't do more work than we have to.
for i in (0..self.inner.len()).rev() {
if self.inner[i].strong_count() == 0 {
self.inner.swap_remove(i);
}
}
// Make sure our capacity is twice the number of live elements.
// Leaving some spare capacity ensures that we won't re-scan immediately.
self.inner.reserve_exact(self.inner.len());
}
self.inner.push(value);
}
}
pub(crate) struct WeakVecIter<T> {
inner: std::vec::IntoIter<Weak<T>>,
}
impl<T> Iterator for WeakVecIter<T> {
type Item = Weak<T>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<T> IntoIterator for WeakVec<T> {
type Item = Weak<T>;
type IntoIter = WeakVecIter<T>;
fn into_iter(self) -> Self::IntoIter {
WeakVecIter {
inner: self.inner.into_iter(),
}
}
}
}
pub(crate) mod cow_arc {}
#[macro_export] #[macro_export]
macro_rules! def_monotonic_id { macro_rules! def_monotonic_id {
($vis:vis $ty:ident) => { ($vis:vis $ty:ident) => {