Skip to content
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

Naga features for platform dependent compilation of msl & hlsl #5919

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

25 changes: 25 additions & 0 deletions naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,37 @@ default = []
dot-out = []
glsl-in = ["dep:pp-rs"]
glsl-out = []

## Enables outputting to the Metal Shading Language (MSL).
##
## This enables MSL output regardless of the target platform.
## If you want to enable it only when targeting iOS/tvOS/watchOS/macOS, use `naga/msl-out-if-target-apple`.
msl-out = []

## Enables outputting to the Metal Shading Language (MSL) only if the target platform is iOS/tvOS/watchOS/macOS.
##
## If you want to enable MSL output it regardless of the target platform, use `naga/msl-out`.
msl-out-if-target-apple = []

serialize = ["dep:serde", "bitflags/serde", "indexmap/serde"]
deserialize = ["dep:serde", "bitflags/serde", "indexmap/serde"]
arbitrary = ["dep:arbitrary", "bitflags/arbitrary", "indexmap/arbitrary"]
spv-in = ["dep:petgraph", "dep:spirv"]
spv-out = ["dep:spirv"]
wgsl-in = ["dep:hexf-parse", "dep:unicode-xid", "compact"]
wgsl-out = []

## Enables outputting to HLSL (Microsoft's High-Level Shader Language).
##
## This enables HLSL output regardless of the target platform.
## If you want to enable it only when targeting Windows, use `hlsl-out-if-target-windows`.
hlsl-out = []

## Enables outputting to HLSL (Microsoft's High-Level Shader Language) only if the target platform is Windows.
##
## If you want to enable HLSL output it regardless of the target platform, use `naga/hlsl-out`.
hlsl-out-if-target-windows = []

compact = []

[dependencies]
Expand All @@ -56,6 +78,9 @@ hexf-parse = { version = "0.2.1", optional = true }
unicode-xid = { version = "0.2.3", optional = true }
arrayvec.workspace = true

[build-dependencies]
cfg_aliases.workspace = true

[dev-dependencies]
diff = "0.1"
env_logger = "0.11"
Expand Down
10 changes: 10 additions & 0 deletions naga/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
cfg_aliases::cfg_aliases! {
dot_out: { feature = "dot-out" },
glsl_out: { feature = "glsl-out" },
hlsl_out: { any(feature = "hlsl-out", all(target_os = "windows", feature = "hlsl-out-if-target-windows")) },
msl_out: { any(feature = "msl-out", all(any(target_os = "ios", target_os = "macos"), feature = "msl-out-if-target-apple")) },
spv_out: { feature = "spv-out" },
wgsl_out: { feature = "wgsl-out" },
}
}
19 changes: 7 additions & 12 deletions naga/src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ Backend functions that export shader [`Module`](super::Module)s into binary and
*/
#![allow(dead_code)] // can be dead if none of the enabled backends need it

#[cfg(feature = "dot-out")]
#[cfg(dot_out)]
pub mod dot;
#[cfg(feature = "glsl-out")]
#[cfg(glsl_out)]
pub mod glsl;
#[cfg(feature = "hlsl-out")]
#[cfg(hlsl_out)]
pub mod hlsl;
#[cfg(feature = "msl-out")]
#[cfg(msl_out)]
pub mod msl;
#[cfg(feature = "spv-out")]
#[cfg(spv_out)]
pub mod spv;
#[cfg(feature = "wgsl-out")]
#[cfg(wgsl_out)]
pub mod wgsl;

#[cfg(any(
feature = "hlsl-out",
feature = "msl-out",
feature = "spv-out",
feature = "glsl-out"
))]
#[cfg(any(hlsl_out, msl_out, spv_out, glsl_out))]
pub mod pipeline_constants;

/// Names of vector components.
Expand Down
2 changes: 1 addition & 1 deletion naga/src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5710,7 +5710,7 @@ mod test {
let _ = super::parse_u8_slice(&bin, &Default::default()).unwrap();
}

