Skip to content

Commit 9257824

Browse files
committed
Add utility function for setting constants map
1 parent 31163e1 commit 9257824

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

wgpu/src/backend/webgpu.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,10 +1875,9 @@ impl crate::context::Context for ContextWebGpu {
18751875
let module: &<ContextWebGpu as crate::Context>::ShaderModuleData =
18761876
downcast_ref(desc.vertex.module.data.as_ref());
18771877
let mut mapped_vertex_state = webgpu_sys::GpuVertexState::new(&module.0.module);
1878-
let _ = js_sys::Reflect::set(
1878+
insert_constants_map(
18791879
&mapped_vertex_state,
1880-
&"constants".into(),
1881-
&hashmap_to_jsvalue(desc.vertex.compilation_options.constants),
1880+
desc.vertex.compilation_options.constants,
18821881
);
18831882
mapped_vertex_state.entry_point(desc.vertex.entry_point);
18841883

@@ -1956,11 +1955,7 @@ impl crate::context::Context for ContextWebGpu {
19561955
downcast_ref(frag.module.data.as_ref());
19571956
let mut mapped_fragment_desc =
19581957
webgpu_sys::GpuFragmentState::new(&module.0.module, &targets);
1959-
let _ = js_sys::Reflect::set(
1960-
&mapped_fragment_desc,
1961-
&"constants".into(),
1962-
&hashmap_to_jsvalue(frag.compilation_options.constants),
1963-
);
1958+
insert_constants_map(&mapped_vertex_state, frag.compilation_options.constants);
19641959
mapped_fragment_desc.entry_point(frag.entry_point);
19651960
mapped_desc.fragment(&mapped_fragment_desc);
19661961
}
@@ -1987,11 +1982,7 @@ impl crate::context::Context for ContextWebGpu {
19871982
downcast_ref(desc.module.data.as_ref());
19881983
let mut mapped_compute_stage =
19891984
webgpu_sys::GpuProgrammableStage::new(&shader_module.0.module);
1990-
let _ = js_sys::Reflect::set(
1991-
&mapped_compute_stage,
1992-
&"constants".into(),
1993-
&hashmap_to_jsvalue(desc.compilation_options.constants),
1994-
);
1985+
insert_constants_map(&mapped_compute_stage, desc.compilation_options.constants);
19951986
mapped_compute_stage.entry_point(desc.entry_point);
19961987
let auto_layout = wasm_bindgen::JsValue::from(webgpu_sys::GpuAutoLayoutMode::Auto);
19971988
let mut mapped_desc = webgpu_sys::GpuComputePipelineDescriptor::new(
@@ -3826,12 +3817,25 @@ impl Drop for BufferMappedRange {
38263817
}
38273818
}
38283819

3820+
/// Adds the constants map to the given pipeline descriptor if the map is nonempty.
3821+
/// Logs an error if the map cannot be set.
3822+
fn insert_constants_map(target: &JsValue, map: &HashMap<String, f64>) {
3823+
if !map.is_empty() {
3824+
let result = js_sys::Reflect::set(&target, &"constants".into(), &hashmap_to_jsvalue(map));
3825+
3826+
if let Err(error) = result {
3827+
log::error!("Failed to set constants map for pipeline: {error:?}");
3828+
}
3829+
}
3830+
}
3831+
38293832
/// Converts a hashmap to a Javascript object.
38303833
fn hashmap_to_jsvalue(map: &HashMap<String, f64>) -> JsValue {
38313834
let obj = js_sys::Object::new();
38323835

38333836
for (k, v) in map.iter() {
3834-
let _ = js_sys::Reflect::set(&obj, &k.into(), &(*v).into());
3837+
js_sys::Reflect::set(&obj, &k.into(), &(*v).into())
3838+
.expect("Setting the values in a Javascript map should never fail");
38353839
}
38363840

38373841
JsValue::from(obj)

0 commit comments

Comments
 (0)