Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer {
    pub(crate) state: ResourceState<BufferState>,
    pub(crate) device: Arc<Device>,
    pub(crate) usage: BufferUsages,
    pub(crate) size: BufferAddress,
    pub(crate) initialization_status: RwLock<InitTracker<BufferAddress>>,
    pub(crate) label: String,
    pub(crate) tracking_data: TrackingData,
    pub(crate) map_state: Mutex<BufferMapState>,
    pub(crate) bind_groups: Mutex<WeakVec<BindGroup>>,
    pub(crate) timestamp_normalization_bind_group: SnatchableInner<Option<TimestampNormalizationBindGroup>>,
    pub(crate) indirect_validation_bind_groups: SnatchableInner<Option<BindGroups>>,
}

Fields§

§state: ResourceState<BufferState>§device: Arc<Device>§usage: BufferUsages§size: BufferAddress§initialization_status: RwLock<InitTracker<BufferAddress>>§label: String

The label from the descriptor used to create the resource.

§tracking_data: TrackingData§map_state: Mutex<BufferMapState>§bind_groups: Mutex<WeakVec<BindGroup>>§timestamp_normalization_bind_group: SnatchableInner<Option<TimestampNormalizationBindGroup>>§indirect_validation_bind_groups: SnatchableInner<Option<BindGroups>>

Implementations§

Source§

impl Buffer

Source

pub unsafe fn as_hal<A: Api>( self: Arc<Self>, ) -> Option<impl Deref<Target = A::Buffer>>

§Safety
  • The raw buffer handle must not be manually destroyed
Source§

impl Buffer

Source

