ComputeBundle

Struct ComputeBundle 

Source
pub struct ComputeBundle<B = BindGroup> { /* private fields */ }
Expand description

A bundle of wgpu::ComputePipeline, its wgpu::BindGroupLayout and optionally wgpu::BindGroup.

§Overview

This is an abstraction of a compute pipeline with its associated resources, so that any compute operations can be easily setup and dispatched.

It is recommended to use ComputeBundleBuilder to create a compute bundle

§Shader Format

The compute shader is suggested to be in the following form:

override workgroup_size: u32;

@compute @workgroup_size(workgroup_size)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
    let index = id.x;

    if index >= arrayLength(&data) {
        return;
    }

    // Do something with `data[index]`
}
  • workgroup_size is an overridable variable of type u32.
  • The entry point function (here main) must have the @compute attribute and a @workgroup_size(workgroup_size) attribute.
  • The entry point function is suggested to have a parameter with @builtin(global_invocation_id) attribute to get the global invocation ID for indexing into the data.

Implementations§

Source§

impl<B> ComputeBundle<B>

Source

pub fn create_bind_group<'a>( &self, device: &Device, index: usize, resources: impl IntoIterator<Item = BindingResource<'a>>, ) -> Option<BindGroup>

Create the bind group at the given index.

index refers to the index in ComputeBundle::bind_group_layouts.

Returns None if the index is out of bounds.

As a good practice, if you are designing API for others to use, do not let the user create bind groups manually as they will have to make sure the binding resources match the layout.

Source

pub fn workgroup_size(&self) -> u32

Get the number of invocations in one workgroup.

Source

pub fn label(&self) -> Option<&str>

Get the label.

Source

pub fn bind_group_layouts(&self) -> &[BindGroupLayout]

Get the bind group layouts.

This corresponds to the bind_group_layout_descriptors provided when creating the compute bundle.

Source

pub fn pipeline(&self) -> &ComputePipeline

Get the compute pipeline.

Source

pub fn dispatch_with_bind_groups<'a>( &self, encoder: &mut CommandEncoder, bind_groups: impl IntoIterator<Item = &'a BindGroup>, count: u32, )

Dispatch the compute bundle for count instances with provided bind group.

Each bind group in bind_groups corresponds to the bind group layout at the same index in ComputeBundle::bind_group_layouts.

Source§

impl ComputeBundle

Source

pub fn new<'a, 'b>( label: Option<&str>, device: &Device, bind_group_layout_descriptors: impl IntoIterator<Item = &'a BindGroupLayoutDescriptor<'a>>, resources: impl IntoIterator<Item = impl IntoIterator<Item = BindingResource<'a>>>, compilation_options: PipelineCompilationOptions<'_>, shader_source: ShaderSource<'_>, entry_point: &str, workgroup_size: Option<u32>, ) -> Result<Self, ComputeBundleCreateError>

Create a new compute bundle.

shader_source requires an overridable variable workgroup_size of u32, see docs of ComputeBundle.

Source

pub fn bind_groups(&self) -> &[BindGroup]

Get the bind groups.

Source

pub fn dispatch(&self, encoder: &mut CommandEncoder, count: u32)

Dispatch the compute bundle for count instances.

Source

pub fn update_bind_group( &mut self, index: usize, bind_group: BindGroup, ) -> Option<BindGroup>

Update the bind group at index.

Returns Some of the previous bind group if it was updated, or None if the index is out of bounds.

Source

pub fn update_bind_group_with_binding_resources<'a>( &mut self, device: &Device, index: usize, resources: impl IntoIterator<Item = BindingResource<'a>>, ) -> Option<BindGroup>

Update the bind group at index with the provided binding resources.

Returns Some of the previous bind group if it was updated, or None if the index is out of bounds.

Source§

impl ComputeBundle<()>

Source

pub fn new_without_bind_groups<'a>( label: Option<&str>, device: &Device, bind_group_layout_descriptors: impl IntoIterator<Item = &'a BindGroupLayoutDescriptor<'a>>, compilation_options: PipelineCompilationOptions<'_>, shader_source: ShaderSource<'_>, entry_point: &str, workgroup_size: Option<u32>, ) -> Result<Self, ComputeBundleCreateError>

Create a new compute bundle without internally managed bind group.

To create a bind group with layout matched to one of the layout in this compute bundle, use the ComputeBundle::create_bind_group method.

Source

pub fn dispatch<'a>( &self, encoder: &mut CommandEncoder, count: u32, bind_groups: impl IntoIterator<Item = &'a BindGroup>, )

Dispatch the compute bundle for count instances.

Trait Implementations§

Source§

impl<B: Clone> Clone for ComputeBundle<B>

Source§

fn clone(&self) -> ComputeBundle<B>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B: Debug> Debug for ComputeBundle<B>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> Freeze for ComputeBundle<B>

§

impl<B = BindGroup> !RefUnwindSafe for ComputeBundle<B>

§

impl<B> Send for ComputeBundle<B>
where B: Send,

§

impl<B> Sync for ComputeBundle<B>
where B: Sync,

§

impl<B> Unpin for ComputeBundle<B>
where B: Unpin,

§

impl<B = BindGroup> !UnwindSafe for ComputeBundle<B>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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,