Skip to content

Commit

Permalink
build: separate out targets
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Sep 6, 2024
1 parent 45f7f8e commit 360c275
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 43 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spirv-cross-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 20 additions & 5 deletions spirv-cross-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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");
}
12 changes: 10 additions & 2 deletions spirv-cross2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
6 changes: 6 additions & 0 deletions spirv-cross2/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
34 changes: 34 additions & 0 deletions spirv-cross2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//!
Expand Down Expand Up @@ -106,6 +139,7 @@
//! compiler.compile(&options)
//! }
//! ```
//!
use spirv_cross_sys::{spvc_compiler_s, SpvId};

use crate::cell::{AllocationDropGuard, CrossAllocationCell};
Expand Down
106 changes: 73 additions & 33 deletions spirv-cross2/src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,110 @@ 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 {}
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.
Expand Down

0 comments on commit 360c275

Please sign in to comment.