From 15b795d7d6a97fd4006dd2c1233050662327dcce Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Tue, 11 Feb 2025 22:16:52 -0800 Subject: [PATCH] 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 --- .../bevy_render/src/renderer/render_device.rs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/renderer/render_device.rs b/crates/bevy_render/src/renderer/render_device.rs index 8cde892f68..2761a94e1c 100644 --- a/crates/bevy_render/src/renderer/render_device.rs +++ b/crates/bevy_render/src/renderer/render_device.rs @@ -64,11 +64,27 @@ impl RenderDevice { }) } } - _ => self.device.create_shader_module(desc), + // SAFETY: we are interfacing with shader code, which may contain undefined behavior, + // such as indexing out of bounds. + // The checks required are prohibitively expensive and a poor default for game engines. + // TODO: split this method into safe and unsafe variants, and propagate the safety requirements from + // https://docs.rs/wgpu/latest/wgpu/struct.Device.html#method.create_shader_module_trusted to the unsafe form. + _ => unsafe { + self.device + .create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked()) + }, } #[cfg(not(feature = "spirv_shader_passthrough"))] - self.device.create_shader_module(desc) + // SAFETY: we are interfacing with shader code, which may contain undefined behavior, + // such as indexing out of bounds. + // The checks required are prohibitively expensive and a poor default for game engines. + // TODO: split this method into safe and unsafe variants, and propagate the safety requirements from + // https://docs.rs/wgpu/latest/wgpu/struct.Device.html#method.create_shader_module_trusted to the unsafe form. + unsafe { + self.device + .create_shader_module_trusted(desc, wgpu::ShaderRuntimeChecks::unchecked()) + } } /// Check for resource cleanups and mapping callbacks.