From 72976f3f7b3e57e899741a5825440cbacd88053e Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Mon, 30 Dec 2024 10:07:54 +0100 Subject: [PATCH] feat: add `Instance::wgsl_language_features` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Co-Authored-By: Erich Gubler --- CHANGELOG.md | 1 + Cargo.lock | 1 + wgpu/Cargo.toml | 1 + wgpu/src/api/instance.rs | 25 +++++++++++++++++++++++++ wgpu/src/backend/webgpu.rs | 34 ++++++++++++++++++++++++++++++++++ wgpu/src/backend/wgpu_core.rs | 12 ++++++++++++ wgpu/src/dispatch.rs | 3 +++ 7 files changed, 77 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12aa7ce6a0..1436eccebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -162,6 +162,7 @@ By @wumpf in [#6849](https://github.com/gfx-rs/wgpu/pull/6849). - Move raytracing alignments into HAL instead of in core. By @Vecvec in [#6563](https://github.com/gfx-rs/wgpu/pull/6563). - Allow for statically linking DXC rather than including separate `.dll` files. By @DouglasDwyer in [#6574](https://github.com/gfx-rs/wgpu/pull/6574). - `DeviceType` and `AdapterInfo` now impl `Hash` by @cwfitzgerald in [#6868](https://github.com/gfx-rs/wgpu/pull/6868) +- Add `wgsl_language_features` for obtaining available WGSL language feature by @sagudev in [#6814](https://github.com/gfx-rs/wgpu/pull/6814) #### Changes diff --git a/Cargo.lock b/Cargo.lock index 49e0517467..66bcb490c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3981,6 +3981,7 @@ name = "wgpu" version = "23.0.1" dependencies = [ "arrayvec", + "bitflags 2.6.0", "cfg_aliases 0.2.1", "document-features", "js-sys", diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index ca757e122c..86a7515223 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -187,6 +187,7 @@ optional = true [dependencies] arrayvec.workspace = true +bitflags.workspace = true document-features.workspace = true log.workspace = true parking_lot.workspace = true diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index 4b1b6b901f..8f43d0aaa9 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -4,6 +4,23 @@ use crate::{dispatch::InstanceInterface, *}; use std::future::Future; +bitflags::bitflags! { + /// WGSL language extensions. + /// + /// WGSL spec.: + #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)] + pub struct WgslLanguageFeatures: u32 { + /// + const ReadOnlyAndReadWriteStorageTextures = 1 << 0; + /// + const Packed4x8IntegerDotProduct = 1 << 1; + /// + const UnrestrictedPointerParameters = 1 << 2; + /// + const PointerCompositeAccess = 1 << 3; + } +} + /// Context for all other wgpu objects. Instance of wgpu. /// /// This is the first thing you create when using wgpu. @@ -385,4 +402,12 @@ impl Instance { pub fn generate_report(&self) -> Option { self.inner.as_core_opt().map(|ctx| ctx.generate_report()) } + + /// Returns set of supported WGSL language extensions supported by this instance. + /// + /// + #[cfg(feature = "wgsl")] + pub fn wgsl_language_features(&self) -> WgslLanguageFeatures { + self.inner.wgsl_language_features() + } } diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 8511aff5ed..c3ac25d925 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1562,6 +1562,40 @@ impl dispatch::InstanceInterface for ContextWebGpu { // Devices are automatically polled. true } + + #[cfg(feature = "wgsl")] + fn wgsl_language_features(&self) -> crate::WGSLLanguageFeatures { + let mut wgsl_language_features = crate::WGSLLanguageFeatures::empty(); + if let Some(gpu) = &self.gpu { + gpu.wgsl_language_features() + .keys() + .into_iter() + .map(|wlf| wlf.expect("`WGSLLanguageFeatures` elements should be valid")) + .map(|wlf| { + wlf.as_string() + .expect("`WGSLLanguageFeatures` should be string set") + }) + .filter_map(|wlf| match wlf.as_str() { + "readonly_and_readwrite_storage_textures" => { + Some(crate::WGSLLanguageFeatures::ReadOnlyAndReadWriteStorageTextures) + } + "packed_4x8_integer_dot_product" => { + Some(crate::WGSLLanguageFeatures::Packed4x8IntegerDotProduct) + } + "unrestricted_pointer_parameters" => { + Some(crate::WGSLLanguageFeatures::UnrestrictedPointerParameters) + } + "pointer_composite_access" => { + Some(crate::WGSLLanguageFeatures::PointerCompositeAccess) + } + _ => None, + }) + .for_each(|wlf| { + wgsl_language_features |= wlf; + }) + } + wgsl_language_features + } } impl Drop for ContextWebGpu { diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index c49c65ab42..85221cf074 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -851,6 +851,18 @@ impl dispatch::InstanceInterface for ContextWgpuCore { Err(err) => self.handle_error_fatal(err, "Instance::poll_all_devices"), } } + + #[cfg(feature = "wgsl")] + fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures { + wgc::naga::front::wgsl::ImplementedLanguageExtension::all() + .iter() + .copied() + .fold( + crate::WgslLanguageFeatures::empty(), + #[allow(unreachable_code)] + |acc, wle| acc | match wle {}, + ) + } } impl dispatch::AdapterInterface for CoreAdapter { diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index 6b99664ea7..999825cf15 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -100,6 +100,9 @@ pub trait InstanceInterface: CommonTraits { ) -> Pin>; fn poll_all_devices(&self, force_wait: bool) -> bool; + + #[cfg(feature = "wgsl")] + fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures; } pub trait AdapterInterface: CommonTraits {