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

Implement WGSLLanguageExtension #6814

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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.

2 changes: 2 additions & 0 deletions naga/src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub use crate::front::wgsl::error::ParseError;
use crate::front::wgsl::lower::Lowerer;
use crate::Scalar;

pub use crate::front::wgsl::parse::directive::language_extension::ImplementedLanguageExtension as WGSLLanguageExtension;

pub struct Frontend {
parser: Parser,
}
Expand Down
22 changes: 19 additions & 3 deletions naga/src/front/wgsl/parse/directive/language_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl LanguageExtension {
/// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL.
pub const fn to_ident(self) -> &'static str {
match self {
Self::Implemented(kind) => match kind {},
Self::Implemented(kind) => kind.to_ident(),
Self::Unimplemented(kind) => match kind {
UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => {
Self::READONLY_AND_READWRITE_STORAGE_TEXTURES
Expand All @@ -60,9 +60,25 @@ impl LanguageExtension {
}
}

/// A variant of [`LanguageExtension::Implemented`].
// A variant of [`LanguageExtension::Implemented`].
/// Enum of all WGSL extensions that are implemented
/// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) enum ImplementedLanguageExtension {}
pub enum ImplementedLanguageExtension {}
sagudev marked this conversation as resolved.
Show resolved Hide resolved

impl ImplementedLanguageExtension {
const ALL: [ImplementedLanguageExtension; 0] = [];

/// Returns slice of all variants of [`ImplementedLanguageExtension`]
pub const fn all() -> &'static [Self] {
&Self::ALL
}

/// Maps this [`ImplementedLanguageExtension`] into the sentinel word associated with it in WGSL.
pub const fn to_ident(self) -> &'static str {
match self {}
}
}

/// A variant of [`LanguageExtension::Unimplemented`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ optional = true

[dependencies]
arrayvec.workspace = true
bitflags.workspace = true
document-features.workspace = true
log.workspace = true
parking_lot.workspace = true
Expand Down
25 changes: 25 additions & 0 deletions wgpu/src/api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ use crate::{dispatch::InstanceInterface, *};

use std::{future::Future, sync::Arc};

bitflags::bitflags! {
/// WGSL language extensions.
///
/// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct WGSLLanguageFeatures: u32 {
/// <https://www.w3.org/TR/WGSL/#language_extension-readonly_and_readwrite_storage_textures>
const ReadOnlyAndReadWriteStorageTextures = 1 << 0;
/// <https://www.w3.org/TR/WGSL/#language_extension-packed_4x8_integer_dot_product>
const Packed4x8IntegerDotProduct = 1 << 1;
/// <https://www.w3.org/TR/WGSL/#language_extension-unrestricted_pointer_parameters>
const UnrestrictedPointerParameters = 1 << 2;
/// <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
const PointerCompositeAccess = 1 << 3;
}
}

/// Context for all other wgpu objects. Instance of wgpu.
///
/// This is the first thing you create when using wgpu.
Expand Down Expand Up @@ -395,4 +412,12 @@ impl Instance {
pub fn generate_report(&self) -> Option<wgc::global::GlobalReport> {
self.inner.as_core_opt().map(|ctx| ctx.generate_report())
}

/// Returns set of supported WGSL language extensions supported by this instance
///
/// <https://www.w3.org/TR/webgpu/#gpuwgsllanguagefeatures>
#[cfg(feature = "wgsl")]
pub fn wgsl_language_features(&self) -> WGSLLanguageFeatures {
self.inner.wgsl_language_features()
}
}
34 changes: 34 additions & 0 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
sagudev marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down
10 changes: 10 additions & 0 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,16 @@ 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 {
use wgc::naga::front::wgsl::WGSLLanguageExtension;
let wgsl_language_features = crate::WGSLLanguageFeatures::empty();
WGSLLanguageExtension::all()
.iter()
.for_each(|wle| match *wle {});
wgsl_language_features
}
}

impl dispatch::AdapterInterface for CoreAdapter {
Expand Down
3 changes: 3 additions & 0 deletions wgpu/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub trait InstanceInterface: CommonTraits {
) -> Pin<Box<dyn RequestAdapterFuture>>;

fn poll_all_devices(&self, force_wait: bool) -> bool;

#[cfg(feature = "wgsl")]
fn wgsl_language_features(&self) -> crate::WGSLLanguageFeatures;
}

pub trait AdapterInterface: CommonTraits {
Expand Down
Loading