Skip to content

Commit e60f614

Browse files
Brian MerchantBrian Merchant
andcommitted
Documenting BufferVec. (#4673)
# Objective Documents the `BufferVec` render resource. `BufferVec` is a fairly low level object, that will likely be managed by a higher level API (e.g. through [`encase`](#4272)) in the future. For now, since it is still used by some simple example crates (e.g. [bevy-vertex-pulling](https://github.com/superdump/bevy-vertex-pulling)), it will be helpful to provide some simple documentation on what `BufferVec` does. ## Solution I looked through Discord discussion on `BufferVec`, and found [a comment](https://discord.com/channels/691052431525675048/953222550568173580/956596218857918464 ) by @superdump to be particularly helpful, in the general discussion around `encase`. I have taken care to clarify where the data is stored (host-side), when the device-side buffer is created (through calls to `reserve`), and when data writes from host to device are scheduled (using `write_buffer` calls). --- ## Changelog - Added doc string for `BufferVec` and two of its methods: `reserve` and `write_buffer`. Co-authored-by: Brian Merchant <bhmerchant@gmail.com>
1 parent 84991d3 commit e60f614

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

crates/bevy_render/src/render_resource/buffer_vec.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ use bevy_core::{cast_slice, Pod};
66
use copyless::VecHelper;
77
use wgpu::BufferUsages;
88

9+
/// A structure for storing raw bytes that have already been properly formatted
10+
/// for use by the GPU.
11+
///
12+
/// "Properly formatted" means that item data already meets the alignment and padding
13+
/// requirements for how it will be used on the GPU.
14+
///
15+
/// Index, vertex, and instance-rate vertex buffers have no alignment nor padding requirements and
16+
/// so this helper type is a good choice for them. Uniform buffers must adhere to std140
17+
/// alignment/padding requirements, and storage buffers to std430. There are helper types for such
18+
/// buffers:
19+
/// - Uniform buffers
20+
/// - Plain: [`UniformBuffer`](crate::render_resource::UniformBuffer)
21+
/// - Dynamic offsets: [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer)
22+
/// - Storage buffers
23+
/// - Plain: [`StorageBuffer`](crate::render_resource::StorageBuffer)
24+
/// - Dynamic offsets: [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer)
25+
///
26+
/// The item type must implement [`Pod`] for its data representation to be directly copyable.
27+
///
28+
/// The contained data is stored in system RAM. Calling [`reserve`](crate::render_resource::BufferVec::reserve)
29+
/// allocates VRAM from the [`RenderDevice`](crate::renderer::RenderDevice).
30+
/// [`write_buffer`](crate::render_resource::BufferVec::write_buffer) queues copying of the data
31+
/// from system RAM to VRAM.
932
pub struct BufferVec<T: Pod> {
1033
values: Vec<T>,
1134
buffer: Option<Buffer>,
@@ -51,6 +74,17 @@ impl<T: Pod> BufferVec<T> {
5174
index
5275
}
5376

77+
/// Creates a [`Buffer`](crate::render_resource::Buffer) on the [`RenderDevice`](crate::renderer::RenderDevice) with size
78+
/// at least `std::mem::size_of::<T>() * capacity`, unless a such a buffer already exists.
79+
///
80+
/// If a [`Buffer`](crate::render_resource::Buffer) exists, but is too small, references to it will be discarded,
81+
/// and a new [`Buffer`](crate::render_resource::Buffer) will be created. Any previously created [`Buffer`](crate::render_resource::Buffer)s
82+
/// that are no longer referenced will be deleted by the [`RenderDevice`](crate::renderer::RenderDevice)
83+
/// once it is done using them (typically 1-2 frames).
84+
///
85+
/// In addition to any [`BufferUsages`](crate::render_resource::BufferUsages) provided when
86+
/// the `BufferVec` was created, the buffer on the [`RenderDevice`](crate::renderer::RenderDevice)
87+
/// is marked as [`BufferUsages::COPY_DST`](crate::render_resource::BufferUsages).
5488
pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) {
5589
if capacity > self.capacity {
5690
self.capacity = capacity;
@@ -64,6 +98,11 @@ impl<T: Pod> BufferVec<T> {
6498
}
6599
}
66100

101+
/// Queues writing of data from system RAM to VRAM using the [`RenderDevice`](crate::renderer::RenderDevice)
102+
/// and the provided [`RenderQueue`](crate::renderer::RenderQueue).
103+
///
104+
/// Before queuing the write, a [`reserve`](crate::render_resource::BufferVec::reserve) operation
105+
/// is executed.
67106
pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) {
68107
if self.values.is_empty() {
69108
return;

0 commit comments

Comments
 (0)