From c9a7c968c5b935854a0a7cd544e5b9fe14d5debc Mon Sep 17 00:00:00 2001 From: Firestar99 <4696087-firestar99@users.noreply.gitlab.com> Date: Mon, 10 Apr 2023 13:55:50 +0200 Subject: [PATCH 1/4] shaders: minor fix that some Vecs were always allocated with capacity of 0 --- vulkano-shaders/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index 63ef83fbd9..1517ba2ea8 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -251,8 +251,8 @@ fn shader_inner(mut input: MacroInput) -> Result { let root_path = Path::new(&root); let shaders = mem::take(&mut input.shaders); // yoink - let mut shaders_code = Vec::with_capacity(input.shaders.len()); - let mut types_code = Vec::with_capacity(input.shaders.len()); + let mut shaders_code = Vec::with_capacity(shaders.len()); + let mut types_code = Vec::with_capacity(shaders.len()); let mut type_registry = TypeRegistry::default(); for (name, (shader_kind, source_kind)) in shaders { From 398c410e39ee1735b2eead2a7b89a5f2b84beb0f Mon Sep 17 00:00:00 2001 From: Firestar99 <4696087-firestar99@users.noreply.gitlab.com> Date: Mon, 10 Apr 2023 14:02:10 +0200 Subject: [PATCH 2/4] shaders: add root_path_env property to allow loading shaders generated by a build script --- vulkano-shaders/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index 1517ba2ea8..b154e53fc8 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -126,6 +126,15 @@ //! in conjunction with the `src` or `path` field. This allows using shaders compiled through a //! separate build system. //! +//! ## `root_path_env: "..."` +//! +//! Instead of searching relative to your `Cargo.toml`, search relative to some other folder +//! specified by this env variable. The intended use case is using `OUT_DIR` to be able to load +//! shaders generated by your build script. Defaults to `CARGO_MANIFEST_DIR` corresponding to the +//! folder of your `Cargo.toml`. +//! +//! See [`cargo-env-vars`] for a full set of env variables set by cargo. +//! //! ## `shaders: { first: { src: "...", ty: "..." }, ... }` //! //! With these options the user can compile several shaders in a single macro invocation. Each @@ -200,6 +209,7 @@ //! | `shaderc-build-from-source` | Build the `shaderc` library from source when compiling. | //! | `shaderc-debug` | Compile shaders with debug information included. | //! +//! [cargo-env-vars]: https://doc.rust-lang.org/cargo/reference/environment-variables.html //! [cargo-expand]: /~https://github.com/dtolnay/cargo-expand //! [`ShaderModule::from_words_with_data`]: vulkano::shader::ShaderModule::from_words_with_data //! [`SpecializationConstants`]: vulkano::shader::SpecializationConstants @@ -247,7 +257,29 @@ pub fn shader(input: proc_macro::TokenStream) -> proc_macro::TokenStream { } fn shader_inner(mut input: MacroInput) -> Result { - let root = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()); + let (root, relative_path_error_msg) = match input.root_path_env.as_ref() { + None => ( + env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()), + "to your Cargo.toml".to_owned(), + ), + Some(root_path_env) => { + let root = match env::var(root_path_env.value()) { + Ok(e) => e, + Err(e) => { + bail!( + root_path_env, + "env variable `{root_path_env:?}` provided by `root_path_env` parameter of \ + the shader was {e:?}! Typical parameters are `OUT_DIR` to gather results \ + from your build script, or left default to search relative to your Cargo.toml." + ) + } + }; + let env = root_path_env.value(); + let error = format!("to the path `{root}` specified by the env variable `{env:?}`"); + (root, error) + } + }; + let root_path = Path::new(&root); let shaders = mem::take(&mut input.shaders); // yoink @@ -273,7 +305,7 @@ fn shader_inner(mut input: MacroInput) -> Result { bail!( path, "file `{full_path:?}` was not found, note that the path must be relative \ - to your Cargo.toml", + {relative_path_error_msg}", ); } @@ -302,7 +334,7 @@ fn shader_inner(mut input: MacroInput) -> Result { bail!( path, "file `{full_path:?}` was not found, note that the path must be relative \ - to your Cargo.toml", + {relative_path_error_msg}", ); } @@ -350,6 +382,7 @@ enum SourceKind { } struct MacroInput { + root_path_env: Option, include_directories: Vec, macro_defines: Vec<(String, String)>, shared_constants: bool, @@ -365,6 +398,7 @@ impl MacroInput { #[cfg(test)] fn empty() -> Self { MacroInput { + root_path_env: None, include_directories: Vec::new(), macro_defines: Vec::new(), shared_constants: false, @@ -382,6 +416,7 @@ impl Parse for MacroInput { fn parse(input: ParseStream<'_>) -> Result { let root = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()); + let mut root_path_env = None; let mut include_directories = Vec::new(); let mut macro_defines = Vec::new(); let mut shared_constants = None; @@ -583,6 +618,13 @@ impl Parse for MacroInput { } } } + "root_path_env" => { + let lit = input.parse::()?; + if root_path_env.is_some() { + bail!(lit, "field `root_path_env` is already defined"); + } + root_path_env = Some(lit); + } "include" => { let in_brackets; bracketed!(in_brackets in input); @@ -709,6 +751,7 @@ impl Parse for MacroInput { } Ok(MacroInput { + root_path_env, include_directories, macro_defines, shared_constants: shared_constants.unwrap_or(false), From 230cce9e8094116d86138fc72c5641c1ef63d7cc Mon Sep 17 00:00:00 2001 From: Firestar99 <31222740+Firestar99@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:48:25 +0200 Subject: [PATCH 3/4] shaders: Apply root_path_env suggestions from code review Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com> --- vulkano-shaders/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index b154e53fc8..403c87d8c4 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -209,7 +209,7 @@ //! | `shaderc-build-from-source` | Build the `shaderc` library from source when compiling. | //! | `shaderc-debug` | Compile shaders with debug information included. | //! -//! [cargo-env-vars]: https://doc.rust-lang.org/cargo/reference/environment-variables.html +//! [`cargo-env-vars`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html //! [cargo-expand]: /~https://github.com/dtolnay/cargo-expand //! [`ShaderModule::from_words_with_data`]: vulkano::shader::ShaderModule::from_words_with_data //! [`SpecializationConstants`]: vulkano::shader::SpecializationConstants @@ -268,9 +268,9 @@ fn shader_inner(mut input: MacroInput) -> Result { Err(e) => { bail!( root_path_env, - "env variable `{root_path_env:?}` provided by `root_path_env` parameter of \ - the shader was {e:?}! Typical parameters are `OUT_DIR` to gather results \ - from your build script, or left default to search relative to your Cargo.toml." + "failed to fetch environment variable: {e}; typical parameters are \ + `OUT_DIR` to gather results from your build script, or left default to \ + search relative to your Cargo.toml", ) } }; From 08f7a57b1f1fa5790486728de099a90c29d423d7 Mon Sep 17 00:00:00 2001 From: Firestar99 <4696087-firestar99@users.noreply.gitlab.com> Date: Tue, 11 Apr 2023 11:23:19 +0200 Subject: [PATCH 4/4] shaders: added root_path_env docs about env vars specified by a build script --- vulkano-shaders/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index 403c87d8c4..47a32cfd7d 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -133,7 +133,12 @@ //! shaders generated by your build script. Defaults to `CARGO_MANIFEST_DIR` corresponding to the //! folder of your `Cargo.toml`. //! -//! See [`cargo-env-vars`] for a full set of env variables set by cargo. +//! See [`cargo-env-vars`] for a full set of env variables set by cargo. It is also possible to +//! specify env variables from within the build script using the following: +//! ```rust +//! # let shader_out_dir = ""; +//! println!("cargo:rustc-env=SHADER_OUT_DIR={shader_out_dir}"); +//! ``` //! //! ## `shaders: { first: { src: "...", ty: "..." }, ... }` //!