aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/draw_buffers.rs
blob: 837356aa4c4ef80c51dfb066b133ababdc3f999c (plain)
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
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::{draw::buffer::StagedBuffer, error::CreationError, types::*};
use hal::buffer::Usage;
use std::mem::ManuallyDrop;
use stockton_types::{Vector2, Vector3};

/// Represents a point of a triangle, including UV and texture information.
#[derive(Debug, Clone, Copy)]
pub struct UVPoint(pub Vector3, pub i32, pub Vector2);

/// Initial size of vertex buffer. TODO: Way of overriding this
pub const INITIAL_VERT_SIZE: u64 = 3 * 3000;

/// Initial size of index buffer. TODO: Way of overriding this
pub const INITIAL_INDEX_SIZE: u64 = 3000;

/// The buffers used for drawing, ie index and vertex buffer
pub struct DrawBuffers<'a, T: Sized> {
    pub vertex_buffer: ManuallyDrop<StagedBuffer<'a, T>>,
    pub index_buffer: ManuallyDrop<StagedBuffer<'a, (u16, u16, u16)>>,
}

impl<'a, T> DrawBuffers<'a, T> {
    pub fn new(device: &mut Device, adapter: &Adapter) -> Result<DrawBuffers<'a, T>, CreationError> {
        let vert = StagedBuffer::new(device, &adapter, Usage::VERTEX, INITIAL_VERT_SIZE)?;
        let index = StagedBuffer::new(device, &adapter, Usage::INDEX, INITIAL_INDEX_SIZE)?;

        Ok(DrawBuffers {
            vertex_buffer: ManuallyDrop::new(vert),
            index_buffer: ManuallyDrop::new(index),
        })
    }

    pub fn deactivate(self, device: &mut Device) {
        unsafe {
            use core::ptr::read;

            ManuallyDrop::into_inner(read(&self.vertex_buffer)).deactivate(device);
            ManuallyDrop::into_inner(read(&self.index_buffer)).deactivate(device);
        }
    }
}