-
Notifications
You must be signed in to change notification settings - Fork 47
/
21_descriptor_layout_and_buffer.rs.diff
201 lines (182 loc) · 6.18 KB
/
21_descriptor_layout_and_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
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
--- a/20_index_buffer.rs
+++ b/21_descriptor_layout_and_buffer.rs
@@ -2,9 +2,11 @@
extern crate vulkano;
extern crate vulkano_win;
extern crate winit;
+extern crate cgmath;
use std::sync::Arc;
use std::collections::HashSet;
+use std::time::Instant;
use winit::{EventsLoop, WindowBuilder, Window, dpi::LogicalSize, Event, WindowEvent};
use vulkano_win::VkSurfaceBuild;
@@ -54,6 +56,14 @@ use vulkano::buffer::{
BufferUsage,
BufferAccess,
TypedBufferAccess,
+ CpuAccessibleBuffer,
+};
+use cgmath::{
+ Rad,
+ Deg,
+ Matrix4,
+ Vector3,
+ Point3
};
const WIDTH: u32 = 800;
@@ -100,8 +110,18 @@ impl Vertex {
Self { pos, color }
}
}
+
+#[allow(clippy:ref_in_deref)]
impl_vertex!(Vertex, pos, color);
+#[allow(dead_code)]
+#[derive(Copy, Clone)]
+struct UniformBufferObject {
+ model: Matrix4<f32>,
+ view: Matrix4<f32>,
+ proj: Matrix4<f32>,
+}
+
fn vertices() -> [Vertex; 4] {
[
Vertex::new([-0.5, -0.5], [1.0, 0.0, 0.0]),
@@ -139,10 +159,17 @@ struct HelloTriangleApplication {
vertex_buffer: Arc<BufferAccess + Send + Sync>,
index_buffer: Arc<TypedBufferAccess<Content=[u16]> + Send + Sync>,
+
+ #[allow(dead_code)]
+ uniform_buffers: Vec<Arc<CpuAccessibleBuffer<UniformBufferObject>>>,
+
command_buffers: Vec<Arc<AutoCommandBuffer>>,
previous_frame_end: Option<Box<GpuFuture>>,
recreate_swap_chain: bool,
+
+ #[allow(dead_code)]
+ start_time: Instant,
}
impl HelloTriangleApplication {
@@ -159,12 +186,16 @@ impl HelloTriangleApplication {
&device, &graphics_queue, &present_queue, None);
let render_pass = Self::create_render_pass(&device, swap_chain.format());
+
let graphics_pipeline = Self::create_graphics_pipeline(&device, swap_chain.dimensions(), &render_pass);
let swap_chain_framebuffers = Self::create_framebuffers(&swap_chain_images, &render_pass);
+ let start_time = Instant::now();
+
let vertex_buffer = Self::create_vertex_buffer(&graphics_queue);
let index_buffer = Self::create_index_buffer(&graphics_queue);
+ let uniform_buffers = Self::create_uniform_buffers(&device, swap_chain_images.len(), start_time, swap_chain.dimensions());
let previous_frame_end = Some(Self::create_sync_objects(&device));
@@ -191,10 +222,14 @@ impl HelloTriangleApplication {
vertex_buffer,
index_buffer,
+ uniform_buffers,
+
command_buffers: vec![],
previous_frame_end,
recreate_swap_chain: false,
+
+ start_time
};
app.create_command_buffers();
@@ -401,14 +436,14 @@ impl HelloTriangleApplication {
mod vertex_shader {
vulkano_shaders::shader! {
ty: "vertex",
- path: "src/bin/17_shader_vertexbuffer.vert"
+ path: "src/bin/21_shader_uniformbuffer.vert"
}
}
mod fragment_shader {
vulkano_shaders::shader! {
ty: "fragment",
- path: "src/bin/17_shader_vertexbuffer.frag"
+ path: "src/bin/21_shader_uniformbuffer.frag"
}
}
@@ -477,6 +512,31 @@ impl HelloTriangleApplication {
buffer
}
+ fn create_uniform_buffers(
+ device: &Arc<Device>,
+ num_buffers: usize,
+ start_time: Instant,
+ dimensions_u32: [u32; 2]
+ ) -> Vec<Arc<CpuAccessibleBuffer<UniformBufferObject>>> {
+ let mut buffers = Vec::new();
+
+ let dimensions = [dimensions_u32[0] as f32, dimensions_u32[1] as f32];
+
+ let uniform_buffer = Self::update_uniform_buffer(start_time, dimensions);
+
+ for _ in 0..num_buffers {
+ let buffer = CpuAccessibleBuffer::from_data(
+ device.clone(),
+ BufferUsage::uniform_buffer_transfer_destination(),
+ uniform_buffer,
+ ).unwrap();
+
+ buffers.push(buffer);
+ }
+
+ buffers
+ }
+
fn create_command_buffers(&mut self) {
let queue_family = self.graphics_queue.family();
self.command_buffers = self.swap_chain_framebuffers.iter()
@@ -485,9 +545,13 @@ impl HelloTriangleApplication {
.unwrap()
.begin_render_pass(framebuffer.clone(), false, vec![[0.0, 0.0, 0.0, 1.0].into()])
.unwrap()
- .draw_indexed(self.graphics_pipeline.clone(), &DynamicState::none(),
+ .draw_indexed(
+ self.graphics_pipeline.clone(),
+ &DynamicState::none(),
vec![self.vertex_buffer.clone()],
- self.index_buffer.clone(), (), ())
+ self.index_buffer.clone(),
+ (),
+ ())
.unwrap()
.end_render_pass()
.unwrap()
@@ -622,6 +686,30 @@ impl HelloTriangleApplication {
}
}
+ fn update_uniform_buffer(start_time: Instant, dimensions: [f32; 2]) -> UniformBufferObject {
+ let duration = Instant::now().duration_since(start_time);
+ let elapsed = (duration.as_secs() * 1000) + u64::from(duration.subsec_millis());
+
+ let model = Matrix4::from_angle_z(Rad::from(Deg(elapsed as f32 * 0.180)));
+
+ let view = Matrix4::look_at(
+ Point3::new(2.0, 2.0, 2.0),
+ Point3::new(0.0, 0.0, 0.0),
+ Vector3::new(0.0, 0.0, 1.0)
+ );
+
+ let mut proj = cgmath::perspective(
+ Rad::from(Deg(45.0)),
+ dimensions[0] as f32 / dimensions[1] as f32,
+ 0.1,
+ 10.0
+ );
+
+ proj.y.y *= -1.0;
+
+ UniformBufferObject { model, view, proj }
+ }
+
fn recreate_swap_chain(&mut self) {
let (swap_chain, images) = Self::create_swap_chain(&self.instance, &self.surface, self.physical_device_index,
&self.device, &self.graphics_queue, &self.present_queue, Some(self.swap_chain.clone()));
@@ -639,4 +727,4 @@ impl HelloTriangleApplication {
fn main() {
let mut app = HelloTriangleApplication::initialize();
app.main_loop();
-}
+}
\ No newline at end of file