[player] simplify sync buffer writing

This commit is contained in:
teoxoy 2024-07-12 13:55:50 +02:00 committed by Teodor Tanasoaia
parent 7502e65213
commit 650054bbcd
3 changed files with 20 additions and 67 deletions

View File

@ -330,8 +330,7 @@ impl GlobalPlay for wgc::global::Global {
self.queue_write_buffer::<A>(device.into_queue_id(), id, range.start, &bin)
.unwrap();
} else {
self.device_wait_for_buffer::<A>(device, id).unwrap();
self.device_set_buffer_sub_data::<A>(device, id, range.start, &bin[..size])
self.device_set_buffer_data::<A>(id, range.start, &bin[..size])
.unwrap();
}
}

View File

@ -30,8 +30,7 @@ use wgt::{BufferAddress, TextureFormat};
use std::{
borrow::Cow,
iter,
ptr::{self, NonNull},
ptr::NonNull,
sync::{atomic::Ordering, Arc},
};
@ -252,70 +251,31 @@ impl Global {
}
#[cfg(feature = "replay")]
pub fn device_wait_for_buffer<A: HalApi>(
pub fn device_set_buffer_data<A: HalApi>(
&self,
device_id: DeviceId,
buffer_id: id::BufferId,
) -> Result<(), WaitIdleError> {
let hub = A::hub(self);
let device = hub
.devices
.get(device_id)
.map_err(|_| DeviceError::InvalidDeviceId)?;
let buffer = match hub.buffers.get(buffer_id) {
Ok(buffer) => buffer,
Err(_) => return Ok(()),
};
let last_submission = device
.lock_life()
.get_buffer_latest_submission_index(&buffer);
if let Some(last_submission) = last_submission {
device.wait_for_submit(last_submission)
} else {
Ok(())
}
}
#[doc(hidden)]
pub fn device_set_buffer_sub_data<A: HalApi>(
&self,
device_id: DeviceId,
buffer_id: id::BufferId,
offset: BufferAddress,
data: &[u8],
) -> BufferAccessResult {
profiling::scope!("Device::set_buffer_sub_data");
let hub = A::hub(self);
let device = hub
.devices
.get(device_id)
.map_err(|_| DeviceError::InvalidDeviceId)?;
let buffer = hub
.buffers
.get(buffer_id)
.map_err(|_| BufferAccessError::InvalidBufferId(buffer_id))?;
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
let data_path = trace.make_binary("bin", data);
trace.add(trace::Action::WriteBuffer {
id: buffer_id,
data: data_path,
range: offset..offset + data.len() as BufferAddress,
queued: false,
});
}
let device = &buffer.device;
device.check_is_valid()?;
buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
//assert!(buffer isn't used by the GPU);
let last_submission = device
.lock_life()
.get_buffer_latest_submission_index(&buffer);
if let Some(last_submission) = last_submission {
device.wait_for_submit(last_submission)?;
}
let snatch_guard = device.snatchable_lock.read();
let raw_buf = buffer.try_raw(&snatch_guard)?;
@ -324,11 +284,12 @@ impl Global {
.raw()
.map_buffer(raw_buf, offset..offset + data.len() as u64)
.map_err(DeviceError::from)?;
ptr::copy_nonoverlapping(data.as_ptr(), mapping.ptr.as_ptr(), data.len());
std::ptr::copy_nonoverlapping(data.as_ptr(), mapping.ptr.as_ptr(), data.len());
if !mapping.is_coherent {
device
.raw()
.flush_mapped_ranges(raw_buf, iter::once(offset..offset + data.len() as u64));
device.raw().flush_mapped_ranges(
raw_buf,
std::iter::once(offset..offset + data.len() as u64),
);
}
device.raw().unmap_buffer(raw_buf);
}

View File

@ -3478,24 +3478,17 @@ impl<A: HalApi> Device<A> {
pub(crate) fn wait_for_submit(
&self,
submission_index: crate::SubmissionIndex,
) -> Result<(), WaitIdleError> {
) -> Result<(), DeviceError> {
let guard = self.fence.read();
let fence = guard.as_ref().unwrap();
let last_done_index = unsafe {
self.raw
.as_ref()
.unwrap()
.get_fence_value(fence)
.map_err(DeviceError::from)?
};
let last_done_index = unsafe { self.raw.as_ref().unwrap().get_fence_value(fence)? };
if last_done_index < submission_index {
log::info!("Waiting for submission {:?}", submission_index);
unsafe {
self.raw
.as_ref()
.unwrap()
.wait(fence, submission_index, !0)
.map_err(DeviceError::from)?
.wait(fence, submission_index, !0)?
};
drop(guard);
let closures = self