From 1aeec072d2e92aecc96204d3fd9f8a841fe5c563 Mon Sep 17 00:00:00 2001 From: Joe O'Connor Date: Sat, 22 Jun 2024 17:47:26 +0100 Subject: [PATCH] Update dependencies --- Cargo.toml | 12 ++++++------ README.md | 7 +++++++ src/lib.rs | 1 + src/result.rs | 11 +++++++---- src/source.rs | 47 +++++++++++++++++++++++++++++++++++++---------- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b905241..1646b85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wgsl-inline" -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "MIT" description = "A macro used to embed WGSL within Rust." @@ -13,8 +13,8 @@ include = ["/Cargo.toml", "/LICENSE", "/README.md", "/src/**"] [dependencies] syn = { version = "2.0", features = [] } -naga = { version = "0.14", features = ["wgsl-in", "wgsl-out"] } -naga-to-tokenstream = "0.4" +naga = { version = "0.20", features = ["wgsl-in", "wgsl-out"] } +naga-to-tokenstream = "0.6" proc-macro2 = "1.0" quote = "1.0" @@ -26,6 +26,6 @@ proc-macro = true [features] minify = ["naga-to-tokenstream/minify"] -glam = ["naga-to-tokenstream/glam"] -naga = ["naga-to-tokenstream/naga"] -encase = ["naga-to-tokenstream/encase"] +glam = [] +naga = [] +encase = [] diff --git a/README.md b/README.md index a6111e3..0da475b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ WGSL Inline adds a macro, `wgsl!`, which takes WGSL sourcecode and validates it, reporting any errors to the Rust compiler. +Note that this crate is intended for small shaders - if you have a large program, with many shaders, you may prefer the [include-wgsl-oil](https://crates.io/crates/include-wgsl-oil) crate, which allows shaders to import other shader files using the `naga-oil` preprocessor. + # Example In your `Cargo.toml`: @@ -49,6 +51,11 @@ Error scopes are propogated to the token in the macro that caused the error. Tha ![Image of a WGSL compile error in an IDE](https://raw.githubusercontent.com/LucentFlux/wgsl-inline/main/docs/images/compile_error.png) +# Exported items + +This crate uses `naga-to-tokenstream` to extract information other than just the shader source into your Rust program. For a full list of the generated items, +see [naga-to-tokenstream](https://crates.io/crates/naga-to-tokenstream). The `encase`, `glam` and `naga` features of the `naga-to-tokenstream` crate can be enabled using feature flags of the same name on this crate. + # Minification This crate comes with a "minification" feature flag `minify`. When enabled, all of your included shader source code will be reduced in size at compile time (removing variable names and excess whitespace). This is intended to be used on release builds, stripping debug information to increase shader parsing startup time and decrease read latency. diff --git a/src/lib.rs b/src/lib.rs index 702fce8..7d624fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::needless_doctest_main)] #![doc = include_str!("../README.md")] mod result; diff --git a/src/result.rs b/src/result.rs index 72db049..9fa5d72 100644 --- a/src/result.rs +++ b/src/result.rs @@ -33,7 +33,7 @@ impl ShaderResult { } else { for (loc, extra) in e.spans() { self.source - .push_naga_error(loc.clone(), format!("{}: {}", message, extra)) + .push_naga_error(*loc, format!("{}: {}", message, extra)) } } @@ -42,19 +42,19 @@ impl ShaderResult { } } - pub(crate) fn to_items(&mut self) -> Vec { + pub(crate) fn to_items(&self) -> Vec { let mut items = Vec::new(); // Errors for (msg, spans) in self.source.errors() { for span in spans { - let span = span.clone().into(); + let span = (*span).into(); items.push(syn::parse_quote_spanned! {span=> compile_error!(#msg); }); } // If an error doesn't have a location, just report it everywhere - if spans.len() == 0 { + if spans.is_empty() { items.push(syn::parse_quote! { compile_error!(#msg); }); @@ -65,6 +65,9 @@ impl ShaderResult { &self.module, naga_to_tokenstream::ModuleToTokensConfig { structs_filter: None, + gen_encase: cfg!(feature = "encase"), + gen_naga: cfg!(feature = "naga"), + gen_glam: cfg!(feature = "glam"), }, ); items.append(&mut module_items); diff --git a/src/source.rs b/src/source.rs index bfecf4b..fcd5c40 100644 --- a/src/source.rs +++ b/src/source.rs @@ -92,12 +92,39 @@ struct SpanInString { /// Returns true only if the given character cannot be in an identifier. Returning false gives no information. fn non_identifier_char(c: char) -> bool { - match c { - '(' | ')' | '{' | '}' | '[' | ']' | '<' | '>' | ',' | '+' | '*' | '/' | '!' | '\\' - | '"' | '\'' | '|' | '=' | '^' | '&' | ';' | ':' | '?' | '%' | '@' | '#' | '~' | '.' - | '£' | '$' | '`' => true, - _ => false, - } + matches!( + c, + '(' | ')' + | '{' + | '}' + | '[' + | ']' + | '<' + | '>' + | ',' + | '+' + | '*' + | '/' + | '!' + | '\\' + | '"' + | '\'' + | '|' + | '=' + | '^' + | '&' + | ';' + | ':' + | '?' + | '%' + | '@' + | '#' + | '~' + | '.' + | '£' + | '$' + | '`' + ) } /// Returns true if the two tokens should be separated by a space within wgsl source code. @@ -114,7 +141,7 @@ fn should_add_space_between(last: char, next: char) -> bool { if last == ':' || next == ':' { return false; // Might be an import path like `a::b` } - return true; + true } /// Shader sourcecode generated from the token stream provided @@ -220,7 +247,7 @@ impl Sourcecode { self.spans[span_start..span_end] .iter() - .map(|span| span.token_span.clone()) + .map(|span| span.token_span) .collect() } @@ -228,7 +255,7 @@ impl Sourcecode { let error_spans = if let Some(loc) = loc.to_range() { self.get_spans_within(loc.start, loc.end) } else { - self.spans.iter().map(|s| s.token_span.clone()).collect() + self.spans.iter().map(|s| s.token_span).collect() }; self.errors.push((msg, error_spans)) @@ -366,7 +393,7 @@ impl Sourcecode { }*/ pub(crate) fn complete(mut self) -> ShaderResult { - let module = self.parse().unwrap_or(naga::Module::default()); + let module = self.parse().unwrap_or_default(); ShaderResult::new(self, module) }