Skip to content

Commit

Permalink
Format shader compilation errors with line numbers and listing of inf…
Browse files Browse the repository at this point in the history
…o log entries (#500)

* Format shader compilation errors with line numbers and listing of info log entries

* Use lines()

---------

Co-authored-by: Ivo Worms <[email protected]>
Co-authored-by: Asger Nyman Christiansen <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent 7e7ebf9 commit 7d61b56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
16 changes: 12 additions & 4 deletions src/core/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))?;
Expand Down Expand Up @@ -645,3 +645,11 @@ impl Drop for Program {
}
}
}
fn shader_compilation_error(typ: &str, log: String, source: String) -> CoreError {
let lines: Vec<String> = source
.lines()
.enumerate()
.map(|(index, l)| format!("{:0>3}: {}", index + 1, l))
.collect();
CoreError::ShaderCompilation(typ.to_string(), lines.join("\n"), log)
}
32 changes: 20 additions & 12 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 7d61b56

Please sign in to comment.