pub(crate) fn check_destroyed( &self, guard: &SnatchGuard<'_>, ) -> Result<(), DestroyedResourceError>

Source

pub(crate) fn check_usage( &self, expected: BufferUsages, ) -> Result<(), MissingBufferUsageError>

Checks that the given buffer usage contains the required buffer usage, returns an error otherwise.

Source

pub(crate) fn state(&self) -> Result<&BufferState, InvalidResourceError>

Source

pub(crate) fn check_is_valid(&self) -> Result<(), InvalidResourceError>

Source

pub fn invalid(device: Arc<Device>, desc: &BufferDescriptor<'_>) -> Arc<Self>

Source

fn resolve_binding_size<S: Copy + Into<BufferAddress> + TryFrom<BufferAddress>>( &self, offset: BufferAddress, binding_size: Option<S>, ) -> Result<S, BindingError>

Resolve the size of a binding for buffer with offset and size.

If size is None, then the remainder of the buffer starting from offset is used.

If the binding would overflow the buffer, then an error is returned.

S is wgt::BufferSize (NonZeroU64) when called from Buffer::binding for a storage or uniform buffer binding. S is wgt::BufferAddress (u64) when called from resolve_vertex_or_index_binding_range for a vertex or index buffer binding.

Source

pub fn resolve_vertex_or_index_binding_range( &self, offset: BufferAddress, size: Option<BufferAddress>, ) -> Result<Range<BufferAddress>, BindingError>

Resolve the binding range for a vertex or index buffer.

This function is for vertex and index buffer bindings, which WebGPU allows to have zero size. For storage and uniform buffer bindings, which must have non-zero size, use Buffer::binding.

Returns an error if the binding would overflow the buffer.

Source

pub fn binding<'a>( &'a self, offset: BufferAddress, binding_size: Option<BufferSize>, snatch_guard: &'a SnatchGuard<'_>, ) -> Result<BufferBinding<'a, dyn DynBuffer, BufferSize>, BindingError>

Create a new hal::BufferBinding for the buffer with offset and binding_size.

If binding_size is None, then the remainder of the buffer starting from offset is used.

Returns an error if the binding would overflow the buffer.

This function is for storage and uniform buffer bindings, which must have non-zero size. For vertex and index buffer bindings, which may have zero size, use Buffer::resolve_vertex_or_index_binding_range.

Source

pub fn map_async( self: &Arc<Self>, offset: BufferAddress, size: Option<BufferAddress>, op: BufferMapOperation, ) -> Option<SubmissionIndex>

Schedule buffer mapping.

op.callback is guaranteed to be called, regardless of the outcome.

Source

fn try_map_async( self: &Arc<Self>, offset: BufferAddress, size: Option<BufferAddress>, op: BufferMapOperation, ) -> Result<SubmissionIndex, (BufferMapOperation, BufferAccessError)>

Try to schedule buffer mapping.

The outcome of this function is one of the following:

  • If there is a queue, and nothing pending in the queue that uses the buffer in question, the buffer is added to Queue::ready_to_map, and will be mapped the next time Device::maintain is called. The queue assumes responsibility for calling the callback, and this function returns Ok(0), but the buffer has not yet been mapped.
  • If there is a queue, and something is pending in the queue that uses the buffer in question, the buffer is scheduled for mapping after that submission completes. The queue assumes responsibility for calling the callback, and this function returns Ok(index) with the index of the submission that must complete. The buffer has not yet been mapped.
  • If there is no queue, the buffer is mapped and the callback is called immediately. The return value is Ok(0).
  • Regardless of the queue state, if there is an error that terminates the buffer mapping attempt, this function returns the callback along with the error, and the caller is responsible for calling the callback.

A return value of Ok(0) means that mapping does not need to wait on the queue, but it does not mean that the buffer has already been mapped.

Source

pub fn get_mapped_range( self: &Arc<Self>, offset: BufferAddress, size: Option<BufferAddress>, ) -> Result<(NonNull<u8>, u64), BufferAccessError>

Source

pub(crate) fn map( &self, snatch_guard: &SnatchGuard<'_>, ) -> Option<BufferMapPendingClosure>

This function returns None only if Self::map_state is not BufferMapState::Waiting. Other errors are returned within BufferMapPendingClosure.

Source

pub fn unmap(self: &Arc<Self>)

Source

fn unmap_inner(self: &Arc<Self>) -> Option<BufferMapPendingClosure>

Per the WebGPU spec, unmap does not raise any errors it just resolves any pending map_async calls with a MapAborted error. It is okay to ignore errors because:

  • if the buffer or device was invalid from the start it couldn’t have been mapped via map_async anyway (no callback to resolve)
  • if the device becomes invalid it calls the callback in poll/maintain
  • if the buffer was destroyed (via Buffer::destroy) it was first unmapped

https://gpuweb.github.io/gpuweb/#dom-gpubuffer-unmap

Source

pub fn destroy(self: &Arc<Self>)

Source

pub fn size(&self) -> BufferAddress

Source

pub fn usage(&self) -> BufferUsages

Trait Implementations§

Source§

impl Debug for Buffer

Source§

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

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

impl Drop for Buffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Labeled for Buffer

Source§

fn label(&self) -> &str

Returns a string identifying this resource for logging and errors. Read more
Source§

fn error_ident(&self) -> ResourceErrorIdent

Source§

impl ParentDevice for Buffer

Source§

fn device(&self) -> &Arc<Device>

Source§

fn is_equal(self: &Arc<Self>, other: &Arc<Self>) -> bool

Source§

fn same_device_as<O: ParentDevice>(&self, other: &O) -> Result<(), DeviceError>

Source§

fn same_device(&self, device: &Device) -> Result<(), DeviceError>

Source§

impl RawResourceAccess for Buffer

Source§

type DynResource = dyn DynBuffer

Source§

fn raw<'a>( &'a self, guard: &'a SnatchGuard<'_>, ) -> Option<&'a Self::DynResource>

Get access to the raw resource if it is not destroyed. Read more
Source§

fn try_raw<'a>( &'a self, guard: &'a SnatchGuard<'_>, ) -> Result<&'a Self::DynResource, DestroyedResourceError>

Get access to the raw resource if it is not destroyed. Read more
Source§

impl ResourceType for Buffer

Source§

const TYPE: &'static str = "Buffer"

Source§

impl Trackable for Buffer

Auto Trait Implementations§

§

impl !Freeze for Buffer

§

impl !RefUnwindSafe for Buffer

§

impl Send for Buffer

§

impl Sync for Buffer

§

impl Unpin for Buffer

§

impl UnsafeUnpin for Buffer

§

impl !UnwindSafe for Buffer

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
§

impl<T> Downcast<T> for T

§

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, 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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