You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.5 KiB

11 months ago
use std::ffi::OsStr;
use std::fs::File;
use std::io::prelude::*;
use std::io::Write;
use std::path::Path;
use std::process::Command;
11 months ago
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();
11 months ago
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")
11 months ago
.arg("-c")
.arg(shader_path)
.arg("-o")
.arg(shader_file)
.status();
11 months ago
}
}
//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")
{
11 months ago
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");
11 months ago
}
}
txt += "]}";
11 months ago
let mut file = File::create("./src/shaders.rs")?;
file.write_all(txt.as_bytes())?;
Ok(())
}