#[cfg(all(feature = "wgsl-in", feature = "wgsl-out"))]
#[cfg(all(feature = "wgsl-in", wgsl_out))]
#[test]
fn atomic_i_inc() {
let _ = env_logger::builder().is_test(true).try_init();
Expand Down
2 changes: 1 addition & 1 deletion naga/src/keywords/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Lists of reserved keywords for each shading language with a [frontend][crate::front] or [backend][crate::back].
*/

#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
#[cfg(any(feature = "wgsl-in", wgsl_out))]
pub mod wgsl;
54 changes: 23 additions & 31 deletions naga/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct SpirvOutParameters {
#[serde(default)]
separate_entry_points: bool,
#[serde(default)]
#[cfg(all(feature = "deserialize", feature = "spv-out"))]
#[cfg(all(feature = "deserialize", spv_out))]
binding_map: naga::back::spv::BindingMap,
}

Expand All @@ -69,34 +69,26 @@ struct Parameters {
bounds_check_policies: naga::proc::BoundsCheckPolicies,
#[serde(default)]
spv: SpirvOutParameters,
#[cfg(all(feature = "deserialize", feature = "msl-out"))]
#[cfg(all(feature = "deserialize", msl_out))]
#[serde(default)]
msl: naga::back::msl::Options,
#[cfg(all(feature = "deserialize", feature = "msl-out"))]
#[cfg(all(feature = "deserialize", msl_out))]
#[serde(default)]
msl_pipeline: naga::back::msl::PipelineOptions,
#[cfg(all(feature = "deserialize", feature = "glsl-out"))]
#[cfg(all(feature = "deserialize", glsl_out))]
#[serde(default)]
glsl: naga::back::glsl::Options,
#[serde(default)]
glsl_exclude_list: naga::FastHashSet<String>,
#[cfg(all(feature = "deserialize", feature = "hlsl-out"))]
#[cfg(all(feature = "deserialize", hlsl_out))]
#[serde(default)]
hlsl: naga::back::hlsl::Options,
#[serde(default)]
wgsl: WgslOutParameters,
#[cfg(all(feature = "deserialize", feature = "glsl-out"))]
#[cfg(all(feature = "deserialize", glsl_out))]
#[serde(default)]
glsl_multiview: Option<std::num::NonZeroU32>,
#[cfg(all(
feature = "deserialize",
any(
feature = "hlsl-out",
feature = "msl-out",
feature = "spv-out",
feature = "glsl-out"
)
))]
#[cfg(all(feature = "deserialize", any(hlsl_out, msl_out, spv_out, glsl_out)))]
#[serde(default)]
pipeline_constants: naga::back::PipelineConstants,
}
Expand Down Expand Up @@ -260,9 +252,9 @@ impl Input {
}
}

#[cfg(feature = "hlsl-out")]
#[cfg(hlsl_out)]
type FragmentEntryPoint<'a> = naga::back::hlsl::FragmentEntryPoint<'a>;
#[cfg(not(feature = "hlsl-out"))]
#[cfg(not(hlsl_out))]
type FragmentEntryPoint<'a> = ();

#[allow(unused_variables)]
Expand Down Expand Up @@ -357,7 +349,7 @@ fn check_targets(
}
}

