Skip to content

Commit 09e4ec6

Browse files
authored
Add experimental support for get_mapped_range (#522)
1 parent acc88fa commit 09e4ec6

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

wgpu/backends/wgpu_native/_api.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,8 @@ def callback(status_, user_data_p):
17591759
self._internal, map_mode, offset, size, callback, ffi.NULL
17601760
)
17611761

1762-
# Let it do some cycles
1762+
# Wait for the queue to process all tasks (including the mapping of the buffer).
1763+
# Also see WebGPU's onSubmittedWorkDone() and C's WGPUQueueWorkDoneCallback.
17631764
self._device._poll()
17641765

17651766
if status != 0: # no-cover
@@ -1863,6 +1864,31 @@ def write_mapped(self, data, buffer_offset=None, size=None):
18631864
# Copy data
18641865
src_m[:] = data
18651866

1867+
def _experimental_get_mapped_range(self, buffer_offset=None, size=None):
1868+
"""Undocumented and experimental. This API can change or be
1869+
removed without notice. Just here so we can benchmark this
1870+
approach. Returns a mapped memoryview object that can be written
1871+
to. Note that once the buffer unmaps, this memoryview object
1872+
becomes unusable, and accessing it may result in a segfault.
1873+
"""
1874+
# Can we even write?
1875+
if self._map_state != enums.BufferMapState.mapped:
1876+
raise RuntimeError("Can only write to a buffer if its mapped.")
1877+
1878+
offset, size = self._check_range(buffer_offset, size)
1879+
if offset < self._mapped_status[0] or (offset + size) > self._mapped_status[1]:
1880+
raise ValueError(
1881+
"The range for buffer writing is not contained in the currently mapped range."
1882+
)
1883+
1884+
# Get mapped memoryview
1885+
# H: void * f(WGPUBuffer buffer, size_t offset, size_t size)
1886+
src_ptr = libf.wgpuBufferGetMappedRange(self._internal, offset, size)
1887+
src_address = int(ffi.cast("intptr_t", src_ptr))
1888+
src_m = get_memoryview_from_address(src_address, size)
1889+
1890+
return src_m
1891+
18661892
def destroy(self):
18671893
# NOTE: destroy means that the wgpu-core object gets into a destroyed
18681894
# state. The wgpu-core object still exists. So destroying is quite

wgpu/resources/codegen_report.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
* Diffs for GPUQueue: add read_buffer, add read_texture, hide copy_external_image_to_texture
2121
* Validated 37 classes, 114 methods, 44 properties
2222
### Patching API for backends/wgpu_native/_api.py
23-
* Validated 37 classes, 110 methods, 0 properties
23+
* Validated 37 classes, 111 methods, 0 properties
2424
## Validating backends/wgpu_native/_api.py
2525
* Enum PipelineErrorReason missing in wgpu.h
2626
* Enum AutoLayoutMode missing in wgpu.h
2727
* Enum field VertexFormat.unorm10-10-10-2 missing in wgpu.h
2828
* Enum CanvasAlphaMode missing in wgpu.h
2929
* Enum field DeviceLostReason.unknown missing in wgpu.h
3030
* Wrote 235 enum mappings and 47 struct-field mappings to wgpu_native/_mappings.py
31-
* Validated 112 C function calls
31+
* Validated 113 C function calls
3232
* Not using 91 C functions
3333
* Validated 76 C structs

0 commit comments

Comments
 (0)