Compare commits
No commits in common. "395e6412506129e0ec2ef391d343cfdfa9657f9d" and "9448ee9f586d4d27c72a7dbaa5f7b0529f78f3ee" have entirely different histories.
395e641250
...
9448ee9f58
|
|
@ -14,4 +14,3 @@ Cargo.lock
|
||||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
*.pdb
|
*.pdb
|
||||||
|
|
||||||
*~
|
|
||||||
11
Cargo.toml
11
Cargo.toml
|
|
@ -1,11 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "wgsl-shader-assembler"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
quote = "1.0"
|
|
||||||
syn = "2.0.90"
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
[toolchain]
|
|
||||||
channel = "nightly"
|
|
||||||
77
src/lib.rs
77
src/lib.rs
|
|
@ -1,77 +0,0 @@
|
||||||
#![feature(proc_macro_span)]
|
|
||||||
|
|
||||||
use {
|
|
||||||
proc_macro::TokenStream,
|
|
||||||
quote::quote,
|
|
||||||
std::{fs, path::PathBuf},
|
|
||||||
syn::{parse, LitStr},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn emit_create_shader_module(file_contents: &str) -> TokenStream {
|
|
||||||
quote! {
|
|
||||||
wgpu::ShaderModuleDescriptor {
|
|
||||||
label: None,
|
|
||||||
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(#file_contents)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
struct WgslModuleMacroInput {
|
|
||||||
source_dir: PathBuf,
|
|
||||||
filename: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Error {
|
|
||||||
Message(String),
|
|
||||||
SynError(syn::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error {
|
|
||||||
fn from_message(message: impl Into<String>) -> Self {
|
|
||||||
Self::Message(message.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<syn::Error> for Error {
|
|
||||||
fn from(value: syn::Error) -> Self {
|
|
||||||
Self::SynError(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_input(token_stream: TokenStream) -> Result<WgslModuleMacroInput, Error> {
|
|
||||||
let token = token_stream
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.next()
|
|
||||||
.ok_or(Error::from_message("Expected single string literal."))?;
|
|
||||||
|
|
||||||
let source_dir = token.span().source_file().path().parent().unwrap().into();
|
|
||||||
let literal: LitStr = parse(token_stream)?;
|
|
||||||
let filename = literal.value();
|
|
||||||
Ok(WgslModuleMacroInput {
|
|
||||||
source_dir,
|
|
||||||
filename,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wgsl_module_inner(input_token_stream: TokenStream) -> Result<TokenStream, Error> {
|
|
||||||
let input = parse_input(input_token_stream)?;
|
|
||||||
let full_filename = input.source_dir.join(&input.filename);
|
|
||||||
let contents = fs::read_to_string(&full_filename).map_err(|err| {
|
|
||||||
Error::from_message(format!(
|
|
||||||
"Could not open \"{}\": {}",
|
|
||||||
&full_filename.as_os_str().to_string_lossy(),
|
|
||||||
err.to_string()
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
Ok(emit_create_shader_module(&contents))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn wgsl_module(input: TokenStream) -> TokenStream {
|
|
||||||
wgsl_module_inner(input).unwrap_or_else(|err| match err {
|
|
||||||
Error::Message(message) => quote! { compile_error!(#message) },
|
|
||||||
Error::SynError(syn_err) => syn_err.into_compile_error(),
|
|
||||||
}.into())
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue