Skip to content

Add compile_spirv proc macro to replace build.rs #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [

"crates/rustc_codegen_spirv",
"crates/spirv-builder",
"crates/spirv-builder-macros",
"crates/spirv-std",

"tests",
Expand Down
23 changes: 23 additions & 0 deletions crates/spirv-builder-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "spirv-builder-macros"
version = "0.4.0-alpha.4"
authors = ["Embark <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
repository = "https://github.com/EmbarkStudios/rust-gpu"
description = "Macros for spirv-builder"

[lib]
proc-macro = true

# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]

[dependencies]
spirv-builder = { path = "../spirv-builder", default-features = false }
proc-macro2 = "1.0.24"
quote = "1.0.8"
syn = { version = "1.0.58", features=["full"] }
243 changes: 243 additions & 0 deletions crates/spirv-builder-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
// BEGIN - Embark standard lints v0.3
// do not change or add/remove here, but one can add exceptions after this section
// for more info see: <https://github.com/EmbarkStudios/rust-ecosystem/issues/59>
#![deny(unsafe_code)]
#![warn(
clippy::all,
clippy::await_holding_lock,
clippy::dbg_macro,
clippy::debug_assert_with_mut_call,
clippy::doc_markdown,
clippy::empty_enum,
clippy::enum_glob_use,
clippy::exit,
clippy::explicit_into_iter_loop,
clippy::filter_map_next,
clippy::fn_params_excessive_bools,
clippy::if_let_mutex,
clippy::imprecise_flops,
clippy::inefficient_to_string,
clippy::large_types_passed_by_value,
clippy::let_unit_value,
clippy::linkedlist,
clippy::lossy_float_literal,
clippy::macro_use_imports,
clippy::map_err_ignore,
clippy::map_flatten,
clippy::map_unwrap_or,
clippy::match_on_vec_items,
clippy::match_same_arms,
clippy::match_wildcard_for_single_variants,
clippy::mem_forget,
clippy::mismatched_target_os,
clippy::needless_borrow,
clippy::needless_continue,
clippy::option_option,
clippy::pub_enum_variant_names,
clippy::ref_option_ref,
clippy::rest_pat_in_fully_bound_structs,
clippy::string_add_assign,
clippy::string_add,
clippy::string_to_string,
clippy::suboptimal_flops,
clippy::todo,
clippy::unimplemented,
clippy::unnested_or_patterns,
clippy::unused_self,
clippy::verbose_file_reads,
future_incompatible,
nonstandard_style,
rust_2018_idioms
)]
// END - Embark standard lints v0.3
// crate-specific exceptions:
#![allow()]

use std::{env, path::PathBuf};

use proc_macro::TokenStream;
use quote::quote;
use spirv_builder::{MemoryModel, SpirvBuilder};
use syn::{
parse::{Parse, ParseStream},
Expr, ExprLit, ExprPath, FieldValue, Lit, Member, Path, Token,
};

#[proc_macro]
pub fn compile_spirv(input: TokenStream) -> TokenStream {
let cargo_target_dir = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from);
let spirv_target_dir = if let Some(ref cargo_target_dir) = cargo_target_dir {
if !(cargo_target_dir.ends_with("spirv-builder")) {
cargo_target_dir.clone().join("spirv-builder")
} else {
cargo_target_dir.clone()
}
} else {
env::current_dir()
.expect("`compile_spirv!` macro uses the current directory to determine it's target directory.")
.join("target/spirv-builder")
};
env::set_var("CARGO_TARGET_DIR", spirv_target_dir);
let args = syn::parse_macro_input!(input as SpirvBuilderArgs);
let spv_path = SpirvBuilder::new(args.path.as_str())
.memory_model(args.memory_model)
.spirv_version(args.version.0, args.version.1)
.release(args.release)
.print_metadata(false)
.build()
.unwrap();
if let Some(cargo_target_dir) = cargo_target_dir {
env::set_var("CARGO_TARGET_DIR", cargo_target_dir);
} else {
env::remove_var("CARGO_TARGET_DIR");
};
let spv_path = spv_path.to_str().unwrap();
let out = quote! {
#spv_path
};
TokenStream::from(out)
}

struct SpirvBuilderArgs {
path: String,
memory_model: MemoryModel,
version: (u8, u8),
release: bool,
}

