pub struct WgpuBackend {
pub device_info: WgpuDeviceInfo,
/* private fields */
}Expand description
WebGPU compute backend.
When compiled without the wgpu-backend feature this struct is a no-op
stub that will return an error from WgpuBackend::try_new. When compiled
with the feature, a real wgpu Device / Queue pair is created.
For the real implementation, try_new should be called within an async
runtime (tokio or wasm-bindgen-futures for browser targets).
Fields§
§device_info: WgpuDeviceInfoDevice info (populated at initialisation).
Implementations§
Source§impl WgpuBackend
impl WgpuBackend
Sourcepub fn try_new() -> Result<Self, WgpuInitError>
pub fn try_new() -> Result<Self, WgpuInitError>
Attempt to create a wgpu backend.
Returns Ok(Self) when a compatible GPU adapter is available, or
Err(WgpuInitError::NotAvailable) when no adapter can be found (e.g.
running headless without a GPU or without the wgpu-backend feature).
In the current stub implementation this always returns a CPU-fallback
instance with available = false. The full implementation calls
wgpu::Instance::request_adapter and adapter.request_device.
Sourcepub fn new_stub() -> Self
pub fn new_stub() -> Self
Create a stub backend for testing that stores data in CPU memory.
This is equivalent to what try_new would return on a headless system
but without returning an error — useful for unit testing backend logic.
Sourcepub fn is_available(&self) -> bool
pub fn is_available(&self) -> bool
Return true if a real GPU device is available.
Sourcepub fn device_info(&self) -> &WgpuDeviceInfo
pub fn device_info(&self) -> &WgpuDeviceInfo
Return device information for diagnostics.
Sourcepub fn create_buffer(&mut self, len: usize) -> WgpuBufferHandle
pub fn create_buffer(&mut self, len: usize) -> WgpuBufferHandle
Allocate a GPU buffer that can hold len f64 values.
Returns a WgpuBufferHandle that can be passed to Self::write_buffer
and Self::read_buffer.
In the stub implementation, a CPU-side shadow Vec<f64> is allocated.
In the full wgpu implementation, wgpu::Device::create_buffer is called
with STORAGE | COPY_SRC | COPY_DST usage flags.
Sourcepub fn write_buffer(&mut self, handle: WgpuBufferHandle, data: &[f64])
pub fn write_buffer(&mut self, handle: WgpuBufferHandle, data: &[f64])
Upload data to the GPU buffer at handle.
In the stub, data is copied into the CPU-side shadow.
In the full implementation, queue.write_buffer is used.
Sourcepub fn read_buffer(&self, handle: WgpuBufferHandle) -> Vec<f64>
pub fn read_buffer(&self, handle: WgpuBufferHandle) -> Vec<f64>
Download data from the GPU buffer at handle.
In the stub, data is read from the CPU-side shadow.
In the full implementation, a staging buffer is created, the command
encoder.copy_buffer_to_buffer is executed, and the staging buffer is
mapped for reading.
Sourcepub fn dispatch(
&mut self,
kernel_name: &str,
buffers: &[WgpuBufferHandle],
work_groups_x: u32,
)
pub fn dispatch( &mut self, kernel_name: &str, buffers: &[WgpuBufferHandle], work_groups_x: u32, )
Dispatch a compute kernel with work_groups_x workgroups.
In the stub, the kernel’s execute method is called on the CPU-side
shadow data. In the full implementation a ComputePipeline is looked
up from the shader registry and encoder.dispatch_workgroups is called.
§Arguments
kernel_name— name of the WGSL shader entry pointbuffers— input/output buffer handleswork_groups_x— number of workgroups in the X dimension
Sourcepub fn register_shader(&mut self, name: &str, wgsl_source: &str)
pub fn register_shader(&mut self, name: &str, wgsl_source: &str)
Register a WGSL compute shader source and associate it with a name.
In the stub, the source is stored but not compiled.
In the full implementation, device.create_shader_module is called and
the resulting ShaderModule is cached.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for WgpuBackend
impl RefUnwindSafe for WgpuBackend
impl Send for WgpuBackend
impl Sync for WgpuBackend
impl Unpin for WgpuBackend
impl UnsafeUnpin for WgpuBackend
impl UnwindSafe for WgpuBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more