|
| 1 | +use std::fmt::Write; |
1 | 2 | use std::path::Path; |
2 | 3 |
|
3 | | -// packages wesl libraries for processing and lygia |
4 | | -// note that bevy does not (as of yet) understand how to use these packages |
5 | | -// they are, instead, used by us as part of shader pre-processing in the |
6 | | -// processing_render crate |
| 4 | +// Embeds the `processing` and `lygia` WESL packages as tables of module sources; they are |
| 5 | +// registered with bevy at startup, so any shader can import them. |
7 | 6 | fn main() { |
8 | | - wesl::PkgBuilder::new("processing") |
9 | | - .scan_root("shaders/processing") |
10 | | - .expect("failed to scan processing WESL files") |
11 | | - .build_artifact() |
12 | | - .expect("failed to build processing package artifact"); |
| 7 | + let mut code = String::new(); |
| 8 | + package( |
| 9 | + &mut code, |
| 10 | + "PROCESSING", |
| 11 | + "processing", |
| 12 | + Path::new("shaders/processing"), |
| 13 | + ); |
| 14 | + package(&mut code, "LYGIA", "lygia", Path::new("../../lygia")); |
| 15 | + engine(&mut code); |
| 16 | + let out = Path::new(&std::env::var_os("OUT_DIR").unwrap()).join("shader_modules.rs"); |
| 17 | + std::fs::write(out, code).unwrap(); |
| 18 | +} |
| 19 | + |
| 20 | +// Bevy's own shader modules that ours import, for editors to resolve those imports. A bevy |
| 21 | +// module is named after its crate and its path under `src`. |
| 22 | +fn engine(code: &mut String) { |
| 23 | + let metadata = std::process::Command::new(std::env::var_os("CARGO").unwrap()) |
| 24 | + .args(["metadata", "--format-version", "1", "--locked", "--offline"]) |
| 25 | + .output() |
| 26 | + .expect("cargo metadata"); |
| 27 | + assert!( |
| 28 | + metadata.status.success(), |
| 29 | + "cargo metadata failed: {}", |
| 30 | + String::from_utf8_lossy(&metadata.stderr) |
| 31 | + ); |
| 32 | + let metadata: serde_json::Value = serde_json::from_slice(&metadata.stdout).unwrap(); |
| 33 | + let crates: std::collections::BTreeMap<String, std::path::PathBuf> = metadata["packages"] |
| 34 | + .as_array() |
| 35 | + .unwrap() |
| 36 | + .iter() |
| 37 | + .filter_map(|package| { |
| 38 | + let name = package["name"].as_str()?; |
| 39 | + let manifest = Path::new(package["manifest_path"].as_str()?); |
| 40 | + name.starts_with("bevy_") |
| 41 | + .then(|| (name.to_string(), manifest.parent().unwrap().join("src"))) |
| 42 | + }) |
| 43 | + .collect(); |
| 44 | + |
| 45 | + // the crates our packages import, and the crates those import |
| 46 | + let mut wanted: Vec<String> = Vec::new(); |
| 47 | + let mut sources: Vec<String> = ["shaders/processing.wesl", "shaders/processing"] |
| 48 | + .iter() |
| 49 | + .flat_map(|path| wesl_files(Path::new(path))) |
| 50 | + .map(|path| std::fs::read_to_string(path).unwrap()) |
| 51 | + .collect(); |
| 52 | + let mut modules = Vec::new(); |
| 53 | + while let Some(source) = sources.pop() { |
| 54 | + for name in crates.keys() { |
| 55 | + if source.contains(&format!("{name}::")) && !wanted.contains(name) { |
| 56 | + wanted.push(name.clone()); |
| 57 | + let mut found = Vec::new(); |
| 58 | + walk(&crates[name], name, "", &mut found); |
| 59 | + for (_, _, path) in &found { |
| 60 | + sources.push(std::fs::read_to_string(path).unwrap()); |
| 61 | + } |
| 62 | + modules.extend(found); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + modules.sort(); |
| 67 | + table(code, "ENGINE", modules); |
| 68 | +} |
| 69 | + |
| 70 | +fn wesl_files(path: &Path) -> Vec<std::path::PathBuf> { |
| 71 | + if path.is_file() { |
| 72 | + return vec![path.to_path_buf()]; |
| 73 | + } |
| 74 | + let mut files = Vec::new(); |
| 75 | + for entry in std::fs::read_dir(path).into_iter().flatten().flatten() { |
| 76 | + let path = entry.path(); |
| 77 | + if path.is_dir() || path.extension().is_some_and(|ext| ext == "wesl") { |
| 78 | + files.extend(wesl_files(&path)); |
| 79 | + } |
| 80 | + } |
| 81 | + files |
| 82 | +} |
13 | 83 |
|
14 | | - let mut lygia = wesl::PkgBuilder::new("lygia") |
15 | | - .scan_root("../../lygia") |
16 | | - .expect("failed to scan Lygia WESL files"); |
17 | | - wesl_only(&mut lygia.root, Path::new("../../lygia")); |
18 | | - lygia.root.submodules.retain(|sub| sub.name != "test"); |
19 | | - lygia |
20 | | - .build_artifact() |
21 | | - .expect("failed to build Lygia package artifact"); |
| 84 | +// A package is a directory of modules; its root module is the `.wesl` file beside it. |
| 85 | +fn package(code: &mut String, table_name: &str, name: &str, dir: &Path) { |
| 86 | + println!("cargo::rerun-if-changed={}", dir.display()); |
| 87 | + let mut modules = Vec::new(); |
| 88 | + let root = dir.with_extension("wesl"); |
| 89 | + if root.is_file() { |
| 90 | + println!("cargo::rerun-if-changed={}", root.display()); |
| 91 | + modules.push((name.to_string(), "package.wesl".to_string(), root)); |
| 92 | + } |
| 93 | + walk(dir, name, "", &mut modules); |
| 94 | + modules.sort(); |
| 95 | + table(code, table_name, modules); |
22 | 96 | } |
23 | 97 |
|
24 | | -// lygia's manifest includes `**/*.wesl` only; the scan also picks up its legacy `.wgsl` |
25 | | -fn wesl_only(module: &mut wesl::Module, path: &Path) { |
26 | | - if !path.with_extension("wesl").is_file() { |
27 | | - module.source.clear(); |
| 98 | +fn table(code: &mut String, table: &str, modules: Vec<(String, String, std::path::PathBuf)>) { |
| 99 | + writeln!(code, "pub static {table}: &[ShaderModule] = &[").unwrap(); |
| 100 | + for (path, file, source) in modules { |
| 101 | + let source = std::fs::canonicalize(source).unwrap(); |
| 102 | + writeln!( |
| 103 | + code, |
| 104 | + " ShaderModule {{ path: {path:?}, file: {file:?}, source: include_str!({source:?}) }}," |
| 105 | + ) |
| 106 | + .unwrap(); |
28 | 107 | } |
29 | | - for sub in &mut module.submodules { |
30 | | - wesl_only(sub, &path.join(&sub.name)); |
| 108 | + writeln!(code, "];").unwrap(); |
| 109 | +} |
| 110 | + |
| 111 | +fn walk( |
| 112 | + dir: &Path, |
| 113 | + module: &str, |
| 114 | + file: &str, |
| 115 | + modules: &mut Vec<(String, String, std::path::PathBuf)>, |
| 116 | +) { |
| 117 | + for entry in std::fs::read_dir(dir).unwrap().flatten() { |
| 118 | + let path = entry.path(); |
| 119 | + let Some(stem) = path.file_stem().and_then(|stem| stem.to_str()) else { |
| 120 | + continue; |
| 121 | + }; |
| 122 | + // a module name has to be an identifier |
| 123 | + let ident = stem.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') |
| 124 | + && !stem.starts_with(|c: char| c.is_ascii_digit()); |
| 125 | + if !ident { |
| 126 | + continue; |
| 127 | + } |
| 128 | + if path.is_dir() { |
| 129 | + // lygia's own tests are not part of the package |
| 130 | + if module == "lygia" && stem == "test" { |
| 131 | + continue; |
| 132 | + } |
| 133 | + walk( |
| 134 | + &path, |
| 135 | + &format!("{module}::{stem}"), |
| 136 | + &format!("{file}{stem}/"), |
| 137 | + modules, |
| 138 | + ); |
| 139 | + } else if path.extension().is_some_and(|ext| ext == "wesl") { |
| 140 | + modules.push(( |
| 141 | + format!("{module}::{stem}"), |
| 142 | + format!("{file}{stem}.wesl"), |
| 143 | + path, |
| 144 | + )); |
| 145 | + } |
31 | 146 | } |
32 | | - module |
33 | | - .submodules |
34 | | - .retain(|sub| !sub.source.is_empty() || !sub.submodules.is_empty()); |
35 | 147 | } |
0 commit comments