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

Add support for pipeline-overridable constants in web backend #5688

Merged
merged 4 commits into from
May 29, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ By @stefnotch in [#5410](https://github.com/gfx-rs/wgpu/pull/5410)
- Fix `ClearColorF`, `ClearColorU` and `ClearColorI` commands being issued before `SetDrawColorBuffers` [#5666](https://github.com/gfx-rs/wgpu/pull/5666)
- Replace `glClear` with `glClearBufferF` because `glDrawBuffers` requires that the ith buffer must be `COLOR_ATTACHMENTi` or `NONE` [#5666](https://github.com/gfx-rs/wgpu/pull/5666)

#### WebGPU

- Added support for pipeline-overridable constants to the WebGPU backend by @DouglasDwyer in [#5688](https://github.com/gfx-rs/wgpu/pull/5688)

## v0.20.0 (2024-04-28)

### Major Changes
Expand Down
34 changes: 34 additions & 0 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use js_sys::Promise;
use std::{
any::Any,
cell::RefCell,
collections::HashMap,
fmt,
future::Future,
marker::PhantomData,
Expand Down Expand Up @@ -1876,6 +1877,10 @@ impl crate::context::Context for ContextWebGpu {
let module: &<ContextWebGpu as crate::Context>::ShaderModuleData =
downcast_ref(desc.vertex.module.data.as_ref());
let mut mapped_vertex_state = webgpu_sys::GpuVertexState::new(&module.0.module);
insert_constants_map(
&mapped_vertex_state,
desc.vertex.compilation_options.constants,
);
mapped_vertex_state.entry_point(desc.vertex.entry_point);

let buffers = desc
Expand Down Expand Up @@ -1952,6 +1957,7 @@ impl crate::context::Context for ContextWebGpu {
downcast_ref(frag.module.data.as_ref());
let mut mapped_fragment_desc =
webgpu_sys::GpuFragmentState::new(&module.0.module, &targets);
insert_constants_map(&mapped_vertex_state, frag.compilation_options.constants);
mapped_fragment_desc.entry_point(frag.entry_point);
mapped_desc.fragment(&mapped_fragment_desc);
}
Expand All @@ -1978,6 +1984,7 @@ impl crate::context::Context for ContextWebGpu {
downcast_ref(desc.module.data.as_ref());
let mut mapped_compute_stage =
webgpu_sys::GpuProgrammableStage::new(&shader_module.0.module);
insert_constants_map(&mapped_compute_stage, desc.compilation_options.constants);
mapped_compute_stage.entry_point(desc.entry_point);
let auto_layout = wasm_bindgen::JsValue::from(webgpu_sys::GpuAutoLayoutMode::Auto);
let mut mapped_desc = webgpu_sys::GpuComputePipelineDescriptor::new(
Expand All @@ -1994,6 +2001,7 @@ impl crate::context::Context for ContextWebGpu {
if let Some(label) = desc.label {
mapped_desc.label(label);
}

create_identified(device_data.0.create_compute_pipeline(&mapped_desc))
}

Expand Down Expand Up @@ -3828,3 +3836,29 @@ impl Drop for BufferMappedRange {
}
}
}

/// Adds the constants map to the given pipeline descriptor if the map is nonempty.
/// Panics if the map cannot be set.
///
/// This function is necessary because the constants array is not currently
/// exposed by `wasm-bindgen`. See the following issues for details:
/// - [gfx-rs/wgpu#5688](https://github.com/gfx-rs/wgpu/pull/5688)
/// - [rustwasm/wasm-bindgen#3587](https://github.com/rustwasm/wasm-bindgen/issues/3587)
fn insert_constants_map(target: &JsValue, map: &HashMap<String, f64>) {
if !map.is_empty() {
js_sys::Reflect::set(target, &"constants".into(), &hashmap_to_jsvalue(map))
.expect("Setting the values in a Javascript pipeline descriptor should never fail");
}
}

/// Converts a hashmap to a Javascript object.
fn hashmap_to_jsvalue(map: &HashMap<String, f64>) -> JsValue {
let obj = js_sys::Object::new();

for (k, v) in map.iter() {
js_sys::Reflect::set(&obj, &k.into(), &(*v).into())
.expect("Setting the values in a Javascript map should never fail");
}

JsValue::from(obj)
}
Loading