From 5bcdd4266eec08d49e16f9d8092180dd3117c270 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:04:01 +0200 Subject: [PATCH] [wgpu-core] use `Fallible` for `ShaderModule` --- wgpu-core/src/device/global.rs | 39 +++++++++++++++++----------------- wgpu-core/src/hub.rs | 2 +- wgpu-core/src/validation.rs | 6 +++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 70cb165c5..455f581fc 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -921,14 +921,14 @@ impl Global { Err(e) => break 'error e, }; - let id = fid.assign(shader); + let id = fid.assign(Fallible::Valid(shader)); api_log!("Device::create_shader_module -> {id:?}"); return (id, None); }; log::error!("Device::create_shader_module error: {error}"); - let id = fid.assign_error(); + let id = fid.assign(Fallible::Invalid(Arc::new(desc.label.to_string()))); (id, Some(error)) } @@ -972,14 +972,14 @@ impl Global { Ok(shader) => shader, Err(e) => break 'error e, }; - let id = fid.assign(shader); + let id = fid.assign(Fallible::Valid(shader)); api_log!("Device::create_shader_module_spirv -> {id:?}"); return (id, None); }; log::error!("Device::create_shader_module_spirv error: {error}"); - let id = fid.assign_error(); + let id = fid.assign(Fallible::Invalid(Arc::new(desc.label.to_string()))); (id, Some(error)) } @@ -989,12 +989,13 @@ impl Global { let hub = &self.hub; - if let Some(shader_module) = hub.shader_modules.unregister(shader_module_id) { - #[cfg(feature = "trace")] + let _shader_module = hub.shader_modules.strict_unregister(shader_module_id); + + #[cfg(feature = "trace")] + if let Ok(shader_module) = _shader_module.get() { if let Some(t) = shader_module.device.trace.lock().as_mut() { t.add(trace::Action::DestroyShaderModule(shader_module_id)); } - drop(shader_module) } } @@ -1241,10 +1242,11 @@ impl Global { let vertex = { let module = hub .shader_modules - .get(desc.vertex.stage.module) - .map_err(|_| pipeline::CreateRenderPipelineError::Stage { + .strict_get(desc.vertex.stage.module) + .get() + .map_err(|e| pipeline::CreateRenderPipelineError::Stage { stage: wgt::ShaderStages::VERTEX, - error: crate::validation::StageError::InvalidModule, + error: e.into(), }); let module = match module { Ok(module) => module, @@ -1266,12 +1268,14 @@ impl Global { }; let fragment = if let Some(ref state) = desc.fragment { - let module = hub.shader_modules.get(state.stage.module).map_err(|_| { - pipeline::CreateRenderPipelineError::Stage { + let module = hub + .shader_modules + .strict_get(state.stage.module) + .get() + .map_err(|e| pipeline::CreateRenderPipelineError::Stage { stage: wgt::ShaderStages::FRAGMENT, - error: crate::validation::StageError::InvalidModule, - } - }); + error: e.into(), + }); let module = match module { Ok(module) => module, Err(e) => break 'error e, @@ -1476,10 +1480,7 @@ impl Global { Err(e) => break 'error e, }; - let module = hub - .shader_modules - .get(desc.stage.module) - .map_err(|_| crate::validation::StageError::InvalidModule); + let module = hub.shader_modules.strict_get(desc.stage.module).get(); let module = match module { Ok(module) => module, Err(e) => break 'error e.into(), diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 7378fcf0d..d937597eb 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -166,7 +166,7 @@ pub struct Hub { pub(crate) devices: Registry>, pub(crate) queues: Registry>, pub(crate) pipeline_layouts: Registry>, - pub(crate) shader_modules: Registry>, + pub(crate) shader_modules: Registry>, pub(crate) bind_group_layouts: Registry>, pub(crate) bind_groups: Registry>, pub(crate) command_buffers: Registry>, diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 1c5f28d90..c1cbdaf18 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -1,4 +1,4 @@ -use crate::{device::bgl, FastHashMap, FastHashSet}; +use crate::{device::bgl, resource::InvalidResourceError, FastHashMap, FastHashSet}; use arrayvec::ArrayVec; use std::{collections::hash_map::Entry, fmt}; use thiserror::Error; @@ -200,8 +200,6 @@ pub enum InputError { #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum StageError { - #[error("Shader module is invalid")] - InvalidModule, #[error( "Shader entry point's workgroup size {current:?} ({current_total} total invocations) must be less or equal to the per-dimension limit {limit:?} and the total invocation limit {total}" )] @@ -241,6 +239,8 @@ pub enum StageError { but no entry point was specified" )] MultipleEntryPointsFound, + #[error(transparent)] + InvalidResource(#[from] InvalidResourceError), } fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option {