impl Parse for SpirvBuilderArgs {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let mut path = None;
let mut memory_model = None;
let mut version = None;
let mut release = None;
for field in input.parse_terminated::<FieldValue, Token![,]>(FieldValue::parse)? {
let arg = if let Member::Named(arg) = field.member {
arg
} else {
return Err(input.error("Unnamed member."));
};
if arg == "path" {
if path.is_some() {
return Err(input
.error("Duplicate `path` specification, please specify it exactly once."));
}
if field.colon_token.is_none() {
return Err(input.error(
"Please provide the crate path as a string literal: `path: \"path/to/crate\"`."
));
}
if let Expr::Lit(ExprLit {
lit: Lit::Str(string),
..
}) = field.expr
{
path = Some(string.value());
} else {
return Err(input.error(
"Please provide the crate path as a string literal: `path: \"path/to/crate\"`."
));
}
} else if arg == "memory_model" {
if memory_model.is_some() {
return Err(input.error(
"Duplicate `memory_model` specification, please specify it at most once. \
The default is `memory_model: Vulkan`.",
));
}
if field.colon_token.is_none() {
return Err(input.error(
"Memory model must be specified as `memory_model: MemoryModel` eg. `memory_model: GLSL450`."
));
}
match field.expr {
Expr::Path(ExprPath {
path: Path {
segments,
..
},
..
}) if segments.len() == 1 => {
let ident = &segments.first().unwrap().ident;
if ident == "Vulkan" {
memory_model = Some(MemoryModel::Vulkan);
} else if ident == "GLSL450" {
memory_model = Some(MemoryModel::GLSL450);
} else if ident == "Simple" {
memory_model = Some(MemoryModel::Simple);
} else {
return Err(input.error(
"Invalid memory model, please specify one of `Vulkan`, `GLSL450`, or `Simple`."
))
}
}
_ => return Err(input.error(
"Memory model must be specified as `memory_model: MemoryModel` eg. `memory_model: GLSL450`."
)),
}
} else if arg == "version" {
if version.is_some() {
return Err(input.error(
"Duplicate `version` specification, please specify it at most once. The default is `version: 1.3`."
));
}
if field.colon_token.is_none() {
return Err(input.error(
"Version must be specified as `version: maj.min`. The default is `version: 1.3`."
));
}
if let Expr::Lit(ExprLit {
lit: Lit::Float(float),
..
}) = field.expr
{
let mut radix_split = float.base10_digits().split('.');
let (major, minor) = (radix_split.next(), radix_split.next());
let major = if major == Some("1") {
1
} else {
return Err(input.error("Major version must be 1."));
};
let minor = match minor {
Some("0") => 0,
Some("1") => 1,
Some("2") => 2,
Some("3") => 3,
Some("4") => 4,
Some("5") => 5,
_ => return Err(input.error("Minor version must be in [0, 5], inclusive.")),
};
version = Some((major, minor));
} else {
return Err(input.error(
"Version must be specified as `version: maj.min`, the default is `version: 1.3`."
));
}
} else if arg == "release" || arg == "debug" {
if release.is_none() {
release = Some(arg == "release");
} else {
return Err(input.error(
"Duplicate compile mode specification, please specify `release` or `debug` at most once. \
If unspecified, the default is `release`."
));
}
} else {
return Err(input.error(format!(
"Unknown parameter: `{:?}`, valid parameters are `path`, `memory_model`, \
`release` and `debug`.",
arg,
)));
}
}
if path.is_none() {
return Err(input.error("Please provide a crate path: `path: \"path/to/crate\"`."));
}
Ok(Self {
path: path.unwrap(),
memory_model: memory_model.unwrap_or(MemoryModel::Vulkan),
version: version.unwrap_or((1, 3)),
release: release.unwrap_or(true),
})
}
}
8 changes: 3 additions & 5 deletions examples/runners/wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ crate-type = ["lib", "cdylib"]
# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
use-installed-tools = ["spirv-builder-macros/use-installed-tools"]
use-compiled-tools = ["spirv-builder-macros/use-compiled-tools"]

[dependencies]
cfg-if = "1.0.0"
Expand All @@ -23,9 +23,7 @@ wgpu = "0.7.0"
winit = { version = "0.24", features = ["web-sys"] }
clap = "3.0.0-beta.2"
strum = { version = "0.20", default_features = false, features = ["derive"] }

[build-dependencies]
spirv-builder = { path = "../../../crates/spirv-builder", default-features = false }
spirv-builder-macros = { path = "../../../crates/spirv-builder-macros", default-features = false }

[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = "0.2"
Expand Down
17 changes: 0 additions & 17 deletions examples/runners/wgpu/build.rs

This file was deleted.

21 changes: 17 additions & 4 deletions examples/runners/wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
)]

use clap::Clap;
use spirv_builder_macros::compile_spirv;
use strum::{Display, EnumString};

mod compute;
Expand All @@ -58,10 +59,22 @@ pub enum RustGPUShader {

fn shader_module(shader: RustGPUShader) -> wgpu::ShaderModuleDescriptor<'static> {
match shader {
RustGPUShader::Simplest => wgpu::include_spirv!(env!("simplest_shader.spv")),
RustGPUShader::Sky => wgpu::include_spirv!(env!("sky_shader.spv")),
RustGPUShader::Compute => wgpu::include_spirv!(env!("compute_shader.spv")),
RustGPUShader::Mouse => wgpu::include_spirv!(env!("mouse_shader.spv")),
RustGPUShader::Simplest => wgpu::include_spirv!(compile_spirv! {
path: "examples/shaders/simplest-shader",
version: 1.0,
}),
RustGPUShader::Sky => wgpu::include_spirv!(compile_spirv! {
path: "examples/shaders/sky-shader",
version: 1.0,
}),
RustGPUShader::Compute => wgpu::include_spirv!(compile_spirv! {
path: "examples/shaders/compute-shader",
version: 1.0,
}),
RustGPUShader::Mouse => wgpu::include_spirv!(compile_spirv! {
path: "examples/shaders/mouse-shader",
version: 1.0,
}),
}
}

Expand Down
1 change: 0 additions & 1 deletion examples/shaders/compute-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ license = "MIT OR Apache-2.0"
crate-type = ["dylib"]

[dependencies]
spirv-std-macros = { path = "../../../crates/spirv-std-macros" }
spirv-std = { path = "../../../crates/spirv-std" }
Loading