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
#![crate_name = "amethyst_renderer"]
#![crate_type = "lib"]
#![doc(html_logo_url = "http://tinyurl.com/hgsb45k")]
extern crate cgmath;
extern crate fnv;
extern crate glutin;
extern crate specs;
#[macro_use]
extern crate gfx;
#[macro_use]
extern crate mopa;
pub mod pass;
pub mod target;
use fnv::FnvHashMap as HashMap;
use specs::{Component, VecStorage};
use std::any::TypeId;
pub use pass::{Pass, PassDescription};
pub use target::Target;
pub struct Renderer<R: gfx::Resources, C: gfx::CommandBuffer<R>> {
cmd_buf: gfx::Encoder<R, C>,
passes: HashMap<(TypeId, TypeId),
Box<Fn(&Box<PassDescription>,
&Target,
&Pipeline,
&Scene<R>,
&mut gfx::Encoder<R, C>)>>,
}
#[allow(missing_docs)]
gfx_vertex_struct!(VertexPosNormal {
pos: [f32; 3] = "a_Pos",
normal: [f32; 3] = "a_Normal",
tex_coord: [f32; 2] = "a_TexCoord",
});
impl<R, C> Renderer<R, C>
where R: gfx::Resources,
C: gfx::CommandBuffer<R>
{
pub fn new(cmd_buf: C) -> Renderer<R, C> {
Renderer {
cmd_buf: cmd_buf.into(),
passes: HashMap::default(),
}
}
pub fn load_all<F>(&mut self, factory: &mut F)
where F: gfx::Factory<R>
{
self.add_pass(pass::forward::Clear);
self.add_pass(pass::forward::DrawFlat::new(factory));
self.add_pass(pass::forward::DrawShaded::new(factory));
self.add_pass(pass::forward::Wireframe::new(factory));
self.add_pass(pass::deferred::Clear);
self.add_pass(pass::deferred::DrawPass::new(factory));
self.add_pass(pass::deferred::DepthPass::new(factory));
self.add_pass(pass::deferred::BlitLayer::new(factory));
self.add_pass(pass::deferred::LightingPass::new(factory));
}
pub fn add_pass<A, T, P>(&mut self, p: P)
where P: Pass<R, Arg = A, Target = T> + 'static,
A: PassDescription,
T: Target
{
let id = (TypeId::of::<A>(), TypeId::of::<T>());
self.passes.insert(id,
Box::new(move |a: &Box<PassDescription>,
t: &Target,
pipeline: &Pipeline,
scene: &Scene<R>,
encoder: &mut gfx::Encoder<R, C>| {
let a = a.downcast_ref::<A>().unwrap();
let t = t.downcast_ref::<T>().unwrap();
p.apply(a, t, pipeline, scene, encoder)
}));
}
pub fn submit<D>(&mut self, pipe: &Pipeline, scene: &Scene<R>, device: &mut D)
where D: gfx::Device<Resources = R, CommandBuffer = C>
{
for layer in &pipe.layers {
let fb = pipe.targets.get(&layer.target).unwrap();
for desc in &layer.passes {
let id = (mopa::Any::get_type_id(&**desc), mopa::Any::get_type_id(&**fb));
if let Some(pass) = self.passes.get(&id) {
pass(desc, &**fb, &pipe, &scene, &mut self.cmd_buf);
} else {
panic!("No pass implementation found for target={}, pass={:?}",
layer.target,
desc);
}
}
}
self.cmd_buf.flush(device);
device.cleanup();
}
}
#[derive(Clone)]
pub struct ConstantColorTexture<R: gfx::Resources> {
texture: gfx::handle::Texture<R, gfx::format::R8_G8_B8_A8>,
view: gfx::handle::ShaderResourceView<R, [f32; 4]>,
}
impl<R: gfx::Resources> ConstantColorTexture<R> {
pub fn new<F>(factory: &mut F) -> ConstantColorTexture<R>
where F: gfx::Factory<R>
{
let kind = gfx::texture::Kind::D2(1, 1, gfx::texture::AaMode::Single);
let text = factory.create_texture::<gfx::format::R8_G8_B8_A8>(kind,
1,
gfx::SHADER_RESOURCE,
gfx::memory::Usage::Dynamic,
Some(gfx::format::ChannelType::Unorm))
.unwrap();
let levels = (0, text.get_info().levels - 1);
let view = factory.view_texture_as_shader_resource::<gfx::format::Rgba8>(&text,
levels,
gfx::format::Swizzle::new())
.unwrap();
ConstantColorTexture {
texture: text,
view: view,
}
}
}
#[derive(Clone)]
pub enum Texture<R: gfx::Resources> {
Constant([f32; 4]),
Texture(gfx::handle::ShaderResourceView<R, [f32; 4]>),
}
impl<R: gfx::Resources> Texture<R> {
pub fn to_view<C>(&self,
texture: &ConstantColorTexture<R>,
encoder: &mut gfx::Encoder<R, C>)
-> gfx::handle::ShaderResourceView<R, [f32; 4]>
where C: gfx::CommandBuffer<R>
{
match *self {
Texture::Constant(ref color) => {
let color: [[u8; 4]; 1] = [[(color[0] * 255.) as u8,
(color[1] * 255.) as u8,
(color[2] * 255.) as u8,
(color[3] * 255.) as u8]];
encoder.update_texture::<_, gfx::format::Rgba8>(&texture.texture,
None,
texture.texture
.get_info()
.to_image_info(0),
&color[..])
.unwrap();
texture.view.clone()
}
Texture::Texture(ref tex) => tex.clone(),
}
}
}
#[derive(Clone)]
pub struct Fragment<R: gfx::Resources> {
pub transform: [[f32; 4]; 4],
pub buffer: gfx::handle::Buffer<R, VertexPosNormal>,
pub slice: gfx::Slice<R>,
pub ka: Texture<R>,
pub kd: Texture<R>,
pub ks: Texture<R>,
pub ns: f32,
}
#[derive(Copy, Clone, Debug)]
pub struct PointLight {
pub center: [f32; 3],
pub color: [f32; 4],
pub intensity: f32,
pub radius: f32,
pub smoothness: f32,
}
impl Default for PointLight {
fn default() -> PointLight {
PointLight {
color: [1.0, 1.0, 1.0, 1.0],
center: [0.0, 0.0, 0.0],
intensity: 10.0,
radius: 10.0,
smoothness: 4.0,
}
}
}
impl Component for PointLight {
type Storage = VecStorage<PointLight>;
}
#[derive(Clone, Copy, Debug)]
pub struct DirectionalLight {
pub color: [f32; 4],
pub direction: [f32; 3],
}
impl Default for DirectionalLight {
fn default() -> DirectionalLight {
DirectionalLight {
color: [1.0; 4],
direction: [-1.0; 3],
}
}
}
impl Component for DirectionalLight {
type Storage = VecStorage<DirectionalLight>;
}
#[derive(Clone, Copy, Debug)]
pub struct AmbientLight {
pub power: f32,
}
impl Default for AmbientLight {
fn default() -> AmbientLight {
AmbientLight { power: 0.01 }
}
}
#[derive(Clone)]
pub struct Scene<R: gfx::Resources> {
pub fragments: Vec<Fragment<R>>,
pub point_lights: Vec<PointLight>,
pub directional_lights: Vec<DirectionalLight>,
pub ambient_light: f32,
pub camera: Camera,
}
impl<R: gfx::Resources> Scene<R> {
pub fn new(camera: Camera) -> Scene<R> {
Scene {
fragments: Vec::new(),
point_lights: Vec::new(),
directional_lights: Vec::new(),
ambient_light: 0.01,
camera: camera,
}
}
}
#[derive(Copy, Clone)]
pub struct Camera {
pub proj: [[f32; 4]; 4],
pub view: [[f32; 4]; 4],
}
impl Camera {
pub fn new(proj: [[f32; 4]; 4], view: [[f32; 4]; 4]) -> Camera {
Camera {
proj: proj,
view: view,
}
}
pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> [[f32; 4]; 4] {
cgmath::perspective(cgmath::Deg(fov), aspect, near, far).into()
}
pub fn orthographic(left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32)
-> [[f32; 4]; 4] {
cgmath::ortho(left, right, bottom, top, near, far).into()
}
pub fn look_at(eye: [f32; 3], target: [f32; 3], up: [f32; 3]) -> [[f32; 4]; 4] {
use cgmath::{Point3, Vector3, Matrix4, Transform};
let view: Matrix4<f32> = Transform::look_at(Point3::new(eye[0], eye[1], eye[2]),
Point3::new(target[0], target[1], target[2]),
Vector3::new(up[0], up[1], up[2]));
view.into()
}
}
pub struct Layer {
pub target: String,
pub passes: Vec<Box<PassDescription>>,
}
impl Layer {
pub fn new<T>(target: T, passes: Vec<Box<PassDescription>>) -> Layer
where String: From<T>
{
Layer {
target: String::from(target),
passes: passes,
}
}
}
pub struct Pipeline {
pub layers: Vec<Layer>,
pub targets: HashMap<String, Box<Target>>,
}
impl Pipeline {
pub fn new() -> Pipeline {
Pipeline {
layers: Vec::new(),
targets: HashMap::default(),
}
}
}