pub struct WebGpuMemoryManager { /* private fields */ }Expand description
Manages a pool of device-resident wgpu::Buffer objects, returning opaque
u64 handles to callers.
All public methods are &self to allow shared references from the backend.
Implementations§
Source§impl WebGpuMemoryManager
impl WebGpuMemoryManager
Sourcepub fn new(device: Arc<WebGpuDevice>) -> Self
pub fn new(device: Arc<WebGpuDevice>) -> Self
Create a new memory manager backed by device.
Sourcepub fn alloc(&self, bytes: usize) -> WebGpuResult<u64>
pub fn alloc(&self, bytes: usize) -> WebGpuResult<u64>
Allocate a new device buffer of at least bytes bytes.
The physical buffer size is rounded up to wgpu::COPY_BUFFER_ALIGNMENT
(4 bytes): WebGPU requires copy_buffer_to_buffer sizes, map_async
ranges, and STORAGE-bound bindings to be multiples of 4, so an
odd-sized allocation (3 bytes, or an odd count of 2-byte f16 elements)
would otherwise pass alloc() cleanly and only fail later — fatally,
with no handler installed — on the first readback or bind. The
rounded-up size is what later copy_to_device/copy_from_device
calls validate against, which is strictly more permissive than the
caller’s requested size, never less.
Returns WebGpuError::InvalidArgument for a zero-byte request and
WebGpuError::OutOfMemory if the (rounded) size exceeds what this
device can bind — checked against the adapter-derived limits resolved
in WebGpuDevice::new, instead of discovered via a fatal wgpu
validation abort inside create_buffer.
Sourcepub fn free(&self, handle: u64) -> WebGpuResult<()>
pub fn free(&self, handle: u64) -> WebGpuResult<()>
Release the buffer associated with handle.
The handle is silently ignored if it is unknown (already freed).
Sourcepub fn copy_to_device(&self, handle: u64, src: &[u8]) -> WebGpuResult<()>
pub fn copy_to_device(&self, handle: u64, src: &[u8]) -> WebGpuResult<()>
Upload src (host bytes) into the device buffer identified by handle.
Sourcepub fn copy_from_device(&self, dst: &mut [u8], handle: u64) -> WebGpuResult<()>
pub fn copy_from_device(&self, dst: &mut [u8], handle: u64) -> WebGpuResult<()>
Download the device buffer identified by handle into dst (host bytes).
Only the bytes actually requested (dst.len(), rounded up to
wgpu::COPY_BUFFER_ALIGNMENT) are staged and copied — previously this
always staged and DMA’d the entire source buffer regardless of how
much the caller asked for, so e.g. reading a single scalar out of a
multi-MiB reduction output moved the whole buffer across the copy
engine to deliver 4 bytes.
Uses a temporary MAP_READ staging buffer and blocks — bounded by a
generous internal timeout, see WebGpuError::Timeout — until the
GPU work completes.