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
|
#![deny(unsafe_code)]
use std::cell::OnceCell;
use std::collections::HashSet;
use std::ffi::{ c_void, CStr };
use vulkanalia::{ Entry, Instance, Version };
use vulkanalia::loader::{ LibloadingLoader, LIBRARY };
use vulkanalia::vk::{ self, HasBuilder,
ApplicationInfo, InstanceCreateInfo,
DeviceV1_4, EntryV1_0, InstanceV1_0,
ExtDebugUtilsExtensionInstanceCommands };
use winit::dpi::LogicalSize;
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ ActiveEventLoop, EventLoop };
use winit::window::{ Window, WindowAttributes, WindowId };
#[derive(Debug)]
struct Error {
message: String,
}
type Result<T> = std::result::Result<T, Error>;
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>)
-> std::result::Result<(), std::fmt::Error>
{
fmt.write_str(&self.message)
}
}
impl From<winit::error::EventLoopError> for Error {
fn from(e: winit::error::EventLoopError) -> Self {
match e {
winit::error::EventLoopError::NotSupported(e) => Self::from(e),
winit::error::EventLoopError::Os(e) => Self::from(e),
winit::error::EventLoopError::RecreationAttempt => Error {
message:
"There may only ever be a single winit event loop.".to_string()
},
winit::error::EventLoopError::ExitFailure(code) => Error {
message:
format!("Clean unhappy exit with code {} via winit event loop",
code)
}
}
}
}
impl From<winit::error::NotSupportedError> for Error {
fn from(e: winit::error::NotSupportedError) -> Self {
Error {
message:
format!("The winit backend does not support an operation: {}",
e.to_string())
}
}
}
impl From<winit::error::OsError> for Error {
fn from(e: winit::error::OsError) -> Self {
Error {
message: format!("The OS told winit about an error: {}", e.to_string())
}
}
}
impl From<libloading::Error> for Error {
fn from(e: libloading::Error) -> Self {
Error {
message: format!("The dynamic object loader reported an error: {}",
e.to_string())
}
}
}
impl From<Box<dyn vulkanalia::loader::LoaderError>> for Error {
fn from(e: Box<dyn vulkanalia::loader::LoaderError>) -> Self {
Error {
message: format!("The Vulkan loader reported an error: {}",
e.to_string())
}
}
}
impl From<vulkanalia::vk::ErrorCode> for Error {
fn from(e: vulkanalia::vk::ErrorCode) -> Self {
Error {
message: format!("Vulkan gave an error code: {}", e.to_string())
}
}
}
fn ignore_errors(mut body: impl FnMut() -> Result<()>) -> () {
if let Err(e) = body() {
eprintln!("Error: {}", e);
}
}
const VULKAN_FIRST_PORTABILITY_VERSION: Version = Version::new(1, 3, 216);
struct Surreality {
window: OnceCell<Window>,
entry: OnceCell<Entry>,
instance: OnceCell<Instance>,
// Vulkan spells "messager" as "messenger", but this is absurd
// over-formality and we don't indulge it.
debug_messager: OnceCell<vk::DebugUtilsMessengerEXT>,
}
impl Surreality {
fn new() -> Self {
Surreality {
window: OnceCell::new(),
entry: OnceCell::new(),
instance: OnceCell::new(),
debug_messager: OnceCell::new(),
}
}
fn init(&mut self, event_loop: &ActiveEventLoop) -> Result<()> {
if self.window.get().is_none() {
self.init_window(event_loop)?;
}
if self.entry.get().is_none() {
self.init_vulkan_entry()?;
}
if self.instance.get().is_none() {
self.init_vulkan_instance()?;
}
Ok(())
}
fn init_window(&mut self, event_loop: &ActiveEventLoop) -> Result<()> {
// Notice that we do this before having a Vulkan instance. The window is
// actually a parameter needed to create the instance; see
// init_vulkan_instance(), below.
let window_attributes = WindowAttributes::default()
.with_title("Love, Curiosity, Justice")
.with_inner_size(LogicalSize::new(1024, 768));
let window: Window = event_loop.create_window(window_attributes)?;
let _ = self.window.set(window);
Ok(())
}
fn init_vulkan_entry(&mut self) -> Result<()> {
// Okay, so, a Vulkan "entry" is a small set of functions which are used
// to dynamically load all the rest of Vulkan. It's our responsibility to
// know how to load the entry, then it will take care of the rest. At
// least, that's the theory, but also see flake.nix for all the
// FHS-centric assumptions it makes that we have to correct.
//
// Anyway, Vulkanalia offers an integration with libloading, which is a
// crate that wraps POSIX dlopen(). We use that; it's enabled by
// Vulkanalia's "libloading" feature.
#[allow(unsafe_code)]
let loader = unsafe { LibloadingLoader::new(LIBRARY) }?;
#[allow(unsafe_code)]
let entry = unsafe { Entry::new(loader) }?;
let _ = self.entry.set(entry);
Ok(())
}
fn init_vulkan_instance(&mut self) -> Result<()> {
let entry = self.entry.get().unwrap();
let enable_validation = cfg!(feature = "vulkan-validation")
|| cfg!(debug_assertions);
// Since there's a lot of factors going into our instance creation
// request, we'll build up the parameters mutably.
let mut flags = vk::InstanceCreateFlags::empty();
let mut extensions = Vec::new();
let mut layers = Vec::new();
// Before we go any further, use Vulkan's introspection to list off
// what's available.
let mut available_extensions = HashSet::new();
#[allow(unsafe_code)]
for extension in
unsafe { entry.enumerate_instance_extension_properties(None) }?
{
available_extensions.insert(extension.extension_name);
}
let available_extensions = available_extensions;
let mut available_layers = HashSet::new();
#[allow(unsafe_code)]
for layer in unsafe { entry.enumerate_instance_layer_properties() }? {
available_layers.insert(layer.layer_name);
}
let available_layers = available_layers;
// There are certain extensions which are required by the nature of our
// windowing system. Happily, vulanaklia knows how to deal with that based
// on the type of window we give it.
//
// This is possible because of an integration between Vulkanalia and
// winit, which is enabled by Vulkanalia's "window" feature.
for extension in vulkanalia::window::get_required_instance_extensions(
self.window.get().unwrap())
{
extensions.push(extension.as_ptr());
}
// Deal with Vulkan's thing about opting in to non-conforming
// implementations.
if entry.version()? >= VULKAN_FIRST_PORTABILITY_VERSION {
if cfg!(target_os = "macos") {
// Vulkan on the Mac is not fully conforming.
extensions.push(
vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_EXTENSION.name.as_ptr());
extensions.push(
vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name.as_ptr());
flags.insert(vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR);
}
}
// Request the LunarG validation layer, when appropriate.
if enable_validation {
let layer_name = vk::ExtensionName::from_bytes(
b"VK_LAYER_KHRONOS_validation");
if available_layers.contains(&layer_name) {
layers.push(layer_name.as_ptr());
} else {
eprintln!("Vulkan validation requested at build time, \
but no validation layer available.");
}
}
// Request the debug extension. This cooperates with code below, which
// runs after the instance is created.
let debug_extension_name = vk::EXT_DEBUG_UTILS_EXTENSION.name;
if available_extensions.contains(&debug_extension_name) {
extensions.push(debug_extension_name.as_ptr());
} else {
eprintln!("Vulkan debug extension not available; \
this may mean other messages don't show up.");
}
let application_info = ApplicationInfo::builder()
.application_name(b"Surreality\0")
.application_version(vk::make_version(1, 0, 0))
.engine_name(b"Surreality\0")
.engine_version(vk::make_version(1, 0, 0))
.api_version(vk::make_version(1, 0, 0));
let instance_create_info = InstanceCreateInfo::builder()
.application_info(&application_info)
.flags(flags)
.enabled_extension_names(&extensions)
.enabled_layer_names(&layers);
#[allow(unsafe_code)]
let instance = unsafe {
entry.create_instance(&instance_create_info, None)
}?;
// Configure the debug extension. This cooperates with code above, which
// requests the extension.
if available_extensions.contains(&debug_extension_name)
&& self.debug_messager.get().is_none()
{
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
.message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all())
.message_type(vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE)
.user_callback(Some(debug_messager_callback));
#[allow(unsafe_code)]
let debug_messager = unsafe {
instance.create_debug_utils_messenger_ext(&debug_info, None)
}?;
let _ = self.debug_messager.set(debug_messager);
}
let _ = self.instance.set(instance);
Ok(())
}
fn render(&mut self, window_id: WindowId) -> Result<()> {
if let Some(window) = self.window.get()
&& window_id == window.id()
{
println!("render the window");
} else {
println!("render something unknown");
}
Ok(())
}
}
impl Drop for Surreality {
fn drop(&mut self) {
if let Some(debug_messager) = self.debug_messager.get()
&& let Some(instance) = self.instance.get()
{
#[allow(unsafe_code)]
unsafe {
instance.destroy_debug_utils_messenger_ext(*debug_messager, None);
}
}
if let Some(instance) = self.instance.get() {
#[allow(unsafe_code)]
unsafe { instance.destroy_instance(None) };
}
}
}
impl ApplicationHandler for Surreality {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
ignore_errors(move || {
self.init(event_loop)?;
Ok(())
});
}
fn window_event(&mut self, event_loop: &ActiveEventLoop,
window_id: WindowId, event: WindowEvent)
{
println!("window event {:?}", event);
match event {
WindowEvent::RedrawRequested => {
if !event_loop.exiting() {
if let Err(e) = self.render(window_id) {
eprintln!("Error: {}", e);
}
}
}
WindowEvent::CloseRequested => {
event_loop.exit();
}
_ => { }
}
}
}
fn main() -> std::process::ExitCode {
let body: fn() -> Result<()> = || {
let event_loop = EventLoop::new()?;
let mut surreality = Surreality::new();
event_loop.run_app(&mut surreality)?;
Ok(())
};
match body() {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(e) => {
eprintln!("Error: {}", e);
std::process::ExitCode::from(1)
}
}
}
extern "system" fn debug_messager_callback(
severity: vk::DebugUtilsMessageSeverityFlagsEXT,
flags: vk::DebugUtilsMessageTypeFlagsEXT,
data: *const vk::DebugUtilsMessengerCallbackDataEXT,
_context: *mut c_void) -> vk::Bool32
{
#[allow(unsafe_code)]
let data = unsafe { *data };
#[allow(unsafe_code)]
let text = unsafe { CStr::from_ptr(data.message) }.to_string_lossy();
let severity = if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::ERROR {
"error"
} else if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::WARNING {
"warning"
} else if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::INFO {
"informational message"
} else {
"message of unknown, very minor severity"
};
eprintln!("Vulkan {}: {} (flags {:?})", severity, text, flags);
// A return value of true would tell the validation layer we're unhappy
// with it, for the sake of conformance testing. We're not a conformance
// test so anything it does is fine with us.
vk::FALSE
}
|