Skip to content

Commit

Permalink
Add HAL method to update buffer descriptor
Browse files Browse the repository at this point in the history
This is WIP because only the Metal implementation is added.

Part of the work for #175
  • Loading branch information
raphlinus committed Jun 23, 2022
1 parent 60d197b commit 9508197
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
30 changes: 30 additions & 0 deletions piet-gpu-hal/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::CmdBuf, Error>;

/// If the command buffer was submitted, it must complete before this is called.
Expand Down
24 changes: 24 additions & 0 deletions piet-gpu-hal/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryPool, Error> {
self.0.device.create_query_pool(n_queries)
Expand Down
18 changes: 18 additions & 0 deletions piet-gpu-hal/src/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::CmdBuf, Error> {
let cmd_queue = self.cmd_queue.lock().unwrap();
// A discussion about autorelease pools.
Expand Down
26 changes: 26 additions & 0 deletions piet-gpu-hal/src/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CmdBuf, Error> {
mux_match! { self;
Device::Vk(d) => d.create_cmd_buf().map(CmdBuf::Vk),
Expand Down

0 comments on commit 9508197

Please sign in to comment.