From f9da63923e46587e41318d37d633d54a053c696b Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Tue, 17 Oct 2023 00:24:19 +0200 Subject: [PATCH] Fix current tests --- tests/test_rs_basics.py | 15 ++++++--------- wgpu/backends/rs.py | 27 ++++++++++++++++----------- wgpu/base.py | 4 ++-- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/test_rs_basics.py b/tests/test_rs_basics.py index e60a04fe..a903a5bf 100644 --- a/tests/test_rs_basics.py +++ b/tests/test_rs_basics.py @@ -216,12 +216,6 @@ def test_buffer_init3(): device = wgpu.utils.get_default_device() data1 = b"abcdefghijkl" - # First fail - with raises(ValueError): - device.create_buffer( - mapped_at_creation=True, size=len(data1), usage=wgpu.BufferUsage.COPY_DST - ) - # Create buffer buf = device.create_buffer( size=len(data1), usage=wgpu.BufferUsage.COPY_DST | wgpu.BufferUsage.COPY_SRC @@ -556,7 +550,9 @@ def test_buffer_map_read_and_write(): # Upload data1 = b"abcdefghijkl" - buf1.map_write(data1) + buf1.map("write") + buf1.write_mapped(data1) + buf1.unmap() # Copy command_encoder = device.create_command_encoder() @@ -564,8 +560,9 @@ def test_buffer_map_read_and_write(): device.queue.submit([command_encoder.finish()]) # Download - data2 = buf2.map_read() - + buf2.map("read") + data2 = buf2.read_mapped() + buf2.unmap() assert data1 == data2 diff --git a/wgpu/backends/rs.py b/wgpu/backends/rs.py index 676065d8..d3f1a54b 100644 --- a/wgpu/backends/rs.py +++ b/wgpu/backends/rs.py @@ -823,16 +823,17 @@ def _create_buffer(self, label, size, usage, mapped_at_creation): mappedAtCreation=mapped_at_creation, # not used: nextInChain ) + map_state = ( + enums.BufferMapState.mapped + if mapped_at_creation + else enums.BufferMapState.unmapped + ) # H: WGPUBuffer f(WGPUDevice device, WGPUBufferDescriptor const * descriptor) id = libf.wgpuDeviceCreateBuffer(self._internal, struct) # Note that there is wgpuBufferGetSize and wgpuBufferGetUsage, # but we already know these, so they are kindof useless? # Return wrapped buffer - b = GPUBuffer(label, id, self, size, usage) - - if mapped_at_creation: - b._map_state = enums.BufferMapState.mapped - return b + return GPUBuffer(label, id, self, size, usage, map_state) def create_texture( self, @@ -1514,15 +1515,16 @@ def _check_range(self, offset, size): raise ValueError("Mapped offset must not be smaller than zero.") if size < 1: raise ValueError("Mapped size must be larger than zero.") - if offset + size >= self.size: + if offset + size > self.size: raise ValueError("Mapped range must not extend beyond total buffer size.") + return offset, size def map(self, mode, offset=0, size=None): # Check mode mode = mode.upper() if isinstance(mode, str) else mode - if mode in (enums.MapMode.READ, "READ"): + if mode in (flags.MapMode.READ, "READ"): map_mode = lib.WGPUMapMode_Read - elif mode in (enums.MapMode.WRITE, "WRITE"): + elif mode in (flags.MapMode.WRITE, "WRITE"): map_mode = lib.WGPUMapMode_Write # Check offset and size @@ -1532,6 +1534,8 @@ def map(self, mode, offset=0, size=None): if self._map_state != enums.BufferMapState.unmapped: raise RuntimeError("Can only map a buffer if its currently unmapped.") + status = 999 + @ffi.callback("void(WGPUBufferMapAsyncStatus, void*)") def callback(status_, user_data_p): nonlocal status @@ -1605,7 +1609,6 @@ def read_mapped(self, offset=0, size=None, *, copy=True): # Return view on the actually mapped data self._mapped_memoryviews.append(src_m) return src_m - # todo: cast to B? def write_mapped(self, data, offset=0, size=None): # Can we even write? @@ -2489,7 +2492,8 @@ def read_buffer(self, buffer, buffer_offset=0, size=None): self.submit([command_buffer]) # Download from mappable buffer - data = tmp_buffer.map_read() + tmp_buffer.map(flags.MapMode.READ) + data = tmp_buffer.read_mapped() tmp_buffer.destroy() return data @@ -2586,7 +2590,8 @@ def read_texture(self, source, data_layout, size): self.submit([command_buffer]) # Download from mappable buffer - data = tmp_buffer.map_read() + tmp_buffer.map(flags.MapMode.READ) + data = tmp_buffer.read_mapped() tmp_buffer.destroy() # Fix data strides if necessary diff --git a/wgpu/base.py b/wgpu/base.py index 0bf636fd..725cbd8b 100644 --- a/wgpu/base.py +++ b/wgpu/base.py @@ -930,11 +930,11 @@ class GPUBuffer(GPUObjectBase): copy data between buffers and textures. """ - def __init__(self, label, internal, device, size, usage): + def __init__(self, label, internal, device, size, usage, map_state): super().__init__(label, internal, device) self._size = size self._usage = usage - self._map_state = enums.BufferMapState.unmapped + self._map_state = map_state # IDL: readonly attribute GPUSize64Out size; @property