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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::marker::PhantomData;
use core::{ConstantBufferSlot, Resources, MAX_VERTEX_ATTRIBUTES};
use core::{handle, pso, shade};
use core::memory::Typed;
use core::format::Format;
use shade::{ToUniform, Usage};
use super::{DataLink, DataBind, ElementError, RawDataSet, AccessInfo};
pub use core::pso::{BufferIndex, 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(Option<BufferIndex>, AttributeSlotSet);
pub struct ConstantBuffer<T: Structure<shade::ConstFormat>>(RawConstantBuffer, PhantomData<T>);
pub struct RawConstantBuffer(Option<(Usage, ConstantBufferSlot)>);
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(DataLink::new(), PhantomData)
}
fn is_active(&self) -> bool {
self.0.is_active()
}
fn link_vertex_buffer(&mut self, index: BufferIndex, _: &Self::Init)
-> Option<pso::VertexBufferDesc> {
use std::mem;
(self.0).0 = Some(index);
let rate = <I as Default>::default().as_ref().len();
Some(pso::VertexBufferDesc {
stride: mem::size_of::<T>() as ElemStride,
rate: rate as InstanceRate,
})
}
fn link_input(&mut self, at: &shade::AttributeVar, _: &Self::Init) ->
Option<Result<pso::AttributeDesc, Format>> {
T::query(&at.name).map(|el| {
self.0.link(at, el)
})
}
}
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>,
access: &mut AccessInfo<R>) {
self.0.bind_to(out, data.raw(), man, access)
}
}
impl RawVertexBuffer {
fn link(&mut self, at: &shade::AttributeVar, el: Element<Format>)
-> Result<pso::AttributeDesc, Format> {
self.1 |= 1 << (at.slot as AttributeSlotSet);
if match_attribute(at, el.format) {
Ok((self.0.unwrap(), el))
}else {
Err(el.format)
}
}
}
impl<'a> DataLink<'a> for RawVertexBuffer {
type Init = (&'a [(&'a str, Element<Format>)], ElemStride, InstanceRate);
fn new() -> Self {
RawVertexBuffer(None, 0)
}
fn is_active(&self) -> bool {
self.0.is_some()
}
fn link_vertex_buffer(&mut self, index: BufferIndex, init: &Self::Init)
-> Option<pso::VertexBufferDesc> {
self.0 = Some(index);
Some(pso::VertexBufferDesc {
stride: init.1,
rate: init.2,
})
}
fn link_input(&mut self, at: &shade::AttributeVar, init: &Self::Init) ->
Option<Result<pso::AttributeDesc, Format>> {
init.0.iter().find(|x| x.0 == &at.name)
.map(|x| self.link(at, x.1))
}
}
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>,
access: &mut AccessInfo<R>) {
let value = Some((man.ref_buffer(data).clone(), 0));
for i in 0 .. MAX_VERTEX_ATTRIBUTES {
if (self.1 & (1<<i)) != 0 {
out.vertex_buffers.0[i] = value;
}
}
if self.1 != 0 { access.buffer_read(data); }
}
}
impl<'a, T: Structure<shade::ConstFormat>>
DataLink<'a> for ConstantBuffer<T> {
type Init = &'a str;
fn new() -> Self {
ConstantBuffer(RawConstantBuffer::new(), PhantomData)
}
fn is_active(&self) -> bool {
self.0.is_active()
}
fn link_constant_buffer<'b>(&mut self, cb: &'b shade::ConstantBufferVar, init: &Self::Init)
-> Option<Result<pso::ConstantBufferDesc, ElementError<&'b str>>> {
let raw_out = self.0.link_constant_buffer(cb, init);
if raw_out.is_some() {
for el in cb.elements.iter() {
let err = match T::query(&el.name) {
Some(e) if e.offset != el.location as pso::ElemOffset =>
ElementError::Offset(el.name.as_str(), el.location as pso::ElemOffset),
None => ElementError::NotFound(el.name.as_str()),
Some(_) => continue,
};
self.0 = RawConstantBuffer::new();
return Some(Err(err));
}
}
raw_out
}
}
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>,
access: &mut AccessInfo<R>) {
self.0.bind_to(out, data.raw(), man, access)
}
}
impl<'a> DataLink<'a> for RawConstantBuffer {
type Init = &'a str;
fn new() -> Self {
RawConstantBuffer(None)
}
fn is_active(&self) -> bool {
self.0.is_some()
}
fn link_constant_buffer<'b>(&mut self, cb: &'b shade::ConstantBufferVar, init: &Self::Init)
-> Option<Result<pso::ConstantBufferDesc, ElementError<&'b str>>> {
if &cb.name == *init {
self.0 = Some((cb.usage, cb.slot));
Some(Ok(cb.usage))
}else {
None
}
}
}
impl<R: Resources> DataBind<R> for RawConstantBuffer {
type Data = handle::RawBuffer<R>;
fn bind_to(&self,
out: &mut RawDataSet<R>,
data: &Self::Data,
man: &mut handle::Manager<R>,
access: &mut AccessInfo<R>) {
if let Some((usage, slot)) = self.0 {
let buf = man.ref_buffer(data).clone();
out.constant_buffers.push(pso::ConstantBufferParam(buf, usage, slot));
access.buffer_read(data)
}
}
}
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>,
_: &mut AccessInfo<R>) {
if let Some(loc) = self.0 {
let value = data.convert();
out.global_constants.push((loc, value));
}
}
}