• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The vulkano developers
2 // Licensed under the Apache License, Version 2.0
3 // <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
6 // at your option. All files in the project carrying such
7 // notice may not be copied, modified, or distributed except
8 // according to those terms.
9 
10 use crate::format::Format;
11 
12 /// Describes an individual `Vertex`. In other words a collection of attributes that can be read
13 /// from a vertex shader.
14 ///
15 /// At this stage, the vertex is in a "raw" format. For example a `[f32; 4]` can match both a
16 /// `vec4` or a `float[4]`. The way the things are bound depends on the shader.
17 pub unsafe trait Vertex: 'static + Send + Sync {
18     /// Returns the characteristics of a vertex member by its name.
member(name: &str) -> Option<VertexMemberInfo>19     fn member(name: &str) -> Option<VertexMemberInfo>;
20 }
21 
22 unsafe impl Vertex for () {
23     #[inline]
member(_: &str) -> Option<VertexMemberInfo>24     fn member(_: &str) -> Option<VertexMemberInfo> {
25         None
26     }
27 }
28 
29 /// Information about a member of a vertex struct.
30 pub struct VertexMemberInfo {
31     /// Offset of the member in bytes from the start of the struct.
32     pub offset: usize,
33     /// Type of data. This is used to check that the interface is matching.
34     pub ty: VertexMemberTy,
35     /// Number of consecutive elements of that type.
36     pub array_size: usize,
37 }
38 
39 /// Type of a member of a vertex struct.
40 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
41 #[allow(missing_docs)]
42 pub enum VertexMemberTy {
43     I8,
44     U8,
45     I16,
46     U16,
47     I32,
48     U32,
49     F32,
50     F64,
51 }
52 
53 impl VertexMemberTy {
54     /// Returns true if a combination of `(type, array_size)` matches a format.
55     #[inline]
matches(&self, array_size: usize, format: Format, num_locs: u32) -> bool56     pub fn matches(&self, array_size: usize, format: Format, num_locs: u32) -> bool {
57         // TODO: implement correctly
58         let my_size = match *self {
59             VertexMemberTy::I8 => 1,
60             VertexMemberTy::U8 => 1,
61             VertexMemberTy::I16 => 2,
62             VertexMemberTy::U16 => 2,
63             VertexMemberTy::I32 => 4,
64             VertexMemberTy::U32 => 4,
65             VertexMemberTy::F32 => 4,
66             VertexMemberTy::F64 => 8,
67         };
68 
69         let format_size = match format.size() {
70             None => return false,
71             Some(s) => s,
72         } as usize;
73 
74         array_size * my_size == format_size * num_locs as usize
75     }
76 }
77