Skip to main content

ArrayPool

Struct ArrayPool 

Source
pub struct ArrayPool {
    array: Array,
    label: Option<String>,
    allocator: Allocator,
    data: Vec<u8>,
    dirty: bool,
    replaced: bool,
}
Expand description

One array sub-allocated into element ranges, with a CPU mirror of its bytes.

See the module documentation for what a pool is for and how growing works.

Fields§

§array: Array

The array every range lives in.

§label: Option<String>

The label the array is created with, reused when it is replaced.

§allocator: Allocator

Where the free elements are.

§data: Vec<u8>

The array’s bytes, element for element, kept so a slice can be written without re-reading the GPU resource.

§dirty: bool

Whether the mirror holds bytes the array has not received yet.

§replaced: bool

Whether the array was replaced since the last Self::upload.

A replacement is what a caller has to act on: every bind group built from the old handle is stale. Reporting it from the upload keeps the check and the copy in one place, so a caller cannot upload without learning that the array moved.

Implementations§

Source§

impl ArrayPool

Source

pub fn new( device: &Device, label: &str, element_size: u64, initial_elements: u32, max_dimension: Option<u32>, ) -> Self

Create a pool whose array holds initial_elements elements of element_size bytes each.

max_dimension is Some of the device’s [wgpu::Limits::max_texture_dimension_2d] to hold the elements in a texture, or None to hold them in a storage buffer.

§Panics

Under the conditions Array::new panics.

Source

pub fn handle(&self) -> ArrayHandle

The bindable handle to the pool’s array.

Cloned rather than borrowed so a caller can move it into the resource graph, which is what makes a replaced array mark the bind groups built from it dirty.

Source

pub fn array(&self) -> &Array

The array itself, for a caller that needs more than the handle.

Source

pub fn capacity(&self) -> u64

How many elements the array can hold.

Source

pub fn allocate(&mut self, device: &Device, count: u32) -> Option<ArrayRange>

Reserves count elements, growing the array if they do not fit.

Returns None only if the pool cannot grow any further (its size would overflow a u32, or it has run out of node slots for its ranges).

Growing replaces the array, so a caller that holds a handle has to read it again — and has to upload before the GPU sees the data the mirror holds.

§Panics

If count is zero, since a range covers at least one element.

Source

pub fn release(&mut self, allocation: Allocation)

Hands a range’s elements back to the pool.

§Panics

Panics if the allocation was already released.

Source

pub fn write(&mut self, range: ArrayRange, bytes: &[u8])

Write bytes — one element per element_size — into range.

This only fills the CPU mirror; the GPU sees the data once upload runs.

§Panics

If bytes is not exactly range’s elements, or if the range does not belong to this pool.

Source

pub fn upload(&mut self, device: &Device, encoder: &mut CommandEncoder) -> bool

Copy the CPU mirror to the array, recording the copy into encoder.

Returns whether the array had to be replaced since the caller last read its handle, which is what says every bind group built from the old one is stale. An upload of an unchanged pool does nothing.

Source

fn reserve(&mut self, device: &Device, end: u32)

Grow the array until it covers end elements.

The mirror is resized with it, so the data already written stays put.

Source

fn grow(&mut self, device: &Device, count: u32) -> Option<()>

Grow the allocator until count elements fit.

Returns None if it cannot grow, leaving the pool unchanged.

Trait Implementations§

Source§

impl Debug for ArrayPool

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
§

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

§

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

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

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> WasmNotSend for T
where T: Send,

§

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

§

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

§

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