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
use {
    super::{DriverError, PhysicalDevice, QueueFamily, QueueFamilyProperties},
    ash::{extensions::ext, vk, Entry},
    log::{debug, error, info, logger, trace, warn, Level, Metadata},
    std::{
        env::var,
        ffi::{c_void, CStr, CString},
        fmt::{Debug, Formatter},
        ops::Deref,
        os::raw::c_char,
        process::id,
        thread::{current, panicking, park},
    },
};

unsafe extern "system" fn vulkan_debug_callback(
    _flags: vk::DebugReportFlagsEXT,
    _obj_type: vk::DebugReportObjectTypeEXT,
    _src_obj: u64,
    _location: usize,
    _msg_code: i32,
    _layer_prefix: *const c_char,
    message: *const c_char,
    _user_data: *mut c_void,
) -> u32 {
    let message = CStr::from_ptr(message).to_str().unwrap();

    if message.starts_with("Validation Warning: [ UNASSIGNED-BestPractices-pipeline-stage-flags ]")
    {
        // vk_sync uses vk::PipelineStageFlags::ALL_COMMANDS with AccessType::NOTHING and others
        warn!("{}", message);
    } else {
        let prefix = "Validation Error: [ ";

        let (vuid, message) = if message.starts_with(prefix) {
            let (vuid, message) = message
                .trim_start_matches(prefix)
                .split_once(" ]")
                .unwrap_or_default();
            let message = message.split(" | ").nth(2).unwrap_or(message);

            (Some(vuid.trim()), message)
        } else {
            (None, message)
        };

        if let Some(vuid) = vuid {
            info!("{vuid}");
        }

        error!("🆘 {message}");

        if !logger().enabled(&Metadata::builder().level(Level::Debug).build())
            || var("RUST_LOG")
                .map(|rust_log| rust_log.is_empty())
                .unwrap_or(true)
        {
            panic!("run with `RUST_LOG=trace` environment variable to display more information - see https://github.com/rust-lang/log#in-executables");
        }

        if current().name() != Some("main") {
            warn!("executing on a child thread!")
        }

        debug!(
            "🛑 PARKING THREAD `{}` -> attach debugger to pid {}!",
            current().name().unwrap_or_default(),
            id()
        );

        logger().flush();

        park();
    }

    vk::FALSE
}

pub struct Instance {
    _debug_callback: Option<vk::DebugReportCallbackEXT>,
    #[allow(deprecated)] // TODO: Remove? Look into this....
    _debug_loader: Option<ext::DebugReport>,
    _debug_utils: Option<ext::DebugUtils>,
    pub entry: Entry,
    instance: ash::Instance,
}

impl Instance {
    pub fn new<'a>(
        debug: bool,
        required_extensions: impl Iterator<Item = &'a CStr>,
    ) -> Result<Self, DriverError> {
        #[cfg(not(target_os = "macos"))]
        let vulkan_api_version = vk::API_VERSION_1_2;

        #[cfg(target_os = "macos")]
        // See https://github.com/KhronosGroup/MoltenVK/issues/1567
        let vulkan_api_version = vk::API_VERSION_1_1;

        let entry = unsafe {
            #[cfg(not(target_os = "macos"))]
            let entry = Entry::load().map_err(|_| {
                error!("Vulkan driver not found");

                DriverError::Unsupported
            })?;

            #[cfg(target_os = "macos")]
            let entry = ash_molten::load();

            entry
        };
        let required_extensions = required_extensions.collect::<Vec<_>>();
        let instance_extensions = required_extensions
            .iter()
            .map(|ext| ext.as_ptr())
            .chain(unsafe { Self::extension_names(debug).into_iter() })
            .collect::<Box<[_]>>();
        let layer_names = Self::layer_names(debug);
        let layer_names: Vec<*const i8> = layer_names
            .iter()
            .map(|raw_name| raw_name.as_ptr())
            .collect();
        let app_desc = vk::ApplicationInfo::builder().api_version(vulkan_api_version);
        let instance_desc = vk::InstanceCreateInfo::builder()
            .application_info(&app_desc)
            .enabled_layer_names(&layer_names)
            .enabled_extension_names(&instance_extensions);

        let instance = unsafe {
            entry.create_instance(&instance_desc, None).map_err(|_| {
                if debug {
                    warn!("Debug may only be enabled with a valid Vulkan SDK installation");
                }

                error!("Vulkan driver does not support API v1.2");

                for layer_name in Self::layer_names(debug) {
                    debug!("Layer: {:?}", layer_name);
                }

                for extension_name in required_extensions {
                    debug!("Extension: {:?}", extension_name);
                }

                DriverError::Unsupported
            })?
        };

        trace!("Created a Vulkan instance");

        let (debug_loader, debug_callback, debug_utils) = if debug {
            let debug_info = vk::DebugReportCallbackCreateInfoEXT {
                flags: vk::DebugReportFlagsEXT::ERROR
                    | vk::DebugReportFlagsEXT::WARNING
                    | vk::DebugReportFlagsEXT::PERFORMANCE_WARNING,
                pfn_callback: Some(vulkan_debug_callback),
                ..Default::default()
            };

            #[allow(deprecated)]
            let debug_loader = ext::DebugReport::new(&entry, &instance);

            let debug_callback = unsafe {
                #[allow(deprecated)]
                debug_loader
                    .create_debug_report_callback(&debug_info, None)
                    .unwrap()
            };

            let debug_utils = ext::DebugUtils::new(&entry, &instance);

            (Some(debug_loader), Some(debug_callback), Some(debug_utils))
        } else {
            (None, None, None)
        };

        Ok(Self {
            _debug_callback: debug_callback,
            _debug_loader: debug_loader,
            _debug_utils: debug_utils,
            entry,
            instance,
        })
    }

    unsafe fn extension_names(debug: bool) -> Vec<*const i8> {
        let mut res = vec![];

        if debug {
            #[allow(deprecated)]
            res.push(ext::DebugReport::name().as_ptr());
            res.push(vk::ExtDebugUtilsFn::name().as_ptr());
        }

        res
    }

    fn layer_names(debug: bool) -> Vec<CString> {
        let mut res = Vec::new();

        if debug {
            if let Ok(name) = CString::new("VK_LAYER_KHRONOS_validation") {
                res.push(name);
            }
        }

        res
    }

    pub fn physical_devices(
        this: &Self,
    ) -> Result<impl Iterator<Item = PhysicalDevice> + '_, DriverError> {
        unsafe {
            Ok(this
                .enumerate_physical_devices()
                .map_err(|err| {
                    warn!("{err}");

                    DriverError::Unsupported
                })?
                .into_iter()
                .map(|physical_device| {
                    let props = this.get_physical_device_properties(physical_device);
                    let queue_families = this
                        .get_physical_device_queue_family_properties(physical_device)
                        .into_iter()
                        .enumerate()
                        .map(|(idx, props)| QueueFamily {
                            idx: idx as _,
                            props: QueueFamilyProperties {
                                queue_flags: props.queue_flags,
                                queue_count: props.queue_count,
                                timestamp_valid_bits: props.timestamp_valid_bits,
                                min_image_transfer_granularity: [
                                    props.min_image_transfer_granularity.width,
                                    props.min_image_transfer_granularity.height,
                                    props.min_image_transfer_granularity.depth,
                                ],
                            },
                        })
                        .collect();
                    let mem_props = this.get_physical_device_memory_properties(physical_device);

                    PhysicalDevice::new(physical_device, mem_props, props, queue_families)
                })
                .filter(|physical_device| {
                    let major = vk::api_version_major(physical_device.props.api_version);
                    let minor = vk::api_version_minor(physical_device.props.api_version);

                    major == 1 && minor >= 1 || major > 1
                }))
        }
    }
}

impl Debug for Instance {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str("Instance")
    }
}

impl Deref for Instance {
    type Target = ash::Instance;

    fn deref(&self) -> &Self::Target {
        &self.instance
    }
}

impl Drop for Instance {
    fn drop(&mut self) {
        if panicking() {
            return;
        }

        unsafe {
            #[allow(deprecated)]
            if let Some(debug_loader) = &self._debug_loader {
                let debug_callback = self._debug_callback.unwrap();
                debug_loader.destroy_debug_report_callback(debug_callback, None);
            }

            self.instance.destroy_instance(None);
        }
    }
}