extern crate glsl_to_spirv;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<(), Box<Error>> {
use glsl_to_spirv::ShaderType;
println!("cargo:rerun-if-changed=src/shaders");
let env_dir = env::var("OUT_DIR")?;
let out_dir = Path::new(&env_dir);
for entry in std::fs::read_dir("src/shaders")? {
let entry = entry?;
if entry.file_type()?.is_file() {
let in_path = entry.path();
let shader_type =
in_path
.extension()
.and_then(|ext| match ext.to_string_lossy().as_ref() {
"vert" => Some(ShaderType::Vertex),
"frag" => Some(ShaderType::Fragment),
_ => None,
});
if let Some(shader_type) = shader_type {
use std::io::Read;
let source = std::fs::read_to_string(&in_path)?;
let mut compiled_file = glsl_to_spirv::compile(&source, shader_type)?;
let mut compiled_bytes = Vec::new();
compiled_file.read_to_end(&mut compiled_bytes)?;
let out_path = out_dir.join(format!(
"{}.spv",
in_path.file_name().unwrap().to_string_lossy()
));
let mut f = File::create(&out_path)?;
f.write_all(&compiled_bytes)?;
}
}
}
Ok(())
}