@@ -1875,10 +1875,9 @@ impl crate::context::Context for ContextWebGpu {
1875
1875
let module: & <ContextWebGpu as crate :: Context >:: ShaderModuleData =
1876
1876
downcast_ref ( desc. vertex . module . data . as_ref ( ) ) ;
1877
1877
let mut mapped_vertex_state = webgpu_sys:: GpuVertexState :: new ( & module. 0 . module ) ;
1878
- let _ = js_sys :: Reflect :: set (
1878
+ insert_constants_map (
1879
1879
& mapped_vertex_state,
1880
- & "constants" . into ( ) ,
1881
- & hashmap_to_jsvalue ( desc. vertex . compilation_options . constants ) ,
1880
+ desc. vertex . compilation_options . constants ,
1882
1881
) ;
1883
1882
mapped_vertex_state. entry_point ( desc. vertex . entry_point ) ;
1884
1883
@@ -1956,11 +1955,7 @@ impl crate::context::Context for ContextWebGpu {
1956
1955
downcast_ref ( frag. module . data . as_ref ( ) ) ;
1957
1956
let mut mapped_fragment_desc =
1958
1957
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 ) ;
1964
1959
mapped_fragment_desc. entry_point ( frag. entry_point ) ;
1965
1960
mapped_desc. fragment ( & mapped_fragment_desc) ;
1966
1961
}
@@ -1987,11 +1982,7 @@ impl crate::context::Context for ContextWebGpu {
1987
1982
downcast_ref ( desc. module . data . as_ref ( ) ) ;
1988
1983
let mut mapped_compute_stage =
1989
1984
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 ) ;
1995
1986
mapped_compute_stage. entry_point ( desc. entry_point ) ;
1996
1987
let auto_layout = wasm_bindgen:: JsValue :: from ( webgpu_sys:: GpuAutoLayoutMode :: Auto ) ;
1997
1988
let mut mapped_desc = webgpu_sys:: GpuComputePipelineDescriptor :: new (
@@ -3826,12 +3817,25 @@ impl Drop for BufferMappedRange {
3826
3817
}
3827
3818
}
3828
3819
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
+
3829
3832
/// Converts a hashmap to a Javascript object.
3830
3833
fn hashmap_to_jsvalue ( map : & HashMap < String , f64 > ) -> JsValue {
3831
3834
let obj = js_sys:: Object :: new ( ) ;
3832
3835
3833
3836
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" ) ;
3835
3839
}
3836
3840
3837
3841
JsValue :: from ( obj)
0 commit comments