[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) self.queue_write_buffer::<A>(device.into_queue_id(), id, range.start, &bin)
.unwrap(); .unwrap();
} else { } else {
self.device_wait_for_buffer::<A>(device, id).unwrap(); self.device_set_buffer_data::<A>(id, range.start, &bin[..size])
self.device_set_buffer_sub_data::<A>(device, id, range.start, &bin[..size])
.unwrap(); .unwrap();
} }
} }

View File

@ -30,8 +30,7 @@ use wgt::{BufferAddress, TextureFormat};
use std::{ use std::{
borrow::Cow, borrow::Cow,
iter, ptr::NonNull,
ptr::{self, NonNull},
sync::{atomic::Ordering, Arc}, sync::{atomic::Ordering, Arc},
}; };
@ -252,70 +251,31 @@ impl Global {
} }
#[cfg(feature = "replay")] #[cfg(feature = "replay")]
pub fn device_wait_for_buffer<A: HalApi>( pub fn device_set_buffer_data<A: HalApi>(
&self, &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, buffer_id: id::BufferId,
offset: BufferAddress, offset: BufferAddress,
data: &[u8], data: &[u8],
) -> BufferAccessResult { ) -> BufferAccessResult {
profiling::scope!("Device::set_buffer_sub_data");
let hub = A::hub(self); let hub = A::hub(self);
let device = hub
.devices
.get(device_id)
.map_err(|_| DeviceError::InvalidDeviceId)?;
let buffer = hub let buffer = hub
.buffers .buffers
.get(buffer_id) .get(buffer_id)
.map_err(|_| BufferAccessError::InvalidBufferId(buffer_id))?; .map_err(|_| BufferAccessError::InvalidBufferId(buffer_id))?;
#[cfg(feature = "trace")] let device = &buffer.device;
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,
});
}
device.check_is_valid()?; device.check_is_valid()?;
buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?; 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 snatch_guard = device.snatchable_lock.read();
let raw_buf = buffer.try_raw(&snatch_guard)?; let raw_buf = buffer.try_raw(&snatch_guard)?;
@ -324,11 +284,12 @@ impl Global {
.raw() .raw()
.map_buffer(raw_buf, offset..offset + data.len() as u64) .map_buffer(raw_buf, offset..offset + data.len() as u64)
.map_err(DeviceError::from)?; .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 { if !mapping.is_coherent {
device device.raw().flush_mapped_ranges(
.raw() raw_buf,
.flush_mapped_ranges(raw_buf, iter::once(offset..offset + data.len() as u64)); std::iter::once(offset..offset + data.len() as u64),
);
} }
device.raw().unmap_buffer(raw_buf); device.raw().unmap_buffer(raw_buf);
} }

View File

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