use std::env; use std::fs; use std::path::Path; fn compile_wgsl_to_spv(src_path: &str, out_name: &str) { println!("cargo:rerun-if-changed={}", src_path); let source = fs::read_to_string(src_path) .unwrap_or_else(|e| panic!("Failed to read shader {}: {}", src_path, e)); let module = naga::front::wgsl::parse_str(&source) .unwrap_or_else(|e| panic!("WGSL parse error in {}:\n{}", src_path, e.emit_to_string(&source))); let info = naga::valid::Validator::new( naga::valid::ValidationFlags::all(), naga::valid::Capabilities::PUSH_CONSTANT, ) .validate(&module) .unwrap_or_else(|e| panic!("WGSL validation error in {}:\n{:?}", src_path, e)); let mut options = naga::back::spv::Options::default(); options.lang_version = (1, 3); options.flags = naga::back::spv::WriterFlags::LABEL_VARYINGS; let words = naga::back::spv::write_vec(&module, &info, &options, None) .unwrap_or_else(|e| panic!("SPIR-V emit error in {}: {:?}", src_path, e)); let bytes: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); let out_dir = env::var("OUT_DIR").unwrap(); let dest = Path::new(&out_dir).join(out_name); fs::write(&dest, bytes).expect("Failed to write SPIR-V output"); } fn main() { compile_wgsl_to_spv("shaders/ui_sdf.wgsl", "ui_sdf.spv"); compile_wgsl_to_spv("shaders/blur.wgsl", "blur.spv"); compile_wgsl_to_spv("shaders/vibe.wgsl", "vibe.spv"); }