pub struct GpuBuffer<T: Pod> { /* private fields */ }Expand description
GPU buffer wrapper with type safety.
This struct wraps a WGPU buffer and provides type-safe operations for uploading and downloading data to/from the GPU.
Implementations§
Source§impl<T: Pod> GpuBuffer<T>
impl<T: Pod> GpuBuffer<T>
Sourcepub fn new(
context: &GpuContext,
len: usize,
usage: BufferUsages,
) -> GpuResult<Self>
pub fn new( context: &GpuContext, len: usize, usage: BufferUsages, ) -> GpuResult<Self>
Create a new GPU buffer with the specified size and usage.
§Errors
Returns an error if buffer creation fails or size is invalid.
Sourcepub fn from_data(
context: &GpuContext,
data: &[T],
usage: BufferUsages,
) -> GpuResult<Self>
pub fn from_data( context: &GpuContext, data: &[T], usage: BufferUsages, ) -> GpuResult<Self>
Create a GPU buffer from existing data.
§Errors
Returns an error if buffer creation or upload fails.
Sourcepub fn staging(context: &GpuContext, len: usize) -> GpuResult<Self>
pub fn staging(context: &GpuContext, len: usize) -> GpuResult<Self>
Sourcepub fn write(&mut self, data: &[T]) -> GpuResult<()>
pub fn write(&mut self, data: &[T]) -> GpuResult<()>
Write data to the GPU buffer.
§Errors
Returns an error if the buffer doesn’t support writes or data size doesn’t match buffer size.
Sourcepub async fn read(&self) -> GpuResult<Vec<T>>
pub async fn read(&self) -> GpuResult<Vec<T>>
Read data from the GPU buffer asynchronously.
§Errors
Returns an error if the buffer doesn’t support reads or mapping fails.
Sourcepub async fn read_async(&self) -> GpuResult<Vec<T>>
pub async fn read_async(&self) -> GpuResult<Vec<T>>
Read data from the GPU buffer asynchronously using a Future.
This is the primary async entry-point for GPU→CPU readback. It uses a
futures::channel::oneshot channel so the mapping callback resolves
the returned Future without blocking any thread.
Equivalent to calling .read().await, but named explicitly so downstream
code can refer to the async path by a stable, unambiguous name when both
read_blocking and the async variant need to co-exist in the same scope.
§Errors
Returns an error if the buffer doesn’t support reads (MAP_READ usage
must be set), if the oneshot channel is dropped before resolution
(GpuError::BufferMapping("Channel closed")), or if wgpu reports a
mapping failure.
Sourcepub fn read_blocking(&self) -> GpuResult<Vec<T>>
pub fn read_blocking(&self) -> GpuResult<Vec<T>>
Read data from the GPU buffer synchronously (blocking).
§Errors
Returns an error if the buffer doesn’t support reads or mapping fails.
Sourcepub fn copy_from(&mut self, source: &GpuBuffer<T>) -> GpuResult<()>
pub fn copy_from(&mut self, source: &GpuBuffer<T>) -> GpuResult<()>
Copy data from another GPU buffer.
§Errors
Returns an error if buffer sizes don’t match or copy is not supported.
Sourcepub fn size_bytes(&self) -> u64
pub fn size_bytes(&self) -> u64
Get the buffer size in bytes.
Sourcepub fn usage(&self) -> BufferUsages
pub fn usage(&self) -> BufferUsages
Get buffer usage flags.