From d0e1b43402e9b9f1cd7b6d7ed9a157e0066d3d10 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Tue, 4 Jun 2024 11:36:08 -0400 Subject: [PATCH] Add `binding()` helper function to `BufferVec` (#13667) # Objective - Other render resources have a convenient `.binding()` helper function to get the binding to the resource ## Solution - Add the same thing to `BufferVec`, `RawBufferVec`, and `UninitBufferVec` --- .../src/render_resource/buffer_vec.rs | 39 ++++++++++++++++++- .../src/render_resource/gpu_array_buffer.rs | 4 +- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/render_resource/buffer_vec.rs b/crates/bevy_render/src/render_resource/buffer_vec.rs index 4db7bacd3e..fdf6411361 100644 --- a/crates/bevy_render/src/render_resource/buffer_vec.rs +++ b/crates/bevy_render/src/render_resource/buffer_vec.rs @@ -9,7 +9,7 @@ use encase::{ internal::{WriteInto, Writer}, ShaderType, }; -use wgpu::{BufferAddress, BufferUsages}; +use wgpu::{BindingResource, BufferAddress, BufferUsages}; use super::GpuArrayBufferable; @@ -47,6 +47,7 @@ pub struct RawBufferVec { } impl RawBufferVec { + /// Creates a new [`RawBufferVec`] with the given [`BufferUsages`]. pub const fn new(buffer_usage: BufferUsages) -> Self { Self { values: Vec::new(), @@ -59,26 +60,39 @@ impl RawBufferVec { } } + /// Returns a handle to the buffer, if the data has been uploaded. #[inline] pub fn buffer(&self) -> Option<&Buffer> { self.buffer.as_ref() } + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + + /// Returns the amount of space that the GPU will use before reallocating. #[inline] pub fn capacity(&self) -> usize { self.capacity } + /// Returns the number of items that have been pushed to this buffer. #[inline] pub fn len(&self) -> usize { self.values.len() } + /// Returns true if the buffer is empty. #[inline] pub fn is_empty(&self) -> bool { self.values.is_empty() } + /// Adds a new value and returns its index. pub fn push(&mut self, value: T) -> usize { let index = self.values.len(); self.values.push(value); @@ -89,6 +103,10 @@ impl RawBufferVec { self.values.append(&mut other.values); } + /// 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. pub fn set_label(&mut self, label: Option<&str>) { let label = label.map(str::to_string); @@ -99,6 +117,7 @@ impl RawBufferVec { self.label = label; } + /// Returns the label pub fn get_label(&self) -> Option<&str> { self.label.as_deref() } @@ -145,10 +164,12 @@ impl RawBufferVec { } } + /// Reduces the length of the buffer. pub fn truncate(&mut self, len: usize) { self.values.truncate(len); } + /// Removes all elements from the buffer. pub fn clear(&mut self) { self.values.clear(); } @@ -217,6 +238,14 @@ where self.buffer.as_ref() } + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + /// Returns the amount of space that the GPU will use before reallocating. #[inline] pub fn capacity(&self) -> usize { @@ -372,6 +401,14 @@ where self.buffer.as_ref() } + /// Returns the binding for the buffer if the data has been uploaded. + #[inline] + pub fn binding(&self) -> Option { + Some(BindingResource::Buffer( + self.buffer()?.as_entire_buffer_binding(), + )) + } + /// Reserves space for one more element in the buffer and returns its index. pub fn add(&mut self) -> usize { let index = self.len; diff --git a/crates/bevy_render/src/render_resource/gpu_array_buffer.rs b/crates/bevy_render/src/render_resource/gpu_array_buffer.rs index 9765cfc627..85d44699c7 100644 --- a/crates/bevy_render/src/render_resource/gpu_array_buffer.rs +++ b/crates/bevy_render/src/render_resource/gpu_array_buffer.rs @@ -91,9 +91,7 @@ impl GpuArrayBuffer { pub fn binding(&self) -> Option { match self { GpuArrayBuffer::Uniform(buffer) => buffer.binding(), - GpuArrayBuffer::Storage(buffer) => { - buffer.buffer().map(|buffer| buffer.as_entire_binding()) - } + GpuArrayBuffer::Storage(buffer) => buffer.binding(), } }