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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use crate::device::{DropManager, ResourceId};
use crate::pipeline::*;
use crate::Device;
use std::sync::Arc;

#[derive(Debug)]
struct BufferIdRef {
    id: u64,
    drop_manager: Arc<DropManager>,
}

impl Drop for BufferIdRef {
    fn drop(&mut self) {
        self.drop_manager.push(ResourceId::Buffer(self.id));
    }
}

/// GPU buffer
#[derive(Debug, Clone)]
pub struct Buffer {
    id: u64,
    _id_ref: Arc<BufferIdRef>,
    pub usage: BufferUsage,
    pub draw: Option<DrawType>,
}

impl Buffer {
    pub(crate) fn new(
        id: u64,
        usage: BufferUsage,
        draw: Option<DrawType>,
        drop_manager: Arc<DropManager>,
    ) -> Self {
        let id_ref = Arc::new(BufferIdRef { id, drop_manager });

        Self {
            id,
            _id_ref: id_ref,
            usage,
            draw,
        }
    }

    #[inline(always)]
    pub fn id(&self) -> u64 {
        self.id
    }
}

impl std::cmp::PartialEq for Buffer {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

pub struct VertexBufferBuilder<'a> {
    device: &'a mut Device,
    data: Option<&'a [f32]>,
    vertex_attrs: Vec<VertexAttr>,
    vertex_step_mode: VertexStepMode,
}

impl<'a> VertexBufferBuilder<'a> {
    pub fn new(device: &'a mut Device) -> Self {
        Self {
            device,
            data: None,
            vertex_attrs: vec![],
            vertex_step_mode: VertexStepMode::Vertex,
        }
    }

    pub fn with_data(mut self, data: &'a [f32]) -> Self {
        self.data = Some(data);
        self
    }

    pub fn with_info(mut self, info: &VertexInfo) -> Self {
        self.vertex_attrs = info.attrs.clone();
        self.vertex_step_mode = info.step_mode;
        self
    }

    pub fn build(self) -> Result<Buffer, String> {
        let Self {
            device,
            data,
            vertex_attrs,
            vertex_step_mode,
        } = self;

        debug_assert!(
            !vertex_attrs.is_empty(),
            "Missing vertex attributes for a VertexBuffer"
        );

        device.inner_create_vertex_buffer(data, &vertex_attrs, vertex_step_mode)
    }
}

pub struct IndexBufferBuilder<'a> {
    device: &'a mut Device,
    data: Option<&'a [u32]>,
}

impl<'a> IndexBufferBuilder<'a> {
    pub fn new(device: &'a mut Device) -> Self {
        Self { device, data: None }
    }

    pub fn with_data(mut self, data: &'a [u32]) -> Self {
        self.data = Some(data);
        self
    }

    pub fn build(self) -> Result<Buffer, String> {
        let Self { device, data } = self;

        device.inner_create_index_buffer(data)
    }
}

pub struct UniformBufferBuilder<'a> {
    device: &'a mut Device,
    data: Option<&'a [f32]>,
    name: String,
    loc: u32,
}

impl<'a> UniformBufferBuilder<'a> {
    pub fn new(device: &'a mut Device, location: u32, name: &str) -> Self {
        Self {
            device,
            data: None,
            name: name.to_string(),
            loc: location,
        }
    }

    pub fn with_data(mut self, data: &'a [f32]) -> Self {
        self.data = Some(data);
        self
    }

    pub fn build(self) -> Result<Buffer, String> {
        let Self {
            device,
            data,
            name,
            loc,
        } = self;

        device.inner_create_uniform_buffer(loc, &name, data)
    }
}

#[derive(Clone, Debug, Default)]
pub struct VertexInfo {
    pub(crate) attrs: Vec<VertexAttr>,
    pub(crate) step_mode: VertexStepMode,
}

impl VertexInfo {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn attr(mut self, location: u32, format: VertexFormat) -> Self {
        self.attrs.push(VertexAttr::new(location, format));
        self
    }

    pub fn step_mode(mut self, mode: VertexStepMode) -> Self {
        self.step_mode = mode;
        self
    }
}

#[derive(Clone, Copy, Debug)]
pub enum BufferUsage {
    Vertex,
    Index,
    Uniform(u32),
}

#[derive(Debug, Copy, Clone)]
pub struct VertexAttr {
    pub location: u32,
    pub format: VertexFormat,
}

impl VertexAttr {
    pub fn new(location: u32, vertex_data: VertexFormat) -> Self {
        Self {
            location,
            format: vertex_data,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub enum VertexStepMode {
    Vertex,
    Instance,
}

impl Default for VertexStepMode {
    fn default() -> Self {
        VertexStepMode::Vertex
    }
}

// FIXME: VertexBuffer only support f32, add u8 support
#[derive(Debug, Clone, Copy)]
pub enum VertexFormat {
    Float1,
    Float2,
    Float3,
    Float4,
    // Uint1,
    // Uint2,
    // Uint3,
    // Uint4
}

impl VertexFormat {
    pub fn size(&self) -> i32 {
        match self {
            VertexFormat::Float1 => 1,
            VertexFormat::Float2 => 2,
            VertexFormat::Float3 => 3,
            VertexFormat::Float4 => 4,
            // VertexFormat::Uint1 => 1,
            // VertexFormat::Uint2 => 2,
            // VertexFormat::Uint3 => 3,
            // VertexFormat::Uint4 => 4,
        }
    }

    pub fn bytes(&self) -> i32 {
        self.size() * 4
        // TODO if Uint return self.size() if float return self.size() * 4
    }

    pub fn normalized(&self) -> bool {
        false //check type
              // todo u8 = true, float = false
    }
}