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
use crate::{Dimension, Position};
#[derive(Clone)]
pub struct Attribute {
name: &'static str,
value: AttributeType,
}
#[derive(Clone)]
pub enum AttributeType {
Scalar,
Vector(Dimension),
Matrix(Dimension, Dimension),
}
impl Position {
#[cfg(target_arch = "wasm32")]
fn glsl_string(self) -> &'static str {
use Position::*;
match self {
Input => "attribute ",
Output => "varying ",
}
}
#[cfg(not(target_arch = "wasm32"))]
fn glsl_string(self) -> &'static str {
use Position::*;
match self {
Input => "in ",
Output => "out ",
}
}
}
impl Attribute {
pub fn new(name: &'static str, value: AttributeType) -> Attribute {
Attribute { name, value }
}
pub fn name(&self) -> &str {
&self.name
}
pub(crate) fn size(&self) -> i32 {
use AttributeType::*;
match self.value {
Scalar => 1,
Vector(n) => n as i32,
Matrix(m, n) => (m as i32) * (n as i32),
}
}
pub(crate) fn as_glsl(&self, _is_vertex: bool, pos: Position, shader: &mut String) {
use AttributeType::*;
#[cfg(target_arch = "wasm32")]
let pos = if _is_vertex { pos } else { Position::Output };
shader.push_str(pos.glsl_string());
let gl_type = match self.value {
Scalar => "float ".to_owned(),
Vector(n) => format!("vec{} ", n as i32),
Matrix(m, n) => format!("mat{}x{} ", m as i32, n as i32),
};
shader.push_str(&gl_type);
shader.push_str(self.name());
shader.push_str(";");
}
}