Compare commits
No commits in common. "7c3f7120b03a26bcc818be08485202bd8242b50a" and "0529ce0a3c3faffcb35ecc1f055dd0e687f14ece" have entirely different histories.
7c3f7120b0
...
0529ce0a3c
|
|
@ -52,9 +52,6 @@ petgraph = "0.7"
|
|||
itertools = "0.14.0"
|
||||
ahash = "0.8"
|
||||
|
||||
# for non-cryptographic hashing of resources like pipelines, e.g. for caching
|
||||
md-5 = "0.11.0"
|
||||
|
||||
parking_lot = "0.12.3"
|
||||
|
||||
tokio = "1.42"
|
||||
|
|
|
|||
|
|
@ -26,8 +26,6 @@ vk-mem = { workspace = true }
|
|||
gpu-allocator = { workspace = true }
|
||||
rectangle-pack = { workspace = true }
|
||||
|
||||
md-5 = { workspace = true }
|
||||
|
||||
raw-window-handle = { workspace = true }
|
||||
egui = { workspace = true , features = ["bytemuck"]}
|
||||
egui_winit_platform = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -547,7 +547,7 @@ pub mod traits {
|
|||
self.device().dev().cmd_bind_pipeline(
|
||||
self.handle(),
|
||||
pipeline.bind_point(),
|
||||
pipeline.raw(),
|
||||
pipeline.handle(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use std::{
|
|||
borrow::Cow,
|
||||
collections::{BTreeSet, HashMap, HashSet},
|
||||
ffi::CStr,
|
||||
mem::ManuallyDrop,
|
||||
ops::{Deref, DerefMut},
|
||||
sync::Arc,
|
||||
};
|
||||
|
|
@ -16,7 +17,6 @@ use raw_window_handle::RawDisplayHandle;
|
|||
|
||||
use crate::{
|
||||
Instance, PhysicalDeviceFeatures, PhysicalDeviceInfo, Result,
|
||||
pipeline::pipeline_cache::PipelineCache,
|
||||
queue::{DeviceQueueInfos, DeviceQueues, Queue},
|
||||
sync::{self, BinarySemaphore, TimelineSemaphore},
|
||||
};
|
||||
|
|
@ -109,16 +109,9 @@ pub struct DeviceInner {
|
|||
pub(crate) device_extensions: DeviceExtensions,
|
||||
#[allow(dead_code)]
|
||||
pub(crate) enabled_extensions: Vec<&'static CStr>,
|
||||
|
||||
_drop: DeviceDrop,
|
||||
}
|
||||
|
||||
impl AsRef<DeviceInner> for DeviceInner {
|
||||
fn as_ref(&self) -> &DeviceInner {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for DeviceInner {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("DeviceInner")
|
||||
|
|
@ -444,30 +437,19 @@ impl PhysicalDeviceInfo {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct DevicePools {
|
||||
pub(crate) fences: Arc<Pool<vk::Fence>>,
|
||||
pub(crate) fences: Pool<vk::Fence>,
|
||||
pub(crate) binary_semaphores: Pool<BinarySemaphore>,
|
||||
pub(crate) timeline_semaphores: Pool<TimelineSemaphore>,
|
||||
pub(crate) pipeline_cache: asdf::DeviceObject<PipelineCache, Arc<DeviceInner>>,
|
||||
}
|
||||
|
||||
impl AsRef<DevicePools> for DevicePools {
|
||||
fn as_ref(&self) -> &DevicePools {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl DevicePools {
|
||||
pub fn new(device: Arc<DeviceInner>) -> Self {
|
||||
Self {
|
||||
fences: Arc::new(Pool::new(device.clone())),
|
||||
fences: Pool::new(device.clone()),
|
||||
binary_semaphores: Pool::new(device.clone()),
|
||||
timeline_semaphores: Pool::new(device.clone()),
|
||||
pipeline_cache: asdf::DeviceObject::new(
|
||||
device.clone(),
|
||||
PipelineCache::new(&device.raw, &device.adapter).unwrap(),
|
||||
),
|
||||
timeline_semaphores: Pool::new(device),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -494,16 +476,6 @@ impl core::ops::Deref for Device {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> AsRef<T> for Device
|
||||
where
|
||||
T: ?Sized,
|
||||
<Self as Deref>::Target: AsRef<T>,
|
||||
{
|
||||
fn as_ref(&self) -> &T {
|
||||
self.deref().as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl DeviceInner {
|
||||
pub fn sync_threadpool(&self) -> &sync::SyncThreadpool {
|
||||
&self.sync_threadpool
|
||||
|
|
@ -729,7 +701,7 @@ impl<T: DeviceHandle> DeviceObject<T> {
|
|||
{
|
||||
unsafe {
|
||||
if let Some(name) = name.as_ref() {
|
||||
device.debug_name_object(inner.clone(), name);
|
||||
device.debug_name_object(inner.clone(), &name);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -754,7 +726,7 @@ impl<T: DeviceHandle> DeviceObject<T> {
|
|||
pub fn name(&self) -> Option<&str> {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
self.name.as_deref()
|
||||
self.name.as_deref().map(|cow| cow.as_ref())
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
|
|
@ -772,8 +744,6 @@ impl<T: DeviceHandle> Drop for DeviceObject<T> {
|
|||
}
|
||||
|
||||
pub trait DeviceHandle {
|
||||
/// # Safety
|
||||
/// The caller must ensure this function is only called once for a given object.
|
||||
unsafe fn destroy(&mut self, device: &Device);
|
||||
}
|
||||
|
||||
|
|
@ -804,9 +774,11 @@ impl DeviceHandle for vk::Buffer {
|
|||
impl DeviceHandle for vk::SwapchainKHR {
|
||||
unsafe fn destroy(&mut self, device: &Device) {
|
||||
unsafe {
|
||||
if let Some(swapchain) = device.device_extensions.swapchain.as_ref() {
|
||||
swapchain.destroy_swapchain(*self, None)
|
||||
}
|
||||
device
|
||||
.device_extensions
|
||||
.swapchain
|
||||
.as_ref()
|
||||
.map(|swapchain| swapchain.destroy_swapchain(*self, None));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -820,9 +792,52 @@ pub trait Pooled: Sized {
|
|||
fn create_from_pool(pool: &Pool<Self>) -> Result<Self>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
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: Mutex<Vec<T>>,
|
||||
pub(crate) pool: Arc<Mutex<Vec<T>>>,
|
||||
pub(crate) device: Arc<DeviceInner>,
|
||||
}
|
||||
|
||||
|
|
@ -832,7 +847,7 @@ impl<T> Pool<T> {
|
|||
}
|
||||
pub fn new(device: Arc<DeviceInner>) -> Self {
|
||||
Self {
|
||||
pool: Mutex::new(Vec::new()),
|
||||
pool: Arc::new(Mutex::new(Vec::new())),
|
||||
device,
|
||||
}
|
||||
}
|
||||
|
|
@ -841,37 +856,27 @@ impl<T> Pool<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> AsRef<Pool<T>> for Pool<T> {
|
||||
fn as_ref(&self) -> &Pool<T> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub type PoolObject<T, U = Arc<Pool<T>>> = asdf::ExternallyManagedObject<T, U>;
|
||||
|
||||
impl<T: Pooled> Pool<T> {
|
||||
pub fn get(&self) -> Result<T> {
|
||||
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(item)
|
||||
Ok(PoolObject {
|
||||
inner: ManuallyDrop::new(item),
|
||||
pool: self.clone(),
|
||||
#[cfg(debug_assertions)]
|
||||
name: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_debug_named(&self, name: Option<impl Into<Cow<'static, str>>>) -> Result<T>
|
||||
where
|
||||
T: asdf::traits::DebugNameable,
|
||||
{
|
||||
let obj = self.get()?;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let name = name.map(Into::into).unwrap_or_default();
|
||||
<T as asdf::traits::DebugNameable>::debug_name(&obj, &self.device, &name);
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -932,233 +937,3 @@ macro_rules! define_device_owned_handle {
|
|||
)?
|
||||
};
|
||||
}
|
||||
|
||||
// This module is an experiment in a more generic way to manage device-owned resources.
|
||||
// #[cfg(false)]
|
||||
pub(crate) mod asdf {
|
||||
use std::{
|
||||
mem::{ManuallyDrop, MaybeUninit},
|
||||
ops::{Deref, DerefMut},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use ash::vk;
|
||||
|
||||
use crate::{device::DeviceInner, util::DebugName};
|
||||
|
||||
pub mod traits {
|
||||
/// A trait describing an object owned by some manager-type, which is
|
||||
/// responsible for destroying it.
|
||||
pub trait ExternallyManagedObject<T> {
|
||||
/// # Safety
|
||||
/// The caller must ensure this function is only called once for a given object.
|
||||
unsafe fn destroy(self, owner: &T);
|
||||
}
|
||||
|
||||
/// A trait describing an object which can have a debug name assigned to it.
|
||||
pub trait DebugNameable {
|
||||
fn debug_name(&self, device: &super::DeviceInner, name: &str);
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper for types which are owned by another type `O`, which is responsible for destruction.
|
||||
#[derive(Debug)]
|
||||
pub struct ExternallyManagedObject<T: traits::ExternallyManagedObject<O>, O> {
|
||||
inner: ManuallyDrop<T>,
|
||||
owner: O,
|
||||
}
|
||||
|
||||
impl<T: traits::ExternallyManagedObject<O>, O> Deref for ExternallyManagedObject<T, O> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: traits::ExternallyManagedObject<O>, O> DerefMut for ExternallyManagedObject<T, O> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: traits::ExternallyManagedObject<O>, O> ExternallyManagedObject<T, O> {
|
||||
pub fn new(inner: T, owner: O) -> Self {
|
||||
Self {
|
||||
inner: ManuallyDrop::new(inner),
|
||||
owner,
|
||||
}
|
||||
}
|
||||
pub fn owner(&self) -> &O {
|
||||
&self.owner
|
||||
}
|
||||
|
||||
pub fn map_inner<U>(self, f: impl FnOnce(T) -> U) -> ExternallyManagedObject<U, O>
|
||||
where
|
||||
U: traits::ExternallyManagedObject<O>,
|
||||
{
|
||||
unsafe {
|
||||
let mut this = MaybeUninit::new(self);
|
||||
let inner = ManuallyDrop::take(&mut this.assume_init_mut().inner);
|
||||
let owner = core::ptr::read(&raw const this.assume_init_mut().owner);
|
||||
|
||||
let new_inner = f(inner);
|
||||
|
||||
ExternallyManagedObject {
|
||||
inner: ManuallyDrop::new(new_inner),
|
||||
owner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_owner<U>(self, f: impl FnOnce(O) -> U) -> ExternallyManagedObject<T, U>
|
||||
where
|
||||
T: traits::ExternallyManagedObject<U>,
|
||||
{
|
||||
unsafe {
|
||||
let mut this = MaybeUninit::new(self);
|
||||
let inner = ManuallyDrop::take(&mut this.assume_init_mut().inner);
|
||||
|
||||
// get the old owner without calling `Self::drop`
|
||||
let owner = core::ptr::read(&raw const this.assume_init_mut().owner);
|
||||
|
||||
ExternallyManagedObject {
|
||||
inner: ManuallyDrop::new(inner),
|
||||
owner: f(owner),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, O> Drop for ExternallyManagedObject<T, O>
|
||||
where
|
||||
T: traits::ExternallyManagedObject<O>,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let inner = ManuallyDrop::take(&mut self.inner);
|
||||
inner.destroy(&self.owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for vulkan types which are owned by the device, taking care of destruction.
|
||||
#[derive(Debug)]
|
||||
pub struct DeviceObject<
|
||||
T: traits::ExternallyManagedObject<O>,
|
||||
O: AsRef<super::DeviceInner> = Arc<super::DeviceInner>,
|
||||
> {
|
||||
inner: ExternallyManagedObject<T, O>,
|
||||
#[allow(dead_code)]
|
||||
name: Option<DebugName>,
|
||||
}
|
||||
|
||||
impl<
|
||||
T: traits::ExternallyManagedObject<O> + traits::DebugNameable,
|
||||
O: AsRef<super::DeviceInner>,
|
||||
> DeviceObject<T, O>
|
||||
{
|
||||
pub fn new_debug_named(owner: O, inner: T, name: Option<impl Into<DebugName>>) -> Self {
|
||||
let name = name.map(Into::into);
|
||||
if let Some(ref name) = name {
|
||||
traits::DebugNameable::debug_name(&inner, owner.as_ref(), name);
|
||||
}
|
||||
|
||||
let obj = ExternallyManagedObject::new(inner, owner);
|
||||
|
||||
Self { inner: obj, name }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: traits::ExternallyManagedObject<O>, O: AsRef<super::DeviceInner>> DeviceObject<T, O> {
|
||||
pub fn new(owner: O, inner: T) -> Self {
|
||||
let inner = ExternallyManagedObject::new(inner, owner);
|
||||
|
||||
Self { inner, name: None }
|
||||
}
|
||||
pub fn device(&self) -> &O {
|
||||
self.inner.owner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, O> Deref for DeviceObject<T, O>
|
||||
where
|
||||
T: traits::ExternallyManagedObject<O>,
|
||||
O: AsRef<super::DeviceInner>,
|
||||
{
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
mod impls {
|
||||
use crate::device::{DeviceInner, DevicePools, GpuAllocation};
|
||||
|
||||
use super::*;
|
||||
|
||||
impl<T: AsRef<DeviceInner>> traits::ExternallyManagedObject<T> for ash::vk::Semaphore {
|
||||
unsafe fn destroy(self, device: &T) {
|
||||
unsafe {
|
||||
device.as_ref().raw.destroy_semaphore(self, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<DeviceInner>> traits::ExternallyManagedObject<T> for GpuAllocation {
|
||||
unsafe fn destroy(self, device: &T) {
|
||||
_ = device.as_ref().alloc2.lock().free(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl traits::ExternallyManagedObject<DevicePools> for vk::Semaphore {
|
||||
unsafe fn destroy(self, owner: &DevicePools) {
|
||||
owner
|
||||
.binary_semaphores
|
||||
.push(crate::sync::BinarySemaphore(self));
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> traits::DebugNameable for T
|
||||
where
|
||||
T: vk::Handle + Copy,
|
||||
{
|
||||
fn debug_name(&self, device: &super::DeviceInner, name: &str) {
|
||||
unsafe {
|
||||
device.debug_name_object(*self, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(test)]
|
||||
fn asdf() {
|
||||
use crate::device::{DevicePools, GpuAllocation};
|
||||
fn summon<T>() -> T {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
let _inner_ref: DeviceObject<vk::Semaphore, &DeviceInner> = DeviceObject::new_debug_named(
|
||||
summon::<&DeviceInner>(),
|
||||
summon::<vk::Semaphore>(),
|
||||
Some("my semaphore"),
|
||||
);
|
||||
|
||||
let _device_owned: DeviceObject<vk::Semaphore, super::Device> =
|
||||
DeviceObject::new_debug_named(
|
||||
summon::<super::Device>(),
|
||||
summon::<vk::Semaphore>(),
|
||||
Some("my other semaphore"),
|
||||
);
|
||||
|
||||
let _allocation: DeviceObject<GpuAllocation, Arc<super::DeviceInner>> = DeviceObject::new(
|
||||
summon::<Arc<super::DeviceInner>>(),
|
||||
summon::<GpuAllocation>(),
|
||||
);
|
||||
|
||||
let _pool_owned: ExternallyManagedObject<vk::Semaphore, DevicePools> =
|
||||
ExternallyManagedObject::new(summon::<vk::Semaphore>(), summon::<DevicePools>());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -809,9 +809,9 @@ impl EguiState {
|
|||
"crates/renderer/shaders/egui_vert.spv",
|
||||
)?;
|
||||
|
||||
let pipeline = pipeline::Pipeline::new_graphics(
|
||||
let pipeline = pipeline::Pipeline::new(
|
||||
device.clone(),
|
||||
pipeline::GraphicsPipelineDesc {
|
||||
pipeline::PipelineDesc::Graphics(pipeline::GraphicsPipelineDesc {
|
||||
flags: Default::default(),
|
||||
name: Some("egui-pipeline".into()),
|
||||
shader_stages: &[
|
||||
|
|
@ -904,7 +904,7 @@ impl EguiState {
|
|||
dynamic_states: &[vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR],
|
||||
..Default::default()
|
||||
}),
|
||||
},
|
||||
}),
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use ash::{ext, prelude::*, vk};
|
|||
|
||||
use crate::{
|
||||
define_device_owned_handle,
|
||||
device::{Device, DeviceHandle, DeviceObject},
|
||||
device::{Device, DeviceOwnedDebugObject},
|
||||
make_extension,
|
||||
};
|
||||
|
||||
|
|
@ -40,6 +40,12 @@ pub struct PipelineLayoutDesc<'a> {
|
|||
pub name: Option<Cow<'static, str>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PipelineDesc<'a> {
|
||||
Compute(ComputePipelineDesc<'a>),
|
||||
Graphics(GraphicsPipelineDesc<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ComputePipelineDesc<'a> {
|
||||
pub flags: vk::PipelineCreateFlags,
|
||||
|
|
@ -245,17 +251,18 @@ impl DescriptorPool {
|
|||
let info = &vk::DescriptorSetAllocateInfo::default()
|
||||
.descriptor_pool(self.handle())
|
||||
.set_layouts(&layouts);
|
||||
let sets = unsafe { self.device().dev().allocate_descriptor_sets(info)? };
|
||||
let sets = unsafe { self.device().dev().allocate_descriptor_sets(&info)? };
|
||||
|
||||
for (&set, desc) in sets.iter().zip(descs) {
|
||||
if let Some(name) = desc.name.as_ref() {
|
||||
unsafe { self.device().debug_name_object(set, name) };
|
||||
unsafe { self.device().debug_name_object(set, &name) };
|
||||
}
|
||||
}
|
||||
|
||||
Ok(sets)
|
||||
}
|
||||
|
||||
// pub fn free(&self) {}
|
||||
#[allow(dead_code)]
|
||||
pub fn reset(&self) -> VkResult<()> {
|
||||
unsafe {
|
||||
|
|
@ -467,13 +474,18 @@ impl ShaderModule {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Pipeline {
|
||||
pipeline: DeviceObject<vk::Pipeline>,
|
||||
pipeline: DeviceOwnedDebugObject<vk::Pipeline>,
|
||||
bind_point: vk::PipelineBindPoint,
|
||||
}
|
||||
|
||||
impl DeviceHandle for vk::Pipeline {
|
||||
unsafe fn destroy(&mut self, device: &Device) {
|
||||
unsafe { device.raw.destroy_pipeline(*self, None) };
|
||||
impl Drop for Pipeline {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
self.pipeline
|
||||
.dev()
|
||||
.dev()
|
||||
.destroy_pipeline(self.pipeline.handle(), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -488,322 +500,224 @@ impl ShaderStageDesc<'_> {
|
|||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new_compute(device: Device, desc: ComputePipelineDesc) -> crate::Result<Self> {
|
||||
let info = &vk::ComputePipelineCreateInfo::default()
|
||||
.flags(desc.flags)
|
||||
.layout(desc.layout.handle())
|
||||
.base_pipeline_handle(
|
||||
desc.base_pipeline
|
||||
.map(|p| p.raw())
|
||||
.unwrap_or(vk::Pipeline::null()),
|
||||
)
|
||||
.stage(desc.shader_stage.as_create_info());
|
||||
pub fn new(device: Device, desc: PipelineDesc) -> VkResult<Self> {
|
||||
let name: Option<Cow<'static, str>>;
|
||||
let bind_point: vk::PipelineBindPoint;
|
||||
let result = match desc {
|
||||
PipelineDesc::Compute(desc) => {
|
||||
name = desc.name;
|
||||
bind_point = vk::PipelineBindPoint::COMPUTE;
|
||||
let info = &vk::ComputePipelineCreateInfo::default()
|
||||
.flags(desc.flags)
|
||||
.layout(desc.layout.handle())
|
||||
.base_pipeline_handle(
|
||||
desc.base_pipeline
|
||||
.map(|p| p.handle())
|
||||
.unwrap_or(vk::Pipeline::null()),
|
||||
)
|
||||
.stage(desc.shader_stage.as_create_info());
|
||||
|
||||
let pipeline = unsafe {
|
||||
device
|
||||
.dev()
|
||||
.create_compute_pipelines(
|
||||
device.pools.pipeline_cache.raw,
|
||||
core::slice::from_ref(info),
|
||||
None,
|
||||
)
|
||||
// It's cool to just take the first one and ignore any
|
||||
// potentially created pipelines since we know there wont be any
|
||||
// others.
|
||||
.map_err(|(_, err)| err)?[0]
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
pipeline: DeviceObject::new(pipeline, device, desc.name),
|
||||
bind_point: vk::PipelineBindPoint::COMPUTE,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_graphics(device: Device, desc: GraphicsPipelineDesc) -> crate::Result<Self> {
|
||||
let stages = desc
|
||||
.shader_stages
|
||||
.iter()
|
||||
.map(|stage| stage.as_create_info())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let vertex_input = desc.vertex_input.map(|vertex| {
|
||||
vk::PipelineVertexInputStateCreateInfo::default()
|
||||
.vertex_attribute_descriptions(vertex.attributes)
|
||||
.vertex_binding_descriptions(vertex.bindings)
|
||||
});
|
||||
let input_assembly = desc.input_assembly.map(|state| {
|
||||
vk::PipelineInputAssemblyStateCreateInfo::default()
|
||||
.primitive_restart_enable(state.primitive_restart)
|
||||
.topology(state.topology)
|
||||
});
|
||||
let tessellation = desc.tessellation.map(|state| {
|
||||
vk::PipelineTessellationStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.patch_control_points(state.patch_control_points)
|
||||
});
|
||||
let viewport = desc.viewport.map(|state| {
|
||||
let mut info = vk::PipelineViewportStateCreateInfo::default()
|
||||
.scissor_count(state.num_scissors)
|
||||
.viewport_count(state.num_viewports);
|
||||
if let Some(viewports) = state.viewports {
|
||||
info = info.viewports(viewports);
|
||||
}
|
||||
if let Some(scissors) = state.scissors {
|
||||
info = info.scissors(scissors);
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let rasterization = desc.rasterization.map(|state| {
|
||||
let mut info = vk::PipelineRasterizationStateCreateInfo::default()
|
||||
.line_width(state.line_width)
|
||||
.cull_mode(state.cull_mode)
|
||||
.polygon_mode(state.polygon_mode)
|
||||
.rasterizer_discard_enable(state.discard_enable)
|
||||
.depth_clamp_enable(state.depth_clamp_enable);
|
||||
|
||||
if let Some(depth_bias) = state.depth_bias {
|
||||
info = info
|
||||
.depth_bias_enable(true)
|
||||
.depth_bias_clamp(depth_bias.clamp)
|
||||
.depth_bias_constant_factor(depth_bias.constant_factor)
|
||||
.depth_bias_slope_factor(depth_bias.slope_factor);
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let multisample = desc.multisample.map(|state| {
|
||||
vk::PipelineMultisampleStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.min_sample_shading(state.min_sample_shading)
|
||||
.rasterization_samples(state.rasterization_samples)
|
||||
.sample_mask(state.sample_mask)
|
||||
.sample_shading_enable(state.sample_shading_enable)
|
||||
.alpha_to_coverage_enable(state.alpha_to_coverage_enable)
|
||||
.alpha_to_one_enable(state.alpha_to_one_enable)
|
||||
});
|
||||
|
||||
let color_blend = desc.color_blend.map(|state| {
|
||||
vk::PipelineColorBlendStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.attachments(state.attachments)
|
||||
.blend_constants(state.blend_constants)
|
||||
.logic_op(state.logic_op.unwrap_or(Default::default()))
|
||||
.logic_op_enable(state.logic_op.is_some())
|
||||
});
|
||||
|
||||
let depth_stencil = desc.depth_stencil.map(|state| {
|
||||
let mut info = vk::PipelineDepthStencilStateCreateInfo::default().flags(state.flags);
|
||||
|
||||
if let Some(depth) = state.depth {
|
||||
info = info
|
||||
.depth_compare_op(depth.compare_op.unwrap_or(vk::CompareOp::default()))
|
||||
.depth_test_enable(depth.compare_op.is_some())
|
||||
.depth_write_enable(depth.write_enable)
|
||||
.depth_bounds_test_enable(depth.bounds.is_some());
|
||||
if let Some(bounds) = depth.bounds {
|
||||
info = info
|
||||
.max_depth_bounds(bounds.max)
|
||||
.min_depth_bounds(bounds.min);
|
||||
unsafe {
|
||||
device.dev().create_compute_pipelines(
|
||||
vk::PipelineCache::null(),
|
||||
core::slice::from_ref(info),
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
PipelineDesc::Graphics(desc) => {
|
||||
name = desc.name;
|
||||
bind_point = vk::PipelineBindPoint::GRAPHICS;
|
||||
|
||||
if let Some(stencil) = state.stencil {
|
||||
info = info
|
||||
.stencil_test_enable(true)
|
||||
.front(stencil.front)
|
||||
.back(stencil.back);
|
||||
let stages = desc
|
||||
.shader_stages
|
||||
.iter()
|
||||
.map(|stage| stage.as_create_info())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let vertex_input = desc.vertex_input.map(|vertex| {
|
||||
vk::PipelineVertexInputStateCreateInfo::default()
|
||||
.vertex_attribute_descriptions(vertex.attributes)
|
||||
.vertex_binding_descriptions(vertex.bindings)
|
||||
});
|
||||
let input_assembly = desc.input_assembly.map(|state| {
|
||||
vk::PipelineInputAssemblyStateCreateInfo::default()
|
||||
.primitive_restart_enable(state.primitive_restart)
|
||||
.topology(state.topology)
|
||||
});
|
||||
let tessellation = desc.tessellation.map(|state| {
|
||||
vk::PipelineTessellationStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.patch_control_points(state.patch_control_points)
|
||||
});
|
||||
let viewport = desc.viewport.map(|state| {
|
||||
let mut info = vk::PipelineViewportStateCreateInfo::default()
|
||||
.scissor_count(state.num_scissors)
|
||||
.viewport_count(state.num_viewports);
|
||||
if let Some(viewports) = state.viewports {
|
||||
info = info.viewports(viewports);
|
||||
}
|
||||
if let Some(scissors) = state.scissors {
|
||||
info = info.scissors(scissors);
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let rasterization = desc.rasterization.map(|state| {
|
||||
let mut info = vk::PipelineRasterizationStateCreateInfo::default()
|
||||
.line_width(state.line_width)
|
||||
.cull_mode(state.cull_mode)
|
||||
.polygon_mode(state.polygon_mode)
|
||||
.rasterizer_discard_enable(state.discard_enable)
|
||||
.depth_clamp_enable(state.depth_clamp_enable);
|
||||
|
||||
if let Some(depth_bias) = state.depth_bias {
|
||||
info = info
|
||||
.depth_bias_enable(true)
|
||||
.depth_bias_clamp(depth_bias.clamp)
|
||||
.depth_bias_constant_factor(depth_bias.constant_factor)
|
||||
.depth_bias_slope_factor(depth_bias.slope_factor);
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let multisample = desc.multisample.map(|state| {
|
||||
let info = vk::PipelineMultisampleStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.min_sample_shading(state.min_sample_shading)
|
||||
.rasterization_samples(state.rasterization_samples)
|
||||
.sample_mask(state.sample_mask)
|
||||
.sample_shading_enable(state.sample_shading_enable)
|
||||
.alpha_to_coverage_enable(state.alpha_to_coverage_enable)
|
||||
.alpha_to_one_enable(state.alpha_to_one_enable);
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let color_blend = desc.color_blend.map(|state| {
|
||||
let info = vk::PipelineColorBlendStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.attachments(state.attachments)
|
||||
.blend_constants(state.blend_constants)
|
||||
.logic_op(state.logic_op.unwrap_or(Default::default()))
|
||||
.logic_op_enable(state.logic_op.is_some());
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let depth_stencil = desc.depth_stencil.map(|state| {
|
||||
let mut info =
|
||||
vk::PipelineDepthStencilStateCreateInfo::default().flags(state.flags);
|
||||
|
||||
if let Some(depth) = state.depth {
|
||||
info = info
|
||||
.depth_compare_op(depth.compare_op.unwrap_or(vk::CompareOp::default()))
|
||||
.depth_test_enable(depth.compare_op.is_some())
|
||||
.depth_write_enable(depth.write_enable)
|
||||
.depth_bounds_test_enable(depth.bounds.is_some());
|
||||
if let Some(bounds) = depth.bounds {
|
||||
info = info
|
||||
.max_depth_bounds(bounds.max)
|
||||
.min_depth_bounds(bounds.min);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(stencil) = state.stencil {
|
||||
info = info
|
||||
.stencil_test_enable(true)
|
||||
.front(stencil.front)
|
||||
.back(stencil.back);
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let dynamic = desc.dynamic.map(|state| {
|
||||
let info = vk::PipelineDynamicStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.dynamic_states(state.dynamic_states);
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let mut rendering = desc.rendering.map(|state| {
|
||||
let info = vk::PipelineRenderingCreateInfo::default()
|
||||
.color_attachment_formats(state.color_formats)
|
||||
.depth_attachment_format(state.depth_format.unwrap_or_default())
|
||||
.stencil_attachment_format(state.stencil_format.unwrap_or_default());
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
fn option_to_ptr<T>(option: &Option<T>) -> *const T {
|
||||
option
|
||||
.as_ref()
|
||||
.map(|t| t as *const T)
|
||||
.unwrap_or(core::ptr::null())
|
||||
}
|
||||
|
||||
let mut info = vk::GraphicsPipelineCreateInfo {
|
||||
flags: desc.flags,
|
||||
stage_count: stages.len() as u32,
|
||||
p_stages: stages.as_ptr(),
|
||||
p_vertex_input_state: option_to_ptr(&vertex_input),
|
||||
p_input_assembly_state: option_to_ptr(&input_assembly),
|
||||
p_tessellation_state: option_to_ptr(&tessellation),
|
||||
p_viewport_state: option_to_ptr(&viewport),
|
||||
p_rasterization_state: option_to_ptr(&rasterization),
|
||||
p_multisample_state: option_to_ptr(&multisample),
|
||||
p_depth_stencil_state: option_to_ptr(&depth_stencil),
|
||||
p_color_blend_state: option_to_ptr(&color_blend),
|
||||
p_dynamic_state: option_to_ptr(&dynamic),
|
||||
layout: desc.layout.handle(),
|
||||
render_pass: desc.render_pass.unwrap_or(vk::RenderPass::null()),
|
||||
subpass: desc.subpass.unwrap_or(0),
|
||||
base_pipeline_handle: desc
|
||||
.base_pipeline
|
||||
.map(|piepline| piepline.pipeline.handle())
|
||||
.unwrap_or(vk::Pipeline::null()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(rendering) = rendering.as_mut() {
|
||||
info = info.push_next(rendering)
|
||||
}
|
||||
|
||||
unsafe {
|
||||
device.dev().create_graphics_pipelines(
|
||||
vk::PipelineCache::null(),
|
||||
core::slice::from_ref(&info),
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
info
|
||||
});
|
||||
|
||||
let dynamic = desc.dynamic.map(|state| {
|
||||
vk::PipelineDynamicStateCreateInfo::default()
|
||||
.flags(state.flags)
|
||||
.dynamic_states(state.dynamic_states)
|
||||
});
|
||||
|
||||
let mut rendering = desc.rendering.map(|state| {
|
||||
vk::PipelineRenderingCreateInfo::default()
|
||||
.color_attachment_formats(state.color_formats)
|
||||
.depth_attachment_format(state.depth_format.unwrap_or_default())
|
||||
.stencil_attachment_format(state.stencil_format.unwrap_or_default())
|
||||
});
|
||||
|
||||
fn option_to_ptr<T>(option: &Option<T>) -> *const T {
|
||||
option
|
||||
.as_ref()
|
||||
.map(|t| t as *const T)
|
||||
.unwrap_or(core::ptr::null())
|
||||
}
|
||||
|
||||
let mut info = vk::GraphicsPipelineCreateInfo {
|
||||
flags: desc.flags,
|
||||
stage_count: stages.len() as u32,
|
||||
p_stages: stages.as_ptr(),
|
||||
p_vertex_input_state: option_to_ptr(&vertex_input),
|
||||
p_input_assembly_state: option_to_ptr(&input_assembly),
|
||||
p_tessellation_state: option_to_ptr(&tessellation),
|
||||
p_viewport_state: option_to_ptr(&viewport),
|
||||
p_rasterization_state: option_to_ptr(&rasterization),
|
||||
p_multisample_state: option_to_ptr(&multisample),
|
||||
p_depth_stencil_state: option_to_ptr(&depth_stencil),
|
||||
p_color_blend_state: option_to_ptr(&color_blend),
|
||||
p_dynamic_state: option_to_ptr(&dynamic),
|
||||
layout: desc.layout.handle(),
|
||||
render_pass: desc.render_pass.unwrap_or(vk::RenderPass::null()),
|
||||
subpass: desc.subpass.unwrap_or(0),
|
||||
base_pipeline_handle: desc
|
||||
.base_pipeline
|
||||
.map(|piepline| *piepline.pipeline)
|
||||
.unwrap_or(vk::Pipeline::null()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(rendering) = rendering.as_mut() {
|
||||
info = info.push_next(rendering)
|
||||
}
|
||||
|
||||
let pipeline = unsafe {
|
||||
device
|
||||
.dev()
|
||||
.create_graphics_pipelines(
|
||||
device.pools.pipeline_cache.raw,
|
||||
core::slice::from_ref(&info),
|
||||
None,
|
||||
)
|
||||
// It's cool to just take the first one and ignore any
|
||||
// potentially created pipelines since we know there wont be any
|
||||
// others.
|
||||
.map_err(|(_, err)| err)?[0]
|
||||
let pipeline = match result {
|
||||
Ok(pipelines) => pipelines[0],
|
||||
Err((pipelines, error)) => {
|
||||
tracing::error!("failed to create pipelines with :{error}");
|
||||
for pipeline in pipelines {
|
||||
unsafe {
|
||||
device.dev().destroy_pipeline(pipeline, None);
|
||||
}
|
||||
}
|
||||
return Err(error.into());
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
pipeline: DeviceObject::new(pipeline, device, desc.name),
|
||||
bind_point: vk::PipelineBindPoint::GRAPHICS,
|
||||
pipeline: DeviceOwnedDebugObject::new(device, pipeline, name)?,
|
||||
bind_point,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> vk::Pipeline {
|
||||
*self.pipeline
|
||||
pub fn handle(&self) -> vk::Pipeline {
|
||||
self.pipeline.handle()
|
||||
}
|
||||
pub fn bind_point(&self) -> vk::PipelineBindPoint {
|
||||
self.bind_point
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod pipeline_cache {
|
||||
use std::sync::Arc;
|
||||
|
||||
use ash::vk;
|
||||
|
||||
use ash::Device;
|
||||
|
||||
use crate::PhysicalDeviceInfo;
|
||||
use crate::device::DeviceInner;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PipelineCache {
|
||||
#[allow(dead_code)]
|
||||
key: u128,
|
||||
pub(crate) raw: vk::PipelineCache,
|
||||
}
|
||||
|
||||
impl crate::device::asdf::traits::ExternallyManagedObject<Arc<DeviceInner>> for PipelineCache {
|
||||
unsafe fn destroy(self, owner: &Arc<DeviceInner>) {
|
||||
if let Ok(data) = self.export(&owner.raw) {
|
||||
_ = Self::write_to_disk(self.key, &data).inspect_err(|err| {
|
||||
tracing::error!("failed to write pipeline cache to disk: {err}");
|
||||
});
|
||||
}
|
||||
unsafe { owner.raw.destroy_pipeline_cache(self.raw, None) };
|
||||
}
|
||||
}
|
||||
|
||||
impl PipelineCache {
|
||||
const MAGIC: [u8; 4] = *b"VYPC";
|
||||
const KEY_VERSION: u32 = 1;
|
||||
const PATH: &'static str = "pipeline_cache.bin";
|
||||
fn calculate_key(adapter: &PhysicalDeviceInfo) -> u128 {
|
||||
use md5::Digest;
|
||||
let mut hasher = md5::Md5::new();
|
||||
let props = &adapter.properties;
|
||||
hasher.update(bytemuck::bytes_of(&[
|
||||
props.core.vendor_id,
|
||||
props.core.api_version,
|
||||
props.core.device_id,
|
||||
props.core.driver_version,
|
||||
]));
|
||||
u128::from_le_bytes(hasher.finalize().into())
|
||||
}
|
||||
|
||||
fn load_from_disk(key: u128) -> Option<(u128, Vec<u8>)> {
|
||||
use std::io::Read;
|
||||
|
||||
let file = std::fs::File::open(Self::PATH).ok()?;
|
||||
let mut reader = std::io::BufReader::new(file);
|
||||
let mut magic = [0; 4];
|
||||
reader.read_exact(&mut magic).ok()?;
|
||||
if magic != Self::MAGIC {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut version = 0;
|
||||
reader
|
||||
.read_exact(bytemuck::bytes_of_mut(&mut version))
|
||||
.ok()?;
|
||||
if version != Self::KEY_VERSION {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut disk_key = 0;
|
||||
reader
|
||||
.read_exact(bytemuck::bytes_of_mut(&mut disk_key))
|
||||
.ok()?;
|
||||
if disk_key != key {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut data = Vec::new();
|
||||
reader.read_to_end(&mut data).ok()?;
|
||||
|
||||
Some((key, data))
|
||||
}
|
||||
|
||||
fn write_to_disk(key: u128, data: &[u8]) -> std::io::Result<()> {
|
||||
use std::io::Write;
|
||||
|
||||
let file = std::fs::File::create(Self::PATH)?;
|
||||
let mut writer = std::io::BufWriter::new(file);
|
||||
writer.write_all(&Self::MAGIC)?;
|
||||
writer.write_all(bytemuck::bytes_of(&Self::KEY_VERSION))?;
|
||||
writer.write_all(bytemuck::bytes_of(&key))?;
|
||||
writer.write_all(data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn new(device: &Device, adapter: &PhysicalDeviceInfo) -> crate::Result<Self> {
|
||||
let key = Self::calculate_key(adapter);
|
||||
let data = Self::load_from_disk(key).map(|(key, data)| {
|
||||
tracing::info!("loaded pipeline cache from disk with key {key:x}");
|
||||
data
|
||||
});
|
||||
|
||||
let info = vk::PipelineCacheCreateInfo::default()
|
||||
// .flags(vk::PipelineCacheCreateFlags::EXTERNALLY_SYNCHRONIZED)
|
||||
.initial_data(data.as_deref().unwrap_or_default());
|
||||
|
||||
let cache = unsafe { device.create_pipeline_cache(&info, None)? };
|
||||
|
||||
Ok(Self { key, raw: cache })
|
||||
}
|
||||
|
||||
pub fn export(&self, device: &ash::Device) -> crate::Result<Vec<u8>> {
|
||||
let data = unsafe { device.get_pipeline_cache_data(self.raw)? };
|
||||
Ok(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,9 +201,9 @@ impl Wireframe {
|
|||
"crates/renderer/shaders/wireframe.spv",
|
||||
)?;
|
||||
|
||||
let pipeline = pipeline::Pipeline::new_graphics(
|
||||
let pipeline = pipeline::Pipeline::new(
|
||||
device.clone(),
|
||||
pipeline::GraphicsPipelineDesc {
|
||||
pipeline::PipelineDesc::Graphics(pipeline::GraphicsPipelineDesc {
|
||||
flags: Default::default(),
|
||||
name: Some("wireframe-pipeline".into()),
|
||||
shader_stages: &[
|
||||
|
|
@ -298,7 +298,7 @@ impl Wireframe {
|
|||
dynamic_states: &[vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR],
|
||||
..Default::default()
|
||||
}),
|
||||
},
|
||||
}),
|
||||
)?;
|
||||
|
||||
Ok((pipeline, pipeline_layout))
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
#[cfg(debug_assertions)]
|
||||
use std::borrow::Cow;
|
||||
use std::{
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
|
|
@ -5,10 +7,7 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::device::{
|
||||
DevicePools, Pool, PoolObject, Pooled,
|
||||
asdf::{DeviceObject, traits::ExternallyManagedObject as ExternallyManagedObjectTrait},
|
||||
};
|
||||
use crate::device::{DeviceObject, Pool, PoolObject, Pooled};
|
||||
use crate::{Result, device::DeviceInner};
|
||||
|
||||
use super::Device;
|
||||
|
|
@ -156,12 +155,8 @@ impl SyncThreadpool {
|
|||
}
|
||||
|
||||
pub enum Fence {
|
||||
Dedicated {
|
||||
fence: DeviceObject<vk::Fence>,
|
||||
},
|
||||
Pooled {
|
||||
fence: PoolObject<vk::Fence, Arc<Pool<vk::Fence>>>,
|
||||
},
|
||||
Dedicated { fence: DeviceObject<vk::Fence> },
|
||||
Pooled { fence: PoolObject<vk::Fence> },
|
||||
}
|
||||
|
||||
impl Pooled for vk::Fence {
|
||||
|
|
@ -176,20 +171,6 @@ impl Pooled for vk::Fence {
|
|||
}
|
||||
}
|
||||
|
||||
impl ExternallyManagedObjectTrait<Arc<Pool<vk::Fence>>> for vk::Fence {
|
||||
unsafe fn destroy(self, pool: &Arc<Pool<vk::Fence>>) {
|
||||
pool.push(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl ExternallyManagedObjectTrait<Arc<DeviceInner>> for vk::Fence {
|
||||
unsafe fn destroy(self, device: &Arc<DeviceInner>) {
|
||||
unsafe {
|
||||
device.raw.destroy_fence(self, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Fence {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Fence").field("fence", &self.raw()).finish()
|
||||
|
|
@ -204,15 +185,16 @@ impl Fence {
|
|||
.create_fence(&vk::FenceCreateInfo::default(), None)?
|
||||
};
|
||||
Ok(Self::Dedicated {
|
||||
fence: DeviceObject::new_debug_named(device.shared, fence, name),
|
||||
fence: DeviceObject::new(fence, device, name.map(Into::into)),
|
||||
})
|
||||
}
|
||||
pub fn from_pool(pool: &Arc<Pool<vk::Fence>>, name: Option<&'static str>) -> Result<Fence> {
|
||||
let fence = pool.get_debug_named(name)?;
|
||||
|
||||
Ok(Self::Pooled {
|
||||
fence: PoolObject::new(fence, pool.clone()),
|
||||
})
|
||||
pub fn from_pool(pool: &Pool<vk::Fence>, name: Option<&'static str>) -> Result<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 {
|
||||
|
|
@ -224,8 +206,8 @@ impl Fence {
|
|||
|
||||
fn device(&self) -> &Arc<DeviceInner> {
|
||||
match self {
|
||||
Fence::Dedicated { fence } => &fence.device(),
|
||||
Fence::Pooled { fence } => &fence.owner().device,
|
||||
Fence::Dedicated { fence } => &fence.device().shared,
|
||||
Fence::Pooled { fence } => fence.device(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -269,82 +251,24 @@ pub enum SemaphoreType {
|
|||
Timeline(u64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum SemaphoreInner {
|
||||
Binary(vk::Semaphore),
|
||||
Timeline(vk::Semaphore),
|
||||
}
|
||||
|
||||
impl ExternallyManagedObjectTrait<Arc<DevicePools>> for SemaphoreInner {
|
||||
unsafe fn destroy(self, owner: &Arc<DevicePools>) {
|
||||
match self {
|
||||
SemaphoreInner::Binary(semaphore) => {
|
||||
owner.binary_semaphores.push(BinarySemaphore(semaphore))
|
||||
}
|
||||
SemaphoreInner::Timeline(semaphore) => {
|
||||
owner.timeline_semaphores.push(TimelineSemaphore(semaphore))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExternallyManagedObjectTrait<Arc<DeviceInner>> for SemaphoreInner {
|
||||
unsafe fn destroy(self, owner: &Arc<DeviceInner>) {
|
||||
match self {
|
||||
SemaphoreInner::Binary(semaphore) | SemaphoreInner::Timeline(semaphore) => {
|
||||
unsafe { owner.raw.destroy_semaphore(semaphore, None) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::device::asdf::traits::DebugNameable for SemaphoreInner {
|
||||
fn debug_name(&self, device: &DeviceInner, name: &str) {
|
||||
unsafe {
|
||||
device.debug_name_object(self.raw(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SemaphoreInner {
|
||||
pub fn raw(&self) -> vk::Semaphore {
|
||||
match self {
|
||||
SemaphoreInner::Binary(semaphore) | SemaphoreInner::Timeline(semaphore) => *semaphore,
|
||||
}
|
||||
}
|
||||
pub fn semaphore_type(&self) -> SemaphoreType {
|
||||
match self {
|
||||
SemaphoreInner::Binary(_) => SemaphoreType::Binary,
|
||||
SemaphoreInner::Timeline(_) => SemaphoreType::Timeline(!0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BinarySemaphore> for SemaphoreInner {
|
||||
fn from(value: BinarySemaphore) -> Self {
|
||||
SemaphoreInner::Binary(value.0)
|
||||
}
|
||||
}
|
||||
impl From<TimelineSemaphore> for SemaphoreInner {
|
||||
fn from(value: TimelineSemaphore) -> Self {
|
||||
SemaphoreInner::Timeline(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Semaphore {
|
||||
Dedicated {
|
||||
semaphore: DeviceObject<SemaphoreInner>,
|
||||
semaphore_type: SemaphoreType,
|
||||
semaphore: DeviceObject<vk::Semaphore>,
|
||||
},
|
||||
Pooled {
|
||||
#[allow(private_interfaces)]
|
||||
semaphore: PoolObject<SemaphoreInner, Arc<DevicePools>>,
|
||||
semaphore_type: SemaphoreType,
|
||||
semaphore: vk::Semaphore,
|
||||
device: Device,
|
||||
#[cfg(debug_assertions)]
|
||||
name: Option<Cow<'static, str>>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) struct BinarySemaphore(pub(crate) vk::Semaphore);
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) struct TimelineSemaphore(pub(crate) vk::Semaphore);
|
||||
#[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 {
|
||||
|
|
@ -392,6 +316,36 @@ impl Pooled for TimelineSemaphore {
|
|||
}
|
||||
}
|
||||
|
||||
impl Drop for Semaphore {
|
||||
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,
|
||||
|
|
@ -412,13 +366,9 @@ impl Semaphore {
|
|||
let create_info = vk::SemaphoreCreateInfo::default().push_next(&mut type_info);
|
||||
let inner = unsafe { device.dev().create_semaphore(&create_info, None)? };
|
||||
|
||||
let inner = match semaphore_type {
|
||||
SemaphoreType::Binary => SemaphoreInner::Binary(inner),
|
||||
SemaphoreType::Timeline(_) => SemaphoreInner::Timeline(inner),
|
||||
};
|
||||
|
||||
Ok(Self::Dedicated {
|
||||
semaphore: DeviceObject::new_debug_named(device.shared, inner, name),
|
||||
semaphore_type,
|
||||
semaphore: DeviceObject::new(inner, device, name.map(Into::into)),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -429,35 +379,48 @@ impl Semaphore {
|
|||
) -> Result<Self> {
|
||||
let semaphore = match semaphore_type {
|
||||
SemaphoreType::Binary => {
|
||||
let semaphore: SemaphoreInner =
|
||||
device.pools.binary_semaphores.get_debug_named(name)?.into();
|
||||
PoolObject::new(semaphore, device.pools)
|
||||
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) => {
|
||||
let semaphore: SemaphoreInner = device
|
||||
.pools
|
||||
.timeline_semaphores
|
||||
.get_debug_named(name)?
|
||||
.into();
|
||||
|
||||
let info = vk::SemaphoreSignalInfo::default()
|
||||
.semaphore(semaphore.raw())
|
||||
.value(value);
|
||||
unsafe {
|
||||
device.raw.signal_semaphore(&info)?;
|
||||
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)? }
|
||||
}
|
||||
|
||||
PoolObject::new(semaphore, device.pools)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self::Pooled { semaphore })
|
||||
#[cfg(debug_assertions)]
|
||||
if let Some(name) = name {
|
||||
unsafe {
|
||||
device.debug_name_object(semaphore, name);
|
||||
}
|
||||
}
|
||||
|
||||
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.raw(),
|
||||
Semaphore::Pooled { semaphore, .. } => semaphore.raw(),
|
||||
Semaphore::Dedicated { semaphore, .. } => **semaphore,
|
||||
Semaphore::Pooled { semaphore, .. } => *semaphore,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue