diff --git a/README.md b/README.md index 14f3089..320d426 100644 --- a/README.md +++ b/README.md @@ -66,13 +66,37 @@ Safe and sound Rust bindings to [SPIRV-Cross](https://github.com/KhronosGroup/SP } ``` +## Features +By default, the `glsl`, `hlsl`, and `msl` features are enabled by default. The `cpp` and `json` targets can be enabled +in Cargo.toml + +```toml + [dependencies] + spirv-cross2 = { features = ["cpp", "json"] } +``` + +SPIRV-Cross will only be built with support for enabled targets. If you want to only perform reflection and shrink the binary size, +you can disable all but the `None` target. + +```toml + [dependencies] + spirv-cross2 = { default-features = false } +``` + +To enable all features, including `f16` and vector constant support, use the `full` feature. + +```toml + [dependencies] + spirv-cross2 = { features = ["full"] } +``` + ### `f16` and vector specialization constants support When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and `Vec2`, `Vec3`, `Vec4`, and `Mat4` types -via [gfx-maths](https://crates.io/crates/gfx-maths). This is included by default, but can be disabled by disabling default features. +via [gfx-maths](https://crates.io/crates/gfx-maths). ```toml -[dependencies] -spirv-cross2 = { default-features = false } + [dependencies] + spirv-cross2 = { features = ["f16", "half"] } ``` ## License diff --git a/spirv-cross-sys/Cargo.toml b/spirv-cross-sys/Cargo.toml index 4b2a842..a1276e8 100644 --- a/spirv-cross-sys/Cargo.toml +++ b/spirv-cross-sys/Cargo.toml @@ -17,6 +17,14 @@ bytemuck = "1.17.1" num-traits = "0.2" num-derive = "0.4.2" +[features] +glsl = [] +msl = ["glsl"] +hlsl = ["glsl"] +json = ["glsl"] +cpp = ["glsl"] + + [build-dependencies] cc = "1.0" glob = "0.3" diff --git a/spirv-cross-sys/build.rs b/spirv-cross-sys/build.rs index af7f41e..a2da1cf 100644 --- a/spirv-cross-sys/build.rs +++ b/spirv-cross-sys/build.rs @@ -11,11 +11,6 @@ pub fn main() { .cpp(true) .std("c++14") .define("SPIRV_CROSS_CLI", "OFF") - .define("SPIRV_CROSS_C_API_GLSL", "1") - .define("SPIRV_CROSS_C_API_HLSL", "1") - .define("SPIRV_CROSS_C_API_MSL", "1") - .define("SPIRV_CROSS_C_API_CPP", "1") - .define("SPIRV_CROSS_C_API_REFLECT", "1") .includes(&["native/SPIRV-Cross", "native/SPIRV-CROSS/include"]) .file("native/SPIRV-Cross/spirv_cfg.cpp") .file("native/SPIRV-Cross/spirv_cpp.cpp") @@ -29,6 +24,26 @@ pub fn main() { .file("native/SPIRV-Cross/spirv_reflect.cpp") .file("native/spirv_cross_c_ext_rs.cpp"); + if cfg!(feature = "glsl") { + spvc_build.define("SPIRV_CROSS_C_API_GLSL", "1"); + } + + if cfg!(feature = "hlsl") { + spvc_build.define("SPIRV_CROSS_C_API_HLSL", "1"); + } + + if cfg!(feature = "msl") { + spvc_build.define("SPIRV_CROSS_C_API_MSL", "1"); + } + + if cfg!(feature = "cpp") { + spvc_build.define("SPIRV_CROSS_C_API_CPP", "1"); + } + + if cfg!(feature = "json") { + spvc_build.define("SPIRV_CROSS_C_API_JSON", "1"); + } + spvc_build.compile("spirv-cross"); println!("cargo:rustc-link-lib=static=spirv-cross"); } diff --git a/spirv-cross2/Cargo.toml b/spirv-cross2/Cargo.toml index dacd67e..5e49826 100644 --- a/spirv-cross2/Cargo.toml +++ b/spirv-cross2/Cargo.toml @@ -28,13 +28,21 @@ memchr = "2.7.4" spirv = "0.3.0" [features] -default = ["gfx-math-types", "f16"] +default = ["glsl", "hlsl", "msl"] +full = ["gfx-math-types", "f16", "glsl", "hlsl", "msl", "json", "cpp"] + f16 = ["dep:half"] gfx-math-types = ["dep:gfx-maths"] +glsl = ["spirv-cross-sys/glsl"] +hlsl = ["spirv-cross-sys/hlsl"] +msl = ["spirv-cross-sys/msl"] +json = ["spirv-cross-sys/json"] +cpp = ["spirv-cross-sys/cpp"] + [dev-dependencies] glslang = "0.4.0" [package.metadata.docs.rs] -features = ["default"] +features = ["full"] rustdoc-args = ["--cfg", "docsrs"] \ No newline at end of file diff --git a/spirv-cross2/src/compile/mod.rs b/spirv-cross2/src/compile/mod.rs index 5e97385..462d762 100644 --- a/spirv-cross2/src/compile/mod.rs +++ b/spirv-cross2/src/compile/mod.rs @@ -7,12 +7,18 @@ use std::fmt::{Display, Formatter}; use std::ops::Deref; /// GLSL compile options. +#[cfg(feature = "glsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "glsl")))] pub mod glsl; /// HLSL compile options. +#[cfg(feature = "hlsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "hlsl")))] pub mod hlsl; /// MSL compile options. +#[cfg(feature = "msl")] +#[cfg_attr(docsrs, doc(cfg(feature = "msl")))] pub mod msl; impl Sealed for CommonOptions {} diff --git a/spirv-cross2/src/lib.rs b/spirv-cross2/src/lib.rs index 94ecffb..e435a62 100644 --- a/spirv-cross2/src/lib.rs +++ b/spirv-cross2/src/lib.rs @@ -61,6 +61,39 @@ //! Some allocations are made directly in the SPIRV-Cross compiler object. Such allocations //! can only live for as long as the borrow to [`Compiler`]. //! +//! ## Features +//! By default, the `glsl`, `hlsl`, and `msl` features are enabled by default. The `cpp` and `json` targets can be enabled +//! in Cargo.toml +//! +//! ```toml +//! [dependencies] +//! spirv-cross2 = { features = ["cpp", "json"] } +//! ``` +//! +//! SPIRV-Cross will only be built with support for enabled targets. If you want to only perform reflection and shrink the binary size, +//! you can disable all but the `None` target. +//! +//! ```toml +//! [dependencies] +//! spirv-cross2 = { default-features = false } +//! ``` +//! +//! To enable all features, including `f16` and vector constant support, use the `full` feature. +//! +//! ```toml +//! [dependencies] +//! spirv-cross2 = { features = ["full"] } +//! ``` +//! +//! ### `f16` and vector specialization constants support +//! When querying specialization constants, spirv-cross2 includes optional support for `f16` via [half](https://crates.io/crates/half) and `Vec2`, `Vec3`, `Vec4`, and `Mat4` types +//! via [gfx-maths](https://crates.io/crates/gfx-maths). +//! +//! ```toml +//! [dependencies] +//! spirv-cross2 = { features = ["f16", "half"] } +//! ``` +//! //! ## Usage //! Here is an example of using the API to do some reflection and compile to GLSL. //! @@ -106,6 +139,7 @@ //! compiler.compile(&options) //! } //! ``` +//! use spirv_cross_sys::{spvc_compiler_s, SpvId}; use crate::cell::{AllocationDropGuard, CrossAllocationCell}; diff --git a/spirv-cross2/src/targets.rs b/spirv-cross2/src/targets.rs index f86959e..0a8b23c 100644 --- a/spirv-cross2/src/targets.rs +++ b/spirv-cross2/src/targets.rs @@ -6,20 +6,35 @@ use spirv_cross_sys::CompilerBackend; /// Reflection only backend, no compilation features /// enabled. pub struct None; + /// Compile SPIR-V to GLSL. +#[cfg(feature = "glsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "glsl")))] pub struct Glsl; + /// Compile SPIR-V to Metal Shading Language (MSL). +#[cfg(feature = "msl")] +#[cfg_attr(docsrs, doc(cfg(feature = "msl")))] pub struct Msl; + /// Compile SPIR-V to HLSL. +#[cfg(feature = "hlsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "hlsl")))] pub struct Hlsl; + /// Compile SPIR-V to debuggable C++. /// /// This backend is deprecated but is included here for completion. /// See the [SPIRV-Cross docs](https://github.com/KhronosGroup/SPIRV-Cross?tab=readme-ov-file#using-shaders-generated-from-c-backend) /// for how to debug shaders generated from the C++ backend #[deprecated = "This backend is deprecated in SPIRV-Cross."] +#[cfg(feature = "cpp")] +#[cfg_attr(docsrs, doc(cfg(feature = "cpp")))] pub struct Cpp; + /// Compile SPIR-V to a JSON reflection format +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] pub struct Json; impl Sealed for None {} @@ -27,49 +42,74 @@ impl Target for None { const BACKEND: CompilerBackend = CompilerBackend::None; } -impl CompilableTarget for Glsl { - type Options = compile::glsl::CompilerOptions; -} -impl Sealed for Glsl {} -impl Target for Glsl { - const BACKEND: CompilerBackend = CompilerBackend::Glsl; +#[cfg(feature = "glsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "glsl")))] +mod glsl { + use super::*; + impl CompilableTarget for Glsl { + type Options = compile::glsl::CompilerOptions; + } + impl Sealed for Glsl {} + impl Target for Glsl { + const BACKEND: CompilerBackend = CompilerBackend::Glsl; + } } -impl CompilableTarget for Hlsl { - type Options = compile::hlsl::CompilerOptions; -} -impl Sealed for Hlsl {} -impl Target for Hlsl { - const BACKEND: CompilerBackend = CompilerBackend::Hlsl; +#[cfg(feature = "hlsl")] +#[cfg_attr(docsrs, doc(cfg(feature = "hlsl")))] +mod hlsl { + use super::*; + impl CompilableTarget for Hlsl { + type Options = compile::hlsl::CompilerOptions; + } + impl Sealed for Hlsl {} + impl Target for Hlsl { + const BACKEND: CompilerBackend = CompilerBackend::Hlsl; + } } -impl CompilableTarget for Msl { - type Options = compile::msl::CompilerOptions; -} -impl Sealed for Msl {} -impl Target for Msl { - const BACKEND: CompilerBackend = CompilerBackend::Msl; +#[cfg(feature = "msl")] +#[cfg_attr(docsrs, doc(cfg(feature = "msl")))] +mod msl { + use super::*; + impl CompilableTarget for Msl { + type Options = compile::msl::CompilerOptions; + } + impl Sealed for Msl {} + impl Target for Msl { + const BACKEND: CompilerBackend = CompilerBackend::Msl; + } } -impl CompilableTarget for Json { - type Options = NoOptions; -} -impl Sealed for Json {} -impl Target for Json { - const BACKEND: CompilerBackend = CompilerBackend::Json; +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +mod json { + use super::*; + impl CompilableTarget for Json { + type Options = NoOptions; + } + impl Sealed for Json {} + impl Target for Json { + const BACKEND: CompilerBackend = CompilerBackend::Json; + } } -#[allow(deprecated)] -impl CompilableTarget for Cpp { - type Options = NoOptions; -} +#[cfg(feature = "cpp")] +#[cfg_attr(docsrs, doc(cfg(feature = "cpp")))] +mod cpp { + use super::*; + #[allow(deprecated)] + impl CompilableTarget for Cpp { + type Options = NoOptions; + } -#[allow(deprecated)] -impl Sealed for Cpp {} + #[allow(deprecated)] + impl Sealed for Cpp {} -#[allow(deprecated)] -impl Target for Cpp { - const BACKEND: CompilerBackend = CompilerBackend::Cpp; + #[allow(deprecated)] + impl Target for Cpp { + const BACKEND: CompilerBackend = CompilerBackend::Cpp; + } } /// Marker trait for a compiler backend target.