Skip to main content

BufferPool

Struct BufferPool 

Source
pub struct BufferPool {
    buffer: Buffer,
    label: &'static str,
    usage: BufferUsages,
    allocator: Allocator,
}
Expand description

One GPU buffer sub-allocated into many ranges.

See the module documentation for what a pool is for.

Fields§

§buffer: Buffer

The buffer every range lives in.

§label: &'static str

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

§usage: BufferUsages

The usages the buffer is created with.

§allocator: Allocator

Where the free space is.

Implementations§

Source§

impl BufferPool

Source

pub fn new( device: &Device, label: &'static str, usage: BufferUsages, size: u64, ) -> Self

Creates a pool over a buffer of size bytes with the given usages.

The buffer is created immediately. size is rounded up to a multiple of COPY_BUFFER_ALIGNMENT.

§Panics

Panics if the usages do not include [wgpu::BufferUsages::COPY_DST], which the pool needs to grow.

Source

pub fn buffer(&self) -> &Buffer

The buffer every range lives in.

This changes when the pool grows, so read it again instead of holding one: a range stays valid across a grow, but names no buffer of its own to read it from.

Source

pub fn size(&self) -> u64

The pool’s total size, in bytes.

Source

pub fn free_space(&self) -> u32

The free space left, in bytes.

Source

pub fn allocation_size(&self, allocation: Allocation) -> u32

The size reserved for allocation, in bytes.

This is the requested size rounded up to the alignment.

Source

pub fn allocate( &mut self, device: &Device, queue: &Queue, size: u32, ) -> Option<BufferRange>

Reserves a range of size bytes, growing the pool if it does 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). The returned range’s offset is a multiple of COPY_BUFFER_ALIGNMENT, and its allocation covers size rounded up to it.

Growing allocates a new buffer 1.5x the pool’s size, copies the old contents into it, and appends the difference to the allocator. Existing ranges keep their offsets, but the buffer they name changes, so a caller holding one must re-read BufferPool::buffer.

Source

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

Hands a range back to the pool.

§Panics

Panics if the allocation was already released.

Source

fn range(&self, allocation: Allocation) -> BufferRange

Builds the handle for a fresh allocation.

Source

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

Grows the pool by 1.5x until size fits, copying the contents across.

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

Trait Implementations§

Source§

impl Debug for BufferPool

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