diff --git a/piet-wgsl/src/shaders.rs b/piet-wgsl/src/shaders.rs index a011c58..8cb6129 100644 --- a/piet-wgsl/src/shaders.rs +++ b/piet-wgsl/src/shaders.rs @@ -18,7 +18,7 @@ mod preprocess; -use std::{collections::HashSet, fs, path::Path}; +use std::collections::HashSet; use wgpu::Device; @@ -30,6 +30,12 @@ pub const PATH_COARSE_WG: u32 = 256; pub const PATH_DRAWOBJ_WG: u32 = 256; pub const CLIP_REDUCE_WG: u32 = 256; +macro_rules! shader { + ($name:expr) => { + include_str!(concat!(concat!("../shader/", $name), ".wgsl")) + }; +} + pub struct Shaders { pub pathtag_reduce: ShaderId, pub pathtag_scan: ShaderId, @@ -57,14 +63,14 @@ pub struct FullShaders { } pub fn init_shaders(device: &Device, engine: &mut Engine) -> Result { - let shader_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/shader")); - let imports = preprocess::get_imports(shader_dir); - let read_shader = - |path: &str| fs::read_to_string(shader_dir.join(path.to_string() + ".wgsl")).unwrap(); + let imports = SHARED_SHADERS + .iter() + .copied() + .collect::>(); let empty = HashSet::new(); let pathtag_reduce = engine.add_shader( device, - preprocess::preprocess(&read_shader("pathtag_reduce"), &empty, &imports).into(), + preprocess::preprocess(shader!("pathtag_reduce"), &empty, &imports).into(), &[ BindType::BufReadOnly, BindType::BufReadOnly, @@ -73,7 +79,7 @@ pub fn init_shaders(device: &Device, engine: &mut Engine) -> Result Result Result Result Result { - let shader_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/shader")); - let imports = preprocess::get_imports(shader_dir); - let read_shader = - |path: &str| fs::read_to_string(shader_dir.join(path.to_string() + ".wgsl")).unwrap(); + let imports = SHARED_SHADERS + .iter() + .copied() + .collect::>(); let empty = HashSet::new(); let mut full_config = HashSet::new(); full_config.insert("full".into()); let pathtag_reduce = engine.add_shader( device, - preprocess::preprocess(&read_shader("pathtag_reduce"), &full_config, &imports).into(), + preprocess::preprocess(shader!("pathtag_reduce"), &full_config, &imports).into(), &[ BindType::BufReadOnly, BindType::BufReadOnly, @@ -138,7 +144,7 @@ pub fn full_shaders(device: &Device, engine: &mut Engine) -> Result Result Result Result Result Result Result Result Result Result Result Result Result { + ( + $name, + include_str!(concat!(concat!("../shader/shared/", $name), ".wgsl")), + ) + }; +} + +const SHARED_SHADERS: &[(&str, &str)] = &[ + shared_shader!("bbox"), + shared_shader!("blend"), + shared_shader!("bump"), + shared_shader!("clip"), + shared_shader!("config"), + shared_shader!("cubic"), + shared_shader!("bbox"), + shared_shader!("drawtag"), + shared_shader!("pathtag"), + shared_shader!("ptcl"), + shared_shader!("segment"), + shared_shader!("tile"), +]; diff --git a/piet-wgsl/src/shaders/preprocess.rs b/piet-wgsl/src/shaders/preprocess.rs index 8da2e28..ac731c2 100644 --- a/piet-wgsl/src/shaders/preprocess.rs +++ b/piet-wgsl/src/shaders/preprocess.rs @@ -34,11 +34,7 @@ pub struct StackItem { else_passed: bool, } -pub fn preprocess( - input: &str, - defines: &HashSet, - imports: &HashMap, -) -> String { +pub fn preprocess(input: &str, defines: &HashSet, imports: &HashMap<&str, &str>) -> String { let mut output = String::with_capacity(input.len()); let mut stack = vec![]; 'all_lines: for (line_number, mut line) in input.lines().enumerate() {