| 1 | fn main() { |
| 2 | #[cfg(feature = "metal")] |
| 3 | { |
| 4 | println!("cargo:rustc-link-lib=framework=CoreGraphics"); |
| 5 | } |
| 6 | #[cfg(feature = "vulkan")] |
| 7 | { |
| 8 | println!("cargo::rerun-if-changed=src/backends/vulkan/ops.wgsl"); |
| 9 | |
| 10 | let wgsl_src = std::fs::read_to_string("src/backends/vulkan/ops.wgsl") |
| 11 | .expect("failed to read ops.wgsl"); |
| 12 | let module = naga::front::wgsl::parse_str(&wgsl_src) |
| 13 | .expect("failed to parse WGSL"); |
| 14 | let info = naga::valid::Validator::new( |
| 15 | naga::valid::ValidationFlags::all(), |
| 16 | naga::valid::Capabilities::empty(), |
| 17 | ) |
| 18 | .validate(&module) |
| 19 | .expect("WGSL validation failed"); |
| 20 | |
| 21 | let options = naga::back::spv::Options { |
| 22 | lang_version: (1, 3), |
| 23 | ..Default::default() |
| 24 | }; |
| 25 | // Generate one SPIR-V module per entry point |
| 26 | let entry_points: Vec<String> = module |
| 27 | .entry_points |
| 28 | .iter() |
| 29 | .map(|ep| ep.name.clone()) |
| 30 | .collect(); |
| 31 | |
| 32 | let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); |
| 33 | let mut includes = String::new(); |
| 34 | |
| 35 | for ep_name in &entry_points { |
| 36 | let pipeline_options = naga::back::spv::PipelineOptions { |
| 37 | shader_stage: naga::ShaderStage::Compute, |
| 38 | entry_point: ep_name.clone(), |
| 39 | }; |
| 40 | let spv_words = naga::back::spv::write_vec( |
| 41 | &module, |
| 42 | &info, |
| 43 | &options, |
| 44 | Some(&pipeline_options), |
| 45 | ) |
| 46 | .unwrap_or_else(|e| panic!("SPIR-V generation failed for {ep_name}: {e}")); |
| 47 | |
| 48 | // Write SPIR-V binary |
| 49 | let spv_path = out_dir.join(format!("ops_{ep_name}.spv")); |
| 50 | let bytes: Vec<u8> = spv_words.iter().flat_map(|w| w.to_le_bytes()).collect(); |
| 51 | std::fs::write(&spv_path, &bytes).unwrap(); |
| 52 | |
| 53 | includes.push_str(&format!( |
| 54 | "(\"{ep_name}\", include_bytes!(concat!(env!(\"OUT_DIR\"), \"/ops_{ep_name}.spv\"))),\n" |
| 55 | )); |
| 56 | } |
| 57 | |
| 58 | // Write a Rust file with all SPIR-V modules |
| 59 | let rs_path = out_dir.join("spirv_ops.rs"); |
| 60 | let code = format!( |