Skip to content

Commit 3028972

Browse files
authored
buffer_map_async should use offset + size (#5185)
1 parent 990324f commit 3028972

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

deno_webgpu/buffer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub async fn op_webgpu_buffer_get_map_async(
9898
// TODO(lucacasonato): error handling
9999
let maybe_err = gfx_select!(buffer => instance.buffer_map_async(
100100
buffer,
101-
offset..(offset + size),
101+
offset,
102+
Some(size),
102103
wgpu_core::resource::BufferMapOperation {
103104
host: match mode {
104105
1 => wgpu_core::device::HostMap::Read,

player/tests/test.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ impl Test<'_> {
131131
let buffer = wgc::id::Id::zip(expect.buffer.index, expect.buffer.epoch, backend);
132132
wgc::gfx_select!(device_id => global.buffer_map_async(
133133
buffer,
134-
expect.offset .. expect.offset+expect.data.len() as wgt::BufferAddress,
134+
expect.offset,
135+
Some(expect.data.len() as u64),
135136
wgc::resource::BufferMapOperation {
136137
host: wgc::device::HostMap::Read,
137138
callback: Some(wgc::resource::BufferMapCallback::from_rust(

wgpu-core/src/device/global.rs

+42-22
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ use wgt::{BufferAddress, TextureFormat};
2626

2727
use std::{
2828
borrow::Cow,
29-
iter,
30-
ops::Range,
31-
ptr,
29+
iter, ptr,
3230
sync::{atomic::Ordering, Arc},
3331
};
3432

@@ -2336,15 +2334,18 @@ impl Global {
23362334
pub fn buffer_map_async<A: HalApi>(
23372335
&self,
23382336
buffer_id: id::BufferId,
2339-
range: Range<BufferAddress>,
2337+
offset: BufferAddress,
2338+
size: Option<BufferAddress>,
23402339
op: BufferMapOperation,
23412340
) -> BufferAccessResult {
2342-
api_log!("Buffer::map_async {buffer_id:?} range {range:?} op: {op:?}");
2341+
api_log!("Buffer::map_async {buffer_id:?} offset {offset:?} size {size:?} op: {op:?}");
23432342

23442343
// User callbacks must not be called while holding buffer_map_async_inner's locks, so we
23452344
// defer the error callback if it needs to be called immediately (typically when running
23462345
// into errors).
2347-
if let Err((mut operation, err)) = self.buffer_map_async_inner::<A>(buffer_id, range, op) {
2346+
if let Err((mut operation, err)) =
2347+
self.buffer_map_async_inner::<A>(buffer_id, offset, size, op)
2348+
{
23482349
if let Some(callback) = operation.callback.take() {
23492350
callback.call(Err(err.clone()));
23502351
}
@@ -2360,7 +2361,8 @@ impl Global {
23602361
fn buffer_map_async_inner<A: HalApi>(
23612362
&self,
23622363
buffer_id: id::BufferId,
2363-
range: Range<BufferAddress>,
2364+
offset: BufferAddress,
2365+
size: Option<BufferAddress>,
23642366
op: BufferMapOperation,
23652367
) -> Result<(), (BufferMapOperation, BufferAccessError)> {
23662368
profiling::scope!("Buffer::map_async");
@@ -2372,22 +2374,43 @@ impl Global {
23722374
HostMap::Write => (wgt::BufferUsages::MAP_WRITE, hal::BufferUses::MAP_WRITE),
23732375
};
23742376

2375-
if range.start % wgt::MAP_ALIGNMENT != 0 || range.end % wgt::COPY_BUFFER_ALIGNMENT != 0 {
2376-
return Err((op, BufferAccessError::UnalignedRange));
2377-
}
2378-
23792377
let buffer = {
2380-
let buffer = hub
2381-
.buffers
2382-
.get(buffer_id)
2383-
.map_err(|_| BufferAccessError::Invalid);
2378+
let buffer = hub.buffers.get(buffer_id);
23842379

23852380
let buffer = match buffer {
23862381
Ok(b) => b,
2387-
Err(e) => {
2388-
return Err((op, e));
2382+
Err(_) => {
2383+
return Err((op, BufferAccessError::Invalid));
23892384
}
23902385
};
2386+
{
2387+
let snatch_guard = buffer.device.snatchable_lock.read();
2388+
if buffer.is_destroyed(&snatch_guard) {
2389+
return Err((op, BufferAccessError::Destroyed));
2390+
}
2391+
}
2392+
2393+
let range_size = if let Some(size) = size {
2394+
size
2395+
} else if offset > buffer.size {
2396+
0
2397+
} else {
2398+
buffer.size - offset
2399+
};
2400+
2401+
if offset % wgt::MAP_ALIGNMENT != 0 {
2402+
return Err((op, BufferAccessError::UnalignedOffset { offset }));
2403+
}
2404+
if range_size % wgt::COPY_BUFFER_ALIGNMENT != 0 {
2405+
return Err((op, BufferAccessError::UnalignedRangeSize { range_size }));
2406+
}
2407+
2408+
let range = offset..(offset + range_size);
2409+
2410+
if range.start % wgt::MAP_ALIGNMENT != 0 || range.end % wgt::COPY_BUFFER_ALIGNMENT != 0
2411+
{
2412+
return Err((op, BufferAccessError::UnalignedRange));
2413+
}
23912414

23922415
let device = &buffer.device;
23932416
if !device.is_valid() {
@@ -2417,11 +2440,6 @@ impl Global {
24172440
));
24182441
}
24192442

2420-
let snatch_guard = device.snatchable_lock.read();
2421-
if buffer.is_destroyed(&snatch_guard) {
2422-
return Err((op, BufferAccessError::Destroyed));
2423-
}
2424-
24252443
{
24262444
let map_state = &mut *buffer.map_state.lock();
24272445
*map_state = match *map_state {
@@ -2442,6 +2460,8 @@ impl Global {
24422460
};
24432461
}
24442462

2463+
let snatch_guard = buffer.device.snatchable_lock.read();
2464+
24452465
{
24462466
let mut trackers = buffer.device.as_ref().trackers.lock();
24472467
trackers.buffers.set_single(&buffer, internal_use);

wgpu/src/backend/wgpu_core.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,8 @@ impl crate::Context for ContextWgpuCore {
14531453
))),
14541454
};
14551455

1456-
match wgc::gfx_select!(buffer => self.0.buffer_map_async(*buffer, range, operation)) {
1456+
match wgc::gfx_select!(buffer => self.0.buffer_map_async(*buffer, range.start, Some(range.end-range.start), operation))
1457+
{
14571458
Ok(()) => (),
14581459
Err(cause) => {
14591460
self.handle_error_nolabel(&buffer_data.error_sink, cause, "Buffer::map_async")

0 commit comments

Comments
 (0)