Skip to content

Commit

Permalink
Add ManualFence to wgpu-rs
Browse files Browse the repository at this point in the history
ManualFence is returned by Queue::submit.
It allows a user to wait for the fence created therein,
i.e. the one stored in the associated wgpu_native::ActiveSubmission.

This is a hackish solution to allow Buffer::map_read_async usage
(since it may read too soon otherwise, i.e. obtain corrupt data).
  • Loading branch information
torss committed Apr 18, 2019
1 parent f5ad767 commit 519822a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
20 changes: 19 additions & 1 deletion wgpu-native/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl<B: hal::Backend> DestroyedResources<B> {
value: buffer,
ref_count,
});
self.ready_to_map.push(buffer); // See https://github.com/gfx-rs/wgpu/pull/123
}

/// Returns the last submission index that is done.
Expand Down Expand Up @@ -1087,7 +1088,7 @@ pub extern "C" fn wgpu_queue_submit(
queue_id: QueueId,
command_buffer_ptr: *const CommandBufferId,
command_buffer_count: usize,
) {
) -> usize {
let command_buffer_ids =
unsafe { slice::from_raw_parts(command_buffer_ptr, command_buffer_count) };

Expand Down Expand Up @@ -1237,6 +1238,23 @@ pub extern "C" fn wgpu_queue_submit(
let cmd_buf = HUB.command_buffers.unregister(cmb_id);
device.com_allocator.after_submit(cmd_buf, submit_index);
}

submit_index
}

pub fn queue_wait_for_fence(
queue_id: QueueId,
submit_index: usize,
) {
let device_guard = HUB.devices.read();
let device = &device_guard[queue_id];

let mut destroyed = device.destroyed.lock();
if let Some(active_submission) = destroyed.active.iter_mut().find(|a| a.index == submit_index) {
unsafe {
device.raw.wait_for_fence(&active_submission.fence, !0).unwrap();
}
}
}

pub fn device_create_render_pipeline(
Expand Down
17 changes: 15 additions & 2 deletions wgpu-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ pub struct SwapChainOutput<'a> {
swap_chain_id: &'a wgn::SwapChainId,
}

pub struct ManualFence {
queue_id: wgn::QueueId,
submit_index: usize,
}

impl ManualFence {
pub fn wait(&self) {
wgn::queue_wait_for_fence(self.queue_id, self.submit_index);
}
}

pub struct BufferCopyView<'a> {
pub buffer: &'a Buffer,
pub offset: u32,
Expand Down Expand Up @@ -856,17 +867,19 @@ impl<'a> Drop for ComputePass<'a> {
}

impl<'a> Queue<'a> {
pub fn submit(&mut self, command_buffers: &[CommandBuffer]) {
pub fn submit(&mut self, command_buffers: &[CommandBuffer]) -> ManualFence {
self.temp.command_buffers.clear();
self.temp
.command_buffers
.extend(command_buffers.iter().map(|cb| cb.id));

wgn::wgpu_queue_submit(
let submit_index = wgn::wgpu_queue_submit(
self.id,
self.temp.command_buffers.as_ptr(),
command_buffers.len(),
);

ManualFence { queue_id: self.id, submit_index }
}
}

Expand Down

0 comments on commit 519822a

Please sign in to comment.