This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
1024 lines (921 loc) · 32.6 KB
/
main.rs
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Okay, so the first step is going to be rendering multiple things with the same
geometry and texture using instanced drawing. DONE.
Next step is to render things with different textures. DONE.
Last step is to render things with different textures and
different geometry. Trivial to do currently but it would be nice
to not re-bind the mesh descriptor if we don't have to...
How much does that actually matter though? Dunno.
Last+1 step might be to make rendering quads a more efficient special case,
for example by reifying the geometry in the vertex shader a la the Rendy
quads example.
Last+2 step is going to be having multiple pipelines with
different shaders.
Then actual last step will be to have multiple render passes with different
render targets.
ALSO, at some point we should stop using SimpleGraphicsPipeline, per
viral, 'cause it's mostly intended for only the simplest of cases.
RenderGroup is what gets us the full power. Looks mostly similar in
concept but with more fiddly bits to fill out. Should be fine, right?
*/
use std::sync::Arc;
use std::{mem::size_of, time};
use rendy::command::{QueueId, RenderPassEncoder};
use rendy::factory::{Config, Factory, ImageState};
use rendy::graph::{
present::PresentNode, render::*, GraphBuilder, GraphContext, NodeBuffer, NodeImage,
};
use rendy::hal;
use rendy::hal::Device as _;
use rendy::hal::PhysicalDevice as _;
use rendy::memory::Dynamic;
use rendy::mesh::{AsVertex, Mesh, PosColorNorm};
use rendy::resource::{
Buffer, BufferInfo, DescriptorSet, DescriptorSetLayout, Escape, Filter, Handle, SamplerInfo,
WrapMode,
};
use rendy::shader::{ShaderKind, SourceLanguage, StaticShaderInfo};
use rendy::texture::Texture;
use rand::distributions::{Distribution, Uniform};
use winit::{Event, EventsLoop, WindowBuilder, WindowEvent};
use euclid;
type Transform3 = euclid::Transform3D<f32>;
type Rect = euclid::Rect<f32>;
#[cfg(target_os = "macos")]
type Backend = rendy::metal::Backend;
#[cfg(not(target_os = "macos"))]
type Backend = rendy::vulkan::Backend;
/// Data we need per instance. DrawParam gets turned into this.
/// We have to be *quite particular* about layout since this gets
/// fed straight to the shader.
///
/// TODO: Currently the shader doesn't use src or color though.
#[repr(C, align(16))]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
struct InstanceData {
// The euclid types don't impl PartialOrd and PartialEq so
// we have to use the lower-level forms for
// actually sending to the GPU. :-/
transform: [f32; 16],
src: [f32; 4],
color: [f32; 4],
}
use rendy::mesh::{Attribute, VertexFormat};
/// Okay, this tripped me up. Instance data is technically
/// part of the per-vertex data. So we describe it as
/// part of the vertex format. This is where that
/// definition happens.
///
/// This trait impl is basically extended from the impl for
/// `rendy::mesh::Transform`
impl AsVertex for InstanceData {
fn vertex() -> VertexFormat {
VertexFormat {
attributes: vec![
Attribute::new(
"Transform1",
0,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 0,
},
),
Attribute::new(
"Transform2",
1,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 16,
},
),
Attribute::new(
"Transform3",
0,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 32,
},
),
Attribute::new(
"Transform4",
0,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 48,
},
),
// rect
Attribute::new(
"rect",
0,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 64,
},
),
// color
Attribute::new(
"color",
0,
hal::pso::Element {
format: hal::format::Format::Rgba32Sfloat,
offset: 80,
},
),
],
stride: 96,
}
}
}
/// Uniform data. Each frame contains one of these.
#[derive(Clone, Copy, Debug, Default)]
#[repr(C, align(16))]
struct UniformData {
proj: Transform3,
view: Transform3,
}
/// What data we need for each frame in flight.
/// Rendy doesn't do any synchronization for us
/// beyond guarenteeing that when we get a new frame
/// index everything touched by that frame index is
/// now free to reuse. So we just make one entire
/// set of data per frame.
///
/// We could make different frames share buffers
/// and descriptor sets and such and only change bits
/// that aren't in use from other frames, but that's
/// more complex than I want to get into right now.
///
/// When we do want to do that though, I think the simple
/// way would be... maybe create a structure through which
/// a FrameInFlight can be altered and which records if
/// things have actually changed. Actually, the DrawCall
/// might the place to handle that? Hm, having a separate
/// Buffer per draw call might be the way to go too? If
/// the buffer does not change from one draw call to the
/// next, we don't need to re-record its data, just issue
/// the draw call directly with the right PrepareReuse...
/// idk, I'm rambling.
#[derive(Debug)]
struct FrameInFlight<B>
where
B: hal::Backend,
{
/// The buffer where we store instance data.
buffer: Escape<Buffer<B>>,
/// One descriptor set per draw call we do.
/// Also has the number of instances in that draw call,
/// so we can find offsets.
descriptor_sets: Vec<Escape<DescriptorSet<B>>>,
/// Offsets in the buffer to start each draw call from.
draw_offsets: Vec<u64>,
/// The frame's local copy of uniform data.
push_constants: [u32; 32],
}
impl<B> FrameInFlight<B>
where
B: hal::Backend,
{
/// All our descriptor sets use the same layout.
/// This one! We have an instance buffer, an
/// image, and a sampler.
const LAYOUT: &'static [hal::pso::DescriptorSetLayoutBinding] = &[
hal::pso::DescriptorSetLayoutBinding {
binding: 0,
ty: hal::pso::DescriptorType::UniformBuffer,
count: 1,
stage_flags: hal::pso::ShaderStageFlags::GRAPHICS,
immutable_samplers: false,
},
hal::pso::DescriptorSetLayoutBinding {
binding: 1,
ty: hal::pso::DescriptorType::SampledImage,
count: 1,
stage_flags: hal::pso::ShaderStageFlags::FRAGMENT,
immutable_samplers: false,
},
hal::pso::DescriptorSetLayoutBinding {
binding: 2,
ty: hal::pso::DescriptorType::Sampler,
count: 1,
stage_flags: hal::pso::ShaderStageFlags::FRAGMENT,
immutable_samplers: false,
},
];
fn get_descriptor_set_layout() -> SetLayout {
SetLayout {
bindings: Self::LAYOUT.to_vec(),
}
}
fn new(
factory: &mut Factory<B>,
align: u64,
draw_calls: &[DrawCall<B>],
descriptor_set: &Handle<DescriptorSetLayout<B>>,
) -> Self {
let buffer_size = gbuffer_size(align) * (draw_calls.len() as u64);
let buffer = factory
.create_buffer(
BufferInfo {
size: buffer_size,
usage: hal::buffer::Usage::UNIFORM | hal::buffer::Usage::VERTEX,
},
Dynamic,
)
.unwrap();
let descriptor_sets = vec![];
let mut ret = Self {
buffer,
descriptor_sets,
draw_offsets: vec![],
push_constants: [0; 32],
};
for draw_call in draw_calls {
// all descriptor sets use the same layout
// we need one per draw call, per frame in flight.
ret.create_descriptor_sets(factory, draw_call, descriptor_set, align);
}
ret
}
/// Takes a draw call and creates a descriptor set that points
/// at its resources.
///
/// For now we do not care about preserving descriptor sets between draw calls.
fn create_descriptor_sets(
&mut self,
factory: &Factory<B>,
draw_call: &DrawCall<B>,
layout: &Handle<DescriptorSetLayout<B>>,
_align: u64,
) {
// Does this sampler need to stay alive? We pass a
// reference to it elsewhere in an `unsafe` block...
// It's cached in the Factory anyway, so I don't think so.
let sampler = factory
.get_sampler(draw_call.sampler_info.clone())
.expect("Could not get sampler");
unsafe {
let set = factory.create_descriptor_set(layout.clone()).unwrap();
factory.write_descriptor_sets(Some(hal::pso::DescriptorSetWrite {
set: set.raw(),
binding: 1,
array_offset: 0,
descriptors: vec![hal::pso::Descriptor::Image(
draw_call.texture.view().raw(),
hal::image::Layout::ShaderReadOnlyOptimal,
)],
}));
factory.write_descriptor_sets(Some(hal::pso::DescriptorSetWrite {
set: set.raw(),
binding: 2,
array_offset: 0,
descriptors: vec![hal::pso::Descriptor::Sampler(sampler.raw())],
}));
self.descriptor_sets.push(set);
}
}
/// This happens before a frame; it should take a LIST of draw calls and take
/// care of uploading EACH of them into the buffer so they don't clash!
fn prepare(
&mut self,
factory: &Factory<B>,
uniforms: &UniformData,
draw_calls: &[DrawCall<B>],
_layout: &Handle<DescriptorSetLayout<B>>,
_align: u64,
) {
assert!(draw_calls.len() > 0);
// Store the uniforms to be shoved into push constants this frame
// TODO: Be less crude about indexing and such.
for (i, vl) in uniforms.proj.to_row_major_array().into_iter().enumerate() {
self.push_constants[i] = vl.to_bits();
}
for (i, vl) in uniforms.view.to_row_major_array().into_iter().enumerate() {
self.push_constants[16 + i] = vl.to_bits();
}
let mut instance_count = 0;
//println!("Preparing frame-in-flight, {} draw calls, first has {} instances.", draw_calls.len(),
//draw_calls[0].objects.len());
unsafe {
for draw_call in draw_calls {
// Upload the instances to the right offset in the
// buffer.
// Vulkan doesn't seem to like zero-size copies.
//println!("Uploading {} instance data to {}", draw_call.objects.len(), ginstance_offset(instance_count));
if draw_call.objects.len() > 0 {
factory
.upload_visible_buffer(
&mut self.buffer,
ginstance_offset(instance_count),
&draw_call.objects[..],
)
.unwrap();
self.draw_offsets.push(ginstance_offset(instance_count));
instance_count += draw_call.objects.len();
}
}
}
}
/// Draws a list of DrawCall's.
fn draw(
&mut self,
draw_calls: &[DrawCall<B>],
layout: &B::PipelineLayout,
encoder: &mut RenderPassEncoder<'_, B>,
_align: u64,
) {
//println!("Drawing {} draw calls", draw_calls.len());
let mut instance_count = 0;
for ((draw_call, descriptor_set), _draw_offset) in draw_calls
.iter()
.zip(&self.descriptor_sets)
.zip(&self.draw_offsets)
{
//println!("Drawing {:#?}, {:#?}, {}", draw_call, descriptor_set, draw_offset);
// This is a bit weird, but basically tells the thing where to find the
// instance data. The stride and such of the instance structure is
// defined in the `AsVertex` definition.
encoder.bind_graphics_descriptor_sets(
layout,
0,
std::iter::once(descriptor_set.raw()),
std::iter::empty(),
);
draw_call
.mesh
.as_ref()
.bind(0, &[PosColorNorm::vertex()], encoder)
.expect("Could not bind mesh?");
// The 1 here is a LITTLE weird; TODO: Investigate! I THINK it is there
// to differentiate which *place* we're binding to; see the 0 in the
// bind_graphics_descriptor_sets().
encoder.bind_vertex_buffers(
1,
std::iter::once((self.buffer.raw(), ginstance_offset(instance_count))),
);
encoder.push_constants(
layout,
hal::pso::ShaderStageFlags::ALL,
0,
&self.push_constants,
);
// The length of the mesh is the number of indices if it has any, the number
// of verts otherwise. See https://github.com/amethyst/rendy/issues/119
let indices = 0..(draw_call.mesh.len() as u32);
// This count is the *number of instances*. What instance
// to start at in the buffer is defined by the offset in
// `bind_vertex_buffers()` above, and the stride/size of an instance
// is defined in `AsVertex`.
let instances = 0..(draw_call.objects.len() as u32);
dbg!(&indices);
dbg!(&instances);
encoder.draw_indexed(indices, 0, instances);
instance_count += draw_call.objects.len();
}
}
}
/// The data we need for a single draw call, which
/// gets bound to descriptor sets and
///
/// For now we re-bind EVERYTHING even if only certain
/// resources have changed (for example, texture changes
/// and mesh stays the same). Should be easy to check
/// that in the future and make the various things
/// in here `Option`s.
#[derive(Debug)]
struct DrawCall<B>
where
B: hal::Backend,
{
objects: Vec<InstanceData>,
mesh: Arc<Mesh<B>>,
texture: Arc<Texture<B>>,
/// We just need the actual config for the sampler 'cause
/// Rendy's `Factory` can manage a sampler cache itself.
sampler_info: SamplerInfo,
}
impl<B> DrawCall<B>
where
B: hal::Backend,
{
fn new(texture: Arc<Texture<B>>, mesh: Arc<Mesh<B>>) -> Self {
let sampler_info = SamplerInfo::new(Filter::Nearest, WrapMode::Clamp);
Self {
objects: vec![],
mesh,
texture,
sampler_info,
}
}
fn add_object(&mut self, rng: &mut rand::rngs::ThreadRng, max_width: f32, max_height: f32) {
if self.objects.len() < MAX_OBJECTS {
let rx = Uniform::new(0.0, max_width);
let ry = Uniform::new(0.0, max_height);
let x = rx.sample(rng);
let y = ry.sample(rng);
//println!("Adding object at {}x{}", x, y);
let transform = Transform3::create_translation(x, y, -100.0);
let src = Rect::from(euclid::Size2D::new(1.0, 1.0));
let color = [1.0, 0.0, 1.0, 1.0];
let instance = InstanceData {
transform: transform.to_row_major_array(),
src: [src.origin.x, src.origin.y, src.size.width, src.size.height],
color,
};
self.objects.push(instance);
}
}
}
#[derive(Debug)]
struct Aux<B: hal::Backend> {
frames: usize,
align: u64,
draws: Vec<DrawCall<B>>,
camera: UniformData,
shader: rendy::shader::ShaderSetBuilder,
}
const MAX_OBJECTS: usize = 10_000;
const INSTANCES_SIZE: u64 = size_of::<InstanceData>() as u64 * MAX_OBJECTS as u64;
/// The size of the buffer in a `FrameInFlight`.
///
/// We pack data from multiple `DrawCall`'s together into one `Buffer`
/// but each draw call can have a varying amount of instance data,
/// so we end up with something like:
///
/// ```
/// | Instances1 ... | Instances2 ... | ... | empty space |
/// ```
///
/// Right now we have a fixed size buffer and just limit the number
/// of objects in it. TODO: Eventually someday we will grow the buffer
/// as needed. Maybe shrink it too? Not sure about that.
const fn gbuffer_size(align: u64) -> u64 {
((INSTANCES_SIZE - 1) / align + 1) * align
}
/// The offset pointing to free space right after the given number
/// of instances.
///
/// TODO: Are there alignment requirements for this?
const fn ginstance_offset(instance_count: usize) -> u64 {
(instance_count * size_of::<InstanceData>()) as u64
}
#[derive(Debug)]
struct MeshRenderGroup<B>
where
B: hal::Backend,
{
frames_in_flight: Vec<FrameInFlight<B>>,
set_layout: Handle<DescriptorSetLayout<B>>,
pipeline_layout: B::PipelineLayout,
graphics_pipeline: B::GraphicsPipeline,
}
impl<B> RenderGroup<B, Aux<B>> for MeshRenderGroup<B>
where
B: hal::Backend,
{
fn prepare(
&mut self,
factory: &Factory<B>,
_queue: QueueId,
index: usize,
_subpass: hal::pass::Subpass<B>,
aux: &Aux<B>,
) -> PrepareResult {
let align = aux.align;
self.frames_in_flight[index].prepare(
factory,
&aux.camera,
&aux.draws,
&self.set_layout,
align,
);
PrepareResult::DrawRecord
}
fn draw_inline(
&mut self,
encoder: RenderPassEncoder<B>,
index: usize,
_subpass: hal::pass::Subpass<B>,
aux: &Aux<B>,
) {
// We own it, we can mutate it if we want, muahahahaha!
let mut encoder = encoder;
// BUG HERE: We should have this here:
// encoder.bind_graphics_pipeline(&self.graphics_pipeline);
// Omitting this causes the vulkan validation layer to
// segfault in `draw_indexed()` on Linux X11 with NVidia
// drivers.
self.frames_in_flight[index].draw(
&aux.draws,
&self.pipeline_layout,
&mut encoder,
aux.align,
);
}
fn dispose(self: Box<Self>, factory: &mut Factory<B>, _aux: &Aux<B>) {
// Our Aux doesn't store any resources, I think!
// TODO: each FrameInFlight needs to be disposed of though.
// But that might or might not be the responsibility of the Pipeline!
//self.pipeline.dispose(factory, aux);
unsafe {
factory
.device()
.destroy_graphics_pipeline(self.graphics_pipeline);
factory
.device()
.destroy_pipeline_layout(self.pipeline_layout);
drop(self.set_layout);
}
}
}
#[derive(Debug)]
struct MeshRenderGroupDesc {
colors: Vec<hal::pso::ColorBlendDesc>,
}
impl MeshRenderGroupDesc {
fn new() -> Self {
Self {
colors: vec![hal::pso::ColorBlendDesc(
hal::pso::ColorMask::ALL,
hal::pso::BlendState::ALPHA,
)],
}
}
}
impl<B> RenderGroupDesc<B, Aux<B>> for MeshRenderGroupDesc
where
B: hal::Backend,
{
/// Creates the RenderGroup associated with this type.
fn build(
self,
_ctx: &GraphContext<B>,
factory: &mut Factory<B>,
_queue: QueueId,
aux: &Aux<B>,
framebuffer_width: u32,
framebuffer_height: u32,
subpass: hal::pass::Subpass<B>,
_buffers: Vec<NodeBuffer>,
_images: Vec<NodeImage>,
) -> Result<Box<dyn RenderGroup<B, Aux<B>> + 'static>, failure::Error> {
let desc_set_layout = Handle::from(
factory.create_descriptor_set_layout(FrameInFlight::<B>::LAYOUT.to_vec())?,
);
let push_constants_layout = vec![(
hal::pso::ShaderStageFlags::ALL,
0..(size_of::<UniformData>() as u32),
)];
let mut shader_set = aux.shader.build(factory, Default::default()).unwrap();
let pipeline_layout = unsafe {
factory
.device()
.create_pipeline_layout(vec![desc_set_layout.raw()], push_constants_layout)
}
.map_err(|e| {
shader_set.dispose(factory);
e
})?;
fn push_vertex_desc(
elements: &[hal::pso::Element<hal::format::Format>],
stride: hal::pso::ElemStride,
rate: hal::pso::VertexInputRate,
vertex_buffers: &mut Vec<hal::pso::VertexBufferDesc>,
attributes: &mut Vec<hal::pso::AttributeDesc>,
) {
let index = vertex_buffers.len() as hal::pso::BufferIndex;
vertex_buffers.push(hal::pso::VertexBufferDesc {
binding: index,
stride,
rate,
});
let mut location = attributes.last().map_or(0, |a| a.location + 1);
for &element in elements {
attributes.push(hal::pso::AttributeDesc {
location,
binding: index,
element,
});
location += 1;
}
}
let mut vertex_buffers = vec![];
let mut attributes = vec![];
let vertices = vec![
PosColorNorm::vertex().gfx_vertex_input_desc(hal::pso::VertexInputRate::Vertex),
InstanceData::vertex().gfx_vertex_input_desc(hal::pso::VertexInputRate::Instance(1)),
];
for &(ref elemets, stride, rate) in &vertices {
push_vertex_desc(elemets, stride, rate, &mut vertex_buffers, &mut attributes);
}
// Now we actually create the pipeline...
let input_assembler = hal::pso::InputAssemblerDesc {
primitive: hal::Primitive::TriangleList,
primitive_restart: hal::pso::PrimitiveRestart::Disabled,
};
let blender = hal::pso::BlendDesc {
logic_op: None,
targets: vec![hal::pso::ColorBlendDesc(
hal::pso::ColorMask::ALL,
hal::pso::BlendState::ALPHA,
)],
};
let depth_stencil = hal::pso::DepthStencilDesc {
depth: hal::pso::DepthTest::On {
fun: hal::pso::Comparison::LessEqual,
write: true,
},
depth_bounds: false,
stencil: hal::pso::StencilTest::Off,
};
let rect = hal::pso::Rect {
x: 0,
y: 0,
w: framebuffer_width as i16,
h: framebuffer_height as i16,
};
let baked_states = hal::pso::BakedStates {
viewport: Some(hal::pso::Viewport {
rect,
depth: 0.0..1.0,
}),
scissor: Some(rect),
blend_color: None,
depth_bounds: None,
};
let pipeline_desc = hal::pso::GraphicsPipelineDesc {
shaders: shader_set.raw().unwrap(),
rasterizer: hal::pso::Rasterizer::FILL,
vertex_buffers,
attributes,
input_assembler,
blender,
depth_stencil,
multisampling: None,
baked_states,
layout: &pipeline_layout,
subpass,
flags: hal::pso::PipelineCreationFlags::empty(),
parent: hal::pso::BasePipeline::None,
};
let graphics_pipeline = unsafe {
// Iterator of pipeline descriptions, pipeline cache.
factory
.device()
.create_graphics_pipelines(Some(pipeline_desc), None)
}
.remove(0)?;
// Apparently we're done with the shader set here anyway.
shader_set.dispose(factory);
let frames = 3;
let mut frames_in_flight = vec![];
frames_in_flight.extend(
(0..frames)
.map(|_| FrameInFlight::new(factory, aux.align, &aux.draws, &desc_set_layout)),
);
let res = MeshRenderGroup {
frames_in_flight: frames_in_flight,
set_layout: desc_set_layout,
pipeline_layout: pipeline_layout,
graphics_pipeline: graphics_pipeline,
};
Ok(Box::new(res))
}
/// Which buffers the render group uses. Default
/// should be okay?
fn buffers(&self) -> Vec<rendy::graph::BufferAccess> {
vec![]
}
/// Which images the render group uses. Default
/// should be okay?
fn images(&self) -> Vec<rendy::graph::ImageAccess> {
vec![]
}
/// How many color blend desc's we use
fn colors(&self) -> usize {
self.colors.len()
}
/// Whether or not we use a depth buffer
fn depth(&self) -> bool {
true
}
}
/// This is how we can load an image and create a new texture.
fn make_texture<B>(
queue_id: QueueId,
factory: &mut Factory<B>,
image_bytes: &[u8],
) -> Arc<Texture<B>>
where
B: hal::Backend,
{
let cursor = std::io::Cursor::new(image_bytes);
let texture_builder = rendy::texture::image::load_from_image(cursor, Default::default())
.expect("Could not load texture?");
let texture = texture_builder
.build(
ImageState {
queue: queue_id,
stage: hal::pso::PipelineStage::FRAGMENT_SHADER,
access: hal::image::Access::SHADER_READ,
layout: hal::image::Layout::ShaderReadOnlyOptimal,
},
factory,
)
.unwrap();
Arc::new(texture)
}
fn make_quad_mesh<B>(queue_id: QueueId, factory: &mut Factory<B>) -> Mesh<B>
where
B: hal::Backend,
{
let verts: Vec<[f32; 3]> = vec![
[0.0, 0.0, 0.0],
[0.0, 100.0, 0.0],
[100.0, 100.0, 0.0],
[100.0, 0.0, 0.0],
];
let indices = rendy::mesh::Indices::from(vec![0u32, 1, 2, 0, 2, 3]);
let vertices: Vec<_> = verts
.into_iter()
.map(|v| PosColorNorm {
position: rendy::mesh::Position::from(v),
color: [1.0, 1.0, 1.0, 1.0].into(),
normal: rendy::mesh::Normal::from([0.0, 0.0, 1.0]),
})
.collect();
let m = Mesh::<Backend>::builder()
.with_indices(indices)
.with_vertices(&vertices[..])
.build(queue_id, &factory)
.unwrap();
m
}
fn load_shaders() -> rendy::shader::ShaderSetBuilder {
let vertex: StaticShaderInfo = StaticShaderInfo::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/src/data/shader.glslv"),
ShaderKind::Vertex,
SourceLanguage::GLSL,
"main",
);
let fragment: StaticShaderInfo = StaticShaderInfo::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/src/data/shader.glslf"),
ShaderKind::Fragment,
SourceLanguage::GLSL,
"main",
);
let shader_builder: rendy::shader::ShaderSetBuilder =
rendy::shader::ShaderSetBuilder::default()
.with_vertex(&vertex)
.unwrap()
.with_fragment(&fragment)
.unwrap();
shader_builder
}
fn main() {
let config: Config = Default::default();
let (mut factory, mut families): (Factory<Backend>, _) = rendy::factory::init(config).unwrap();
let mut event_loop = EventsLoop::new();
let window = WindowBuilder::new()
.with_title("Rendy example")
.build(&event_loop)
.unwrap();
let window_size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
event_loop.poll_events(|_| ());
let surface = factory.create_surface(&window);
let mut graph_builder = GraphBuilder::<Backend, Aux<Backend>>::new();
let size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
let window_kind = hal::image::Kind::D2(size.width as u32, size.height as u32, 1, 1);
let color = graph_builder.create_image(
window_kind,
1,
factory.get_surface_format(&surface),
Some(hal::command::ClearValue::Color([0.1, 0.2, 0.3, 1.0].into())),
);
let depth = graph_builder.create_image(
window_kind,
1,
hal::format::Format::D16Unorm,
Some(hal::command::ClearValue::DepthStencil(
hal::command::ClearDepthStencil(1.0, 0),
)),
);
/*
let pass = graph_builder.add_node(
MeshRenderPipeline::builder()
.into_subpass()
.with_color(color)
.with_depth_stencil(depth)
.into_pass(),
);
*/
let render_group_desc = MeshRenderGroupDesc::new();
let pass = graph_builder.add_node(
render_group_desc
.builder()
.into_subpass()
.with_color(color)
.with_depth_stencil(depth)
.into_pass(),
);
let present_builder = PresentNode::builder(&factory, surface, color).with_dependency(pass);
let frames = present_builder.image_count();
graph_builder.add_node(present_builder);
// HACK suggested by Frizi, just use queue 0 for everything
// instead of getting it from `graph.node_queue(pass)`.
// Since we control in our `Config` what families we have
// and what they have, as long as we only ever use one family
// (which is probably fine) then we're prooooobably okay with
// this.
let queue_id = QueueId {
family: families.family_by_index(0).id(),
index: 0,
};
let rendy_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/data/rendy_logo.png"
));
let gfx_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/data/gfx_logo.png"
));
let heart_bytes = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/data/heart.png"));
let rust_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/data/rust_logo.png"
));
let width = window_size.width as f32;
let height = window_size.height as f32;
println!("dims: {}x{}", width, height);
let texture1 = make_texture(queue_id, &mut factory, gfx_bytes);
let texture2 = make_texture(queue_id, &mut factory, rendy_bytes);
let texture3 = make_texture(queue_id, &mut factory, heart_bytes);
let texture4 = make_texture(queue_id, &mut factory, rust_bytes);
let object_mesh = Arc::new(make_quad_mesh(queue_id, &mut factory));
let align = factory
.physical()
.limits()
.min_uniform_buffer_offset_alignment;
let draws = vec![
DrawCall::new(texture1, object_mesh.clone()),
DrawCall::new(texture2, object_mesh.clone()),
DrawCall::new(texture3, object_mesh.clone()),
DrawCall::new(texture4, object_mesh.clone()),
];
let mut aux = Aux {
frames: frames as _,
align,
draws,
camera: UniformData {
proj: Transform3::ortho(0.0, width, height, 0.0, 1.0, 200.0),
view: Transform3::create_translation(0.0, 0.0, 10.0),
},
shader: load_shaders(),
};
let mut graph = graph_builder
.with_frames_in_flight(frames)
.build(&mut factory, &mut families, &aux)
.unwrap();
let mut frames = 0u64..;
let mut rng = rand::thread_rng();
let mut should_close = false;
let started = time::Instant::now();
// TODO: Someday actually check against MAX_OBJECTS
while !should_close {
for _i in &mut frames {
factory.maintain(&mut families);
event_loop.poll_events(|event| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => should_close = true,
_ => (),