Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental support for get_mapped_range #522

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion wgpu/backends/wgpu_native/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,8 @@ def callback(status_, user_data_p):
self._internal, map_mode, offset, size, callback, ffi.NULL
)

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

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

def _experimental_get_mapped_range(self, buffer_offset=None, size=None):
"""Undocumented and experimental. This API can change or be
removed without notice. Just here so we can benchmark this
approach. Returns a mapped memoryview object that can be written
to. Note that once the buffer unmaps, this memoryview object
becomes unusable, and accessing it may result in a segfault.
"""
# Can we even write?
if self._map_state != enums.BufferMapState.mapped:
raise RuntimeError("Can only write to a buffer if its mapped.")

offset, size = self._check_range(buffer_offset, size)
if offset < self._mapped_status[0] or (offset + size) > self._mapped_status[1]:
raise ValueError(
"The range for buffer writing is not contained in the currently mapped range."
)

# Get mapped memoryview
# H: void * f(WGPUBuffer buffer, size_t offset, size_t size)
src_ptr = libf.wgpuBufferGetMappedRange(self._internal, offset, size)
src_address = int(ffi.cast("intptr_t", src_ptr))
src_m = get_memoryview_from_address(src_address, size)

return src_m

def destroy(self):
# NOTE: destroy means that the wgpu-core object gets into a destroyed
# state. The wgpu-core object still exists. So destroying is quite
Expand Down
4 changes: 2 additions & 2 deletions wgpu/resources/codegen_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
* Diffs for GPUQueue: add read_buffer, add read_texture, hide copy_external_image_to_texture
* Validated 37 classes, 114 methods, 44 properties
### Patching API for backends/wgpu_native/_api.py
* Validated 37 classes, 110 methods, 0 properties
* Validated 37 classes, 111 methods, 0 properties
## Validating backends/wgpu_native/_api.py
* Enum PipelineErrorReason missing in wgpu.h
* Enum AutoLayoutMode missing in wgpu.h
* Enum field VertexFormat.unorm10-10-10-2 missing in wgpu.h
* Enum CanvasAlphaMode missing in wgpu.h
* Enum field DeviceLostReason.unknown missing in wgpu.h
* Wrote 235 enum mappings and 47 struct-field mappings to wgpu_native/_mappings.py
* Validated 112 C function calls
* Validated 113 C function calls
* Not using 91 C functions
* Validated 76 C structs
Loading