Skip to content

Commit

Permalink
Readable shader error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Apr 5, 2018
1 parent 2494f70 commit fc8a16c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions webrender/src/device.rs
Expand Up @@ -53,7 +53,6 @@ const SHADER_VERSION_GLES: &str = "#version 300 es\n";
const SHADER_KIND_VERTEX: &str = "#define WR_VERTEX_SHADER\n";
const SHADER_KIND_FRAGMENT: &str = "#define WR_FRAGMENT_SHADER\n";
const SHADER_IMPORT: &str = "#include ";
const SHADER_LINE_MARKER: &str = "#line 1\n";

pub struct TextureSlot(pub usize);

Expand Down Expand Up @@ -170,9 +169,7 @@ fn get_shader_source(shader_name: &str, base_path: &Option<PathBuf>) -> Option<S
// Parse a shader string for imports. Imports are recursively processed, and
// prepended to the list of outputs.
fn parse_shader_source(source: String, base_path: &Option<PathBuf>, output: &mut String) {
output.push_str(SHADER_LINE_MARKER);

for (line_num, line) in source.lines().enumerate() {
for line in source.lines() {
if line.starts_with(SHADER_IMPORT) {
let imports = line[SHADER_IMPORT.len() ..].split(",");

Expand All @@ -182,8 +179,6 @@ fn parse_shader_source(source: String, base_path: &Option<PathBuf>, output: &mut
parse_shader_source(include, base_path, output);
}
}

output.push_str(&format!("#line {}\n", line_num+1));
} else {
output.push_str(line);
output.push_str("\n");
Expand Down Expand Up @@ -777,6 +772,25 @@ impl Device {
self.bound_draw_fbo = FBOId(0);
}

#[cfg(debug_assertions)]
fn print_shader_errors(source: &str, log: &str) {
// hacky way to extract the offending lines
if !log.starts_with("0:") {
return;
}
let end_pos = match log[2..].chars().position(|c| !c.is_digit(10)) {
Some(pos) => 2 + pos,
None => return,
};
let base_line_number = match log[2 .. end_pos].parse::<usize>() {
Ok(number) if number >= 2 => number - 2,
_ => return,
};
for (line, prefix) in source.lines().skip(base_line_number).zip(&["|",">","|"]) {
println!("{}\t{}", prefix, line);
}
}

pub fn compile_shader(
gl: &gl::Gl,
name: &str,
Expand All @@ -790,6 +804,8 @@ impl Device {
let log = gl.get_shader_info_log(id);
if gl.get_shader_iv(id, gl::COMPILE_STATUS) == (0 as gl::GLint) {
println!("Failed to compile shader: {}\n{}", name, log);
#[cfg(debug_assertions)]
Self::print_shader_errors(source, &log);
Err(ShaderError::Compilation(name.to_string(), log))
} else {
if !log.is_empty() {
Expand Down

0 comments on commit fc8a16c

Please sign in to comment.