bevy_render/render_resource/
buffer.rs1use crate::define_atomic_id;
2use crate::renderer::WgpuWrapper;
3use core::ops::{Bound, Deref, RangeBounds};
4
5define_atomic_id!(BufferId);
6
7#[derive(Clone, Debug)]
8pub struct Buffer {
9 id: BufferId,
10 value: WgpuWrapper<wgpu::Buffer>,
11}
12
13impl Buffer {
14 #[inline]
15 pub fn id(&self) -> BufferId {
16 self.id
17 }
18
19 pub fn slice(&self, bounds: impl RangeBounds<wgpu::BufferAddress>) -> BufferSlice {
20 let offset = match bounds.start_bound() {
22 Bound::Included(&bound) => bound,
23 Bound::Excluded(&bound) => bound + 1,
24 Bound::Unbounded => 0,
25 };
26 let size = match bounds.end_bound() {
27 Bound::Included(&bound) => bound + 1,
28 Bound::Excluded(&bound) => bound,
29 Bound::Unbounded => self.value.size(),
30 } - offset;
31 BufferSlice {
32 id: self.id,
33 offset,
34 size,
35 value: self.value.slice(bounds),
36 }
37 }
38
39 #[inline]
40 pub fn unmap(&self) {
41 self.value.unmap();
42 }
43}
44
45impl From<wgpu::Buffer> for Buffer {
46 fn from(value: wgpu::Buffer) -> Self {
47 Buffer {
48 id: BufferId::new(),
49 value: WgpuWrapper::new(value),
50 }
51 }
52}
53
54impl Deref for Buffer {
55 type Target = wgpu::Buffer;
56
57 #[inline]
58 fn deref(&self) -> &Self::Target {
59 &self.value
60 }
61}
62
63#[derive(Clone, Debug)]
64pub struct BufferSlice<'a> {
65 id: BufferId,
66 offset: wgpu::BufferAddress,
67 value: wgpu::BufferSlice<'a>,
68 size: wgpu::BufferAddress,
69}
70
71impl<'a> BufferSlice<'a> {
72 #[inline]
73 pub fn id(&self) -> BufferId {
74 self.id
75 }
76
77 #[inline]
78 pub fn offset(&self) -> wgpu::BufferAddress {
79 self.offset
80 }
81
82 #[inline]
83 pub fn size(&self) -> wgpu::BufferAddress {
84 self.size
85 }
86}
87
88impl<'a> Deref for BufferSlice<'a> {
89 type Target = wgpu::BufferSlice<'a>;
90
91 #[inline]
92 fn deref(&self) -> &Self::Target {
93 &self.value
94 }
95}