forked from bwasty/vulkan-tutorial-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_window_surface.rs.diff
183 lines (154 loc) · 6.76 KB
/
05_window_surface.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
--- a/04_logical_device.rs
+++ b/05_window_surface.rs
@@ -3,8 +3,10 @@ extern crate vulkano_win;
extern crate winit;
use std::sync::Arc;
+use std::collections::HashSet;
-use winit::{EventsLoop, WindowBuilder, dpi::LogicalSize, Event, WindowEvent};
+use winit::{EventsLoop, WindowBuilder, Window, dpi::LogicalSize, Event, WindowEvent};
+use vulkano_win::VkSurfaceBuild;
use vulkano::instance::{
Instance,
@@ -17,6 +19,9 @@ use vulkano::instance::{
};
use vulkano::instance::debug::{DebugCallback, MessageTypes};
use vulkano::device::{Device, DeviceExtensions, Queue};
+use vulkano::swapchain::{
+ Surface,
+};
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
@@ -32,14 +37,15 @@ const ENABLE_VALIDATION_LAYERS: bool = false;
struct QueueFamilyIndices {
graphics_family: i32,
+ present_family: i32,
}
impl QueueFamilyIndices {
fn new() -> Self {
- Self { graphics_family: -1 }
+ Self { graphics_family: -1, present_family: -1 }
}
fn is_complete(&self) -> bool {
- self.graphics_family >= 0
+ self.graphics_family >= 0 && self.present_family >= 0
}
}
@@ -49,46 +55,40 @@ struct HelloTriangleApplication {
debug_callback: Option<DebugCallback>,
events_loop: EventsLoop,
+ surface: Arc<Surface<Window>>,
physical_device_index: usize, // can't store PhysicalDevice directly (lifetime issues)
device: Arc<Device>,
graphics_queue: Arc<Queue>,
+ present_queue: Arc<Queue>,
}
impl HelloTriangleApplication {
pub fn initialize() -> Self {
let instance = Self::create_instance();
let debug_callback = Self::setup_debug_callback(&instance);
+ let (events_loop, surface) = Self::create_surface(&instance);
- let events_loop = Self::init_window();
-
- let physical_device_index = Self::pick_physical_device(&instance);
- let (device, graphics_queue) = Self::create_logical_device(
- &instance, physical_device_index);
+ let physical_device_index = Self::pick_physical_device(&instance, &surface);
+ let (device, graphics_queue, present_queue) = Self::create_logical_device(
+ &instance, &surface, physical_device_index);
Self {
instance,
debug_callback,
events_loop,
+ surface,
physical_device_index,
device,
graphics_queue,
+ present_queue,
}
}
- fn init_window() -> EventsLoop {
- let events_loop = EventsLoop::new();
- let _window_builder = WindowBuilder::new()
- .with_title("Vulkan")
- .with_dimensions(LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)));
- // .build(&self.events_loop.as_ref().unwrap());
- events_loop
- }
-
fn create_instance() -> Arc<Instance> {
if ENABLE_VALIDATION_LAYERS && !Self::check_validation_layer_support() {
println!("Validation layers requested, but not available!")
@@ -149,18 +149,18 @@ impl HelloTriangleApplication {
}).ok()
}
- fn pick_physical_device(instance: &Arc<Instance>) -> usize {
+ fn pick_physical_device(instance: &Arc<Instance>, surface: &Arc<Surface<Window>>) -> usize {
PhysicalDevice::enumerate(&instance)
- .position(|device| Self::is_device_suitable(&device))
+ .position(|device| Self::is_device_suitable(surface, &device))
.expect("failed to find a suitable GPU!")
}
- fn is_device_suitable(device: &PhysicalDevice) -> bool {
- let indices = Self::find_queue_families(device);
+ fn is_device_suitable(surface: &Arc<Surface<Window>>, device: &PhysicalDevice) -> bool {
+ let indices = Self::find_queue_families(surface, device);
indices.is_complete()
}
- fn find_queue_families(device: &PhysicalDevice) -> QueueFamilyIndices {
+ fn find_queue_families(surface: &Arc<Surface<Window>>, device: &PhysicalDevice) -> QueueFamilyIndices {
let mut indices = QueueFamilyIndices::new();
// TODO: replace index with id to simplify?
for (i, queue_family) in device.queue_families().enumerate() {
@@ -168,6 +168,10 @@ impl HelloTriangleApplication {
indices.graphics_family = i as i32;
}
+ if surface.is_supported(queue_family).unwrap() {
+ indices.present_family = i as i32;
+ }
+
if indices.is_complete() {
break;
}
@@ -178,27 +182,43 @@ impl HelloTriangleApplication {
fn create_logical_device(
instance: &Arc<Instance>,
+ surface: &Arc<Surface<Window>>,
physical_device_index: usize,
- ) -> (Arc<Device>, Arc<Queue>) {
+ ) -> (Arc<Device>, Arc<Queue>, Arc<Queue>) {
let physical_device = PhysicalDevice::from_index(&instance, physical_device_index).unwrap();
- let indices = Self::find_queue_families(&physical_device);
+ let indices = Self::find_queue_families(&surface, &physical_device);
- let queue_family = physical_device.queue_families()
- .nth(indices.graphics_family as usize).unwrap();
+ let families = [indices.graphics_family, indices.present_family];
+ use std::iter::FromIterator;
+ let unique_queue_families: HashSet<&i32> = HashSet::from_iter(families.iter());
let queue_priority = 1.0;
+ let queue_families = unique_queue_families.iter().map(|i| {
+ (physical_device.queue_families().nth(**i as usize).unwrap(), queue_priority)
+ });
// NOTE: the tutorial recommends passing the validation layers as well
// for legacy reasons (if ENABLE_VALIDATION_LAYERS is true). Vulkano handles that
// for us internally.
- let (device, mut queues) = Device::new(physical_device, &Features::none(), &DeviceExtensions::none(),
- [(queue_family, queue_priority)].iter().cloned())
+ let (device, mut queues) = Device::new(physical_device, &Features::none(),
+ &DeviceExtensions::none(), queue_families)
.expect("failed to create logical device!");
let graphics_queue = queues.next().unwrap();
+ let present_queue = queues.next().unwrap_or_else(|| graphics_queue.clone());
+
+ (device, graphics_queue, present_queue)
+ }
- (device, graphics_queue)
+ fn create_surface(instance: &Arc<Instance>) -> (EventsLoop, Arc<Surface<Window>>) {
+ let events_loop = EventsLoop::new();
+ let surface = WindowBuilder::new()
+ .with_title("Vulkan")
+ .with_dimensions(LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)))
+ .build_vk_surface(&events_loop, instance.clone())
+ .expect("failed to create window surface!");
+ (events_loop, surface)
}
#[allow(unused)]