|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
println!("building shaders...");
|
|
|
|
//shaders path
|
|
|
|
let shaders = Path::new("./src/shaders");
|
|
|
|
//shader target path
|
|
|
|
let out = Command::new("mkdir").arg("target/shaders/").output();
|
|
|
|
|
|
|
|
let shader_target = Path::new("./target/shaders/");
|
|
|
|
//compile all glsl shaders
|
|
|
|
for entry in shaders.read_dir().expect("reading shader directory failed") {
|
|
|
|
if let Ok(entry) = entry {
|
|
|
|
let shader_path = entry.path();
|
|
|
|
println!("cargo:warning=compiling shader: {:?}", shader_path);
|
|
|
|
let shader_path_string: String = "./target/shaders/".to_string()
|
|
|
|
+ shader_path.file_name().unwrap().to_str().unwrap()
|
|
|
|
+ ".spv";
|
|
|
|
println!("cargo:warning=compiling destination: {:?}", shader_path_string);
|
|
|
|
let shader_file: &OsStr = OsStr::new::<str>(shader_path_string.as_str());
|
|
|
|
Command::new("glslc")
|
|
|
|
.arg("-c")
|
|
|
|
.arg(shader_path)
|
|
|
|
.arg("-o")
|
|
|
|
.arg(shader_file)
|
|
|
|
.status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//include all compiled shaders in shaders.rs file in src dir
|
|
|
|
let mut txt = "use ash::vk::ShaderStageFlags;\n\n".to_string()
|
|
|
|
+ "pub fn shaders() -> Vec<(Vec<u8>, ShaderStageFlags)> {\nvec![\n";
|
|
|
|
for entry in shader_target
|
|
|
|
.read_dir()
|
|
|
|
.expect("reading compiled shader target directory failed")
|
|
|
|
{
|
|
|
|
if let Ok(entry) = entry {
|
|
|
|
let bin_path = entry.path();
|
|
|
|
let bin_path_string = bin_path.file_name().unwrap().to_str().unwrap().to_string();
|
|
|
|
|
|
|
|
let shader_type = (
|
|
|
|
match bin_path_string.split(".").nth(1).unwrap() {
|
|
|
|
"vert" => "VERTEX",
|
|
|
|
"frag" => "FRAGMENT",
|
|
|
|
"comp" => "COMPUTE",
|
|
|
|
"tesc" => "TESSELLATION_CONTROL",
|
|
|
|
"tese" => "TESSELLATION_EVALUATION",
|
|
|
|
"geom" => "GEOMETRY",
|
|
|
|
_ => panic!("unknown shader type"),
|
|
|
|
}
|
|
|
|
).to_string();
|
|
|
|
txt += &("(include_bytes!(\"../target/shaders/".to_owned()
|
|
|
|
+ &bin_path_string
|
|
|
|
+ "\").to_vec(), ShaderStageFlags::"
|
|
|
|
+ &shader_type
|
|
|
|
+ "),\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txt += "]}";
|
|
|
|
let mut file = File::create("./src/shaders.rs")?;
|
|
|
|
file.write_all(txt.as_bytes())?;
|
|
|
|
Ok(())
|
|
|
|
}
|