-
Notifications
You must be signed in to change notification settings - Fork 2
feat(lambda-rs): buffers to default to DEVICE_LOCAL as opposed to CPU_VISIBLE #180
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
Changes from 1 commit
51a70ea
f9c7357
bfc65a4
6071c93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,8 +81,13 @@ impl Default for Usage { | |
| /// Buffer allocation properties that control residency and CPU visibility. | ||
| #[derive(Clone, Copy, Debug)] | ||
| /// | ||
| /// Use `CPU_VISIBLE` for frequently updated data (e.g., uniform uploads). | ||
| /// Prefer `DEVICE_LOCAL` for static geometry uploaded once. | ||
| /// Use `CPU_VISIBLE` for buffers you plan to update from the CPU using | ||
| /// `Buffer::write_*` (this enables `wgpu::Queue::write_buffer` by adding the | ||
| /// required `COPY_DST` usage). | ||
| /// | ||
| /// Prefer `DEVICE_LOCAL` for static geometry uploaded once and never modified. | ||
| /// This is typically the best default for vertex and index buffers on discrete | ||
| /// GPUs, where CPU-visible memory may live in system RAM rather than VRAM. | ||
|
Comment on lines
+84
to
+90
|
||
| pub struct Properties { | ||
| cpu_visible: bool, | ||
| } | ||
|
|
@@ -101,7 +106,7 @@ impl Properties { | |
|
|
||
| impl Default for Properties { | ||
| fn default() -> Self { | ||
| Properties::CPU_VISIBLE | ||
| Properties::DEVICE_LOCAL | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -287,7 +292,7 @@ impl<T: PlainOldData> UniformBuffer<T> { | |
| /// let vertices: Vec<Vertex> = build_vertices(); | ||
| /// let vb = BufferBuilder::new() | ||
| /// .with_usage(Usage::VERTEX) | ||
| /// .with_properties(Properties::DEVICE_LOCAL) | ||
| /// // Defaults to `Properties::DEVICE_LOCAL` (recommended for static geometry). | ||
| /// .with_buffer_type(BufferType::Vertex) | ||
|
Comment on lines
354
to
359
|
||
| /// .build(render_context, vertices) | ||
| /// .unwrap(); | ||
|
|
@@ -308,11 +313,16 @@ impl Default for BufferBuilder { | |
|
|
||
| impl BufferBuilder { | ||
| /// Creates a new buffer builder of type vertex. | ||
| /// | ||
| /// Defaults: | ||
| /// - `usage`: `Usage::VERTEX` | ||
| /// - `properties`: `Properties::DEVICE_LOCAL` | ||
| /// - `buffer_type`: `BufferType::Vertex` | ||
| pub fn new() -> Self { | ||
| Self { | ||
| buffer_length: 0, | ||
| usage: Usage::VERTEX, | ||
| properties: Properties::CPU_VISIBLE, | ||
| properties: Properties::default(), | ||
| buffer_type: BufferType::Vertex, | ||
| label: None, | ||
| } | ||
|
|
@@ -396,7 +406,7 @@ impl BufferBuilder { | |
| return builder | ||
| .with_length(std::mem::size_of_val(mesh.vertices())) | ||
| .with_usage(Usage::VERTEX) | ||
| .with_properties(Properties::CPU_VISIBLE) | ||
| .with_properties(Properties::DEVICE_LOCAL) | ||
| .with_buffer_type(BufferType::Vertex) | ||
| .build(gpu, mesh.vertices().to_vec()); | ||
| } | ||
|
|
@@ -426,6 +436,17 @@ impl BufferBuilder { | |
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn properties_default_is_device_local() { | ||
| assert!(!Properties::default().cpu_visible()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn buffer_builder_defaults_to_device_local_properties() { | ||
| let builder = BufferBuilder::new(); | ||
| assert!(!builder.properties.cpu_visible()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_length_rejects_zero() { | ||
| let builder = BufferBuilder::new(); | ||
|
|
||
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.
With
Propertiesnow defaulting toDEVICE_LOCAL(i.e., noCOPY_DST), it becomes easy for callers to create a buffer and then callBuffer::write_*, which will hit a wgpu validation error/panic becauseQueue::write_bufferrequiresCOPY_DST. Consider makingwrite_bytes/write_value/write_slicereturn aResult(or adding an explicit runtime check that produces a clear error) when the underlying buffer wasn’t created withCPU_VISIBLE/COPY_DST.