mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-29 01:23:57 +01:00
Move set_buffer_data and get_mapped_range out of Global
This commit is contained in:
1 parent
f314b1611a
commit
21ea8f2979
3 files changed
+146
-62
No files matched your search
@@ -11,7 +11,7 @@ use crate::{
|
||||
},
|
||||
command::{self, CommandEncoder},
|
||||
conv,
|
||||
device::{bgl, life::WaitIdleError, DeviceError, DeviceLostClosure},
|
||||
device::{life::WaitIdleError, DeviceError, DeviceLostClosure},
|
||||
global::Global,
|
||||
id::{self, AdapterId, DeviceId, QueueId, SurfaceId},
|
||||
instance::{self, Adapter, Surface},
|
||||
@@ -222,51 +222,9 @@ impl Global {
|
||||
offset: BufferAddress,
|
||||
data: &[u8],
|
||||
) -> BufferAccessResult {
|
||||
use crate::resource::RawResourceAccess;
|
||||
|
||||
let hub = &self.hub;
|
||||
|
||||
let buffer = hub.buffers.get(buffer_id).get()?;
|
||||
|
||||
let buffer = self.hub.buffers.get(buffer_id).get()?;
|
||||
let device = &buffer.device;
|
||||
|
||||
device.check_is_valid()?;
|
||||
buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
|
||||
|
||||
let last_submission = device.get_queue().and_then(|queue| {
|
||||
queue
|
||||
.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)?;
|
||||
|
||||
let mapping = unsafe {
|
||||
device
|
||||
.raw()
|
||||
.map_buffer(raw_buf, offset..offset + data.len() as u64)
|
||||
}
|
||||
.map_err(|e| device.handle_hal_error(e))?;
|
||||
|
||||
unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), mapping.ptr.as_ptr(), data.len()) };
|
||||
|
||||
if !mapping.is_coherent {
|
||||
#[allow(clippy::single_range_in_vec_init)]
|
||||
unsafe {
|
||||
device
|
||||
.raw()
|
||||
.flush_mapped_ranges(raw_buf, &[offset..offset + data.len() as u64])
|
||||
};
|
||||
}
|
||||
|
||||
unsafe { device.raw().unmap_buffer(raw_buf) };
|
||||
|
||||
Ok(())
|
||||
device.set_buffer_data(&buffer, offset, data)
|
||||
}
|
||||
|
||||
pub fn buffer_destroy(&self, buffer_id: id::BufferId) {
|
||||
@@ -693,21 +651,7 @@ impl Global {
|
||||
break 'error e.into();
|
||||
}
|
||||
|
||||
let entry_map = match bgl::EntryMap::from_entries(&desc.entries) {
|
||||
Ok(map) => map,
|
||||
Err(e) => break 'error e,
|
||||
};
|
||||
|
||||
let bgl_result = device.bgl_pool.get_or_init(entry_map, |entry_map| {
|
||||
let bgl =
|
||||
device.create_bind_group_layout(&desc.label, entry_map, bgl::Origin::Pool)?;
|
||||
bgl.exclusive_pipeline
|
||||
.set(binding_model::ExclusivePipeline::None)
|
||||
.unwrap();
|
||||
Ok(bgl)
|
||||
});
|
||||
|
||||
let layout = match bgl_result {
|
||||
let layout = match device.create_bind_group_layout(desc) {
|
||||
Ok(layout) => layout,
|
||||
Err(e) => break 'error e,
|
||||
};
|
||||
|
||||
@@ -1029,6 +1029,54 @@ impl Device {
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
#[cfg(feature = "replay")]
|
||||
pub fn set_buffer_data(
|
||||
self: &Arc<Self>,
|
||||
buffer: &Arc<Buffer>,
|
||||
offset: wgt::BufferAddress,
|
||||
data: &[u8],
|
||||
) -> resource::BufferAccessResult {
|
||||
use crate::resource::RawResourceAccess;
|
||||
|
||||
let device = &buffer.device;
|
||||
|
||||
device.check_is_valid()?;
|
||||
buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
|
||||
|
||||
let last_submission = device
|
||||
.get_queue()
|
||||
.and_then(|queue| queue.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)?;
|
||||
|
||||
let mapping = unsafe {
|
||||
device
|
||||
.raw()
|
||||
.map_buffer(raw_buf, offset..offset + data.len() as u64)
|
||||
}
|
||||
.map_err(|e| device.handle_hal_error(e))?;
|
||||
|
||||
unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), mapping.ptr.as_ptr(), data.len()) };
|
||||
|
||||
if !mapping.is_coherent {
|
||||
#[allow(clippy::single_range_in_vec_init)]
|
||||
unsafe {
|
||||
device
|
||||
.raw()
|
||||
.flush_mapped_ranges(raw_buf, &[offset..offset + data.len() as u64])
|
||||
};
|
||||
}
|
||||
|
||||
unsafe { device.raw().unmap_buffer(raw_buf) };
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn create_texture_from_hal(
|
||||
self: &Arc<Self>,
|
||||
hal_texture: Box<dyn hal::DynTexture>,
|
||||
@@ -2315,7 +2363,33 @@ impl Device {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn create_bind_group_layout(
|
||||
pub fn create_bind_group_layout(
|
||||
self: &Arc<Self>,
|
||||
desc: &binding_model::BindGroupLayoutDescriptor,
|
||||
) -> Result<Arc<BindGroupLayout>, binding_model::CreateBindGroupLayoutError> {
|
||||
// this check can't go in the body of `create_bind_group_layout_internal` since the closure might not get called
|
||||
self.check_is_valid()?;
|
||||
|
||||
let entry_map = bgl::EntryMap::from_entries(&desc.entries)?;
|
||||
|
||||
let bgl_result = self.bgl_pool.get_or_init(entry_map, |entry_map| {
|
||||
let bgl =
|
||||
self.create_bind_group_layout_internal(&desc.label, entry_map, bgl::Origin::Pool)?;
|
||||
bgl.exclusive_pipeline
|
||||
.set(binding_model::ExclusivePipeline::None)
|
||||
.unwrap();
|
||||
Ok(bgl)
|
||||
});
|
||||
|
||||
match bgl_result {
|
||||
Ok(layout) => Ok(layout),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal function exposed for use by `player` crate only.
|
||||
#[doc(hidden)]
|
||||
pub fn create_bind_group_layout_internal(
|
||||
self: &Arc<Self>,
|
||||
label: &crate::Label,
|
||||
entry_map: bgl::EntryMap,
|
||||
@@ -3518,7 +3592,7 @@ impl Device {
|
||||
match unique_bind_group_layouts.entry(bgl_entry_map) {
|
||||
hashbrown::hash_map::Entry::Occupied(v) => Ok(Arc::clone(v.get())),
|
||||
hashbrown::hash_map::Entry::Vacant(e) => {
|
||||
match self.create_bind_group_layout(
|
||||
match self.create_bind_group_layout_internal(
|
||||
&None,
|
||||
e.key().clone(),
|
||||
bgl::Origin::Derived,
|
||||
|
||||
@@ -677,6 +677,72 @@ impl Buffer {
|
||||
Ok(submit_index)
|
||||
}
|
||||
|
||||
pub fn get_mapped_range(
|
||||
self: &Arc<Self>,
|
||||
offset: wgt::BufferAddress,
|
||||
size: Option<wgt::BufferAddress>,
|
||||
) -> Result<(NonNull<u8>, u64), BufferAccessError> {
|
||||
{
|
||||
let snatch_guard = self.device.snatchable_lock.read();
|
||||
self.check_destroyed(&snatch_guard)?;
|
||||
}
|
||||
|
||||
let range_size = if let Some(size) = size {
|
||||
size
|
||||
} else {
|
||||
self.size.saturating_sub(offset)
|
||||
};
|
||||
|
||||
if offset % wgt::MAP_ALIGNMENT != 0 {
|
||||
return Err(BufferAccessError::UnalignedOffset { offset });
|
||||
}
|
||||
if range_size % wgt::COPY_BUFFER_ALIGNMENT != 0 {
|
||||
return Err(BufferAccessError::UnalignedRangeSize { range_size });
|
||||
}
|
||||
let map_state = &*self.map_state.lock();
|
||||
match *map_state {
|
||||
BufferMapState::Init { ref staging_buffer } => {
|
||||
// offset (u64) can not be < 0, so no need to validate the lower bound
|
||||
if offset + range_size > self.size {
|
||||
return Err(BufferAccessError::OutOfBoundsOverrun {
|
||||
index: offset + range_size - 1,
|
||||
max: self.size,
|
||||
});
|
||||
}
|
||||
let ptr = unsafe { staging_buffer.ptr() };
|
||||
let ptr = unsafe { NonNull::new_unchecked(ptr.as_ptr().offset(offset as isize)) };
|
||||
Ok((ptr, range_size))
|
||||
}
|
||||
BufferMapState::Active {
|
||||
ref mapping,
|
||||
ref range,
|
||||
..
|
||||
} => {
|
||||
if offset < range.start {
|
||||
return Err(BufferAccessError::OutOfBoundsUnderrun {
|
||||
index: offset,
|
||||
min: range.start,
|
||||
});
|
||||
}
|
||||
if offset + range_size > range.end {
|
||||
return Err(BufferAccessError::OutOfBoundsOverrun {
|
||||
index: offset + range_size - 1,
|
||||
max: range.end,
|
||||
});
|
||||
}
|
||||
// ptr points to the beginning of the range we mapped in map_async
|
||||
// rather than the beginning of the buffer.
|
||||
let relative_offset = (offset - range.start) as isize;
|
||||
unsafe {
|
||||
Ok((
|
||||
NonNull::new_unchecked(mapping.ptr.as_ptr().offset(relative_offset)),
|
||||
range_size,
|
||||
))
|
||||
}
|
||||
}
|
||||
BufferMapState::Idle | BufferMapState::Waiting(_) => Err(BufferAccessError::NotMapped),
|
||||
}
|
||||
}
|
||||
/// This function returns [`None`] only if [`Self::map_state`] is not [`BufferMapState::Waiting`].
|
||||
#[must_use]
|
||||
pub(crate) fn map(&self, snatch_guard: &SnatchGuard) -> Option<BufferMapPendingClosure> {
|
||||
|
||||
Reference in new issue
Block a user