gb-emu/lib/build.rs
2023-10-07 10:10:24 +11:00

62 lines
2.3 KiB
Rust

fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "vulkan-renderer")]
{
let host_triple = std::env::var("HOST").unwrap();
let target_triple = std::env::var("TARGET").unwrap();
if host_triple != target_triple {
// cross compile
if !host_triple.contains("windows") && target_triple.contains("x86_64-pc-windows") {
println!(
"cargo:rustc-link-search={}",
std::env::var("VULKAN_SDK_WIN")
.expect("Could not find windows vulkan sdk path")
);
println!(
"cargo:rustc-link-search={}",
std::env::var("SHADERC_WIN").expect("Could not find windows shaderc path")
);
}
}
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(())
}