/
githubmirror
/
servo
Обзор
Документация
Войти
/
githubmirror
/
servo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
components/script_webgpu/gpusupportedfeatures.rs
200 строк
7 KB
Narfinger
script: Move GPUAdapter, GPUSupportedFeatures, GPUSupportedLimits and WGSLLanguageFeatures to script_webgpu (#46964)
10 авг 2026, 21:29
Не верифицирован
10 авг 2026, 21:29
e4c9034
Код
Авторство
О чём код?
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ // check-tidy: no specs after this line use std::marker::PhantomData; use dom_struct::dom_struct; use indexmap::IndexSet; use js::context::JSContext; use js::rust::HandleObject; use malloc_size_of_derive::MallocSizeOf; use script_bindings::DomTypes; use script_bindings::cell::DomRefCell; use script_bindings::codegen::GenericBindings::WebGPUBinding::{ GPUFeatureName, GPUSupportedFeaturesMethods, GPUSupportedFeaturesWrap, }; use script_bindings::like::Setlike; use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_wrap}; use wgpu_types::Features; use crate::JSTraceable; use crate::dom::bindings::error::Fallible; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::str::DOMString; #[dom_struct] pub struct GPUSupportedFeatures<D: DomTypes> { reflector: Reflector, // internal storage for features #[custom_trace] internal: DomRefCell<IndexSet<GPUFeatureName>>, #[ignore_malloc_size_of = "defined in wgpu-types"] #[no_trace] features: Features, #[no_trace = "PhantomData does not exist"] phantom: PhantomData<D>, } impl<D> GPUSupportedFeatures<D> where D: DomTypes<GPUSupportedFeatures = GPUSupportedFeatures<D>>, { fn new( cx: &mut JSContext, global: &D::GlobalScope, proto: Option<HandleObject>, features: Features, ) -> DomRoot<GPUSupportedFeatures<D>> { let mut set = IndexSet::new(); // everything that wgpu currently does is considered as part of "core" set.insert(GPUFeatureName::Core_features_and_limits); if features.contains(Features::DEPTH_CLIP_CONTROL) { set.insert(GPUFeatureName::Depth_clip_control); } if features.contains(Features::DEPTH32FLOAT_STENCIL8) { set.insert(GPUFeatureName::Depth32float_stencil8); } if features.contains(Features::TEXTURE_COMPRESSION_BC) { set.insert(GPUFeatureName::Texture_compression_bc); } // TODO: texture-compression-bc-sliced-3d when wgpu supports it if features.contains(Features::TEXTURE_COMPRESSION_ETC2) { set.insert(GPUFeatureName::Texture_compression_etc2); } if features.contains(Features::TEXTURE_COMPRESSION_ASTC) { set.insert(GPUFeatureName::Texture_compression_astc); } if features.contains(Features::TIMESTAMP_QUERY) { set.insert(GPUFeatureName::Timestamp_query); } if features.contains(Features::INDIRECT_FIRST_INSTANCE) { set.insert(GPUFeatureName::Indirect_first_instance); } // While this feature exists in wgpu, it's not supported by naga yet // https://github.com/gfx-rs/wgpu/issues/4384 /* if features.contains(Features::SHADER_F16) { set.insert(GPUFeatureName::Shader_f16); } */ if features.contains(Features::RG11B10UFLOAT_RENDERABLE) { set.insert(GPUFeatureName::Rg11b10ufloat_renderable); } if features.contains(Features::BGRA8UNORM_STORAGE) { set.insert(GPUFeatureName::Bgra8unorm_storage); } if features.contains(Features::FLOAT32_FILTERABLE) { set.insert(GPUFeatureName::Float32_filterable); } // TODO: clip-distances when wgpu supports it if features.contains(Features::DUAL_SOURCE_BLENDING) { set.insert(GPUFeatureName::Dual_source_blending); } // While this feature exists in wgpu, it's not supported by naga yet // https://github.com/gfx-rs/wgpu/issues/5555 /* if features.contains(Features::SUBGROUP) { set.insert(GPUFeatureName::Subgroups); } */ reflect_dom_object_with_proto_and_wrap::<D, _, _>( Box::new(GPUSupportedFeatures { reflector: Reflector::new(), internal: DomRefCell::new(set), features, phantom: PhantomData, }), global, proto, cx, GPUSupportedFeaturesWrap::<D>, ) } #[expect(non_snake_case)] pub fn Constructor( cx: &mut JSContext, global: &D::GlobalScope, proto: Option<HandleObject>, features: Features, ) -> Fallible<DomRoot<GPUSupportedFeatures<D>>> { Ok(GPUSupportedFeatures::new(cx, global, proto, features)) } } impl<D: DomTypes> GPUSupportedFeatures<D> { pub fn wgpu_features(&self) -> &Features { &self.features } } impl<D: DomTypes> GPUSupportedFeaturesMethods<D> for GPUSupportedFeatures<D> { fn Size(&self) -> u32 { self.internal.borrow().len() as u32 } } pub(crate) fn gpu_to_wgt_feature(feature: GPUFeatureName) -> Option<Features> { match feature { // everything that wgpu currently does is considered as part of "core" GPUFeatureName::Core_features_and_limits => Some(Features::empty()), GPUFeatureName::Depth_clip_control => Some(Features::DEPTH_CLIP_CONTROL), GPUFeatureName::Depth32float_stencil8 => Some(Features::DEPTH32FLOAT_STENCIL8), GPUFeatureName::Texture_compression_bc => Some(Features::TEXTURE_COMPRESSION_BC), GPUFeatureName::Texture_compression_etc2 => Some(Features::TEXTURE_COMPRESSION_ETC2), GPUFeatureName::Texture_compression_astc => Some(Features::TEXTURE_COMPRESSION_ASTC), GPUFeatureName::Timestamp_query => Some(Features::TIMESTAMP_QUERY), GPUFeatureName::Indirect_first_instance => Some(Features::INDIRECT_FIRST_INSTANCE), // While this feature exists in wgpu, it's not supported by naga yet // https://github.com/gfx-rs/wgpu/issues/4384 GPUFeatureName::Shader_f16 => None, GPUFeatureName::Rg11b10ufloat_renderable => Some(Features::RG11B10UFLOAT_RENDERABLE), GPUFeatureName::Bgra8unorm_storage => Some(Features::BGRA8UNORM_STORAGE), GPUFeatureName::Float32_filterable => Some(Features::FLOAT32_FILTERABLE), GPUFeatureName::Dual_source_blending => Some(Features::DUAL_SOURCE_BLENDING), GPUFeatureName::Texture_compression_bc_sliced_3d => None, GPUFeatureName::Clip_distances => None, // While this feature exists in wgpu, it's not supported by naga yet // https://github.com/gfx-rs/wgpu/issues/5555 GPUFeatureName::Subgroups => None, } } impl<D: DomTypes> Setlike for GPUSupportedFeatures<D> { type Key = DOMString; #[inline(always)] fn get_index(&self, cx: &mut JSContext, index: u32) -> Option<Self::Key> { self.internal .get_index(cx, index) .map(|key| key.as_str().into()) } #[inline(always)] fn size(&self, cx: &mut JSContext) -> u32 { self.internal.size(cx) } #[inline(always)] fn add(&self, _cx: &mut JSContext, _key: Self::Key) { unreachable!("readonly"); } #[inline(always)] fn has(&self, cx: &mut JSContext, key: Self::Key) -> bool { if let Ok(key) = key.parse() { self.internal.has(cx, key) } else { false } } #[inline(always)] fn clear(&self, _cx: &mut JSContext) { unreachable!("readonly"); } #[inline(always)] fn delete(&self, _cx: &mut JSContext, _key: Self::Key) -> bool { unreachable!("readonly"); } }