Skip to content

Commit

Permalink
use error scope to handle errors on shader module creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Mar 20, 2022
1 parent af24576 commit 9151787
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ flate2 = { version = "1.0.22", optional = true }
ruzstd = { version = "0.2.4", optional = true }
# For transcoding of UASTC/ETC1S universal formats, and for .basis file support
basis-universal = { version = "0.2.0", optional = true }
futures-util = "0.3"
23 changes: 20 additions & 3 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,20 @@ impl ShaderCache {
return Err(RenderPipelineError::AsModuleDescriptorError(err, processed));
}
};
entry.insert(Arc::new(
render_device.create_shader_module(&module_descriptor),
))

render_device
.wgpu_device()
.push_error_scope(wgpu::ErrorFilter::Validation);
let shader_module = render_device.create_shader_module(&module_descriptor);
use futures_util::future::FutureExt;
let error = render_device.wgpu_device().pop_error_scope();
if let Some(Some(wgpu::Error::Validation { description, .. })) =
error.now_or_never()
{
return Err(RenderPipelineError::CreateShaderModule(description));
}

entry.insert(Arc::new(shader_module))
}
};

Expand Down Expand Up @@ -226,6 +237,8 @@ pub enum RenderPipelineError {
AsModuleDescriptorError(AsModuleDescriptorError, ProcessedShader),
#[error("Shader import not yet available.")]
ShaderImportNotYetAvailable,
#[error("{0}")]
CreateShaderModule(String),
}

impl RenderPipelineCache {
Expand Down Expand Up @@ -304,6 +317,10 @@ impl RenderPipelineCache {
log_shader_error(source, err);
continue;
}
RenderPipelineError::CreateShaderModule(description) => {
error!("failed to create shader module: {}", description);
continue;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/render_resource/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ impl ProcessedShader {
source: match self {
ProcessedShader::Wgsl(source) => {
#[cfg(debug_assertions)]
// This isn't neccessary, but catches errors early during hot reloading of invalid wgsl shaders.
// Eventually, wgpu will have features that will make this unneccessary like compilation info
// or error scopes, but until then parsing the shader twice during development the easiest solution.
// Parse and validate the shader early, so that (e.g. while hot reloading) we can
// display nicely formatted error messages instead of relying on just displaying the error string
// returned by wgpu upon creating the shader module.
let _ = self.reflect()?;

ShaderSource::Wgsl(source.clone())
Expand Down

0 comments on commit 9151787

Please sign in to comment.