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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#[macro_export]
macro_rules! gfx_vertex {
($name:ident {
$($gl_name:ident@ $field:ident: $ty:ty,)*
}) => {
#[derive(Clone, Copy, Debug)]
pub struct $name {
$(pub $field: $ty,)*
}
impl $crate::VertexFormat for $name {
fn generate<R: $crate::Resources>(buffer: &$crate::handle::Buffer<R, $name>)
-> Vec<$crate::Attribute<R>> {
use std::mem::{size_of, forget};
use $crate::attrib::{Offset, Stride};
use $crate::attrib::format::ToFormat;
let stride = size_of::<$name>() as Stride;
let mut attributes = Vec::new();
$(
let (count, etype) = <$ty as ToFormat>::describe();
let tmp: $name = unsafe{ ::std::mem::uninitialized() };
let format = $crate::attrib::Format {
elem_count: count,
elem_type: etype,
offset: ((&tmp.$field as *const _ as usize) - (&tmp as *const _ as usize)) as Offset,
stride: stride,
instance_rate: 0,
};
forget(tmp);
attributes.push($crate::Attribute {
name: stringify!($gl_name).to_string(),
format: format,
buffer: buffer.raw().clone(),
});
)*
attributes
}
}
}
}
#[macro_export]
macro_rules! gfx_parameters {
($name:ident {
$($gl_name:ident@ $field:ident: $ty:ty,)*
}) => {
#[derive(Clone, Debug)]
pub struct $name<R: $crate::Resources> {
$(pub $field: $ty,)*
pub _r: ::std::marker::PhantomData<R>,
}
impl<R: $crate::Resources> $crate::shade::ShaderParam for $name<R> {
type Resources = R;
type Link = ($((Option<$crate::shade::ParameterId>, ::std::marker::PhantomData<$ty>),)*);
fn create_link(_: Option<&$name<R>>, info: &$crate::ProgramInfo)
-> Result<Self::Link, $crate::shade::ParameterError>
{
use $crate::shade::Parameter;
$(
let mut $field = None;
)*
for (i, u) in info.uniforms.iter().enumerate() {
match &u.name[..] {
$(
stringify!($gl_name) => {
if !<$ty as Parameter<R>>::check_uniform(u) {
return Err($crate::shade::ParameterError::BadUniform(u.name.clone()))
}
$field = Some(i as $crate::shade::ParameterId);
},
)*
_ => return Err($crate::shade::ParameterError::MissingUniform(u.name.clone()))
}
}
for (i, b) in info.blocks.iter().enumerate() {
match &b.name[..] {
$(
stringify!($gl_name) => {
if !<$ty as Parameter<R>>::check_block(b) {
return Err($crate::shade::ParameterError::BadBlock(b.name.clone()))
}
$field = Some(i as $crate::shade::ParameterId);
},
)*
_ => return Err($crate::shade::ParameterError::MissingBlock(b.name.clone()))
}
}
for (i, t) in info.textures.iter().enumerate() {
match &t.name[..] {
$(
stringify!($gl_name) => {
if !<$ty as Parameter<R>>::check_texture(t) {
return Err($crate::shade::ParameterError::BadBlock(t.name.clone()))
}
$field = Some(i as $crate::shade::ParameterId);
},
)*
_ => return Err($crate::shade::ParameterError::MissingBlock(t.name.clone()))
}
}
Ok(( $(($field, ::std::marker::PhantomData),)* ))
}
fn fill_params(&self, link: &Self::Link, storage: &mut $crate::ParamStorage<R>) {
use $crate::shade::Parameter;
let &($(($field, _),)*) = link;
$(
if let Some(id) = $field {
self.$field.put(id, storage);
}
)*
}
}
}
}