1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::*;

impl BufferHandler {
    /// Creates a buffer handler from a slice.
    #[inline(always)]
    pub fn from_slice<T: Sized + Pod + Zeroable, A: AsRef<[T]>>(
        vec: &A,
        device: &Device,
        usage: BufferUsage,
    ) -> Self
    {
        let buffer = device.create_buffer_init(&BufferInitDescriptor {
            contents: bytemuck::cast_slice(vec.as_ref()),
            usage,
            label: None,
        });
        let size = (vec.as_ref().len() * std::mem::size_of::<T>()) as u64;
        BufferHandler { buffer, size }
    }
    
    /// Returns the reference of the buffer.
    #[inline(always)]
    pub fn buffer(&self) -> &Buffer { &self.buffer }

    /// Returns the size of the buffer.
    #[inline(always)]
    pub fn size(&self) -> u64 { self.size }

    /// Creates a binding resource from buffer slice.
    #[inline(always)]
    pub fn binding_resource<'a>(&'a self) -> BindingResource<'a> {
        BindingResource::Buffer(self.buffer.slice(..))
    }

    /// Copy the values of buffer to `dest`.
    /// # Panic
    /// Panic occurs if the size of `dest` is smaller than the one of `self`.
    #[inline(always)]
    pub fn copy_buffer(&self, encoder: &mut CommandEncoder, dest: &BufferHandler) {
        assert!(
            self.size < dest.size,
            "The destination buffer size must be shorter than the source buffer size."
        );
        encoder.copy_buffer_to_buffer(&self.buffer, 0, &dest.buffer, 0, self.size);
    }
}