diff --git a/piet-gpu-hal/src/backend.rs b/piet-gpu-hal/src/backend.rs index f2c67a1..d3a1f09 100644 --- a/piet-gpu-hal/src/backend.rs +++ b/piet-gpu-hal/src/backend.rs @@ -98,6 +98,36 @@ pub trait Device: Sized { builder.build(self, pipeline) } + /// Update a descriptor in a descriptor set. + /// + /// The index is the same as the binding number in Vulkan. + /// + /// # Safety + /// + /// The descriptor set must not be used in any in-flight command buffer. The index must be valid. + /// The resource type must match that at descriptor set creation time. + unsafe fn update_buffer_descriptor( + &self, + ds: &mut Self::DescriptorSet, + index: u32, + buf: &Self::Buffer, + ); + + /// Update a descriptor in a descriptor set. + /// + /// The index is the same as the binding number in Vulkan. + /// + /// # Safety + /// + /// The descriptor set must not be used in any in-flight command buffer. The index must be valid. + /// The resource type must match that at descriptor set creation time. + unsafe fn update_image_descriptor( + &self, + ds: &mut Self::DescriptorSet, + index: u32, + image: &Self::Image, + ); + fn create_cmd_buf(&self) -> Result; /// If the command buffer was submitted, it must complete before this is called. diff --git a/piet-gpu-hal/src/hub.rs b/piet-gpu-hal/src/hub.rs index ea17754..bbb52c1 100644 --- a/piet-gpu-hal/src/hub.rs +++ b/piet-gpu-hal/src/hub.rs @@ -366,6 +366,30 @@ impl Session { DescriptorSetBuilder(self.0.device.descriptor_set_builder()) } + /// Update a buffer in a descriptor set. + pub unsafe fn update_buffer_descriptor( + &self, + ds: &mut DescriptorSet, + index: u32, + buffer: &Buffer, + ) { + self.0 + .device + .update_buffer_descriptor(ds, index, &buffer.0.buffer) + } + + /// Update an image in a descriptor set. + pub unsafe fn update_image_descriptor( + &self, + ds: &mut DescriptorSet, + index: u32, + image: &Image, + ) { + self.0 + .device + .update_image_descriptor(ds, index, &image.0.image) + } + /// Create a query pool for timestamp queries. pub fn create_query_pool(&self, n_queries: u32) -> Result { self.0.device.create_query_pool(n_queries) diff --git a/piet-gpu-hal/src/metal.rs b/piet-gpu-hal/src/metal.rs index 03a23b0..2918df0 100644 --- a/piet-gpu-hal/src/metal.rs +++ b/piet-gpu-hal/src/metal.rs @@ -385,6 +385,24 @@ impl crate::backend::Device for MtlDevice { DescriptorSetBuilder::default() } + unsafe fn update_buffer_descriptor( + &self, + ds: &mut Self::DescriptorSet, + index: u32, + buf: &Self::Buffer, + ) { + ds.buffers[index as usize] = buf.clone(); + } + + unsafe fn update_image_descriptor( + &self, + ds: &mut Self::DescriptorSet, + index: u32, + image: &Self::Image, + ) { + ds.images[index as usize - ds.buffers.len()] = image.clone(); + } + fn create_cmd_buf(&self) -> Result { let cmd_queue = self.cmd_queue.lock().unwrap(); // A discussion about autorelease pools. diff --git a/piet-gpu-hal/src/mux.rs b/piet-gpu-hal/src/mux.rs index 9795193..97c65c8 100644 --- a/piet-gpu-hal/src/mux.rs +++ b/piet-gpu-hal/src/mux.rs @@ -391,6 +391,32 @@ impl Device { } } + pub unsafe fn update_buffer_descriptor( + &self, + ds: &mut DescriptorSet, + index: u32, + buffer: &Buffer, + ) { + mux_match! { self; + Device::Vk(d) => d.update_buffer_descriptor(ds.vk_mut(), index, buffer.vk()), + Device::Dx12(d) => d.update_buffer_descriptor(ds.dx12_mut(), index, buffer.dx12()), + Device::Mtl(d) => d.update_buffer_descriptor(ds.mtl_mut(), index, buffer.mtl()), + } + } + + pub unsafe fn update_image_descriptor( + &self, + ds: &mut DescriptorSet, + index: u32, + image: &Image, + ) { + mux_match! { self; + Device::Vk(d) => d.update_image_descriptor(ds.vk_mut(), index, image.vk()), + Device::Dx12(d) => d.update_image_descriptor(ds.dx12_mut(), index, image.dx12()), + Device::Mtl(d) => d.update_image_descriptor(ds.mtl_mut(), index, image.mtl()), + } + } + pub fn create_cmd_buf(&self) -> Result { mux_match! { self; Device::Vk(d) => d.create_cmd_buf().map(CmdBuf::Vk),