Skip to main content

WgpuBackend

Struct WgpuBackend 

Source
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: WgpuDeviceInfo

Device info (populated at initialisation).

Implementations§

Source§

impl WgpuBackend

Source

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.

Source

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.

Source

pub fn is_available(&self) -> bool

Return true if a real GPU device is available.

Source

pub fn device_info(&self) -> &WgpuDeviceInfo

Return device information for diagnostics.

Source

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.

Source

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.

Source

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.

Source

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 point
  • buffers — input/output buffer handles
  • work_groups_x — number of workgroups in the X dimension
Source

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§

Source§

impl Debug for WgpuBackend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,