From fed822722e44a0f79fd4546feae08987d0fcba14 Mon Sep 17 00:00:00 2001 From: Ivo Worms Date: Thu, 24 Oct 2024 17:33:11 +0200 Subject: [PATCH 1/2] Format shader compilation errors with line numbers and listing of info log entries --- src/core.rs | 2 +- src/core/program.rs | 17 +++++++++++++---- src/renderer.rs | 32 ++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/core.rs b/src/core.rs index 95ba725a..9a1860b7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -55,7 +55,7 @@ pub enum CoreError { ContextCreation(String), #[error("failed rendering with error: {0}")] ContextError(String), - #[error("failed compiling {0} shader: {1}\n{2}")] + #[error("failed compiling {0} shader\n\nsource:\n{1}\n\nlog:\n{2}")] ShaderCompilation(String, String, String), #[error("failed to link shader program: {0}")] ShaderLink(String), diff --git a/src/core/program.rs b/src/core/program.rs index 01821af6..8316a30a 100644 --- a/src/core/program.rs +++ b/src/core/program.rs @@ -66,16 +66,16 @@ impl Program { if !context.get_program_link_status(id) { let log = context.get_shader_info_log(vert_shader); if !log.is_empty() { - Err(CoreError::ShaderCompilation( - "vertex".to_string(), + Err(shader_compilation_error( + "vertex", log, vertex_shader_source, ))?; } let log = context.get_shader_info_log(frag_shader); if !log.is_empty() { - Err(CoreError::ShaderCompilation( - "fragment".to_string(), + Err(shader_compilation_error( + "fragment", log, fragment_shader_source, ))?; @@ -645,3 +645,12 @@ impl Drop for Program { } } } +fn shader_compilation_error(typ: &str, log: String, source: String) -> CoreError { + let lines: Vec<&str> = source.split('\n').collect(); + let lines: Vec = lines + .into_iter() + .enumerate() + .map(|(index, l)| format!("{:0>3}: {}", index + 1, l)) + .collect(); + CoreError::ShaderCompilation(typ.to_string(), lines.join("\n"), log) +} diff --git a/src/renderer.rs b/src/renderer.rs index ed576fd1..cf25ca67 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -421,12 +421,14 @@ pub fn render_with_material( let mut programs = context.programs.write().unwrap(); let program = programs.entry(id).or_insert_with(|| { - Program::from_source( + match Program::from_source( context, &geometry.vertex_shader_source(fragment_attributes), &material.fragment_shader_source(lights), - ) - .expect("Failed compiling shader") + ) { + Ok(program) => program, + Err(err) => panic!("{}", err.to_string()), + } }); material.use_uniforms(program, camera, lights); geometry.draw( @@ -460,12 +462,14 @@ pub fn render_with_effect( let mut programs = context.programs.write().unwrap(); let program = programs.entry(id).or_insert_with(|| { - Program::from_source( + match Program::from_source( context, &geometry.vertex_shader_source(fragment_attributes), &effect.fragment_shader_source(lights, color_texture, depth_texture), - ) - .expect("Failed compiling shader") + ) { + Ok(program) => program, + Err(err) => panic!("{}", err.to_string()), + } }); effect.use_uniforms(program, camera, lights, color_texture, depth_texture); geometry.draw(camera, program, effect.render_states(), fragment_attributes); @@ -494,12 +498,14 @@ pub fn apply_screen_material( let mut programs = context.programs.write().unwrap(); let program = programs.entry(id).or_insert_with(|| { - Program::from_source( + match Program::from_source( context, full_screen_vertex_shader_source(), &material.fragment_shader_source(lights), - ) - .expect("Failed compiling shader") + ) { + Ok(program) => program, + Err(err) => panic!("{}", err.to_string()), + } }); material.use_uniforms(program, camera, lights); full_screen_draw( @@ -535,12 +541,14 @@ pub fn apply_screen_effect( let mut programs = context.programs.write().unwrap(); let program = programs.entry(id).or_insert_with(|| { - Program::from_source( + match Program::from_source( context, full_screen_vertex_shader_source(), &effect.fragment_shader_source(lights, color_texture, depth_texture), - ) - .expect("Failed compiling shader") + ) { + Ok(program) => program, + Err(err) => panic!("{}", err.to_string()), + } }); effect.use_uniforms(program, camera, lights, color_texture, depth_texture); full_screen_draw(context, program, effect.render_states(), camera.viewport()); From bddcdbce34cc8a62c3393be167d6c75a708d1256 Mon Sep 17 00:00:00 2001 From: Asger Nyman Christiansen Date: Mon, 11 Nov 2024 13:09:13 +0100 Subject: [PATCH 2/2] Use lines() --- src/core/program.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/program.rs b/src/core/program.rs index 8316a30a..ca1dc5ac 100644 --- a/src/core/program.rs +++ b/src/core/program.rs @@ -646,9 +646,8 @@ impl Drop for Program { } } fn shader_compilation_error(typ: &str, log: String, source: String) -> CoreError { - let lines: Vec<&str> = source.split('\n').collect(); - let lines: Vec = lines - .into_iter() + let lines: Vec = source + .lines() .enumerate() .map(|(index, l)| format!("{:0>3}: {}", index + 1, l)) .collect();