pub struct StagingBelt { /* private fields */ }unstable-wgpu-28 only.Expand description
Efficiently performs many buffer writes by sharing and reusing temporary buffers.
Internally it uses a ring-buffer of staging buffers that are sub-allocated.
Its advantage over Queue::write_buffer_with() is that the individual allocations
are cheaper; StagingBelt is most useful when you are writing very many small pieces
of data. It can be understood as a sort of arena allocator.
Using a staging belt is slightly complicated, and generally goes as follows:
- Use
StagingBelt::write_buffer()orStagingBelt::allocate()to allocate buffer slices, then write your data to them. - Call
StagingBelt::finish(). - Submit all command encoders that were used in step 1.
- Call
StagingBelt::recall().
Implementations§
Source§impl StagingBelt
impl StagingBelt
Sourcepub fn new(device: Device, chunk_size: u64) -> StagingBelt
Available on crate feature wgpu-28 only.
pub fn new(device: Device, chunk_size: u64) -> StagingBelt
wgpu-28 only.Create a new staging belt.
The chunk_size is the unit of internal buffer allocation; writes will be
sub-allocated within each chunk. Therefore, for optimal use of memory, the
chunk size should be:
- larger than the largest single
StagingBelt::write_buffer()operation; - 1-4 times less than the total amount of data uploaded per submission
(per
StagingBelt::finish()); and - bigger is better, within these bounds.
Sourcepub fn write_buffer(
&mut self,
encoder: &mut CommandEncoder,
target: &Buffer,
offset: u64,
size: NonZero<u64>,
) -> BufferViewMut
Available on crate feature wgpu-28 only.
pub fn write_buffer( &mut self, encoder: &mut CommandEncoder, target: &Buffer, offset: u64, size: NonZero<u64>, ) -> BufferViewMut
wgpu-28 only.Allocate a staging belt slice of size to be copied into the target buffer
at the specified offset.
offset and size must be multiples of COPY_BUFFER_ALIGNMENT
(as is required by the underlying buffer operations).
The upload will be placed into the provided command encoder. This encoder
must be submitted after StagingBelt::finish() is called and before
StagingBelt::recall() is called.
If the size is greater than the size of any free internal buffer, a new buffer
will be allocated for it. Therefore, the chunk_size passed to StagingBelt::new()
should ideally be larger than every such size.
Sourcepub fn allocate(
&mut self,
size: NonZero<u64>,
alignment: NonZero<u64>,
) -> BufferSlice<'_>
Available on crate feature wgpu-28 only.
pub fn allocate( &mut self, size: NonZero<u64>, alignment: NonZero<u64>, ) -> BufferSlice<'_>
wgpu-28 only.Allocate a staging belt slice with the given size and alignment and return it.
size must be a multiple of COPY_BUFFER_ALIGNMENT
(as is required by the underlying buffer operations).
To use this slice, call BufferSlice::get_mapped_range_mut() and write your data into
that BufferViewMut.
(The view must be dropped before StagingBelt::finish() is called.)
You can then record your own GPU commands to perform with the slice,
such as copying it to a texture or executing a compute shader that reads it (whereas
StagingBelt::write_buffer() can only write to other buffers).
All commands involving this slice must be submitted after
StagingBelt::finish() is called and before StagingBelt::recall() is called.
If the size is greater than the space available in any free internal buffer, a new buffer
will be allocated for it. Therefore, the chunk_size passed to StagingBelt::new()
should ideally be larger than every such size.
The chosen slice will be positioned within the buffer at a multiple of alignment,
which may be used to meet alignment requirements for the operation you wish to perform
with the slice. This does not necessarily affect the alignment of the BufferViewMut.
Sourcepub fn finish(&mut self)
Available on crate feature wgpu-28 only.
pub fn finish(&mut self)
wgpu-28 only.Prepare currently mapped buffers for use in a submission.
This must be called before the command encoder(s) provided to
StagingBelt::write_buffer() are submitted.
At this point, all the partially used staging buffers are closed (cannot be used for
further writes) until after StagingBelt::recall() is called and the GPU is done
copying the data from them.
Sourcepub fn recall(&mut self)
Available on crate feature wgpu-28 only.
pub fn recall(&mut self)
wgpu-28 only.Recall all of the closed buffers back to be reused.
This must only be called after the command encoder(s) provided to
StagingBelt::write_buffer() are submitted. Additional calls are harmless.
Not calling this as soon as possible may result in increased buffer memory usage.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StagingBelt
impl !RefUnwindSafe for StagingBelt
impl Send for StagingBelt
impl Sync for StagingBelt
impl Unpin for StagingBelt
impl UnsafeUnpin for StagingBelt
impl !UnwindSafe for StagingBelt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.