Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeoc2001 committed Jun 22, 2024
1 parent e7a0829 commit 1aeec07
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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"

Expand All @@ -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 = []
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::needless_doctest_main)]
#![doc = include_str!("../README.md")]

mod result;
Expand Down
11 changes: 7 additions & 4 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -42,19 +42,19 @@ impl ShaderResult {
}
}

pub(crate) fn to_items(&mut self) -> Vec<syn::Item> {
pub(crate) fn to_items(&self) -> Vec<syn::Item> {
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);
});
Expand All @@ -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);
Expand Down
47 changes: 37 additions & 10 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -220,15 +247,15 @@ impl Sourcecode {

self.spans[span_start..span_end]
.iter()
.map(|span| span.token_span.clone())
.map(|span| span.token_span)
.collect()
}

pub(crate) fn push_naga_error(&mut self, loc: naga::Span, msg: String) {
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))
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1aeec07

Please sign in to comment.