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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#![deny(missing_docs)]
use std::mem;
use draw_state::DrawState;
use draw_state::target::{ClearData, Mask, Mirror, Rect};
use device;
use device::Resources;
use device::{attrib, handle};
use device::attrib::IntSize;
use device::draw::{Access, Gamma, Target};
use device::draw::{CommandBuffer, DataBuffer, InstanceOption};
use device::shade::{ProgramInfo, UniformValue};
use render::batch::Batch;
use render::mesh::SliceKind;
pub mod batch;
pub mod mesh;
pub mod shade;
pub mod target;
type CachedAttribute<R: Resources> = (handle::RawBuffer<R>, attrib::Format);
struct RenderState<R: Resources> {
frame_buffer: Option<handle::FrameBuffer<R>>,
frame: target::Frame<R>,
gamma: Gamma,
is_array_buffer_set: bool,
program: Option<handle::Program<R>>,
index: Option<handle::RawBuffer<R>>,
attributes: Vec<Option<CachedAttribute<R>>>,
draw: DrawState,
}
impl<R: Resources> RenderState<R> {
fn new() -> RenderState<R> {
RenderState {
frame_buffer: None,
frame: target::Frame::empty(0,0),
gamma: Gamma::Original,
is_array_buffer_set: false,
program: None,
index: None,
attributes: Vec::new(),
draw: DrawState::new(),
}
}
}
pub struct ParamStorage<R: Resources> {
pub uniforms: Vec<Option<UniformValue>>,
pub blocks : Vec<Option<handle::RawBuffer<R>>>,
pub textures: Vec<Option<shade::TextureParam<R>>>,
}
impl<R: Resources> ParamStorage<R> {
fn new() -> ParamStorage<R> {
ParamStorage {
uniforms: Vec::new(),
blocks: Vec::new(),
textures: Vec::new(),
}
}
fn reserve(&mut self, pinfo: &ProgramInfo) {
self.uniforms.clear();
self.blocks .clear();
self.textures.clear();
self.uniforms.extend(pinfo.uniforms.iter().map(|_| None));
self.blocks .extend(pinfo.blocks .iter().map(|_| None));
self.textures.extend(pinfo.textures.iter().map(|_| None));
}
}
trait CommandBufferExt<R: Resources>: CommandBuffer<R> {
fn bind_target(&mut self, &mut handle::Manager<R>, Access, Target,
Option<&target::Plane<R>>);
}
impl<R: Resources, C: CommandBuffer<R>> CommandBufferExt<R> for C {
fn bind_target(&mut self, handles: &mut handle::Manager<R>, access: Access,
to: Target, plane: Option<&target::Plane<R>>) {
match plane {
None => self.unbind_target(access, to),
Some(&target::Plane::Surface(ref suf)) =>
self.bind_target_surface(access, to, handles.ref_surface(suf)),
Some(&target::Plane::Texture(ref tex, level, layer)) =>
self.bind_target_texture(access, to, handles.ref_texture(tex), level, layer),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum DrawError<E> {
InvalidBatch(E),
MissingTarget(Mask),
BadViewport,
BadVertexCount,
BadIndexCount,
}
pub struct Renderer<R: Resources, C: CommandBuffer<R>> {
command_buffer: C,
data_buffer: DataBuffer,
handles: handle::Manager<R>,
common_array_buffer: Result<handle::ArrayBuffer<R>, device::NotSupported>,
draw_frame_buffer: Result<handle::FrameBuffer<R>, device::NotSupported>,
read_frame_buffer: Result<handle::FrameBuffer<R>, device::NotSupported>,
render_state: RenderState<R>,
parameters: ParamStorage<R>,
}
impl<R: Resources, C: CommandBuffer<R>> Renderer<R, C> {
pub fn reset(&mut self) {
self.command_buffer.clear();
self.data_buffer.clear();
self.handles.clear();
self.render_state = RenderState::new();
}
pub fn as_buffer(&self) -> (&C, &DataBuffer, &handle::Manager<R>) {
(&self.command_buffer, &self.data_buffer, &self.handles)
}
pub fn clone_empty(&self) -> Renderer<R, C> {
Renderer {
command_buffer: CommandBuffer::new(),
data_buffer: DataBuffer::new(),
handles: handle::Manager::new(),
common_array_buffer: self.common_array_buffer.clone(),
draw_frame_buffer: self.draw_frame_buffer.clone(),
read_frame_buffer: self.read_frame_buffer.clone(),
render_state: RenderState::new(),
parameters: ParamStorage::new(),
}
}
pub fn clear<O: target::Output<R>>(&mut self, data: ClearData, mask: Mask, output: &O) {
let has_mask = output.get_mask();
if has_mask.is_empty() {
panic!("Clearing a frame without any attachments is not possible!
If you are using `Frame::empty` in place of a real output window,
please see https://github.com/gfx-rs/gfx-rs/pull/682");
}
debug_assert!(has_mask.contains(mask));
self.bind_output(output);
self.command_buffer.call_clear(data, mask);
}
pub fn draw<B: Batch<R>, O: target::Output<R>>(&mut self, batch: &B, output: &O)
-> Result<(), DrawError<B::Error>> {
self.draw_all(batch, None, output)
}
pub fn draw_instanced<B: Batch<R>, O: target::Output<R>>(&mut self, batch: &B,
count: device::InstanceCount, base: device::VertexCount,
output: &O) -> Result<(), DrawError<B::Error>> {
self.draw_all(batch, Some((count, base)), output)
}
fn draw_all<B: Batch<R>, O: target::Output<R>>(&mut self, batch: &B,
instances: InstanceOption, output: &O)
-> Result<(), DrawError<B::Error>> {
let (mesh, attrib_iter, slice, state) = match batch.get_data() {
Ok(data) => data,
Err(e) => return Err(DrawError::InvalidBatch(e)),
};
let target_missing = state.get_target_mask() - output.get_mask();
if !target_missing.is_empty() {
error!("Error drawing to the output {:?}. ", output.get_handle());
error!("Output mask: {:?}, State mask: {:?}, difference: {:?}",
output.get_mask(), state.get_target_mask(), target_missing);
return Err(DrawError::MissingTarget(target_missing))
}
self.bind_output(output);
let program = match self.bind_program(batch) {
Ok(p) => p,
Err(e) => return Err(DrawError::InvalidBatch(e)),
};
self.bind_state(state);
self.bind_mesh(mesh, attrib_iter, program.get_info());
self.draw_slice(slice, instances);
Ok(())
}
pub fn blit<I: target::Output<R>, O: target::Output<R>>(&mut self,
source: &I, source_rect: Rect,
destination: &O, dest_rect: Rect,
mirror: Mirror, mask: Mask) {
debug_assert!(source.get_mask().contains(mask));
debug_assert!(destination.get_mask().contains(mask));
self.bind_output(destination);
self.bind_pixel_input(source);
self.command_buffer.call_blit(source_rect, dest_rect, mirror, mask);
}
pub fn update_buffer_vec<T: Copy>(&mut self, buf: &handle::Buffer<R, T>,
data: &[T], offset_elements: usize) {
let esize = mem::size_of::<T>();
let offset_bytes = esize * offset_elements;
debug_assert!(data.len() * esize + offset_bytes <= buf.get_info().size);
let pointer = self.data_buffer.add_vec(data);
self.command_buffer.update_buffer(
self.handles.ref_buffer(buf.raw()), pointer, offset_bytes);
}
pub fn update_buffer_struct<U, T: Copy>(&mut self,
buf: &handle::Buffer<R, U>, data: &T) {
assert!(mem::size_of::<T>() <= buf.get_info().size);
let pointer = self.data_buffer.add_struct(data);
self.command_buffer.update_buffer(
self.handles.ref_buffer(buf.raw()), pointer, 0);
}
pub fn update_texture<T: Copy>(&mut self, tex: &handle::Texture<R>,
img: device::tex::ImageInfo, data: &[T]) {
debug_assert!(tex.get_info().contains(&img));
let pointer = self.data_buffer.add_vec(data);
self.command_buffer.update_texture(tex.get_info().kind, self.handles.ref_texture(tex), img, pointer);
}
fn bind_output<O: target::Output<R>>(&mut self, output: &O) {
let (width, height) = output.get_size();
if self.render_state.frame.width != width ||
self.render_state.frame.height != height {
self.command_buffer.set_viewport(Rect {x: 0, y: 0, w: width, h: height});
self.render_state.frame.width = width;
self.render_state.frame.height = height;
}
let gamma = output.get_gamma();
let change_gamma = self.render_state.gamma != gamma;
match output.get_handle() {
Some(ref handle) => {
if self.render_state.frame_buffer.as_ref() != Some(handle) || change_gamma {
self.command_buffer.bind_frame_buffer(Access::Draw,
self.handles.ref_frame_buffer(handle),
gamma);
self.render_state.frame_buffer = Some((*handle).clone());
self.render_state.gamma = gamma;
}
},
None => {
let draw_fbo = self.draw_frame_buffer.as_ref().ok().expect(
"Unable to use off-screen draw targets: not supported by the backend");
if self.render_state.frame_buffer.as_ref() != Some(draw_fbo) || change_gamma {
self.command_buffer.bind_frame_buffer(Access::Draw,
self.handles.ref_frame_buffer(draw_fbo),
gamma);
self.render_state.frame_buffer = Some(draw_fbo.clone());
self.render_state.gamma = gamma;
}
let colors = output.get_colors();
for (i, _) in self.render_state.frame.colors.iter().enumerate()
.skip(colors.len()) {
self.command_buffer.unbind_target(Access::Draw, Target::Color(i as u8));
}
self.render_state.frame.colors.truncate(colors.len());
for (i, (cur, new)) in self.render_state.frame.colors.iter_mut()
.zip(colors.iter()).enumerate() {
if *cur != *new {
self.command_buffer.bind_target(&mut self.handles,
Access::Draw, Target::Color(i as u8), Some(new));
*cur = new.clone();
}
}
self.command_buffer.set_draw_color_buffers(colors.len());
for (i, new) in colors.iter().enumerate()
.skip(self.render_state.frame.colors.len()) {
self.command_buffer.bind_target(&mut self.handles,
Access::Draw, Target::Color(i as u8), Some(new));
self.render_state.frame.colors.push(new.clone());
}
let depth = output.get_depth();
if self.render_state.frame.depth.as_ref() != depth {
self.command_buffer.bind_target(&mut self.handles,
Access::Draw, Target::Depth, depth);
self.render_state.frame.depth = depth.map(|p| p.clone());
}
let stencil = output.get_stencil();
if self.render_state.frame.stencil.as_ref() != stencil {
self.command_buffer.bind_target(&mut self.handles,
Access::Draw, Target::Stencil, stencil);
self.render_state.frame.stencil = stencil.map(|p| p.clone());
}
},
}
}
fn bind_pixel_input<I: target::Output<R>>(&mut self, input: &I) {
if let Some(ref handle) = input.get_handle() {
self.command_buffer.bind_frame_buffer(Access::Read,
self.handles.ref_frame_buffer(handle),
Gamma::Original);
}else if let Ok(ref fbo) = self.read_frame_buffer {
self.command_buffer.bind_frame_buffer(Access::Read,
self.handles.ref_frame_buffer(fbo),
Gamma::Original);
}else {
panic!("Unable to use off-screen read targets: not supported by the backend");
}
match input.get_colors().first() {
Some(ref color) => {
self.command_buffer.bind_target(&mut self.handles,
Access::Read, Target::Color(0), Some(color));
},
None => {
self.command_buffer.unbind_target(Access::Read, Target::Color(0));
},
}
self.command_buffer.bind_target(&mut self.handles,
Access::Read, Target::Depth, input.get_depth());
self.command_buffer.bind_target(&mut self.handles,
Access::Read, Target::Stencil, input.get_stencil());
}
fn bind_state(&mut self, state: &DrawState) {
if self.render_state.draw.primitive != state.primitive {
self.command_buffer.set_primitive(state.primitive);
}
if self.render_state.draw.multi_sample != state.multi_sample {
self.command_buffer.set_multi_sample(state.multi_sample);
}
if self.render_state.draw.scissor != state.scissor {
self.command_buffer.set_scissor(state.scissor);
}
if self.render_state.draw.depth != state.depth || self.render_state.draw.stencil != state.stencil ||
self.render_state.draw.primitive.get_cull_face() != state.primitive.get_cull_face() {
self.command_buffer.set_depth_stencil(state.depth, state.stencil,
state.primitive.get_cull_face());
}
if self.render_state.draw.blend != state.blend {
self.command_buffer.set_blend(state.blend);
}
if self.render_state.draw.color_mask != state.color_mask {
self.command_buffer.set_color_mask(state.color_mask);
}
self.render_state.draw = *state;
}
fn bind_program<'a, B: Batch<R>>(&mut self, batch: &'a B)
-> Result<&'a handle::Program<R>, B::Error> {
let program = match batch.fill_params(&mut self.parameters) {
Ok(p) => p,
Err(e) => return Err(e),
};
if self.render_state.program.as_ref() != Some(&program) {
self.render_state.program = Some(program.clone());
self.command_buffer.bind_program(
self.handles.ref_program(&program));
}
self.upload_parameters(program);
Ok(program)
}
fn upload_parameters(&mut self, program: &handle::Program<R>) {
let info = program.get_info();
for (var, value) in info.uniforms.iter()
.zip(self.parameters.uniforms.iter()) {
match value {
&Some(v) => self.command_buffer.bind_uniform(var.location, v),
&None => error!("Missed uniform {}", var.name),
}
}
for (i, (var, value)) in info.blocks.iter()
.zip(self.parameters.blocks.iter()).enumerate() {
match value {
&Some(ref buf) => self.command_buffer.bind_uniform_block(
self.handles.ref_program(program),
i as device::UniformBufferSlot,
i as device::UniformBlockIndex,
self.handles.ref_buffer(buf)
),
&None => error!("Missed block {}", var.name),
}
}
for (i, (var, value)) in info.textures.iter()
.zip(self.parameters.textures.iter()).enumerate() {
match value {
&Some((ref tex, ref sampler)) => {
let sam = match sampler {
&Some(ref s) => {
if tex.get_info().kind.get_aa_mode().is_some() {
error!("A sampler provided for an AA texture: {}", var.name);
}
Some((self.handles.ref_sampler(s), *s.get_info()))
},
&None => None,
};
self.command_buffer.bind_uniform(var.location, UniformValue::I32(i as i32));
self.command_buffer.bind_texture(i as device::TextureSlot, tex.get_info().kind,
self.handles.ref_texture(tex), sam);
},
&None => error!("Missed texture {}", var.name),
}
}
}
fn bind_mesh<I: Iterator<Item = mesh::AttributeIndex>>(&mut self,
mesh: &mesh::Mesh<R>, attrib_iter: I, info: &ProgramInfo) {
if !self.render_state.is_array_buffer_set {
match self.common_array_buffer {
Ok(ref ab) => self.command_buffer.bind_array_buffer(
self.handles.ref_array_buffer(ab)
),
Err(_) => (),
};
self.render_state.is_array_buffer_set = true;
}
for (attr_index, sat) in attrib_iter.zip(info.attributes.iter()) {
let vat = &mesh.attributes[attr_index];
let loc = sat.location;
if loc >= self.render_state.attributes.len() {
let range = self.render_state.attributes.len() .. loc+1;
self.render_state.attributes.extend(range.map(|_| None));
}
let need_update = match self.render_state.attributes[loc] {
Some((ref buf, fmt)) => *buf != vat.buffer || fmt != vat.format,
None => true,
};
if need_update {
self.command_buffer.bind_attribute(loc as device::AttributeSlot,
self.handles.ref_buffer(&vat.buffer), vat.format);
self.render_state.attributes[loc] = Some((vat.buffer.clone(), vat.format));
}
}
}
fn bind_index<T>(&mut self, buf: &handle::IndexBuffer<R, T>) {
if self.render_state.index.as_ref() != Some(buf.raw()) {
self.render_state.index = Some(buf.raw().clone());
self.command_buffer.bind_index(self.handles.ref_buffer(buf.raw()));
}
}
fn draw_slice(&mut self, slice: &mesh::Slice<R>, instances: InstanceOption) {
let &mesh::Slice { start, end, prim_type, ref kind } = slice;
match *kind {
SliceKind::Vertex => {
self.command_buffer.call_draw(prim_type, start, end - start, instances);
},
SliceKind::Index8(ref buf, base) => {
self.bind_index(buf);
self.command_buffer.call_draw_indexed(prim_type, IntSize::U8,
start, end - start, base, instances);
},
SliceKind::Index16(ref buf, base) => {
self.bind_index(buf);
self.command_buffer.call_draw_indexed(prim_type, IntSize::U16,
start, end - start, base, instances);
},
SliceKind::Index32(ref buf, base) => {
self.bind_index(buf);
self.command_buffer.call_draw_indexed(prim_type, IntSize::U32,
start, end - start, base, instances);
},
}
}
}
pub trait RenderFactory<R: Resources, C: CommandBuffer<R>> {
fn create_renderer(&mut self) -> Renderer<R, C>;
}
impl<
R: Resources,
C: CommandBuffer<R>,
F: device::Factory<R>,
> RenderFactory<R, C> for F {
fn create_renderer(&mut self) -> Renderer<R, C> {
Renderer {
command_buffer: CommandBuffer::new(),
data_buffer: DataBuffer::new(),
handles: handle::Manager::new(),
common_array_buffer: self.create_array_buffer(),
draw_frame_buffer: self.create_frame_buffer(),
read_frame_buffer: self.create_frame_buffer(),
render_state: RenderState::new(),
parameters: ParamStorage::new(),
}
}
}