diff --git a/Cargo.lock b/Cargo.lock index 0d37d52..37dd523 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,18 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9fe5e32de01730eb1f6b7f5b51c17e03e2325bf40a74f754f04f130043affff" +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + [[package]] name = "ahash" version = "0.4.7" @@ -188,6 +200,12 @@ dependencies = [ "objc", ] +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "copyless" version = "0.1.5" @@ -276,6 +294,60 @@ dependencies = [ "objc", ] +[[package]] +name = "crc32fast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "lazy_static", +] + [[package]] name = "d3d12" version = "0.3.2" @@ -322,6 +394,16 @@ dependencies = [ "syn", ] +[[package]] +name = "deflate" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +dependencies = [ + "adler32", + "byteorder", +] + [[package]] name = "derivative" version = "2.2.0" @@ -363,6 +445,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "fixedbitset" version = "0.2.0" @@ -659,6 +747,16 @@ dependencies = [ "thiserror", ] +[[package]] +name = "gif" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a668f699973d0f573d15749b7002a9ac9e1f9c6b220e7b165601334c173d8de" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "glob" version = "0.3.0" @@ -727,12 +825,40 @@ dependencies = [ "ahash", ] +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + [[package]] name = "ident_case" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "image" +version = "0.23.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "gif", + "jpeg-decoder", + "num-iter", + "num-rational", + "num-traits", + "png", + "scoped_threadpool", + "tiff", +] + [[package]] name = "indexmap" version = "1.6.2" @@ -782,6 +908,15 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-decoder" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" +dependencies = [ + "rayon", +] + [[package]] name = "js-sys" version = "0.3.46" @@ -897,6 +1032,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d" +dependencies = [ + "autocfg", +] + [[package]] name = "metal" version = "0.21.0" @@ -911,6 +1055,25 @@ dependencies = [ "objc", ] +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "mio" version = "0.6.23" @@ -1060,6 +1223,38 @@ dependencies = [ "version_check", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -1069,6 +1264,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "num_enum" version = "0.4.3" @@ -1184,6 +1389,18 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "png" +version = "0.16.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide 0.3.7", +] + [[package]] name = "proc-macro-crate" version = "0.1.5" @@ -1238,6 +1455,31 @@ dependencies = [ "libc", ] +[[package]] +name = "rayon" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.5" @@ -1272,6 +1514,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1414,6 +1662,17 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7572415bd688d401c52f6e36f4c8e805b9ae1622619303b9fa835d531db0acae" +[[package]] +name = "tiff" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437" +dependencies = [ + "jpeg-decoder", + "miniz_oxide 0.4.4", + "weezl", +] + [[package]] name = "toml" version = "0.5.8" @@ -1633,6 +1892,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "weezl" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a32b378380f4e9869b22f0b5177c68a5519f03b3454fde0b291455ddbae266c" + [[package]] name = "wgpu" version = "0.7.1" @@ -1701,6 +1966,7 @@ dependencies = [ "fs_extra", "futures", "glob", + "image", "shaderc", "wgpu", "winit", diff --git a/Cargo.toml b/Cargo.toml index d321f23..90e5019 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,8 @@ edition = "2018" winit = "0.24" wgpu = "0.7" futures = "0.3" -bytemuck = { version = "1.4", features = [ "derive" ] } - +bytemuck = { version = "1.5", features = [ "derive" ] } +image = "0.23.14" [build-dependencies] anyhow = "1.0" diff --git a/build.rs b/build.rs index 8d9aae3..fe05e6e 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ -use anyhow::*; use glob::glob; use std::fs::{read_to_string, write, create_dir_all}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use shaderc::{ResolvedInclude, ShaderKind, IncludeType}; struct ShaderData { source: String, @@ -12,55 +12,55 @@ struct ShaderData { impl ShaderData { // Load data from file - pub fn load(src_path: PathBuf) -> Result { + pub fn load(src_path: PathBuf, kind: ShaderKind) -> Option { // Get extension and filename let filename = src_path.file_name().unwrap().to_str().unwrap(); - let extension = src_path.extension().context("File has no extension")?.to_str().unwrap(); - - // Figure out kind from extension - let kind = match extension { - "vert" => shaderc::ShaderKind::Vertex, - "frag" => shaderc::ShaderKind::Fragment, - "comp" => shaderc::ShaderKind::Compute, - _ => bail!("Unsupported shader: {}", src_path.display()), - }; + let extension = src_path.extension().expect("File has no extension").to_str().unwrap(); // Create output path let directory = src_path.parent().unwrap().join(".bin"); - create_dir_all(directory.clone()).context("Failed to create output directory")?; + create_dir_all(directory.clone()).expect("Failed to create output directory"); let spv_path = directory.join(filename).with_extension(format!("{}.spv", extension)); // Read file - let source = read_to_string(src_path.clone())?; + let source = read_to_string(src_path.clone()).expect("Failed to read shader file content"); // Return struct - Ok(Self{ source, src_path, spv_path, kind }) + Some(Self { source, src_path, spv_path, kind }) } } -fn main() -> Result<()> { - - // Collect all shaders recursively within /shaders/ - let mut shader_paths = [ - glob("./src/**/*.vert")?, - glob("./src/**/*.frag")?, - glob("./src/**/*.comp")?, - ]; - let shaders = shader_paths.iter_mut().flatten() - .map(|glob_result| ShaderData::load(glob_result?)) - .collect::>>().into_iter() - .collect::>>()?; - - // Compile all shaders - let mut compiler = shaderc::Compiler::new().context("Unable to create shader compiler")?; - for shader in shaders { - println!("cargo:rerun-if-changed={}", shader.src_path.as_os_str().to_str().unwrap()); - let compiled = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", None, )?; - write(shader.spv_path, compiled.as_binary_u8())?; +fn resolve_include(inc_name: &str, _inc_type: IncludeType, src_name: &str, _depth: usize) -> Result { + + let path = Path::new(src_name).parent().unwrap().join(inc_name); + if path.is_file() { + let resolved_name = path.to_str().unwrap().to_owned(); + let content = read_to_string(path.clone()).unwrap(); + return Ok(ResolvedInclude { resolved_name, content }) } - Ok(()) + Err("".to_string()) +} + +fn main() { + // Load all shaders + let vert = glob("./src/**/*.vert").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Vertex)); + let frag = glob("./src/**/*.frag").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Fragment)); + let comp = glob("./src/**/*.comp").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Compute)); + let shaders = vert.chain(frag).chain(comp); + + // Options + let mut options = shaderc::CompileOptions::new().expect("Unable to create compile options"); + options.set_include_callback(resolve_include); + + // Compile all shaders + let mut compiler = shaderc::Compiler::new().expect("Unable to create shader compiler"); + for shader in shaders { + println!("cargo:rerun-if-changed={}", shader.src_path.as_os_str().to_str().unwrap()); + let compiled = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", Some(&options)).unwrap(); + write(shader.spv_path, compiled.as_binary_u8()).unwrap(); + } } diff --git a/src/.bin/shader.frag.spv b/src/.bin/shader.frag.spv index 108354f..8ac5422 100644 Binary files a/src/.bin/shader.frag.spv and b/src/.bin/shader.frag.spv differ