#[cfg(all(feature = "deserialize", feature = "spv-out"))]
#[cfg(all(feature = "deserialize", spv_out))]
{
let debug_info = source_code.map(|code| naga::back::spv::DebugInfo {
source_code: code,
Expand All @@ -376,7 +368,7 @@ fn check_targets(
);
}
}
#[cfg(all(feature = "deserialize", feature = "msl-out"))]
#[cfg(all(feature = "deserialize", msl_out))]
{
if targets.contains(Targets::METAL) {
write_output_msl(
Expand All @@ -390,7 +382,7 @@ fn check_targets(
);
}
}
#[cfg(all(feature = "deserialize", feature = "glsl-out"))]
#[cfg(all(feature = "deserialize", glsl_out))]
{
if targets.contains(Targets::GLSL) {
for ep in module.entry_points.iter() {
Expand All @@ -411,14 +403,14 @@ fn check_targets(
}
}
}
#[cfg(feature = "dot-out")]
#[cfg(dot_out)]
{
if targets.contains(Targets::DOT) {
let string = naga::back::dot::write(module, Some(&info), Default::default()).unwrap();
input.write_output_file("dot", "dot", string);
}
}
#[cfg(all(feature = "deserialize", feature = "hlsl-out"))]
#[cfg(all(feature = "deserialize", hlsl_out))]
{
if targets.contains(Targets::HLSL) {
write_output_hlsl(
Expand All @@ -431,15 +423,15 @@ fn check_targets(
);
}
}
#[cfg(all(feature = "deserialize", feature = "wgsl-out"))]
#[cfg(all(feature = "deserialize", wgsl_out))]
{
if targets.contains(Targets::WGSL) {
write_output_wgsl(input, module, &info, &params.wgsl);
}
}
}

#[cfg(feature = "spv-out")]
#[cfg(spv_out)]
fn write_output_spv(
input: &Input,
module: &naga::Module,
Expand Down Expand Up @@ -499,7 +491,7 @@ fn write_output_spv(
}
}

#[cfg(feature = "spv-out")]
#[cfg(spv_out)]
fn write_output_spv_inner(
input: &Input,
module: &naga::Module,
Expand All @@ -525,7 +517,7 @@ fn write_output_spv_inner(
input.write_output_file("spv", extension, dis);
}

#[cfg(feature = "msl-out")]
#[cfg(msl_out)]
fn write_output_msl(
input: &Input,
module: &naga::Module,
Expand Down Expand Up @@ -557,7 +549,7 @@ fn write_output_msl(
input.write_output_file("msl", "msl", string);
}

#[cfg(feature = "glsl-out")]
#[cfg(glsl_out)]
#[allow(clippy::too_many_arguments)]
fn write_output_glsl(
input: &Input,
Expand Down Expand Up @@ -599,7 +591,7 @@ fn write_output_glsl(
input.write_output_file("glsl", &extension, buffer);
}

#[cfg(feature = "hlsl-out")]
#[cfg(hlsl_out)]
fn write_output_hlsl(
input: &Input,
module: &naga::Module,
Expand Down Expand Up @@ -652,7 +644,7 @@ fn write_output_hlsl(
config.to_file(input.output_path("hlsl", "ron")).unwrap();
}

#[cfg(feature = "wgsl-out")]
#[cfg(wgsl_out)]
fn write_output_wgsl(
input: &Input,
module: &naga::Module,
Expand Down Expand Up @@ -957,7 +949,7 @@ fn convert_wgsl() {
}
}

#[cfg(all(feature = "wgsl-in", feature = "hlsl-out"))]
#[cfg(all(feature = "wgsl-in", hlsl_out))]
#[test]
fn unconsumed_vertex_outputs_hlsl_out() {
let load_and_parse = |name| {
Expand Down Expand Up @@ -1110,7 +1102,7 @@ fn convert_glsl_folder() {
.validate(&module)
.unwrap();

#[cfg(feature = "wgsl-out")]
#[cfg(wgsl_out)]
{
write_output_wgsl(&input, &module, &info, &WgslOutParameters::default());
}
Expand Down
2 changes: 1 addition & 1 deletion naga/tests/spirv_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Test SPIR-V backend capability checks.
*/

#![cfg(all(feature = "wgsl-in", feature = "spv-out"))]
#![cfg(all(feature = "wgsl-in", spv_out))]

use spirv::Capability as Ca;

Expand Down
15 changes: 13 additions & 2 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ ignored = ["cfg_aliases"]
[lib]

[features]
metal = ["naga/msl-out", "dep:block"]
## Enables the Metal backend when targeting Apple platforms.
##
## Has no effect on non-Apple platforms.
metal = [
# Metal is only available on Apple platforms, therefore request MSL output also only if we target an Apple platform.
"naga/msl-out-if-target-apple",
"dep:block",
]
vulkan = [
"naga/spv-out",
"dep:ash",
Expand All @@ -56,8 +63,12 @@ gles = [
"dep:ndk-sys",
"winapi/libloaderapi",
]
## Enables the DX12 backend when targeting Windows.
##
## Has no effect if not targeting Windows.
dx12 = [
"naga/hlsl-out",
# DX12 is only available on Windows, therefore request HLSL output also only if we target Windows.
"naga/hlsl-out-if-target-windows",
"dep:d3d12",
"dep:bit-set",
"dep:libloading",
Expand Down
Loading