Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading