Skip to content

Commit

Permalink
Fix current tests
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Oct 16, 2023
1 parent 9f1cde8 commit f9da639
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
15 changes: 6 additions & 9 deletions tests/test_rs_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -556,16 +550,19 @@ 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()
command_encoder.copy_buffer_to_buffer(buf1, 0, buf2, 0, nbytes)
device.queue.submit([command_encoder.finish()])

# Download
data2 = buf2.map_read()

buf2.map("read")
data2 = buf2.read_mapped()
buf2.unmap()
assert data1 == data2


Expand Down
27 changes: 16 additions & 11 deletions wgpu/backends/rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions wgpu/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9da639

Please sign in to comment.