
# Objective - alternative to #2895 - as mentioned in #2535 the uuid based ids in the render module should be replaced with atomic-counted ones ## Solution - instead of generating a random UUID for each render resource, this implementation increases an atomic counter - this might be replaced by the ids of wgpu if they expose them directly in the future - I have not benchmarked this solution yet, but this should be slightly faster in theory. - Bevymark does not seem to be affected much by this change, which is to be expected. - Nothing of our API has changed, other than that the IDs have lost their IMO rather insignificant documentation. - Maybe the documentation could be added back into the macro, but this would complicate the code.
83 lines
1.8 KiB
Rust
83 lines
1.8 KiB
Rust
use crate::{define_atomic_id, render_resource::resource_macros::render_resource_wrapper};
|
|
use std::ops::{Bound, Deref, RangeBounds};
|
|
|
|
define_atomic_id!(BufferId);
|
|
render_resource_wrapper!(ErasedBuffer, wgpu::Buffer);
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Buffer {
|
|
id: BufferId,
|
|
value: ErasedBuffer,
|
|
}
|
|
|
|
impl Buffer {
|
|
#[inline]
|
|
pub fn id(&self) -> BufferId {
|
|
self.id
|
|
}
|
|
|
|
pub fn slice(&self, bounds: impl RangeBounds<wgpu::BufferAddress>) -> BufferSlice {
|
|
BufferSlice {
|
|
id: self.id,
|
|
// need to compute and store this manually because wgpu doesn't export offset on wgpu::BufferSlice
|
|
offset: match bounds.start_bound() {
|
|
Bound::Included(&bound) => bound,
|
|
Bound::Excluded(&bound) => bound + 1,
|
|
Bound::Unbounded => 0,
|
|
},
|
|
value: self.value.slice(bounds),
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn unmap(&self) {
|
|
self.value.unmap();
|
|
}
|
|
}
|
|
|
|
impl From<wgpu::Buffer> for Buffer {
|
|
fn from(value: wgpu::Buffer) -> Self {
|
|
Buffer {
|
|
id: BufferId::new(),
|
|
value: ErasedBuffer::new(value),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Deref for Buffer {
|
|
type Target = wgpu::Buffer;
|
|
|
|
#[inline]
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.value
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct BufferSlice<'a> {
|
|
id: BufferId,
|
|
offset: wgpu::BufferAddress,
|
|
value: wgpu::BufferSlice<'a>,
|
|
}
|
|
|
|
impl<'a> BufferSlice<'a> {
|
|
#[inline]
|
|
pub fn id(&self) -> BufferId {
|
|
self.id
|
|
}
|
|
|
|
#[inline]
|
|
pub fn offset(&self) -> wgpu::BufferAddress {
|
|
self.offset
|
|
}
|
|
}
|
|
|
|
impl<'a> Deref for BufferSlice<'a> {
|
|
type Target = wgpu::BufferSlice<'a>;
|
|
|
|
#[inline]
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.value
|
|
}
|
|
}
|