Skip to content

Commit

Permalink
Add utility function for setting constants map
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasDwyer committed May 14, 2024
1 parent 31163e1 commit 500a8b1
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1875,10 +1875,9 @@ 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);
let _ = js_sys::Reflect::set(
insert_constants_map(
&mapped_vertex_state,
&"constants".into(),
&hashmap_to_jsvalue(desc.vertex.compilation_options.constants),
desc.vertex.compilation_options.constants,
);
mapped_vertex_state.entry_point(desc.vertex.entry_point);

Expand Down Expand Up @@ -1956,11 +1955,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);
let _ = js_sys::Reflect::set(
&mapped_fragment_desc,
&"constants".into(),
&hashmap_to_jsvalue(frag.compilation_options.constants),
);
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 @@ -1987,11 +1982,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);
let _ = js_sys::Reflect::set(
&mapped_compute_stage,
&"constants".into(),
&hashmap_to_jsvalue(desc.compilation_options.constants),
);
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 Down Expand Up @@ -3826,12 +3817,30 @@ impl Drop for BufferMappedRange {
}
}

/// Adds the constants map to the given pipeline descriptor if the map is nonempty.
/// Logs an error 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() {
let result = js_sys::Reflect::set(target, &"constants".into(), &hashmap_to_jsvalue(map));

if let Err(error) = result {
log::error!("Failed to set constants map for pipeline: {error:?}");
}
}
}

/// 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() {
let _ = js_sys::Reflect::set(&obj, &k.into(), &(*v).into());
js_sys::Reflect::set(&obj, &k.into(), &(*v).into())
.expect("Setting the values in a Javascript map should never fail");
}

JsValue::from(obj)
Expand Down

0 comments on commit 500a8b1

Please sign in to comment.