bevy/pipelined/bevy_render2/src/render_resource/buffer.rs

82 lines
1.7 KiB
Rust
Raw Normal View History

2021-04-11 20:13:07 +00:00
use bevy_utils::Uuid;
2021-06-21 23:28:52 +00:00
use std::{ops::{Bound, Deref, RangeBounds}, sync::Arc};
2021-04-11 20:13:07 +00:00
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
pub struct BufferId(Uuid);
2021-06-21 23:28:52 +00:00
#[derive(Clone, Debug)]
pub struct Buffer {
id: BufferId,
value: Arc<wgpu::Buffer>,
2021-04-11 20:13:07 +00:00
}
2021-06-21 23:28:52 +00:00
impl Buffer {
#[inline]
pub fn id(&self) -> BufferId {
self.id
}
2021-04-11 20:13:07 +00:00
2021-06-21 23:28:52 +00:00
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),
2021-04-11 20:13:07 +00:00
}
}
2021-06-21 23:28:52 +00:00
#[inline]
pub fn unmap(&self) {
self.value.unmap()
}
2021-04-11 20:13:07 +00:00
}
2021-06-21 23:28:52 +00:00
impl From<wgpu::Buffer> for Buffer {
fn from(value: wgpu::Buffer) -> Self {
Buffer {
id: BufferId(Uuid::new_v4()),
value: Arc::new(value),
}
}
}
impl Deref for Buffer {
type Target = wgpu::Buffer;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
2021-04-11 20:13:07 +00:00
}
}
2021-06-21 23:28:52 +00:00
#[derive(Clone, Debug)]
pub struct BufferSlice<'a> {
id: BufferId,
offset: wgpu::BufferAddress,
value: wgpu::BufferSlice<'a>,
2021-04-11 20:13:07 +00:00
}
2021-06-21 23:28:52 +00:00
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
}
}