diff --git a/piet-gpu-hal/examples/dx12_toy.rs b/piet-gpu-hal/examples/dx12_toy.rs index 63e3db4..23737bf 100644 --- a/piet-gpu-hal/examples/dx12_toy.rs +++ b/piet-gpu-hal/examples/dx12_toy.rs @@ -73,7 +73,7 @@ fn toy() -> Result<(), Error> { let pipeline = device.create_simple_compute_pipeline(SHADER_CODE, 1, 1)?; let ds = device.create_descriptor_set(&pipeline, &[&dev_buf], &[&img])?; let mut cmd_buf = device.create_cmd_buf()?; - let fence = device.create_fence(false)?; + let mut fence = device.create_fence(false)?; cmd_buf.begin(); cmd_buf.copy_buffer(&buf, &dev_buf); cmd_buf.memory_barrier(); @@ -86,8 +86,8 @@ fn toy() -> Result<(), Error> { cmd_buf.finish_timestamps(&query_pool); cmd_buf.host_barrier(); cmd_buf.finish(); - device.run_cmd_bufs(&[&cmd_buf], &[], &[], Some(&fence))?; - device.wait_and_reset(&[&fence])?; + device.run_cmd_bufs(&[&cmd_buf], &[], &[], Some(&mut fence))?; + device.wait_and_reset(&[&mut fence])?; let mut readback: Vec = vec![0u32; 256]; device.read_buffer(&buf, readback.as_mut_ptr() as *mut u8, 0, 1024)?; println!("{:?}", readback); diff --git a/piet-gpu-hal/src/dx12.rs b/piet-gpu-hal/src/dx12.rs index 0194e1e..637aa2f 100644 --- a/piet-gpu-hal/src/dx12.rs +++ b/piet-gpu-hal/src/dx12.rs @@ -348,7 +348,7 @@ impl crate::Device for Dx12Device { cmd_bufs: &[&Self::CmdBuf], _wait_semaphores: &[&Self::Semaphore], _signal_semaphores: &[&Self::Semaphore], - fence: Option<&Self::Fence>, + fence: Option<&mut Self::Fence>, ) -> Result<(), Error> { // TODO: handle semaphores let lists = cmd_bufs @@ -405,7 +405,7 @@ impl crate::Device for Dx12Device { Ok(Fence { fence, event, val }) } - unsafe fn wait_and_reset(&self, fences: &[&Self::Fence]) -> Result<(), Error> { + unsafe fn wait_and_reset(&self, fences: &[&mut Self::Fence]) -> Result<(), Error> { for fence in fences { // TODO: probably handle errors here. let _status = fence.event.wait(winapi::um::winbase::INFINITE); diff --git a/piet-gpu-hal/src/hub.rs b/piet-gpu-hal/src/hub.rs index 80c03cf..a53ef2f 100644 --- a/piet-gpu-hal/src/hub.rs +++ b/piet-gpu-hal/src/hub.rs @@ -124,9 +124,9 @@ impl Session { let mut i = 0; while i < pending.len() { if let Ok(true) = self.0.device.get_fence_status(&pending[i].fence) { - let item = pending.swap_remove(i); + let mut item = pending.swap_remove(i); // TODO: wait is superfluous, can just reset - let _ = self.0.device.wait_and_reset(&[&item.fence]); + let _ = self.0.device.wait_and_reset(vec![&mut item.fence]); let mut pool = self.0.cmd_buf_pool.lock().unwrap(); pool.push((item.cmd_buf, item.fence)); std::mem::drop(item.resources); @@ -143,7 +143,7 @@ impl Session { pub unsafe fn run_cmd_buf( &self, - cmd_buf: CmdBuf, + mut cmd_buf: CmdBuf, wait_semaphores: &[&Semaphore], signal_semaphores: &[&Semaphore], ) -> Result { @@ -162,7 +162,7 @@ impl Session { &cmd_bufs, wait_semaphores, signal_semaphores, - Some(&cmd_buf.fence), + Some(&mut cmd_buf.fence), )?; Ok(SubmittedCmdBuf( Some(SubmittedCmdBufInner { @@ -313,10 +313,10 @@ impl CmdBuf { impl SubmittedCmdBuf { pub fn wait(mut self) -> Result<(), Error> { - let item = self.0.take().unwrap(); + let mut item = self.0.take().unwrap(); if let Some(session) = Weak::upgrade(&self.1) { unsafe { - session.device.wait_and_reset(&[&item.fence])?; + session.device.wait_and_reset(vec![&mut item.fence])?; } session .cmd_buf_pool diff --git a/piet-gpu-hal/src/lib.rs b/piet-gpu-hal/src/lib.rs index 51ad9e4..e9c62fd 100644 --- a/piet-gpu-hal/src/lib.rs +++ b/piet-gpu-hal/src/lib.rs @@ -196,7 +196,7 @@ pub trait Device: Sized { cmd_buf: &[&Self::CmdBuf], wait_semaphores: &[&Self::Semaphore], signal_semaphores: &[&Self::Semaphore], - fence: Option<&Self::Fence>, + fence: Option<&mut Self::Fence>, ) -> Result<(), Error>; /// Copy data from the buffer to memory. @@ -233,7 +233,7 @@ pub trait Device: Sized { unsafe fn create_semaphore(&self) -> Result; unsafe fn create_fence(&self, signaled: bool) -> Result; - unsafe fn wait_and_reset(&self, fences: &[&Self::Fence]) -> Result<(), Error>; + unsafe fn wait_and_reset(&self, fences: &[&mut Self::Fence]) -> Result<(), Error>; unsafe fn get_fence_status(&self, fence: &Self::Fence) -> Result; unsafe fn create_sampler(&self, params: SamplerParams) -> Result; diff --git a/piet-gpu-hal/src/macros.rs b/piet-gpu-hal/src/macros.rs index 98178e2..1810a3c 100644 --- a/piet-gpu-hal/src/macros.rs +++ b/piet-gpu-hal/src/macros.rs @@ -58,6 +58,16 @@ macro_rules! mux_enum { } } } + $crate::mux_cfg! { + #[cfg(vk)] + #[allow(unused)] + fn vk_mut(&mut self) -> &mut $vk { + match self { + $name::Vk(x) => x, + _ => panic!("downcast error") + } + } + } $crate::mux_cfg! { #[cfg(dx12)] @@ -69,6 +79,16 @@ macro_rules! mux_enum { } } } + $crate::mux_cfg! { + #[cfg(dx12)] + #[allow(unused)] + fn dx12_mut(&mut self) -> &mut $dx12 { + match self { + $name::Dx12(x) => x, + _ => panic!("downcast error") + } + } + } $crate::mux_cfg! { #[cfg(mtl)] @@ -76,7 +96,15 @@ macro_rules! mux_enum { fn mtl(&self) -> &$mtl { match self { $name::Mtl(x) => x, - _ => panic!("downcast error") + } + } + } + $crate::mux_cfg! { + #[cfg(mtl)] + #[allow(unused)] + fn mtl_mut(&mut self) -> &mut $mtl { + match self { + $name::Mtl(x) => x, } } } diff --git a/piet-gpu-hal/src/metal.rs b/piet-gpu-hal/src/metal.rs index f7a97d6..9818c75 100644 --- a/piet-gpu-hal/src/metal.rs +++ b/piet-gpu-hal/src/metal.rs @@ -200,7 +200,7 @@ impl crate::Device for MtlDevice { cmd_bufs: &[&Self::CmdBuf], wait_semaphores: &[&Self::Semaphore], signal_semaphores: &[&Self::Semaphore], - fence: Option<&Self::Fence>, + fence: Option<&mut Self::Fence>, ) -> Result<(), Error> { todo!() } @@ -251,7 +251,7 @@ impl crate::Device for MtlDevice { todo!() } - unsafe fn wait_and_reset(&self, fences: &[&Self::Fence]) -> Result<(), Error> { + unsafe fn wait_and_reset(&self, fences: &[&mut Self::Fence]) -> Result<(), Error> { todo!() } diff --git a/piet-gpu-hal/src/mux.rs b/piet-gpu-hal/src/mux.rs index 9e13a87..90be57a 100644 --- a/piet-gpu-hal/src/mux.rs +++ b/piet-gpu-hal/src/mux.rs @@ -228,31 +228,29 @@ impl Device { } } - pub unsafe fn wait_and_reset(&self, fences: &[&Fence]) -> Result<(), Error> { + // Consider changing Vec to iterator (as is done in gfx-hal) + pub unsafe fn wait_and_reset(&self, fences: Vec<&mut Fence>) -> Result<(), Error> { mux_match! { self; Device::Vk(d) => { - let fences = fences - .iter() - .copied() - .map(Fence::vk) + let mut fences = fences + .into_iter() + .map(|f| f.vk_mut()) .collect::>(); - d.wait_and_reset(&*fences) + d.wait_and_reset(&mut fences) } Device::Dx12(d) => { - let fences = fences - .iter() - .copied() - .map(Fence::dx12) + let mut fences = fences + .into_iter() + .map(|f| f.dx12_mut()) .collect::>(); - d.wait_and_reset(&*fences) + d.wait_and_reset(&mut fences) } Device::Mtl(d) => { - let fences = fences - .iter() - .copied() - .map(Fence::mtl) + let mut fences = fences + .into_iter() + .map(|f| f.mtl_mut()) .collect::>(); - d.wait_and_reset(&*fences) + d.wait_and_reset(&mut fences) } } } @@ -318,7 +316,7 @@ impl Device { cmd_bufs: &[&CmdBuf], wait_semaphores: &[&Semaphore], signal_semaphores: &[&Semaphore], - fence: Option<&Fence>, + fence: Option<&mut Fence>, ) -> Result<(), Error> { mux_match! { self; Device::Vk(d) => d.run_cmd_bufs( @@ -336,7 +334,7 @@ impl Device { .copied() .map(Semaphore::vk) .collect::>(), - fence.map(Fence::vk), + fence.map(Fence::vk_mut), ), Device::Dx12(d) => d.run_cmd_bufs( &cmd_bufs @@ -353,7 +351,7 @@ impl Device { .copied() .map(Semaphore::dx12) .collect::>(), - fence.map(Fence::dx12), + fence.map(Fence::dx12_mut_mut), ), Device::Mtl(d) => d.run_cmd_bufs( &cmd_bufs @@ -370,7 +368,7 @@ impl Device { .copied() .map(Semaphore::mtl) .collect::>(), - fence.map(Fence::mtl), + fence.map(Fence::mtl_mut), ), } } diff --git a/piet-gpu-hal/src/vulkan.rs b/piet-gpu-hal/src/vulkan.rs index 619c6bd..4d73c2f 100644 --- a/piet-gpu-hal/src/vulkan.rs +++ b/piet-gpu-hal/src/vulkan.rs @@ -619,12 +619,11 @@ impl crate::Device for VkDevice { Ok(device.create_semaphore(&vk::SemaphoreCreateInfo::default(), None)?) } - unsafe fn wait_and_reset(&self, fences: &[&Self::Fence]) -> Result<(), Error> { + unsafe fn wait_and_reset(&self, fences: &[&mut Self::Fence]) -> Result<(), Error> { let device = &self.device.device; let fences = fences .iter() - .copied() - .copied() + .map(|f| **f) .collect::>(); device.wait_for_fences(&fences, true, !0)?; device.reset_fences(&fences)?; @@ -718,7 +717,7 @@ impl crate::Device for VkDevice { cmd_bufs: &[&CmdBuf], wait_semaphores: &[&Self::Semaphore], signal_semaphores: &[&Self::Semaphore], - fence: Option<&Self::Fence>, + fence: Option<&mut Self::Fence>, ) -> Result<(), Error> { let device = &self.device.device;