Skip to main content

ShaderBuffer

Struct ShaderBuffer 

Source
pub struct ShaderBuffer { /* private fields */ }

Methods from Deref<Target = Buffer>§

pub fn as_entire_binding(&self) -> BindingResource<'_>

Return the binding view of the entire buffer.

pub fn as_entire_buffer_binding(&self) -> BufferBinding<'_>

Return the binding view of the entire buffer.

pub unsafe fn as_hal<A>( &self, ) -> Option<impl Deref<Target = <A as Api>::Buffer> + WasmNotSendSync>
where A: Api,

Available on wgpu_core only.

Get the [wgpu_hal] buffer from this Buffer.

Find the Api struct corresponding to the active backend in [wgpu_hal::api], and pass that struct to the to the A type parameter.

Returns a guard that dereferences to the type of the hal backend which implements A::Buffer.

§Types

The returned type depends on the backend:

  • [hal::api::Vulkan] uses [hal::vulkan::Buffer]
  • hal::api::Metal uses hal::metal::Buffer
  • hal::api::Dx12 uses hal::dx12::Buffer
  • [hal::api::Gles] uses [hal::gles::Buffer]
§Deadlocks
  • The returned guard holds a read-lock on a device-local “destruction” lock, which will cause all calls to destroy to block until the guard is released.
§Errors

This method will return None if:

  • The buffer is not from the backend specified by A.
  • The buffer is from the webgpu or custom backend.
  • The buffer has had [Self::destroy()] called on it.
§Safety
  • The returned resource must not be destroyed unless the guard is the last reference to it and it is not in use by the GPU. The guard and handle may be dropped at any time however.
  • All the safety requirements of wgpu-hal must be upheld.

pub fn slice<S>(&self, bounds: S) -> BufferSlice<'_>
where S: RangeBounds<u64>,

Returns a [BufferSlice] referring to the portion of self’s contents indicated by bounds. Regardless of what sort of data self stores, bounds start and end are given in bytes.

A [BufferSlice] can be used to supply vertex and index data, or to map buffer contents for access from the CPU. See the [BufferSlice] documentation for details.

The range argument can be half or fully unbounded: for example, buffer.slice(..) refers to the entire buffer, and buffer.slice(n..) refers to the portion starting at the nth byte and extending to the end of the buffer.

§Panics
  • If bounds is outside of the bounds of self.
  • If bounds has a length less than 1.

pub fn unmap(&self)

Unmaps the buffer from host memory.

This terminates the effect of all previous map_async() operations and makes the buffer available for use by the GPU again.

pub fn destroy(&self)

Destroy the associated native resources as soon as possible.

pub fn size(&self) -> u64

Returns the length of the buffer allocation in bytes.

This is always equal to the size that was specified when creating the buffer.

pub fn usage(&self) -> BufferUsages

Returns the allowed usages for this Buffer.

This is always equal to the usage that was specified when creating the buffer.

pub fn map_async<S>( &self, mode: MapMode, bounds: S, callback: impl FnOnce(Result<(), BufferAsyncError>) + WasmNotSend + 'static, )
where S: RangeBounds<u64>,

Map the buffer to host (CPU) memory, making it available for reading or writing via get_mapped_range(). The buffer becomes accessible once the callback is invoked with Ok.

Use this when you want to map the buffer immediately. If you need to submit GPU work that uses the buffer before mapping it, use map_buffer_on_submit on CommandEncoder, CommandBuffer, RenderPass, or ComputePass to schedule the mapping after submission. This avoids extra calls to [Buffer::map_async()] or [BufferSlice::map_async()] and lets you initiate mapping from a more convenient place.

For the callback to run, either queue.submit(..), instance.poll_all(..), or device.poll(..) must be called elsewhere in the runtime, possibly integrated into an event loop or run on a separate thread.

The callback runs on the thread that first calls one of the above functions after the GPU work completes. There are no restrictions on the code you can run in the callback; however, on native the polling call will not return until the callback finishes, so keep callbacks short (set flags, send messages, etc.).

While a buffer is mapped, it cannot be used by other commands; at any time, either the GPU or the CPU has exclusive access to the buffer’s contents.

This can also be performed using [BufferSlice::map_async()].

§Panics
  • If the buffer is already mapped.
  • If the buffer’s [BufferUsages] do not allow the requested [MapMode].
  • If bounds is outside of the bounds of self.
  • If bounds does not start at a multiple of [MAP_ALIGNMENT].
  • If bounds has a length that is not a multiple of 4 greater than 0.

pub fn get_mapped_range<S>(&self, bounds: S) -> BufferView
where S: RangeBounds<u64>,

Gain read-only access to the bytes of a mapped [Buffer].

Returns a [BufferView] referring to the buffer range represented by self. See the documentation for [BufferView] for details.

bounds may be less than the bounds passed to [Self::map_async()], and multiple views may be obtained and used simultaneously as long as they do not overlap.

This can also be performed using [BufferSlice::get_mapped_range()].

§Panics
  • If bounds is outside of the bounds of self.
  • If bounds does not start at a multiple of [MAP_ALIGNMENT].
  • If bounds has a length that is not a multiple of 4 greater than 0.
  • If the buffer to which self refers is not currently mapped.
  • If you try to create a view which overlaps an existing [BufferViewMut].

pub fn get_mapped_range_mut<S>(&self, bounds: S) -> BufferViewMut
where S: RangeBounds<u64>,

Gain write access to the bytes of a mapped [Buffer].

Returns a [BufferViewMut] referring to the buffer range represented by self. See the documentation for [BufferViewMut] for more details.

bounds may be less than the bounds passed to [Self::map_async()], and multiple views may be obtained and used simultaneously as long as they do not overlap.

This can also be performed using [BufferSlice::get_mapped_range_mut()].

§Panics
  • If bounds is outside of the bounds of self.
  • If bounds does not start at a multiple of [MAP_ALIGNMENT].
  • If bounds has a length that is not a multiple of 4 greater than 0.
  • If the buffer to which self refers is not currently mapped.
  • If you try to create a view which overlaps an existing [BufferView] or [BufferViewMut].

Trait Implementations§

Source§

impl Clone for ShaderBuffer

Source§

fn clone(&self) -> ShaderBuffer

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 Debug for ShaderBuffer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Deref for ShaderBuffer

Source§

type Target = Buffer

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Hash for ShaderBuffer

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl MewBuffer for ShaderBuffer

Source§

type Inner = ExampleShaderStruct

The “inner” type - what the buffer is supposed to hold. When creating buffers their capacity in bytes will be the input size times the size of this struct.
Source§

fn new(buffer: Buffer) -> Self

Build this buffer type from a wgpu::Buffer.
Source§

fn buffer_desc(inner_size: u64) -> BufferDescriptor<'static>

Get a wgpu::BufferDescriptor to build this type of buffer, with the capacity to fit some number of Self::Inners.
Source§

impl MewBufferBinding for ShaderBuffer

Source§

fn binding_type() -> BindingType

Get a wgpu::BindingType to use for this type of buffer.
Source§

fn as_binding<'a>(&'a self) -> BindingResource<'a>

Get this buffer’s wgpu::BindingResource to bind it in a bind group.
Source§

impl Ord for ShaderBuffer

Source§

fn cmp(&self, other: &ShaderBuffer) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ShaderBuffer

Source§

fn eq(&self, other: &ShaderBuffer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ShaderBuffer

Source§

fn partial_cmp(&self, other: &ShaderBuffer) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for ShaderBuffer

Source§

impl StructuralPartialEq for ShaderBuffer

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
§

impl<T> Upcast<T> for T

§

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

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

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

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

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