forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
20_index_buffer.rs.diff
86 lines (75 loc) · 3.05 KB
/
20_index_buffer.rs.diff
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
--- a/19_staging_buffer.rs
+++ b/20_index_buffer.rs
@@ -56,6 +56,7 @@ use vulkano::buffer::{
immutable::ImmutableBuffer,
BufferUsage,
BufferAccess,
+ TypedBufferAccess,
};
const WIDTH: u32 = 800;
@@ -104,14 +105,19 @@ impl Vertex {
}
impl_vertex!(Vertex, pos, color);
-fn vertices() -> [Vertex; 3] {
+fn vertices() -> [Vertex; 4] {
[
- Vertex::new([0.0, -0.5], [1.0, 1.0, 1.0]),
- Vertex::new([0.5, 0.5], [0.0, 1.0, 0.0]),
- Vertex::new([-0.5, 0.5], [0.0, 0.0, 1.])
+ Vertex::new([-0.5, -0.5], [1.0, 0.0, 0.0]),
+ Vertex::new([0.5, -0.5], [0.0, 1.0, 0.0]),
+ Vertex::new([0.5, 0.5], [0.0, 0.0, 1.0]),
+ Vertex::new([-0.5, 0.5], [1.0, 1.0, 1.0])
]
}
+fn indices() -> [u16; 6] {
+ [0, 1, 2, 2, 3, 0]
+}
+
struct HelloTriangleApplication {
instance: Arc<Instance>,
#[allow(unused)]
@@ -135,6 +141,7 @@ struct HelloTriangleApplication {
swap_chain_framebuffers: Vec<Arc<FramebufferAbstract + Send + Sync>>,
vertex_buffer: Arc<BufferAccess + Send + Sync>,
+ index_buffer: Arc<TypedBufferAccess<Content=[u16]> + Send + Sync>,
command_buffers: Vec<Arc<AutoCommandBuffer>>,
previous_frame_end: Option<Box<GpuFuture>>,
@@ -160,6 +167,7 @@ impl HelloTriangleApplication {
let swap_chain_framebuffers = Self::create_framebuffers(&swap_chain_images, &render_pass);
let vertex_buffer = Self::create_vertex_buffer(&graphics_queue);
+ let index_buffer = Self::create_index_buffer(&graphics_queue);
let previous_frame_end = Some(Self::create_sync_objects(&device));
@@ -185,6 +193,7 @@ impl HelloTriangleApplication {
swap_chain_framebuffers,
vertex_buffer,
+ index_buffer,
command_buffers: vec![],
previous_frame_end,
@@ -464,6 +473,15 @@ impl HelloTriangleApplication {
buffer
}
+ fn create_index_buffer(graphics_queue: &Arc<Queue>) -> Arc<TypedBufferAccess<Content=[u16]> + Send + Sync> {
+ let (buffer, future) = ImmutableBuffer::from_iter(
+ indices().iter().cloned(), BufferUsage::index_buffer(),
+ graphics_queue.clone())
+ .unwrap();
+ future.flush().unwrap();
+ buffer
+ }
+
fn create_command_buffers(&mut self) {
let queue_family = self.graphics_queue.family();
self.command_buffers = self.swap_chain_framebuffers.iter()
@@ -472,8 +490,9 @@ impl HelloTriangleApplication {
.unwrap()
.begin_render_pass(framebuffer.clone(), false, vec![[0.0, 0.0, 0.0, 1.0].into()])
.unwrap()
- .draw(self.graphics_pipeline.clone(), &DynamicState::none(),
- vec![self.vertex_buffer.clone()], (), ())
+ .draw_indexed(self.graphics_pipeline.clone(), &DynamicState::none(),
+ vec![self.vertex_buffer.clone()],
+ self.index_buffer.clone(), (), ())
.unwrap()
.end_render_pass()
.unwrap()