gfx-rs/wgpu-rs

Empty buffer slice's mapped range isn't empty

Closed this issue · 0 comments

Code:

fn main() {
    let (device, _queue) = futures::executor::block_on(async {
        let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
        instance.request_adapter(&wgpu::RequestAdapterOptions::default())
            .await
            .unwrap()
            .request_device(&wgpu::DeviceDescriptor::default(), None)
            .await
            .unwrap()
    });

    let buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: 4,
        usage: wgpu::BufferUsage::empty(),
        mapped_at_creation: true,
    });

    assert_eq!(
        buffer.slice(..0).get_mapped_range().len(),
        0,
    );
}

Expected outcome: program exits successfully, or panics in slice/get_mapped_range if empty buffer slices are not allowed
Actual outcome:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `4`,
 right: `0`', src/main.rs:19:5

Creating an empty slice at any starting point always maps the whole remainder of the buffer (so if the starting point is the end of the buffer the the mapped range is empty as expected).

Looks like the problem is here:

wgpu-rs/src/lib.rs

Lines 1682 to 1688 in f882abc

let size = match bounds.end_bound() {
Bound::Included(&bound) => BufferSize::new(bound + 1 - offset),
Bound::Excluded(&bound) => BufferSize::new(bound - offset),
Bound::Unbounded => None,
};
(offset, size)

If offset is equal to bound, BufferSize::new (which is NonZeroU64::new) returns None, producing the same result as if the range was unbounded.