pub struct AtomicSparseBufferVec<T>where
T: AtomicPod,{ /* private fields */ }Expand description
A GPU buffer that can grow, can be updated atomically from multiple threads on the CPU, and is sparsely updated on the GPU if only a small number of elements have changed.
This type is similar to
crate::render_resource::buffer_vec::AtomicRawBufferVec, but instead of
reuploading the entire buffer to the GPU when it’s changed, it tracks
changes on a per-element level and uploads only the elements that changed if
the number of such elements is small. It uses a compute shader to scatter
those changed elements.
As the stored data is AtomicPod, multiple threads may update the buffer
simultaneously. Note that, like
crate::render_resource::buffer_vec::AtomicRawBufferVec, only existing
elements may be updated from multiple threads; new data still requires
exclusive access.
T must have a size that’s a multiple of 4.
Implementations§
Source§impl<T> AtomicSparseBufferVec<T>where
T: AtomicPod,
impl<T> AtomicSparseBufferVec<T>where
T: AtomicPod,
Sourcepub fn new(
buffer_usages: BufferUsages,
label: Arc<str>,
) -> AtomicSparseBufferVec<T>
pub fn new( buffer_usages: BufferUsages, label: Arc<str>, ) -> AtomicSparseBufferVec<T>
Creates a new AtomicSparseBufferVec with the given set of buffer
usages and label.
buffer_usages specifies the set of allowed wgpu buffer usages for
the buffer that AtomicSparseBufferVec manages.
BufferUsages::COPY_DST is automatically added to this set.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no elements in the CPU side copy of the buffer.
Sourcepub fn buffer(&self) -> Option<&Buffer>
pub fn buffer(&self) -> Option<&Buffer>
Returns a handle to the buffer, if the data has been uploaded.
Sourcepub fn set(&self, index: u32, value: T)
pub fn set(&self, index: u32, value: T)
Sets the value at the given index.
If the index isn’t in range of the buffer, this method panics.
Internally, the value is converted to its blob representation.
Note that this method is thread-safe and doesn’t require &mut self.
It’s your responsibility, however, to ensure synchronization; though
this method is memory-safe, it’s possible for other threads to observe
partially-overwritten values if Self::get or similar methods are
called while the write operation is occurring.
Sourcepub fn reserve(&mut self, new_capacity: usize, render_device: &RenderDevice)
pub fn reserve(&mut self, new_capacity: usize, render_device: &RenderDevice)
Ensures that the backing buffer for this buffer vector is present and appropriately sized on the GPU.
Sourcepub fn grow(&mut self, new_len: u32)
pub fn grow(&mut self, new_len: u32)
Grows the buffer by adding default values so that it’s at least the given size.
This method sets all the newly-added values to dirty.
If the buffer is already large enough, this method does nothing.
Sourcepub fn write_buffers(
&mut self,
render_device: &RenderDevice,
render_queue: &RenderQueue,
)
pub fn write_buffers( &mut self, render_device: &RenderDevice, render_queue: &RenderQueue, )
Writes the data to the GPU, either via a sparse upload or a bulk data upload.
Sourcepub fn prepare_to_populate_buffers(
&mut self,
render_device: &RenderDevice,
pipeline_cache: &PipelineCache,
sparse_buffer_update_jobs: &mut SparseBufferUpdateJobs,
sparse_buffer_update_bind_groups: &mut SparseBufferUpdateBindGroups,
sparse_buffer_update_pipelines: &SparseBufferUpdatePipelines,
)
pub fn prepare_to_populate_buffers( &mut self, render_device: &RenderDevice, pipeline_cache: &PipelineCache, sparse_buffer_update_jobs: &mut SparseBufferUpdateJobs, sparse_buffer_update_bind_groups: &mut SparseBufferUpdateBindGroups, sparse_buffer_update_pipelines: &SparseBufferUpdatePipelines, )
If a sparse update has been scheduled, prepares all GPU resources necessary to perform a sparse buffer update, other than updating the metadata uniform.
Source§impl<T> AtomicSparseBufferVec<T>
impl<T> AtomicSparseBufferVec<T>
Sourcepub fn grow_and_set(&mut self, index: u32, value: T)
pub fn grow_and_set(&mut self, index: u32, value: T)
Sets the value at the given index, growing the buffer if the index isn’t in range.
If the buffer already holds value there, this does nothing, leaving the element’s dirty
bit clear so that a sparse update skips it.
Requires &mut self because growing reallocates.
Use Self::set_if_changed when the index is known to be in range and
only a shared reference is available.
Sourcepub fn set_if_changed(&self, index: u32, value: T)
pub fn set_if_changed(&self, index: u32, value: T)
Sets the value at an index that’s already in range, without growing the buffer.
If the buffer already holds value there, this does nothing, leaving the element’s dirty
bit clear so that a sparse update skips it.
Like Self::set, this is thread-safe, doesn’t require &mut self, and panics if the
index isn’t in range.
Auto Trait Implementations§
impl<T> !RefUnwindSafe for AtomicSparseBufferVec<T>
impl<T> !UnwindSafe for AtomicSparseBufferVec<T>
impl<T> Freeze for AtomicSparseBufferVec<T>
impl<T> Send for AtomicSparseBufferVec<T>
impl<T> Sync for AtomicSparseBufferVec<T>
impl<T> Unpin for AtomicSparseBufferVec<T>
impl<T> UnsafeUnpin for AtomicSparseBufferVec<T>
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ConditionalSend for Twhere
T: Send,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§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>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§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>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§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.Source§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.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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