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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::marker::PhantomData;
use gfx_core::{ConstantBufferSlot, Resources, MAX_VERTEX_ATTRIBUTES};
use gfx_core::{handle, pso, shade};
use gfx_core::factory::Typed;
use gfx_core::format::Format;
use shade::{ToUniform, Usage};
use super::{DataLink, DataBind, RawDataSet};
pub use gfx_core::pso::{Element, ElemOffset, ElemStride, InstanceRate};
pub trait Structure<F> {
fn query(&str) -> Option<Element<F>>;
}
type AttributeSlotSet = usize;
pub struct VertexBufferCommon<T, I>(RawVertexBuffer, PhantomData<(T, I)>);
pub type VertexBuffer<T> = VertexBufferCommon<T, [(); 0]>;
pub type InstanceBuffer<T> = VertexBufferCommon<T, [(); 1]>;
pub struct RawVertexBuffer(AttributeSlotSet);
pub struct ConstantBuffer<T: Structure<shade::ConstFormat>>(Option<(Usage, ConstantBufferSlot)>, PhantomData<T>);
pub struct Global<T: ToUniform>(Option<shade::Location>, PhantomData<T>);
fn match_attribute(_: &shade::AttributeVar, _: Format) -> bool {
true
}
impl<'a,
T: Structure<Format>,
I: AsRef<[()]> + Default,
> DataLink<'a> for VertexBufferCommon<T, I> {
type Init = ();
fn new() -> Self {
VertexBufferCommon(RawVertexBuffer(0), PhantomData)
}
fn is_active(&self) -> bool {
self.0.is_active()
}
fn link_input(&mut self, at: &shade::AttributeVar, _: &Self::Init) ->
Option<Result<pso::AttributeDesc, Format>> {
T::query(&at.name).map(|el| {
let rate = <I as Default>::default().as_ref().len();
self.0.link(at, el, rate as InstanceRate)
})
}
}
impl<R: Resources, T, I> DataBind<R> for VertexBufferCommon<T, I> {
type Data = handle::Buffer<R, T>;
fn bind_to(&self, out: &mut RawDataSet<R>, data: &Self::Data, man: &mut handle::Manager<R>) {
self.0.bind_to(out, data.raw(), man)
}
}
impl RawVertexBuffer {
fn link(&mut self, at: &shade::AttributeVar, el: Element<Format>, rate: InstanceRate)
-> Result<pso::AttributeDesc, Format> {
self.0 |= 1 << (at.slot as AttributeSlotSet);
if match_attribute(at, el.format) {
Ok((el, rate))
}else {
Err(el.format)
}
}
}
impl<'a> DataLink<'a> for RawVertexBuffer {
type Init = &'a [(&'a str, Element<Format>, InstanceRate)];
fn new() -> Self {
RawVertexBuffer(0)
}
fn is_active(&self) -> bool {
self.0 != 0
}
fn link_input(&mut self, at: &shade::AttributeVar, init: &Self::Init) ->
Option<Result<pso::AttributeDesc, Format>> {
init.iter().find(|x| x.0 == &at.name)
.map(|x| self.link(at, x.1, x.2))
}
}
impl<R: Resources> DataBind<R> for RawVertexBuffer {
type Data = handle::RawBuffer<R>;
fn bind_to(&self, out: &mut RawDataSet<R>, data: &Self::Data, man: &mut handle::Manager<R>) {
let value = Some((man.ref_buffer(data).clone(), 0));
for i in 0 .. MAX_VERTEX_ATTRIBUTES {
if (self.0 & (1<<i)) != 0 {
out.vertex_buffers.0[i] = value;
}
}
}
}
impl<'a, T: Structure<shade::ConstFormat>>
DataLink<'a> for ConstantBuffer<T> {
type Init = &'a str;
fn new() -> Self {
ConstantBuffer(None, PhantomData)
}
fn is_active(&self) -> bool {
self.0.is_some()
}
fn link_constant_buffer(&mut self, cb: &shade::ConstantBufferVar, init: &Self::Init) ->
Option<Result<(), shade::ConstFormat>> {
if &cb.name == *init {
self.0 = Some((cb.usage, cb.slot));
Some(Ok(()))
}else {
None
}
}
}
impl<R: Resources, T: Structure<shade::ConstFormat>>
DataBind<R> for ConstantBuffer<T> {
type Data = handle::Buffer<R, T>;
fn bind_to(&self, out: &mut RawDataSet<R>, data: &Self::Data, man: &mut handle::Manager<R>) {
if let Some((usage, slot)) = self.0 {
let buf = man.ref_buffer(data.raw()).clone();
out.constant_buffers.push(pso::ConstantBufferParam(buf, usage, slot));
}
}
}
impl<'a, T: ToUniform> DataLink<'a> for Global<T> {
type Init = &'a str;
fn new() -> Self {
Global(None, PhantomData)
}
fn is_active(&self) -> bool {
self.0.is_some()
}
fn link_global_constant(&mut self, var: &shade::ConstVar, init: &Self::Init) ->
Option<Result<(), shade::UniformValue>> {
if &var.name == *init {
self.0 = Some(var.location);
Some(Ok(()))
}else {
None
}
}
}
impl<R: Resources, T: ToUniform> DataBind<R> for Global<T> {
type Data = T;
fn bind_to(&self, out: &mut RawDataSet<R>, data: &Self::Data, _: &mut handle::Manager<R>) {
if let Some(loc) = self.0 {
let value = data.convert();
out.global_constants.push((loc, value));
}
}
}