enable standard validation layer

This commit is contained in:
Robert 2023-02-08 03:15:17 +01:00
parent caeb083a3d
commit b2a0edb123
5 changed files with 128 additions and 10 deletions

56
Cargo.lock generated
View file

@ -2,6 +2,18 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
dependencies = [
"cfg-if",
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "aho-corasick"
version = "0.7.20"
@ -147,6 +159,17 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "glfw"
version = "0.50.0"
@ -263,6 +286,12 @@ dependencies = [
"malloc_buf",
]
[[package]]
name = "once_cell"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]]
name = "parking_lot"
version = "0.12.1"
@ -399,12 +428,27 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thread_local"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "vk-parse"
version = "0.8.0"
@ -424,10 +468,11 @@ dependencies = [
[[package]]
name = "vulkano"
version = "0.31.1"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49e6f6f908670b33ec1fcb1e9c25677cb4d6783893f89bc11d49d2eb5061ccb5"
checksum = "f80b11c6c46ecb2c42155c8bcbc3ff04b783185181d4bbe19d9635a711ec747f"
dependencies = [
"ahash",
"ash",
"bytemuck",
"core-graphics-types",
@ -445,9 +490,16 @@ dependencies = [
"serde",
"serde_json",
"smallvec",
"thread_local",
"vk-parse",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
vulkano = "0.31.0"
vulkano = "0.32.3"
glfw = { version = "0.50.0", features = ["vulkan"] }
[profile.dev]

View file

@ -1,5 +1,7 @@
use std::sync::Arc;
use vulkano::buffer::ExternalBufferInfo;
use crate::{window::Window, error::ApplicationError};
fn key_callback_func(window: &mut glfw::Window, key: glfw::Key, _: glfw::Scancode, action: glfw::Action, _: glfw::Modifiers) {
@ -36,25 +38,80 @@ impl Application {
}
fn init_vulkan(glfw: &glfw::Glfw) -> Result<Arc<vulkano::instance::Instance>, ApplicationError> {
let library = vulkano::library::VulkanLibrary::new()?;
let mut create_info = vulkano::instance::InstanceCreateInfo::application_from_cargo_toml();
create_info.engine_name = Some("No engine".into());
create_info.engine_version = vulkano::Version::V1_0;
create_info.max_api_version = Some(vulkano::Version::V1_0);
let required_extensions = glfw.get_required_instance_extensions().ok_or(
ApplicationError::new("GLFW", "Failed to get required GLFW extensions")
)?;
create_info.enabled_extensions = vulkano::instance::InstanceExtensions::from_iter(
required_extensions.iter().map(|ext| ext.as_str())
);
let library = vulkano::library::VulkanLibrary::new()?;
if cfg!(debug_assertions) {
println!("Enabled extensions:");
for extension in required_extensions {
println!("\t{extension}");
}
Application::register_validation_layers(library.clone(), &mut create_info)?;
}
let instance = vulkano::instance::Instance::new(library, create_info)?;
Ok(instance)
}
#[cfg(debug_assertions)]
fn register_validation_layers(library: Arc<vulkano::library::VulkanLibrary>, create_info: &mut vulkano::instance::InstanceCreateInfo) -> Result<(), ApplicationError>{
const VALIDATION_LAYERS: [&'static str; 1] = [
"VK_LAYER_KHRONOS_validation"
];
Application::check_validation_layer_support(library.clone(), &VALIDATION_LAYERS)?;
create_info.enabled_layers = VALIDATION_LAYERS.iter().map(|&layer| layer.into()).collect::<Vec<String>>();
if cfg!(debug_assertions) {
println!("Enabled validation layers:");
for layer in VALIDATION_LAYERS {
println!("\t{layer}");
}
}
Ok(())
}
#[cfg(debug_assertions)]
fn check_validation_layer_support(library: Arc<vulkano::library::VulkanLibrary>, layers: &[&'static str]) -> Result<(), ApplicationError>{
let mut supported_layers = library.layer_properties()?;
let mut missing_validation_layer: &str = "";
let contains_all_layers = layers.iter().all(|&layer| {
if supported_layers.find(|supported_layer|
supported_layer.name() == layer
).is_none() {
missing_validation_layer = layer;
return false;
}
true
});
if !contains_all_layers {
return Err(ApplicationError::new("Validation Layers", format!("This driver does not support the \"{missing_validation_layer}\" validation layer").as_str()));
}
Ok(())
}
pub fn run(&mut self) -> Result<(), ApplicationError> {
while !self.window.should_close() {
self.glfw.poll_events();

View file

@ -1,6 +1,6 @@
use std::fmt::Display;
use vulkano::{LoadingError, instance::{self, InstanceCreationError, Instance, InstanceCreateInfo}};
use vulkano::{LoadingError, instance::{self, InstanceCreationError, Instance, InstanceCreateInfo}, OomError};
#[derive(Debug)]
pub struct ApplicationError {
@ -35,8 +35,8 @@ impl From<LoadingError> for ApplicationError {
impl From<InstanceCreationError> for ApplicationError {
fn from(value: InstanceCreationError) -> Self {
ApplicationError {
what: "Instance Creation Error".into(),
which: match value {
which: "Instance Creation Error".into(),
what: match value {
InstanceCreationError::ExtensionNotPresent => "Extension not present".into(),
InstanceCreationError::ExtensionRestrictionNotMet(error) => error.to_string(),
InstanceCreationError::IncompatibleDriver => "Incompatible driver".into(),
@ -47,4 +47,10 @@ impl From<InstanceCreationError> for ApplicationError {
}
}
}
}
impl From<OomError> for ApplicationError {
fn from(value: OomError) -> Self {
ApplicationError { what: value.to_string(), which: "Out of Memory".into() }
}
}

View file

@ -5,7 +5,10 @@ mod window;
mod error;
fn main() {
let mut app = Application::new().unwrap();
let mut app = match Application::new() {
Ok(app) => app,
Err(err) => { eprintln!("{err}"); return; }
};
if let Err(err) = app.run() {
eprintln!("Application terminated unexplectedly: {err}");