pub struct RawBufferVec<T>where
T: NoUninit,{ /* private fields */ }Expand description
A structure for storing raw bytes that have already been properly formatted for use by the GPU.
“Properly formatted” means that item data already meets the alignment and padding
requirements for how it will be used on the GPU. The item type must implement NoUninit
for its data representation to be directly copyable.
Index, vertex, and instance-rate vertex buffers have no alignment nor padding requirements and so this helper type is a good choice for them.
The contained data is stored in system RAM. Calling reserve
allocates VRAM from the RenderDevice.
write_buffer queues copying of the data
from system RAM to VRAM.
Other options for storing GPU-accessible data are:
Implementations§
Source§impl<T> RawBufferVec<T>where
T: NoUninit,
impl<T> RawBufferVec<T>where
T: NoUninit,
Sourcepub const fn new(buffer_usage: BufferUsages) -> RawBufferVec<T>
pub const fn new(buffer_usage: BufferUsages) -> RawBufferVec<T>
Creates a new RawBufferVec with the given BufferUsages.
Examples found in repository?
365 fn from_world(world: &mut World) -> Self {
366 let render_device = world.resource::<RenderDevice>();
367 let render_queue = world.resource::<RenderQueue>();
368
369 // Create the vertex and index buffers.
370 let mut vbo = RawBufferVec::new(BufferUsages::VERTEX);
371 let mut ibo = RawBufferVec::new(BufferUsages::INDEX);
372
373 for vertex in &VERTICES {
374 vbo.push(*vertex);
375 }
376 for index in 0..3 {
377 ibo.push(index);
378 }
379
380 // These two lines are required in order to trigger the upload to GPU.
381 vbo.write_buffer(render_device, render_queue);
382 ibo.write_buffer(render_device, render_queue);
383
384 CustomPhaseItemBuffers {
385 vertices: vbo,
386 indices: ibo,
387 }
388 }Sourcepub fn buffer(&self) -> Option<&Buffer>
pub fn buffer(&self) -> Option<&Buffer>
Returns a handle to the buffer, if the data has been uploaded.
Examples found in repository?
69 fn render<'w>(
70 _: &P,
71 _: ROQueryItem<'w, '_, Self::ViewQuery>,
72 _: Option<ROQueryItem<'w, '_, Self::ItemQuery>>,
73 custom_phase_item_buffers: SystemParamItem<'w, '_, Self::Param>,
74 pass: &mut TrackedRenderPass<'w>,
75 ) -> RenderCommandResult {
76 // Borrow check workaround.
77 let custom_phase_item_buffers = custom_phase_item_buffers.into_inner();
78
79 // Tell the GPU where the vertices are.
80 pass.set_vertex_buffer(
81 0,
82 custom_phase_item_buffers
83 .vertices
84 .buffer()
85 .unwrap()
86 .slice(..),
87 );
88
89 // Tell the GPU where the indices are.
90 pass.set_index_buffer(
91 custom_phase_item_buffers
92 .indices
93 .buffer()
94 .unwrap()
95 .slice(..),
96 0,
97 IndexFormat::Uint32,
98 );
99
100 // Draw one triangle (3 vertices).
101 pass.draw_indexed(0..3, 0, 0..1);
102
103 RenderCommandResult::Success
104 }Sourcepub fn binding(&self) -> Option<BindingResource<'_>>
pub fn binding(&self) -> Option<BindingResource<'_>>
Returns the binding for the buffer if the data has been uploaded.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the amount of space that the GPU will use before reallocating.
Sourcepub fn push(&mut self, value: T) -> usize
pub fn push(&mut self, value: T) -> usize
Adds a new value and returns its index.
Examples found in repository?
365 fn from_world(world: &mut World) -> Self {
366 let render_device = world.resource::<RenderDevice>();
367 let render_queue = world.resource::<RenderQueue>();
368
369 // Create the vertex and index buffers.
370 let mut vbo = RawBufferVec::new(BufferUsages::VERTEX);
371 let mut ibo = RawBufferVec::new(BufferUsages::INDEX);
372
373 for vertex in &VERTICES {
374 vbo.push(*vertex);
375 }
376 for index in 0..3 {
377 ibo.push(index);
378 }
379
380 // These two lines are required in order to trigger the upload to GPU.
381 vbo.write_buffer(render_device, render_queue);
382 ibo.write_buffer(render_device, render_queue);
383
384 CustomPhaseItemBuffers {
385 vertices: vbo,
386 indices: ibo,
387 }
388 }pub fn append(&mut self, other: &mut RawBufferVec<T>)
Sourcepub fn set(&mut self, index: u32, value: T)
pub fn set(&mut self, index: u32, value: T)
Sets the value at the given index.
The index must be less than RawBufferVec::len.
Sourcepub fn reserve_internal(&mut self, count: usize)
pub fn reserve_internal(&mut self, count: usize)
Preallocates space for count elements in the internal CPU-side buffer.
Unlike RawBufferVec::reserve, this doesn’t have any effect on the GPU buffer.
Sourcepub fn set_label(&mut self, label: Option<&str>)
pub fn set_label(&mut self, label: Option<&str>)
Changes the debugging label of the buffer.
The next time the buffer is updated (via reserve), Bevy will inform
the driver of the new label.
Sourcepub fn reserve(&mut self, capacity: usize, device: &RenderDevice)
pub fn reserve(&mut self, capacity: usize, device: &RenderDevice)
Creates a Buffer on the RenderDevice with size
at least size_of::<T>() * capacity, unless a such a buffer already exists.
If a Buffer exists, but is too small, references to it will be discarded,
and a new Buffer will be created. Any previously created Buffers
that are no longer referenced will be deleted by the RenderDevice
once it is done using them (typically 1-2 frames).
In addition to any BufferUsages provided when
the RawBufferVec was created, the buffer on the RenderDevice
is marked as BufferUsages::COPY_DST.
Sourcepub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue)
pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue)
Queues writing of data from system RAM to VRAM using the RenderDevice
and the provided RenderQueue.
Before queuing the write, a reserve operation
is executed.
Examples found in repository?
365 fn from_world(world: &mut World) -> Self {
366 let render_device = world.resource::<RenderDevice>();
367 let render_queue = world.resource::<RenderQueue>();
368
369 // Create the vertex and index buffers.
370 let mut vbo = RawBufferVec::new(BufferUsages::VERTEX);
371 let mut ibo = RawBufferVec::new(BufferUsages::INDEX);
372
373 for vertex in &VERTICES {
374 vbo.push(*vertex);
375 }
376 for index in 0..3 {
377 ibo.push(index);
378 }
379
380 // These two lines are required in order to trigger the upload to GPU.
381 vbo.write_buffer(render_device, render_queue);
382 ibo.write_buffer(render_device, render_queue);
383
384 CustomPhaseItemBuffers {
385 vertices: vbo,
386 indices: ibo,
387 }
388 }Sourcepub fn write_buffer_range(
&mut self,
render_queue: &RenderQueue,
range: Range<usize>,
) -> Result<(), WriteBufferRangeError>
pub fn write_buffer_range( &mut self, render_queue: &RenderQueue, range: Range<usize>, ) -> Result<(), WriteBufferRangeError>
Queues writing of data from system RAM to VRAM using the RenderDevice
and the provided RenderQueue.
If the buffer is not initialized on the GPU or the range is bigger than the capacity it will return an error. You’ll need to either reserve a new buffer which will lose data on the GPU or create a new buffer and copy the old data to it.
This will only write the data contained in the given range. It is useful if you only want to update a part of the buffer.
pub fn values(&self) -> &Vec<T>
pub fn values_mut(&mut self) -> &mut Vec<T>
Trait Implementations§
Source§impl<T> Extend<T> for RawBufferVec<T>where
T: NoUninit,
impl<T> Extend<T> for RawBufferVec<T>where
T: NoUninit,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Auto Trait Implementations§
impl<T> Freeze for RawBufferVec<T>
impl<T> !RefUnwindSafe for RawBufferVec<T>
impl<T> Send for RawBufferVec<T>where
T: Send,
impl<T> Sync for RawBufferVec<T>where
T: Sync,
impl<T> Unpin for RawBufferVec<T>where
T: Unpin,
impl<T> !UnwindSafe for RawBufferVec<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
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> 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>. Box<dyn Any> can
then be further downcast into Box<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>. Rc<Any> 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> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
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> InitializeFromFunction<T> for T
impl<T> InitializeFromFunction<T> for T
Source§fn initialize_from_function(f: fn() -> T) -> T
fn initialize_from_function(f: fn() -> T) -> T
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 moreSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Source§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<Ret> SpawnIfAsync<(), Ret> for Ret
impl<Ret> SpawnIfAsync<(), Ret> for Ret
Source§impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
Source§fn super_from(input: T) -> O
fn super_from(input: T) -> O
Source§impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
Source§fn super_into(self) -> O
fn super_into(self) -> O
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.