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
use super::VertexFormat;
use bevy_utils::HashMap;
use std::borrow::Cow;

pub use bevy_derive::AsVertexBufferDescriptor;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VertexBufferDescriptor {
    pub name: Cow<'static, str>,
    pub stride: u64,
    pub step_mode: InputStepMode,
    pub attributes: Vec<VertexAttributeDescriptor>,
}

impl VertexBufferDescriptor {
    pub fn sync_with_descriptor(&mut self, descriptor: &VertexBufferDescriptor) {
        for attribute in self.attributes.iter_mut() {
            let descriptor_attribute = descriptor
                .attributes
                .iter()
                .find(|a| a.name == attribute.name)
                .unwrap_or_else(|| {
                    panic!(
                        "Encountered unsupported Vertex Buffer Attribute: {}",
                        attribute.name
                    );
                });
            attribute.offset = descriptor_attribute.offset;
        }

        self.stride = descriptor.stride;
    }
}

#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum InputStepMode {
    Vertex = 0,
    Instance = 1,
}

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct VertexAttributeDescriptor {
    pub name: Cow<'static, str>,
    pub offset: u64,
    pub format: VertexFormat,
    pub shader_location: u32,
}

#[derive(Default)]
pub struct VertexBufferDescriptors {
    pub descriptors: HashMap<String, VertexBufferDescriptor>,
}

impl VertexBufferDescriptors {
    pub fn set(&mut self, vertex_buffer_descriptor: VertexBufferDescriptor) {
        self.descriptors.insert(
            vertex_buffer_descriptor.name.to_string(),
            vertex_buffer_descriptor,
        );
    }

    pub fn get(&self, name: &str) -> Option<&VertexBufferDescriptor> {
        self.descriptors.get(name)
    }
}

pub trait AsVertexBufferDescriptor {
    fn as_vertex_buffer_descriptor() -> &'static VertexBufferDescriptor;
}