Skip to main content

StagingBuffer

Struct StagingBuffer 

Source
pub struct StagingBuffer {
    slots: Vec<Slot>,
}
Expand description

A pool of staging buffers for one destination buffer, reused across frames.

One of these belongs to each buffer a caller uploads to every frame; see the module docs for the recycling scheme.

Fields§

§slots: Vec<Slot>

Implementations§

Source§

impl StagingBuffer

Source

pub fn new() -> Self

A pool holding no staging buffer yet.

Source

pub fn write( &mut self, device: &Device, encoder: &mut CommandEncoder, target: &Buffer, offset: u64, data: &[u8], )

Upload data into target at offset, recording the copy into encoder.

The bytes reach a reused staging buffer by host write and the GPU on the copy, so target needs [wgpu::BufferUsages::COPY_DST] and must not be mapped. offset and the length of data must both be multiples of [wgpu::COPY_BUFFER_ALIGNMENT], as [wgpu::Queue::write_buffer] requires. An empty data records no copy.

§Panics

If offset or data.len() is not a multiple of [wgpu::COPY_BUFFER_ALIGNMENT].

Source

pub fn write_texture( &mut self, device: &Device, encoder: &mut CommandEncoder, texture: &Texture, layout: TexelCopyBufferLayout, size: Extent3d, data: &[u8], )

Upload data into texture, recording the copy into encoder.

The image is layout-described and spans size, exactly as [wgpu::Queue::write_texture] takes them, but reaches the texture through a reused staging buffer rather than a fresh one per call. The texture needs [wgpu::TextureUsages::COPY_DST].

layout.bytes_per_row must be a multiple of [wgpu::COPY_BYTES_PER_ROW_ALIGNMENT]: a buffer-to-texture copy requires it, unlike [wgpu::Queue::write_texture], which re-aligns the rows itself. Only layout.offset into the staging buffer is always zero — the data starts there — so a non-zero layout.offset is rejected rather than silently reading from the wrong place. An empty image records no copy.

§Panics

If layout.offset is non-zero, if layout.bytes_per_row is set and not a multiple of [wgpu::COPY_BYTES_PER_ROW_ALIGNMENT], or if data is not a multiple of [wgpu::COPY_BUFFER_ALIGNMENT] long.

Source

fn writable_slot(&mut self, device: &Device, size: u64) -> &mut Slot

A buffer of at least size bytes the host may write into.

The smallest writable buffer that fits is preferred, so a buffer grown for one heavy frame does not become the only candidate. A writable buffer too small for the frame is rebuilt in place: the frame outgrew it, and a buffer sized for the largest frame so far is the one later frames will reuse — accumulating one buffer per size ever seen is what makes reuse grow without bound. When every buffer is in flight another is added, so the pool settles at as many buffers as there are frames in flight rather than growing per frame.

Source

pub fn release_idle(&mut self)

Release every staging buffer the host is free to write, keeping only the ones still in flight.

A buffer is kept automatically because it may be the one a later frame reuses, so a pool settles at the frames in flight rather than growing. That also means a frame that needed far more data than the ones after it leaves its buffer behind. Call this once uploads are known to have shrunk — a scene change, a resize — to give the memory back; the pool rebuilds from the frames in flight.

Source

pub fn len(&self) -> usize

How many staging buffers the pool holds.

Mostly useful to a caller deciding whether to release idle buffers.

Source

pub fn is_empty(&self) -> bool

Whether the pool holds no staging buffer at all.

Trait Implementations§

Source§

impl Default for StagingBuffer

Source§

fn default() -> Self

Returns the “default value” for a type. 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