Skip to content

Commit

Permalink
web: reimplement adapter|device_features (gfx-rs#3428)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili authored and cwfitzgerald committed Feb 9, 2023
1 parent e1c6239 commit 2f2206e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Bottom level categories:
#### WebGPU

- Implement `CommandEncoder::clear_buffer`. By @raphlinus in [#3426](https://github.com/gfx-rs/wgpu/pull/3426)
- Reimplement `adapter|device_features`. By @jinleili in [#3428](https://github.com/gfx-rs/wgpu/pull/3428)

#### Vulkan

Expand Down
67 changes: 34 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async-executor = "1.0"
bitflags = "1"
bit-vec = "0.6"
bytemuck = "1.4"
cargo-run-wasm = "0.2.0"
cargo-run-wasm = "0.3.0"
cfg_aliases = "0.1"
cfg-if = "1"
codespan-reporting = "0.11"
Expand Down Expand Up @@ -114,11 +114,11 @@ glutin = "0.29.1"
# wasm32 dependencies
console_error_panic_hook = "0.1.7"
console_log = "0.2"
js-sys = "0.3.60"
wasm-bindgen = "0.2.83"
wasm-bindgen-futures = "0.4.33"
js-sys = "0.3.61"
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.34"
wasm-bindgen-test = "0.3"
web-sys = "0.3.60"
web-sys = "0.3.61"

# deno dependencies
deno_console = "0.84.0"
Expand Down
8 changes: 4 additions & 4 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ objc = "0.2.5"
core-graphics-types = "0.1"

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = "0.2.83"
web-sys = { version = "0.3.60", features = ["Window", "HtmlCanvasElement", "WebGl2RenderingContext", "OffscreenCanvas"] }
js-sys = "0.3.60"
wasm-bindgen = "0.2.84"
web-sys = { version = "0.3.61", features = ["Window", "HtmlCanvasElement", "WebGl2RenderingContext", "OffscreenCanvas"] }
js-sys = "0.3.61"

[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand All @@ -127,7 +127,7 @@ features = ["wgsl-in"]

[dev-dependencies]
env_logger = "0.9"
winit = "0.27.1" # for "halmark" example
winit = "0.27.1" # for "halmark" example

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
glutin = "0.29.1" # for "gles" example
47 changes: 13 additions & 34 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@ 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();
for (wgpu_feat, web_feat) in FEATURES_MAPPING {
match wasm_bindgen::JsValue::from(web_feat).as_string() {
Some(value) if supported_features.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 +942,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 +1107,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 2f2206e

Please sign in to comment.