aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/buffers/draw.rs
blob: cd571e3e330e7301d7b655db74d3f8a1883f3603 (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
//! A vertex and index buffer set for drawing

use super::staged::StagedBuffer;
use crate::{
    context::RenderingContext,
    mem::{MappableBlock, MemoryPool},
};

use anyhow::{Context, Result};
use hal::buffer::Usage;
use std::mem::ManuallyDrop;

/// 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;

/// A vertex and index buffer set for drawing
pub struct DrawBuffers<'a, T: Sized, P: MemoryPool, SP: MemoryPool> {
    pub vertex_buffer: ManuallyDrop<StagedBuffer<'a, T, P, SP>>,
    pub index_buffer: ManuallyDrop<StagedBuffer<'a, (u16, u16, u16), P, SP>>,
}

impl<'a, T, P, SP> DrawBuffers<'a, T, P, SP>
where
    P: MemoryPool,
    SP: MemoryPool,
    SP::Block: MappableBlock,
{
    /// Create a new set of drawbuffers given a render context.
    /// This will allocate memory from `P` and `SP`, and currently has a fixed size (WIP).
    pub fn from_context(context: &mut RenderingContext) -> Result<Self> {
        let vert = StagedBuffer::from_context(context, Usage::VERTEX, INITIAL_VERT_SIZE)
            .context("Error creating vertex buffer")?;
        let index = StagedBuffer::from_context(context, Usage::INDEX, INITIAL_INDEX_SIZE)
            .context("Error creating index buffer")?;

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

    /// Destroy all Vulkan objects. Should be called before dropping.
    pub fn deactivate(self, context: &mut RenderingContext) {
        unsafe {
            use core::ptr::read;

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