Skip to content

Commit 9151787

Browse files
use error scope to handle errors on shader module creation
1 parent af24576 commit 9151787

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

crates/bevy_render/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ flate2 = { version = "1.0.22", optional = true }
6666
ruzstd = { version = "0.2.4", optional = true }
6767
# For transcoding of UASTC/ETC1S universal formats, and for .basis file support
6868
basis-universal = { version = "0.2.0", optional = true }
69+
futures-util = "0.3"

crates/bevy_render/src/render_resource/pipeline_cache.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,20 @@ impl ShaderCache {
8585
return Err(RenderPipelineError::AsModuleDescriptorError(err, processed));
8686
}
8787
};
88-
entry.insert(Arc::new(
89-
render_device.create_shader_module(&module_descriptor),
90-
))
88+
89+
render_device
90+
.wgpu_device()
91+
.push_error_scope(wgpu::ErrorFilter::Validation);
92+
let shader_module = render_device.create_shader_module(&module_descriptor);
93+
use futures_util::future::FutureExt;
94+
let error = render_device.wgpu_device().pop_error_scope();
95+
if let Some(Some(wgpu::Error::Validation { description, .. })) =
96+
error.now_or_never()
97+
{
98+
return Err(RenderPipelineError::CreateShaderModule(description));
99+
}
100+
101+
entry.insert(Arc::new(shader_module))
91102
}
92103
};
93104

@@ -226,6 +237,8 @@ pub enum RenderPipelineError {
226237
AsModuleDescriptorError(AsModuleDescriptorError, ProcessedShader),
227238
#[error("Shader import not yet available.")]
228239
ShaderImportNotYetAvailable,
240+
#[error("{0}")]
241+
CreateShaderModule(String),
229242
}
230243

231244
impl RenderPipelineCache {
@@ -304,6 +317,10 @@ impl RenderPipelineCache {
304317
log_shader_error(source, err);
305318
continue;
306319
}
320+
RenderPipelineError::CreateShaderModule(description) => {
321+
error!("failed to create shader module: {}", description);
322+
continue;
323+
}
307324
}
308325
}
309326
}

crates/bevy_render/src/render_resource/shader.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ impl ProcessedShader {
161161
source: match self {
162162
ProcessedShader::Wgsl(source) => {
163163
#[cfg(debug_assertions)]
164-
// This isn't neccessary, but catches errors early during hot reloading of invalid wgsl shaders.
165-
// Eventually, wgpu will have features that will make this unneccessary like compilation info
166-
// or error scopes, but until then parsing the shader twice during development the easiest solution.
164+
// Parse and validate the shader early, so that (e.g. while hot reloading) we can
165+
// display nicely formatted error messages instead of relying on just displaying the error string
166+
// returned by wgpu upon creating the shader module.
167167
let _ = self.reflect()?;
168168

169169
ShaderSource::Wgsl(source.clone())

0 commit comments

Comments
 (0)