Skip to content

Commit

Permalink
Improved GLSLang error logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Nov 9, 2023
1 parent d0a555d commit 3be2deb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<render_system::RenderSystemImplementation> {
orchestrator.spawn_entity(vulkan_render_system::VulkanRenderSystem::new_as_system()).unwrap()
}
23 changes: 23 additions & 0 deletions src/rendering/shader_compilation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use colored::Colorize;

pub fn format_glslang_error(shader_name: &str, error_string: &str, source_code: &str) -> Option<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::<usize>().unwrap() - 1, error.trim())
).collect::<Vec<_>>();

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)
}
13 changes: 2 additions & 11 deletions src/rendering/vulkan_render_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<usize>().unwrap(), error.trim())).collect::<Vec<_>>();
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);
Expand Down
14 changes: 2 additions & 12 deletions src/resource_manager/material_resource_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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::<usize>().unwrap(), error.trim())).collect::<Vec<_>>();
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()));
} };
Expand Down

0 comments on commit 3be2deb

Please sign in to comment.