Skip to content

Commit

Permalink
web: don't panic when calling GpuSupportedFeatures::dyn_into()
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Feb 2, 2023
1 parent 2562f32 commit d93e9a5
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,21 @@ const FEATURES_MAPPING: [(wgt::Features, web_sys::GpuFeatureName); 8] = [
),
];

fn map_wgt_features(supported_features: web_sys::GpuSupportedFeatures) -> wgt::Features {
let mut features = wgt::Features::empty();
if let Ok(features_set) = supported_features.dyn_into::<js_sys::Set>() {
for (wgpu_feat, web_feat) in FEATURES_MAPPING {
let value = wasm_bindgen::JsValue::from(web_feat);

if features_set.has(&value) {
features |= wgpu_feat;
}
}
};

features
}

type JsFutureResult = Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>;

fn future_request_adapter(result: JsFutureResult) -> Option<(Identified<web_sys::GpuAdapter>, ())> {
Expand Down Expand Up @@ -931,23 +946,7 @@ impl crate::context::Context for Context {
adapter: &Self::AdapterId,
_adapter_data: &Self::AdapterData,
) -> wgt::Features {
let features = adapter.0.features();

let features_set: js_sys::Set = features
.dyn_into()
.expect("adapter.features() is not setlike");

let mut features = wgt::Features::empty();

for (wgpu_feat, web_feat) in FEATURES_MAPPING {
let value = wasm_bindgen::JsValue::from(web_feat);

if features_set.has(&value) {
features |= wgpu_feat;
}
}

features
map_wgt_features(adapter.0.features())
}

fn adapter_limits(
Expand Down Expand Up @@ -1112,23 +1111,7 @@ impl crate::context::Context for Context {
device: &Self::DeviceId,
_device_data: &Self::DeviceData,
) -> wgt::Features {
let features = device.0.features();

let features_set: js_sys::Set = features
.dyn_into()
.expect("device.features() is not setlike");

let mut features = wgt::Features::empty();

for (wgpu_feat, web_feat) in FEATURES_MAPPING {
let value = wasm_bindgen::JsValue::from(web_feat);

if features_set.has(&value) {
features |= wgpu_feat;
}
}

features
map_wgt_features(device.0.features())
}

fn device_limits(
Expand Down

0 comments on commit d93e9a5

Please sign in to comment.