Skip to content

Commit dc9b2eb

Browse files
Add Instance::wgsl_language_features (#6814)
* docs(naga): clarify desc. for `LanguageExtension` * feat(naga): expose `{,Implemented,Unimplemented}LanguageExtension` * feat(naga): expose `ImplementedLanguageExtension` as `WgslLanguageExtension` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Co-Authored-By: Erich Gubler <erichdongubler@gmail.com> * feat: add `Instance::wgsl_language_features` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Co-Authored-By: Erich Gubler <erichdongubler@gmail.com> * refactor: reimpl. `ImplementedLanguageExtension::all` w/ `strum::VariantArray` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
1 parent 98f1c72 commit dc9b2eb

File tree

10 files changed

+102
-7
lines changed

10 files changed

+102
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ By @wumpf in [#6849](https://github.com/gfx-rs/wgpu/pull/6849).
162162
- Move raytracing alignments into HAL instead of in core. By @Vecvec in [#6563](https://github.com/gfx-rs/wgpu/pull/6563).
163163
- Allow for statically linking DXC rather than including separate `.dll` files. By @DouglasDwyer in [#6574](https://github.com/gfx-rs/wgpu/pull/6574).
164164
- `DeviceType` and `AdapterInfo` now impl `Hash` by @cwfitzgerald in [#6868](https://github.com/gfx-rs/wgpu/pull/6868)
165+
- Add `wgsl_language_features` for obtaining available WGSL language feature by @sagudev in [#6814](https://github.com/gfx-rs/wgpu/pull/6814)
165166

166167
##### Vulkan
167168

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

naga/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ codespan-reporting = { version = "0.11.0" }
7575
rustc-hash.workspace = true
7676
indexmap.workspace = true
7777
log = "0.4"
78+
strum.workspace = true
7879
spirv = { version = "0.3", optional = true }
7980
thiserror.workspace = true
8081
serde = { version = "1.0.217", features = ["derive"], optional = true }
@@ -102,4 +103,3 @@ ron = "0.8.0"
102103
rspirv = { version = "0.11", git = "https://github.com/gfx-rs/rspirv", rev = "b969f175d5663258b4891e44b76c1544da9661ab" }
103104
serde = { workspace = true, features = ["derive"] }
104105
spirv = { version = "0.3", features = ["deserialize"] }
105-
strum.workspace = true

naga/src/front/wgsl/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub use crate::front::wgsl::error::ParseError;
2020
use crate::front::wgsl::lower::Lowerer;
2121
use crate::Scalar;
2222

23+
pub use crate::front::wgsl::parse::directive::language_extension::{
24+
ImplementedLanguageExtension, LanguageExtension, UnimplementedLanguageExtension,
25+
};
26+
2327
pub struct Frontend {
2428
parser: Parser,
2529
}

naga/src/front/wgsl/parse/directive/language_extension.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
//!
33
//! The focal point of this module is the [`LanguageExtension`] API.
44
5-
/// A language extension not guaranteed to be present in all environments.
5+
use strum::VariantArray;
6+
7+
/// A language extension recognized by Naga, but not guaranteed to be present in all environments.
68
///
79
/// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
810
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
9-
pub(crate) enum LanguageExtension {
11+
pub enum LanguageExtension {
1012
#[allow(unused)]
1113
Implemented(ImplementedLanguageExtension),
1214
Unimplemented(UnimplementedLanguageExtension),
@@ -41,7 +43,7 @@ impl LanguageExtension {
4143
/// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL.
4244
pub const fn to_ident(self) -> &'static str {
4345
match self {
44-
Self::Implemented(kind) => match kind {},
46+
Self::Implemented(kind) => kind.to_ident(),
4547
Self::Unimplemented(kind) => match kind {
4648
UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => {
4749
Self::READONLY_AND_READWRITE_STORAGE_TEXTURES
@@ -61,12 +63,24 @@ impl LanguageExtension {
6163
}
6264

6365
/// A variant of [`LanguageExtension::Implemented`].
64-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
65-
pub(crate) enum ImplementedLanguageExtension {}
66+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, VariantArray)]
67+
pub enum ImplementedLanguageExtension {}
68+
69+
impl ImplementedLanguageExtension {
70+
/// Returns slice of all variants of [`ImplementedLanguageExtension`].
71+
pub const fn all() -> &'static [Self] {
72+
Self::VARIANTS
73+
}
74+
75+
/// Maps this [`ImplementedLanguageExtension`] into the sentinel word associated with it in WGSL.
76+
pub const fn to_ident(self) -> &'static str {
77+
match self {}
78+
}
79+
}
6680

6781
/// A variant of [`LanguageExtension::Unimplemented`].
6882
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
69-
pub(crate) enum UnimplementedLanguageExtension {
83+
pub enum UnimplementedLanguageExtension {
7084
ReadOnlyAndReadWriteStorageTextures,
7185
Packed4x8IntegerDotProduct,
7286
UnrestrictedPointerParameters,

wgpu/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ optional = true
187187

188188
[dependencies]
189189
arrayvec.workspace = true
190+
bitflags.workspace = true
190191
document-features.workspace = true
191192
log.workspace = true
192193
parking_lot.workspace = true

wgpu/src/api/instance.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ use crate::{dispatch::InstanceInterface, *};
44

55
use std::future::Future;
66

7+
bitflags::bitflags! {
8+
/// WGSL language extensions.
9+
///
10+
/// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
11+
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
12+
pub struct WgslLanguageFeatures: u32 {
13+
/// <https://www.w3.org/TR/WGSL/#language_extension-readonly_and_readwrite_storage_textures>
14+
const ReadOnlyAndReadWriteStorageTextures = 1 << 0;
15+
/// <https://www.w3.org/TR/WGSL/#language_extension-packed_4x8_integer_dot_product>
16+
const Packed4x8IntegerDotProduct = 1 << 1;
17+
/// <https://www.w3.org/TR/WGSL/#language_extension-unrestricted_pointer_parameters>
18+
const UnrestrictedPointerParameters = 1 << 2;
19+
/// <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
20+
const PointerCompositeAccess = 1 << 3;
21+
}
22+
}
23+
724
/// Context for all other wgpu objects. Instance of wgpu.
825
///
926
/// This is the first thing you create when using wgpu.
@@ -385,4 +402,12 @@ impl Instance {
385402
pub fn generate_report(&self) -> Option<wgc::global::GlobalReport> {
386403
self.inner.as_core_opt().map(|ctx| ctx.generate_report())
387404
}
405+
406+
/// Returns set of supported WGSL language extensions supported by this instance.
407+
///
408+
/// <https://www.w3.org/TR/webgpu/#gpuwgsllanguagefeatures>
409+
#[cfg(feature = "wgsl")]
410+
pub fn wgsl_language_features(&self) -> WgslLanguageFeatures {
411+
self.inner.wgsl_language_features()
412+
}
388413
}

wgpu/src/backend/webgpu.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,40 @@ impl dispatch::InstanceInterface for ContextWebGpu {
15631563
// Devices are automatically polled.
15641564
true
15651565
}
1566+
1567+
#[cfg(feature = "wgsl")]
1568+
fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures {
1569+
let mut wgsl_language_features = crate::WgslLanguageFeatures::empty();
1570+
if let Some(gpu) = &self.gpu {
1571+
gpu.wgsl_language_features()
1572+
.keys()
1573+
.into_iter()
1574+
.map(|wlf| wlf.expect("`WgslLanguageFeatures` elements should be valid"))
1575+
.map(|wlf| {
1576+
wlf.as_string()
1577+
.expect("`WgslLanguageFeatures` should be string set")
1578+
})
1579+
.filter_map(|wlf| match wlf.as_str() {
1580+
"readonly_and_readwrite_storage_textures" => {
1581+
Some(crate::WgslLanguageFeatures::ReadOnlyAndReadWriteStorageTextures)
1582+
}
1583+
"packed_4x8_integer_dot_product" => {
1584+
Some(crate::WgslLanguageFeatures::Packed4x8IntegerDotProduct)
1585+
}
1586+
"unrestricted_pointer_parameters" => {
1587+
Some(crate::WgslLanguageFeatures::UnrestrictedPointerParameters)
1588+
}
1589+
"pointer_composite_access" => {
1590+
Some(crate::WgslLanguageFeatures::PointerCompositeAccess)
1591+
}
1592+
_ => None,
1593+
})
1594+
.for_each(|wlf| {
1595+
wgsl_language_features |= wlf;
1596+
})
1597+
}
1598+
wgsl_language_features
1599+
}
15661600
}
15671601

15681602
impl Drop for ContextWebGpu {

wgpu/src/backend/wgpu_core.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,18 @@ impl dispatch::InstanceInterface for ContextWgpuCore {
851851
Err(err) => self.handle_error_fatal(err, "Instance::poll_all_devices"),
852852
}
853853
}
854+
855+
#[cfg(feature = "wgsl")]
856+
fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures {
857+
wgc::naga::front::wgsl::ImplementedLanguageExtension::all()
858+
.iter()
859+
.copied()
860+
.fold(
861+
crate::WgslLanguageFeatures::empty(),
862+
#[allow(unreachable_code)]
863+
|acc, wle| acc | match wle {},
864+
)
865+
}
854866
}
855867

856868
impl dispatch::AdapterInterface for CoreAdapter {

wgpu/src/dispatch.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub trait InstanceInterface: CommonTraits {
100100
) -> Pin<Box<dyn RequestAdapterFuture>>;
101101

102102
fn poll_all_devices(&self, force_wait: bool) -> bool;
103+
104+
#[cfg(feature = "wgsl")]
105+
fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures;
103106
}
104107

105108
pub trait AdapterInterface: CommonTraits {

0 commit comments

Comments
 (0)