bevy_render/render_resource/
buffer.rs1use crate::{define_atomic_id, render_resource::resource_macros::render_resource_wrapper};
2use std::ops::{Bound, Deref, RangeBounds};
3
4define_atomic_id!(BufferId);
5render_resource_wrapper!(ErasedBuffer, wgpu::Buffer);
6
7#[derive(Clone, Debug)]
8pub struct Buffer {
9 id: BufferId,
10 value: ErasedBuffer,
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 BufferSlice {
21 id: self.id,
22 offset: match bounds.start_bound() {
24 Bound::Included(&bound) => bound,
25 Bound::Excluded(&bound) => bound + 1,
26 Bound::Unbounded => 0,
27 },
28 value: self.value.slice(bounds),
29 }
30 }
31
32 #[inline]
33 pub fn unmap(&self) {
34 self.value.unmap();
35 }
36}
37
38impl From<wgpu::Buffer> for Buffer {
39 fn from(value: wgpu::Buffer) -> Self {
40 Buffer {
41 id: BufferId::new(),
42 value: ErasedBuffer::new(value),
43 }
44 }
45}
46
47impl Deref for Buffer {
48 type Target = wgpu::Buffer;
49
50 #[inline]
51 fn deref(&self) -> &Self::Target {
52 &self.value
53 }
54}
55
56#[derive(Clone, Debug)]
57pub struct BufferSlice<'a> {
58 id: BufferId,
59 offset: wgpu::BufferAddress,
60 value: wgpu::BufferSlice<'a>,
61}
62
63impl<'a> BufferSlice<'a> {
64 #[inline]
65 pub fn id(&self) -> BufferId {
66 self.id
67 }
68
69 #[inline]
70 pub fn offset(&self) -> wgpu::BufferAddress {
71 self.offset
72 }
73}
74
75impl<'a> Deref for BufferSlice<'a> {
76 type Target = wgpu::BufferSlice<'a>;
77
78 #[inline]
79 fn deref(&self) -> &Self::Target {
80 &self.value
81 }
82}