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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::rc::Rc;

use wgpu::{util::DeviceExt, Buffer};

use crate::{Graphics, ToBinder};

pub trait ToGpuBuf {
    fn build_buffer(&self, graphics: &impl Graphics) -> GpuBuffer;
}

/// Handle to a GPU buffer.
pub struct GpuBuffer {
    buffer: Rc<Buffer>,
}

impl GpuBuffer {
    /// Creates a new buffer with uninitialized data.
    pub fn new(graphics: &impl Graphics) -> Self {
        let buffer = graphics
            .get_device()
            .create_buffer(&wgpu::BufferDescriptor {
                label: None,
                size: todo!(),
                usage: todo!(),
                mapped_at_creation: todo!(),
            });
        Self {
            buffer: Rc::new(buffer),
        }
    }

    /// Creates a buffer with the given bytes.
    pub fn init(graphics: &impl Graphics, data: &[u8]) -> Self {
        let buffer = graphics
            .get_device()
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: data,
                //TODO: User config
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            });

        Self {
            buffer: Rc::new(buffer),
        }
    }

    /// Update buffer GPU data with bytes provided.
    pub fn write(&mut self, graphics: &impl Graphics, data: &[u8]) {
        graphics.get_queue().write_buffer(&self.buffer, 0, data);
    }
}

impl ToBinder for GpuBuffer {
    fn get_layout(&self, index: u32) -> wgpu::BindGroupLayoutEntry {
        wgpu::BindGroupLayoutEntry {
            binding: index,
            //TODO: User should be able to config this.
            visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
            ty: wgpu::BindingType::Buffer {
                //TODO: User should be able to config this.
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        }
    }

    fn get_group(&self, index: u32) -> wgpu::BindGroupEntry {
        wgpu::BindGroupEntry {
            binding: index,
            resource: self.buffer.as_entire_binding(),
        }
    }
}

impl Clone for GpuBuffer {
    fn clone(&self) -> Self {
        Self {
            buffer: Rc::clone(&self.buffer),
        }
    }
}