diff --git a/wgpu-types/src/features.rs b/wgpu-types/src/features.rs index 6f1745c60..0ec6ed66c 100644 --- a/wgpu-types/src/features.rs +++ b/wgpu-types/src/features.rs @@ -108,6 +108,71 @@ impl From for FeatureBits { } } +/// Maps a feature's `#[web(...)]` attribute to whether the feature is allowed on the web +/// backend, rejecting a missing or unrecognized value. +macro_rules! web_attr_allows { + ($flag:tt; allow) => { + true + }; + ($flag:tt; ignore) => { + false + }; + ($flag:tt; $other:tt) => { + compile_error!(concat!( + "`#[web(", + stringify!($other), + ")]` on `", + stringify!($flag), + "`: the value must be `allow` or `ignore`", + )) + }; + ($flag:tt;) => { + compile_error!(concat!( + "WebGPU feature `", + stringify!($flag), + "` needs a `#[web(allow)]` or `#[web(ignore)]` attribute", + )) + }; +} + +/// Generate the `Features::ALLOWED_ON_WEB_BACKEND` constant. +/// +/// Each [`FeaturesWebGPU`] feature must have the `web` attribute, specifying +/// whether or not to expose it on the web backend (typically yes, unless +/// there are known deficiencies in the bindings that prevent using the +/// feature). Standards-track native-only features can be added as special +/// cases. +macro_rules! features_web { + (FeaturesWebGPU; $($flag:tt [$($web:tt)?])*) => { + impl Features { + /// The features that the web backend will allow through from an underlying + /// browser WebGPU implementation. + /// + /// This may include bits in [`FeaturesWGPU`] for standards-track features + /// that have not yet been migrated to [`FeaturesWebGPU`]. + pub const ALLOWED_ON_WEB_BACKEND: Self = Self::from_parts( + FeaturesWGPU::SUBGROUP, + FeaturesWebGPU::from_bits_retain( + 0 $(| if web_attr_allows!($flag; $($web)?) { + FeaturesWebGPU::$flag.bits() + } else { + 0 + })* + ), + ); + } + }; + ($inner_name:tt; $($flag:tt [$($web:tt)?])*) => { + $($( + compile_error!(concat!( + "`#[web(", stringify!($web), ")]` on `", stringify!($flag), + "`: only `FeaturesWebGPU` features have a `web` attribute, and `", + stringify!($flag), "` is a `", stringify!($inner_name), "` feature", + )); + )?)* + }; +} + /// Defines the feature bitflags, using [`bitflags_array`](crate::bitflags_array) for storage. /// /// On top of what `bitflags_array!` accepts, each flag carries feature-specific attributes, @@ -115,9 +180,16 @@ impl From for FeatureBits { /// `bitflags_array!`. Adding another one means adding a matcher for it, in a fixed position /// relative to the existing ones, and generating whatever it should produce. /// -/// The only such attribute today is `#[name("kebab-case-name", "alias", ...)]`, giving the -/// names that the WebGPU API uses for the feature: the name from the spec, for features in -/// `FeaturesWebGPU`, and otherwise a `wgpu-` prefixed name. +/// `#[name("kebab-case-name", "alias", ...)]` gives the names that the WebGPU API uses for +/// the feature: the name from the spec, for features in [`FeaturesWebGPU`], and otherwise a +/// `wgpu-` prefixed name. It is required on every feature. +/// +/// `#[web(allow)]` or `#[web(ignore)]` controls whether `wgpu`'s WebGPU backend will +/// offer the feature when the underlying WebGPU implementation does. Typically should be +/// `allow`, but `ignore` may be appropriate in cases where missing logic in the WebGPU +/// backend means the feature will not work via `wgpu` even though it is supported by the +/// underlying backend, or where there is concern about interactions or confusion with +/// `wgpu` native features having subtly different API or behavior. macro_rules! features { ( $(#[$outer:meta])* @@ -129,6 +201,7 @@ macro_rules! features { $( $(#[doc $($args:tt)*])* #[name($str_name:literal $(, $alias:literal)*)] + $(#[web($web:tt)])? const $Flag:tt = $value:expr; )* } @@ -149,6 +222,8 @@ macro_rules! features { )* } + $( features_web! { $inner_name; $($Flag [$($web)?])* } )* + // Parses kebab-case feature names (i.e. the names given in the spec, for features // in FeaturesWebGPU, and otherwise the `wgpu-` prefixed names). impl FromStr for $name { @@ -754,10 +829,11 @@ features! { /// - Vulkan /// - DX12 /// - Metal + /// - WebGPU /// /// The `subgroups` feature has been added to WebGPU, but there may be /// differences between the standard and the `wgpu` implementation, - /// so it remains a native-only feature in wgpu for now. + /// so it has not yet been moved to [`FeaturesWebGPU`]. /// See . /// /// Because it is expected to move to the WebGPU feature set in the @@ -1144,6 +1220,7 @@ features! { /// /// This is a web and native feature. #[name("depth-clip-control")] + #[web(allow)] const DEPTH_CLIP_CONTROL = WEBGPU_FEATURE_DEPTH_CLIP_CONTROL; /// Allows for explicit creation of textures of format [`TextureFormat::Depth32FloatStencil8`] @@ -1159,6 +1236,7 @@ features! { /// /// [`TextureFormat::Depth32FloatStencil8`]: super::TextureFormat::Depth32FloatStencil8 #[name("depth32float-stencil8")] + #[web(allow)] const DEPTH32FLOAT_STENCIL8 = WEBGPU_FEATURE_DEPTH32FLOAT_STENCIL8; /// Enables BCn family of compressed textures. All BCn textures use 4x4 pixel blocks @@ -1179,6 +1257,7 @@ features! { /// /// This is a web and native feature. #[name("texture-compression-bc")] + #[web(allow)] const TEXTURE_COMPRESSION_BC = WEBGPU_FEATURE_TEXTURE_COMPRESSION_BC; @@ -1194,6 +1273,7 @@ features! { /// /// This is a web and native feature. #[name("texture-compression-bc-sliced-3d")] + #[web(allow)] const TEXTURE_COMPRESSION_BC_SLICED_3D = WEBGPU_FEATURE_TEXTURE_COMPRESSION_BC_SLICED_3D; /// Enables ETC family of compressed textures. All ETC textures use 4x4 pixel blocks. @@ -1212,6 +1292,7 @@ features! { /// /// This is a web and native feature. #[name("texture-compression-etc2")] + #[web(allow)] const TEXTURE_COMPRESSION_ETC2 = WEBGPU_FEATURE_TEXTURE_COMPRESSION_ETC2; /// Enables ASTC family of compressed textures. ASTC textures use pixel blocks varying from 4x4 to 12x12. @@ -1233,6 +1314,7 @@ features! { /// /// This is a web and native feature. #[name("texture-compression-astc")] + #[web(allow)] const TEXTURE_COMPRESSION_ASTC = WEBGPU_FEATURE_TEXTURE_COMPRESSION_ASTC; @@ -1252,6 +1334,7 @@ features! { /// /// This is a web and native feature. #[name("texture-compression-astc-sliced-3d")] + #[web(allow)] const TEXTURE_COMPRESSION_ASTC_SLICED_3D = WEBGPU_FEATURE_TEXTURE_COMPRESSION_ASTC_SLICED_3D; /// Enables use of Timestamp Queries. These queries tell the current gpu timestamp when @@ -1284,6 +1367,7 @@ features! { #[doc = link_to_wgpu_docs!(["`CommandEncoder::resolve_query_set`"]: "struct.CommandEncoder.html#method.resolve_query_set")] #[doc = link_to_wgpu_docs!(["`Queue::get_timestamp_period`"]: "struct.Queue.html#method.get_timestamp_period")] #[name("timestamp-query")] + #[web(allow)] const TIMESTAMP_QUERY = WEBGPU_FEATURE_TIMESTAMP_QUERY; /// Allows non-zero value for the `first_instance` member in indirect draw calls. @@ -1305,6 +1389,7 @@ features! { /// /// This is a web and native feature. #[name("indirect-first-instance")] + #[web(allow)] const INDIRECT_FIRST_INSTANCE = WEBGPU_FEATURE_INDIRECT_FIRST_INSTANCE; /// Allows shaders to use 16-bit floating point types. You may use them uniform buffers, @@ -1321,6 +1406,7 @@ features! { /// /// This is a web and native feature. #[name("shader-f16")] + #[web(allow)] const SHADER_F16 = WEBGPU_FEATURE_SHADER_F16; /// Allows for usage of textures of format [`TextureFormat::Rg11b10Ufloat`] as a render target @@ -1335,6 +1421,7 @@ features! { /// /// [`TextureFormat::Rg11b10Ufloat`]: super::TextureFormat::Rg11b10Ufloat #[name("rg11b10ufloat-renderable")] + #[web(allow)] const RG11B10UFLOAT_RENDERABLE = WEBGPU_FEATURE_RG11B10UFLOAT_RENDERABLE; /// Allows the [`TextureUsages::STORAGE_BINDING`] usage on textures with format [`TextureFormat::Bgra8Unorm`] @@ -1350,6 +1437,7 @@ features! { /// [`TextureFormat::Bgra8Unorm`]: super::TextureFormat::Bgra8Unorm /// [`TextureUsages::STORAGE_BINDING`]: super::TextureUsages::STORAGE_BINDING #[name("bgra8unorm-storage")] + #[web(allow)] const BGRA8UNORM_STORAGE = WEBGPU_FEATURE_BGRA8UNORM_STORAGE; @@ -1364,6 +1452,7 @@ features! { /// /// This is a web and native feature. #[name("float32-filterable")] + #[web(allow)] const FLOAT32_FILTERABLE = WEBGPU_FEATURE_FLOAT32_FILTERABLE; /// Allows textures with formats "r32float", "rg32float", and "rgba32float" to be blendable. @@ -1372,6 +1461,7 @@ features! { /// - Vulkan /// - WebGPU #[name("float32-blendable")] + #[web(allow)] const FLOAT32_BLENDABLE = WEBGPU_FEATURE_FLOAT32_BLENDABLE; /// Allows two outputs from a shader to be used for blending. @@ -1388,6 +1478,7 @@ features! { /// /// This is a web and native feature. #[name("dual-source-blending")] + #[web(allow)] const DUAL_SOURCE_BLENDING = WEBGPU_FEATURE_DUAL_SOURCE_BLENDING; /// Allows the use of `@builtin(clip_distances)` in WGSL. @@ -1400,6 +1491,7 @@ features! { /// /// This is a web and native feature. #[name("clip-distances")] + #[web(allow)] const CLIP_DISTANCES = WEBGPU_FEATURE_CLIP_DISTANCES; /// Allows the use of immediate data: small, fast bits of memory that can be updated @@ -1433,6 +1525,7 @@ features! { #[doc = link_to_wgpu_docs!(["`RenderPass::set_immediates`"]: "struct.RenderPass.html#method.set_immediates")] /// [`Limits::max_immediate_size`]: super::Limits #[name("immediates")] + #[web(allow)] const IMMEDIATES = WEBGPU_FEATURE_IMMEDIATES; /// Enables `builtin(primitive_index)` in fragment shaders. @@ -1451,6 +1544,7 @@ features! { /// WebGPU-defined name, and `shader-primitive-index` is accepted to /// remain compatible with previous wgpu behavior. #[name("primitive-index", "shader-primitive-index")] + #[web(allow)] const PRIMITIVE_INDEX = WEBGPU_FEATURE_PRIMITIVE_INDEX; /// Allows `TextureView`s to rearrange or replace the color components @@ -1466,6 +1560,7 @@ features! { /// /// This is a web and native feature. #[name("texture-component-swizzle")] + #[web(allow)] const TEXTURE_COMPONENT_SWIZZLE = WEBGPU_FEATURE_TEXTURE_COMPONENT_SWIZZLE; } } @@ -1566,6 +1661,16 @@ mod tests { } } + #[test] + fn allowed_on_web_backend() { + // Every WebGPU feature is currently allowed on the web backend, + // as well as `subgroups`. + assert_eq!( + Features::ALLOWED_ON_WEB_BACKEND, + Features::from_parts(FeaturesWGPU::SUBGROUP, FeaturesWebGPU::all()), + ); + } + #[test] fn check_features_display() { use alloc::format; diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index c70f67f37..c0c67bc7a 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1764,15 +1764,35 @@ impl dispatch::AdapterInterface for WebAdapter { let required_limits = map_js_sys_limits(&desc.required_limits); mapped_desc.set_required_limits(&required_limits); - let required_features = desc + // Note that `ALLOWED_ON_WEB_BACKEND` may include bits in `FeaturesWGPU` for + // standards-track features that have not yet been migrated to `FeaturesWebGPU`. + let blocked_features = wgt::Features::ALLOWED_ON_WEB_BACKEND.complement(); + let required_features = match desc .required_features .iter() .map(|feat| { - wasm_bindgen::JsValue::from(feat.as_str()) - .dyn_into::() - .unwrap() + let name = feat.as_str().unwrap(); + if feat.intersects(blocked_features) { + Err(crate::RequestDeviceError { + inner: crate::RequestDeviceErrorKind::WebGpu(format!( + "Requested unsupported feature `{name}`. \ + This feature is unsupported by the WebGPU backend, even \ + if the underlying implementation supports it." + )), + }) + } else { + // Any other name is handed to the implementation, which rejects the + // features it does not support. + Ok(js_sys::JsString::from(name)) + } }) - .collect::>(); + .collect::, _>>() + { + Ok(features) => features, + Err(err) => { + return Box::pin(core::future::ready(Err(err))); + } + }; mapped_desc.set_required_features(&required_features); if let Some(label) = desc.label { @@ -1799,7 +1819,19 @@ impl dispatch::AdapterInterface for WebAdapter { } fn features(&self) -> crate::Features { - map_wgt_features(self.inner.features()) + // We generally want to expose WebGPU features offered by the underlying + // implementation if we know about them, and not if we don't (because in that case + // we don't know if changes are needed in the WebGPU backend to correctly pass them + // through). Cleared bits in the `ALLOWED_ON_WEB_BACKEND` mask specify individual + // features that should never be exposed by the WebGPU backend regardless of + // underlying support. This may be appropriate in cases where missing logic in the + // WebGPU backend means the feature will not work via `wgpu` even though it is + // supported by the underlying backend, or where there is concern about interactions + // or confusion with `wgpu` native features having subtly different API or behavior. + // + // Note that `ALLOWED_ON_WEB_BACKEND` may include bits in `FeaturesWGPU` for + // standards-track features that have not yet been migrated to `FeaturesWebGPU`. + map_wgt_features(self.inner.features()) & wgt::Features::ALLOWED_ON_WEB_BACKEND } fn limits(&self) -> crate::Limits {