mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
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`
This commit is contained in:
parent
2b6bfdb5ea
commit
d0e1b43402
2 changed files with 39 additions and 4 deletions
|
@ -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<T: NoUninit> {
|
|||
}
|
||||
|
||||
impl<T: NoUninit> RawBufferVec<T> {
|
||||
/// 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<T: NoUninit> RawBufferVec<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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<BindingResource> {
|
||||
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<T: NoUninit> RawBufferVec<T> {
|
|||
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<T: NoUninit> RawBufferVec<T> {
|
|||
self.label = label;
|
||||
}
|
||||
|
||||
/// Returns the label
|
||||
pub fn get_label(&self) -> Option<&str> {
|
||||
self.label.as_deref()
|
||||
}
|
||||
|
@ -145,10 +164,12 @@ impl<T: NoUninit> RawBufferVec<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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<BindingResource> {
|
||||
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<BindingResource> {
|
||||
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;
|
||||
|
|
|
@ -91,9 +91,7 @@ impl<T: GpuArrayBufferable> GpuArrayBuffer<T> {
|
|||
pub fn binding(&self) -> Option<BindingResource> {
|
||||
match self {
|
||||
GpuArrayBuffer::Uniform(buffer) => buffer.binding(),
|
||||
GpuArrayBuffer::Storage(buffer) => {
|
||||
buffer.buffer().map(|buffer| buffer.as_entire_binding())
|
||||
}
|
||||
GpuArrayBuffer::Storage(buffer) => buffer.binding(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue