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},
|
internal::{WriteInto, Writer},
|
||||||
ShaderType,
|
ShaderType,
|
||||||
};
|
};
|
||||||
use wgpu::{BufferAddress, BufferUsages};
|
use wgpu::{BindingResource, BufferAddress, BufferUsages};
|
||||||
|
|
||||||
use super::GpuArrayBufferable;
|
use super::GpuArrayBufferable;
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@ pub struct RawBufferVec<T: NoUninit> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: NoUninit> RawBufferVec<T> {
|
impl<T: NoUninit> RawBufferVec<T> {
|
||||||
|
/// Creates a new [`RawBufferVec`] with the given [`BufferUsages`].
|
||||||
pub const fn new(buffer_usage: BufferUsages) -> Self {
|
pub const fn new(buffer_usage: BufferUsages) -> Self {
|
||||||
Self {
|
Self {
|
||||||
values: Vec::new(),
|
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]
|
#[inline]
|
||||||
pub fn buffer(&self) -> Option<&Buffer> {
|
pub fn buffer(&self) -> Option<&Buffer> {
|
||||||
self.buffer.as_ref()
|
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]
|
#[inline]
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.capacity
|
self.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the number of items that have been pushed to this buffer.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.values.len()
|
self.values.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if the buffer is empty.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.values.is_empty()
|
self.values.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a new value and returns its index.
|
||||||
pub fn push(&mut self, value: T) -> usize {
|
pub fn push(&mut self, value: T) -> usize {
|
||||||
let index = self.values.len();
|
let index = self.values.len();
|
||||||
self.values.push(value);
|
self.values.push(value);
|
||||||
|
@ -89,6 +103,10 @@ impl<T: NoUninit> RawBufferVec<T> {
|
||||||
self.values.append(&mut other.values);
|
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>) {
|
pub fn set_label(&mut self, label: Option<&str>) {
|
||||||
let label = label.map(str::to_string);
|
let label = label.map(str::to_string);
|
||||||
|
|
||||||
|
@ -99,6 +117,7 @@ impl<T: NoUninit> RawBufferVec<T> {
|
||||||
self.label = label;
|
self.label = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the label
|
||||||
pub fn get_label(&self) -> Option<&str> {
|
pub fn get_label(&self) -> Option<&str> {
|
||||||
self.label.as_deref()
|
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) {
|
pub fn truncate(&mut self, len: usize) {
|
||||||
self.values.truncate(len);
|
self.values.truncate(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes all elements from the buffer.
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.values.clear();
|
self.values.clear();
|
||||||
}
|
}
|
||||||
|
@ -217,6 +238,14 @@ where
|
||||||
self.buffer.as_ref()
|
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.
|
/// Returns the amount of space that the GPU will use before reallocating.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
|
@ -372,6 +401,14 @@ where
|
||||||
self.buffer.as_ref()
|
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.
|
/// Reserves space for one more element in the buffer and returns its index.
|
||||||
pub fn add(&mut self) -> usize {
|
pub fn add(&mut self) -> usize {
|
||||||
let index = self.len;
|
let index = self.len;
|
||||||
|
|
|
@ -91,9 +91,7 @@ impl<T: GpuArrayBufferable> GpuArrayBuffer<T> {
|
||||||
pub fn binding(&self) -> Option<BindingResource> {
|
pub fn binding(&self) -> Option<BindingResource> {
|
||||||
match self {
|
match self {
|
||||||
GpuArrayBuffer::Uniform(buffer) => buffer.binding(),
|
GpuArrayBuffer::Uniform(buffer) => buffer.binding(),
|
||||||
GpuArrayBuffer::Storage(buffer) => {
|
GpuArrayBuffer::Storage(buffer) => buffer.binding(),
|
||||||
buffer.buffer().map(|buffer| buffer.as_entire_binding())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue