mirror of
https://github.com/italicsjenga/vello.git
synced 2025-01-10 12:41:30 +11:00
Merge pull request #2 from bzm3r/validation-layer
Set up validation layers and debug reporting.
This commit is contained in:
commit
144f46c5fa
|
@ -8,3 +8,4 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ash = "0.30"
|
ash = "0.30"
|
||||||
|
once_cell = "1.3.1"
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
//! Vulkan implemenation of HAL trait.
|
//! Vulkan implemenation of HAL trait.
|
||||||
|
|
||||||
use std::ffi::CString;
|
use std::borrow::Cow;
|
||||||
|
use std::ffi::{CStr, CString};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use ash::extensions::ext::DebugUtils;
|
||||||
use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0};
|
use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0};
|
||||||
use ash::{vk, Device, Entry, Instance};
|
use ash::{vk, Device, Entry, Instance};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
|
@ -12,8 +15,9 @@ pub struct VkInstance {
|
||||||
/// Retain the dynamic lib.
|
/// Retain the dynamic lib.
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
entry: Entry,
|
entry: Entry,
|
||||||
|
|
||||||
instance: Instance,
|
instance: Instance,
|
||||||
|
_dbg_loader: Option<DebugUtils>,
|
||||||
|
_dbg_callbk: Option<vk::DebugUtilsMessengerEXT>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VkDevice {
|
pub struct VkDevice {
|
||||||
|
@ -61,6 +65,56 @@ pub struct QueryPool {
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct MemFlags(vk::MemoryPropertyFlags);
|
pub struct MemFlags(vk::MemoryPropertyFlags);
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
unsafe extern "system" fn vulkan_debug_callback(
|
||||||
|
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
|
||||||
|
message_type: vk::DebugUtilsMessageTypeFlagsEXT,
|
||||||
|
p_callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT,
|
||||||
|
_user_data: *mut std::os::raw::c_void,
|
||||||
|
) -> vk::Bool32 {
|
||||||
|
let callback_data = &*p_callback_data;
|
||||||
|
let message_id_number: i32 = callback_data.message_id_number as i32;
|
||||||
|
|
||||||
|
let message_id_name = if callback_data.p_message_id_name.is_null() {
|
||||||
|
Cow::from("")
|
||||||
|
} else {
|
||||||
|
CStr::from_ptr(callback_data.p_message_id_name).to_string_lossy()
|
||||||
|
};
|
||||||
|
|
||||||
|
let message = if callback_data.p_message.is_null() {
|
||||||
|
Cow::from("")
|
||||||
|
} else {
|
||||||
|
CStr::from_ptr(callback_data.p_message).to_string_lossy()
|
||||||
|
};
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{:?}:\n{:?} [{} ({})] : {}\n",
|
||||||
|
message_severity,
|
||||||
|
message_type,
|
||||||
|
message_id_name,
|
||||||
|
message_id_number,
|
||||||
|
message,
|
||||||
|
);
|
||||||
|
|
||||||
|
vk::FALSE
|
||||||
|
}
|
||||||
|
|
||||||
|
static LAYERS: Lazy<Vec<&'static CStr>> = Lazy::new(|| {
|
||||||
|
let mut layers: Vec<&'static CStr> = vec![];
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
layers.push(CStr::from_bytes_with_nul(b"VK_LAYER_KHRONOS_validation\0").unwrap());
|
||||||
|
}
|
||||||
|
layers
|
||||||
|
});
|
||||||
|
|
||||||
|
static EXTS: Lazy<Vec<&'static CStr>> = Lazy::new(|| {
|
||||||
|
let mut exts: Vec<&'static CStr> = vec![];
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
exts.push(DebugUtils::name());
|
||||||
|
}
|
||||||
|
exts
|
||||||
|
});
|
||||||
|
|
||||||
impl VkInstance {
|
impl VkInstance {
|
||||||
/// Create a new instance.
|
/// Create a new instance.
|
||||||
///
|
///
|
||||||
|
@ -70,18 +124,74 @@ impl VkInstance {
|
||||||
unsafe {
|
unsafe {
|
||||||
let app_name = CString::new("VkToy").unwrap();
|
let app_name = CString::new("VkToy").unwrap();
|
||||||
let entry = Entry::new()?;
|
let entry = Entry::new()?;
|
||||||
|
|
||||||
|
let exist_layers = entry
|
||||||
|
.enumerate_instance_layer_properties()?;
|
||||||
|
let layers = LAYERS.iter().filter_map(|&lyr| {
|
||||||
|
exist_layers
|
||||||
|
.iter()
|
||||||
|
.find(|x|
|
||||||
|
CStr::from_ptr(x.layer_name.as_ptr()) == lyr
|
||||||
|
)
|
||||||
|
.map(|_| lyr.as_ptr())
|
||||||
|
.or_else(|| {
|
||||||
|
println!("Unable to find layer: {}, have you installed the Vulkan SDK?", lyr.to_string_lossy());
|
||||||
|
None
|
||||||
|
})
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let exist_exts = entry
|
||||||
|
.enumerate_instance_extension_properties()?;
|
||||||
|
let exts = EXTS.iter().filter_map(|&ext| {
|
||||||
|
exist_exts
|
||||||
|
.iter()
|
||||||
|
.find(|x|
|
||||||
|
CStr::from_ptr(x.extension_name.as_ptr()) == ext
|
||||||
|
)
|
||||||
|
.map(|_| ext.as_ptr())
|
||||||
|
.or_else(|| {
|
||||||
|
println!("Unable to find extension: {}, have you installed the Vulkan SDK?", ext.to_string_lossy());
|
||||||
|
None
|
||||||
|
})
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
let instance = entry.create_instance(
|
let instance = entry.create_instance(
|
||||||
&vk::InstanceCreateInfo::builder().application_info(
|
&vk::InstanceCreateInfo::builder()
|
||||||
&vk::ApplicationInfo::builder()
|
.application_info(
|
||||||
.application_name(&app_name)
|
&vk::ApplicationInfo::builder()
|
||||||
.application_version(0)
|
.application_name(&app_name)
|
||||||
.engine_name(&app_name)
|
.application_version(0)
|
||||||
.api_version(vk::make_version(1, 0, 0)),
|
.engine_name(&app_name)
|
||||||
),
|
.api_version(vk::make_version(1, 0, 0)),
|
||||||
|
)
|
||||||
|
.enabled_layer_names(&layers)
|
||||||
|
.enabled_extension_names(&exts),
|
||||||
None,
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(VkInstance { entry, instance })
|
let (_dbg_loader, _dbg_callbk) = if cfg!(debug_assertions) {
|
||||||
|
let dbg_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
|
||||||
|
.message_severity(
|
||||||
|
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
|
||||||
|
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING,
|
||||||
|
)
|
||||||
|
.message_type(vk::DebugUtilsMessageTypeFlagsEXT::all())
|
||||||
|
.pfn_user_callback(Some(vulkan_debug_callback));
|
||||||
|
let dbg_loader = DebugUtils::new(&entry, &instance);
|
||||||
|
let dbg_callbk = dbg_loader
|
||||||
|
.create_debug_utils_messenger(&dbg_info, None)
|
||||||
|
.unwrap();
|
||||||
|
(Some(dbg_loader), Some(dbg_callbk))
|
||||||
|
} else {
|
||||||
|
(None, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(VkInstance {
|
||||||
|
entry,
|
||||||
|
instance,
|
||||||
|
_dbg_loader,
|
||||||
|
_dbg_callbk,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue