gb-emu/lib/build.rs

44 lines
1.6 KiB
Rust

fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "vulkan-renderer")]
{
const SHADER_SRC_DIR: &str = "src/renderer/vulkan/shaders";
const SHADER_OUT_DIR: &str = "shaders";
println!("cargo:rerun-if-changed={}", SHADER_SRC_DIR);
std::fs::create_dir_all(SHADER_OUT_DIR)?;
for shader in std::fs::read_dir(SHADER_SRC_DIR)?.filter_map(|v| v.ok()) {
if shader.file_type()?.is_file() {
let in_path = shader.path();
if in_path
.extension()
.is_some_and(|e| e.to_string_lossy() == "wgsl")
{
let out_path = format!(
"{}/{}.spv",
SHADER_OUT_DIR,
in_path.file_stem().unwrap().to_string_lossy()
);
let module = naga::front::wgsl::parse_str(&std::fs::read_to_string(in_path)?)?;
let info = naga::valid::Validator::new(Default::default(), Default::default())
.validate(&module)?;
let spv =
naga::back::spv::write_vec(&module, &info, &Default::default(), None)?;
let compiled =
spv.iter()
.fold(Vec::with_capacity(spv.len() * 4), |mut v, w| {
v.extend_from_slice(&w.to_le_bytes());
v
});
std::fs::write(out_path, compiled.as_slice())?;
}
}
}
}
Ok(())
}