Skip to content

Commit 15b795d

Browse files
Use unchecked shaders for better performance (#17767)
# Objective - Wgpu has some expensive code it injects into shaders to avoid the possibility of things like infinite loops. Generally our shaders are written by users who won't do this, so it just makes our shaders perform worse. ## Solution - Turn off the checks. - We could try to conditionally keep them, but that complicates the code and 99.9% of users won't want this. ## Migration Guide - Bevy no longer turns on wgpu's runtime safety checks https://docs.rs/wgpu/latest/wgpu/struct.ShaderRuntimeChecks.html. If you were using Bevy with untrusted shaders, please file an issue. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent 153ce46 commit 15b795d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

crates/bevy_render/src/renderer/render_device.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,27 @@ impl RenderDevice {
6464
})
6565
}
6666
}
67-
_ => self.device.create_shader_module(desc),
67+
// SAFETY: we are interfacing with shader code, which may contain undefined behavior,
68+
// such as indexing out of bounds.
69+
// The checks required are prohibitively expensive and a poor default for game engines.
70+
// TODO: split this method into safe and unsafe variants, and propagate the safety requirements from
71+
// https://docs.rs/wgpu/latest/wgpu/struct.Device.html#method.create_shader_module_trusted to the unsafe form.
72+
_ => unsafe {
73+
self.device
74+
.create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked())
75+
},
6876
}
6977

7078
#[cfg(not(feature = "spirv_shader_passthrough"))]
71-
self.device.create_shader_module(desc)
79+
// SAFETY: we are interfacing with shader code, which may contain undefined behavior,
80+
// such as indexing out of bounds.
81+
// The checks required are prohibitively expensive and a poor default for game engines.
82+
// TODO: split this method into safe and unsafe variants, and propagate the safety requirements from
83+
// https://docs.rs/wgpu/latest/wgpu/struct.Device.html#method.create_shader_module_trusted to the unsafe form.
84+
unsafe {
85+
self.device
86+
.create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked())
87+
}
7288
}
7389

7490
/// Check for resource cleanups and mapping callbacks.

0 commit comments

Comments
 (0)