-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Merged by Bors] - Documenting BufferVec
.
#4673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11727c5
Documenting `BufferVec`.
9ef6d24
Replace RenderDevice by GPU.
5ffd580
State Pod as the direct requirement; and reflect reality of encase no…
da1f666
Say "system RAM" instead of "host memory" and "VRAM" instead of "devi…
e407b97
remove repetition of "data"
c50f80c
run cargo fmt
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,29 @@ use bevy_core::{cast_slice, Pod}; | |
use copyless::VecHelper; | ||
use wgpu::BufferUsages; | ||
|
||
/// 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. | ||
/// | ||
/// Index, vertex, and instance-rate vertex buffers have no alignment nor padding requirements and | ||
/// so this helper type is a good choice for them. Uniform buffers must adhere to std140 | ||
/// alignment/padding requirements, and storage buffers to std430. There are helper types for such | ||
/// buffers: | ||
/// - Uniform buffers | ||
/// - Plain: [`UniformBuffer`](crate::render_resource::UniformBuffer) | ||
/// - Dynamic offsets: [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer) | ||
/// - Storage buffers | ||
/// - Plain: [`StorageBuffer`](crate::render_resource::StorageBuffer) | ||
/// - Dynamic offsets: [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer) | ||
/// | ||
/// The item type must implement [`Pod`] for its data representation to be directly copyable. | ||
/// | ||
/// The contained data is stored in system RAM. Calling [`reserve`](crate::render_resource::BufferVec::reserve) | ||
/// allocates VRAM from the [`RenderDevice`](crate::renderer::RenderDevice). | ||
/// [`write_buffer`](crate::render_resource::BufferVec::write_buffer) queues copying of the data | ||
/// from system RAM to VRAM. | ||
pub struct BufferVec<T: Pod> { | ||
values: Vec<T>, | ||
buffer: Option<Buffer>, | ||
|
@@ -60,6 +83,17 @@ impl<T: Pod> BufferVec<T> { | |
index | ||
} | ||
|
||
/// Creates a [`Buffer`](crate::render_resource::Buffer) on the [`RenderDevice`](crate::renderer::RenderDevice) with size | ||
/// at least `std::mem::size_of::<T>() * capacity`, unless a such a buffer already exists. | ||
/// | ||
/// If a [`Buffer`](crate::render_resource::Buffer) exists, but is too small, references to it will be discarded, | ||
/// and a new [`Buffer`](crate::render_resource::Buffer) will be created. Any previously created [`Buffer`](crate::render_resource::Buffer)s | ||
/// that are no longer referenced will be deleted by the [`RenderDevice`](crate::renderer::RenderDevice) | ||
/// once it is done using them (typically 1-2 frames). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice attention to detail! |
||
/// | ||
/// In addition to any [`BufferUsages`](crate::render_resource::BufferUsages) provided when | ||
/// the `BufferVec` was created, the buffer on the [`RenderDevice`](crate::renderer::RenderDevice) | ||
/// is marked as [`BufferUsages::COPY_DST`](crate::render_resource::BufferUsages). | ||
pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) { | ||
if capacity > self.capacity { | ||
self.capacity = capacity; | ||
|
@@ -73,6 +107,11 @@ impl<T: Pod> BufferVec<T> { | |
} | ||
} | ||
|
||
/// Queues writing of data from system RAM to VRAM using the [`RenderDevice`](crate::renderer::RenderDevice) | ||
/// and the provided [`RenderQueue`](crate::renderer::RenderQueue). | ||
/// | ||
/// Before queuing the write, a [`reserve`](crate::render_resource::BufferVec::reserve) operation | ||
/// is executed. | ||
pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) { | ||
if self.values.is_empty() { | ||
return; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small usability nit: Please update these other types' docs to point back to this more detailed one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? Over at UniformBuffer add a link to here? Or you mean for each of the helpers, link to the others?
I definitely think we should have some bevy book docs for how to get data into your shader, and how to decide which way is appropriate for your needs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my understanding, and what I would like to see.