From 3be2deb841a25b4f8e57892e6e2a87664ceef7f1 Mon Sep 17 00:00:00 2001 From: Facundo Villa Date: Thu, 9 Nov 2023 18:34:11 -0300 Subject: [PATCH] Improved GLSLang error logging. --- Cargo.lock | 1 + Cargo.toml | 1 + src/rendering/mod.rs | 2 ++ src/rendering/shader_compilation.rs | 23 +++++++++++++++++++ src/rendering/vulkan_render_system.rs | 13 ++--------- .../material_resource_handler.rs | 14 ++--------- 6 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 src/rendering/shader_compilation.rs diff --git a/Cargo.lock b/Cargo.lock index a64c5100..e42ddf0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,7 @@ version = "0.1.0" dependencies = [ "ash", "bitflags 2.4.1", + "colored", "component_derive", "dual_quaternion", "futures", diff --git a/Cargo.toml b/Cargo.toml index 7f3907eb..b9db6bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ serde = "1.0.187" log = "0.4.20" simple_logger = "4.2.0" meshopt = "0.1.9" +colored = "2.0.4" [profile.dev] incremental = true diff --git a/src/rendering/mod.rs b/src/rendering/mod.rs index 541ce338..29d72fcf 100644 --- a/src/rendering/mod.rs +++ b/src/rendering/mod.rs @@ -15,6 +15,8 @@ mod vulkan_render_system; pub mod visibility_model; +pub(crate) mod shader_compilation; + pub fn create_render_system(orchestrator: &orchestrator::Orchestrator) -> orchestrator::EntityHandle { orchestrator.spawn_entity(vulkan_render_system::VulkanRenderSystem::new_as_system()).unwrap() } \ No newline at end of file diff --git a/src/rendering/shader_compilation.rs b/src/rendering/shader_compilation.rs new file mode 100644 index 00000000..3508a456 --- /dev/null +++ b/src/rendering/shader_compilation.rs @@ -0,0 +1,23 @@ +use colored::Colorize; + +pub fn format_glslang_error(shader_name: &str, error_string: &str, source_code: &str) -> Option { + let errors = error_string.lines().filter(|error| + error.starts_with(shader_name)).map(|error| + (error.split(':').nth(1).unwrap(), error.split(':').nth(4).unwrap())).map(|(error_line_number_string, error)| + (error_line_number_string.trim().parse::().unwrap() - 1, error.trim()) + ).collect::>(); + + let mut error_string = String::new(); + + for (error_line_index, error) in errors { + let previous_previous_line = format!("{}| {}", error_line_index - 2, source_code.lines().nth(error_line_index - 2).unwrap_or("").dimmed()); + let previous_line = format!("{}| {}", error_line_index - 1, source_code.lines().nth(error_line_index - 1).unwrap_or("").dimmed()); + let current_line = format!("{}| {} {} {}", error_line_index, source_code.lines().nth(error_line_index).unwrap_or("").bold(), "←".red().bold(), error.red()); + let next_line = format!("{}| {}", error_line_index + 1, source_code.lines().nth(error_line_index + 1).unwrap_or("").dimmed()); + let next_next_line = format!("{}| {}", error_line_index + 2, source_code.lines().nth(error_line_index + 2).unwrap_or("").dimmed()); + + error_string.push_str(&format!("{}\n{}\n{}\n{}\n{}\n", previous_previous_line, previous_line, current_line, next_line, next_next_line)); + } + + Some(error_string) +} \ No newline at end of file diff --git a/src/rendering/vulkan_render_system.rs b/src/rendering/vulkan_render_system.rs index a7f1dfab..176be34c 100644 --- a/src/rendering/vulkan_render_system.rs +++ b/src/rendering/vulkan_render_system.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap,}; use ash::vk; -use crate::{orchestrator, window_system, render_debugger::RenderDebugger, rendering::render_system}; +use crate::{orchestrator, window_system, render_debugger::RenderDebugger, rendering::{render_system, self}}; #[cfg(not(test))] use log::{warn, error, debug}; @@ -134,16 +134,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { }, Err(err) => { let error_string = err.to_string(); - let errors = error_string.lines().filter(|error| error.starts_with("shader_name:")).map(|error| (error.split(':').nth(1).unwrap(), error.split(':').nth(4).unwrap())).map(|(error_line_number_string, error)| (error_line_number_string.trim().parse::().unwrap(), error.trim())).collect::>(); - let mut error_string = String::new(); - - for (error_line_index, error) in errors { - let previous_line = shader_text.lines().nth(error_line_index - 1).unwrap_or(""); - let current_line = shader_text.lines().nth(error_line_index).unwrap_or(""); - let next_line = shader_text.lines().nth(error_line_index + 1).unwrap_or(""); - - error_string.push_str(&format!("{}\n{} Error: {}\n{}\n", previous_line, current_line, error, next_line)); - } + let error_string = rendering::shader_compilation::format_glslang_error("shader_name:", &error_string, &shader_text).unwrap_or(error_string); error!("Error compiling shader:\n{}", error_string); panic!("Error compiling shader: {}", err); diff --git a/src/resource_manager/material_resource_handler.rs b/src/resource_manager/material_resource_handler.rs index 575c84ef..e84906e1 100644 --- a/src/resource_manager/material_resource_handler.rs +++ b/src/resource_manager/material_resource_handler.rs @@ -3,7 +3,7 @@ use std::io::Read; use log::{warn, debug, error}; use serde::{Serialize, Deserialize}; -use crate::{rendering::render_system, jspd::{self}}; +use crate::{rendering::{render_system, self}, jspd::{self}}; use super::{GenericResourceSerialization, Resource, ProcessedResources, resource_handler::ResourceHandler, resource_manager::ResourceManager}; @@ -192,17 +192,7 @@ impl MaterialResourcerHandler { let compilation_artifact = match binary { Ok(binary) => { binary } Err(err) => { let error_string = err.to_string(); - let errors = error_string.lines().filter(|error| error.starts_with("shader_name:")).map(|error| (error.split(':').nth(1).unwrap(), error.split(':').nth(4).unwrap())).map(|(error_line_number_string, error)| (error_line_number_string.trim().parse::().unwrap(), error.trim())).collect::>(); - let mut error_string = String::new(); - - for (error_line_index, error) in errors { - let previous_line = glsl.lines().nth(error_line_index - 1).unwrap_or(""); - let current_line = glsl.lines().nth(error_line_index).unwrap_or(""); - let next_line = glsl.lines().nth(error_line_index + 1).unwrap_or(""); - - error_string.push_str(&format!("{} Error: {}\n{}\n{}\n", previous_line, current_line, next_line, error)); - } - + let error_string = rendering::shader_compilation::format_glslang_error(path, &error_string, &glsl).unwrap_or(error_string); error!("Error compiling shader:\n{}", error_string); return Some(Err(err.to_string())); } };