diff --git a/deno_webgpu/device.rs b/deno_webgpu/device.rs index cd9cf95d4..dfd7559a0 100644 --- a/deno_webgpu/device.rs +++ b/deno_webgpu/device.rs @@ -481,8 +481,11 @@ impl GPUDevice { )), ); - let compilation_info = - GPUCompilationInfo::new(scope, err.iter(), &descriptor.code); + let compilation_info = GPUCompilationInfo::new( + scope, + wgpu_shader_module.compilation_info(), + &descriptor.code, + ); let compilation_info = make_cppgc_object(scope, compilation_info); let compilation_info = v8::Global::new(scope, compilation_info); self.error_handler.push_error(err); diff --git a/deno_webgpu/shader.rs b/deno_webgpu/shader.rs index d05a7b2dc..011e74029 100644 --- a/deno_webgpu/shader.rs +++ b/deno_webgpu/shader.rs @@ -8,7 +8,6 @@ use deno_core::v8; use deno_core::webidl::WebIdlInterfaceConverter; use deno_core::GarbageCollected; use deno_core::WebIDL; -use wgpu_core::pipeline; use crate::error::GPUGenericError; @@ -123,16 +122,10 @@ impl GPUCompilationMessage { } impl GPUCompilationMessage { - fn new(error: &pipeline::CreateShaderModuleError, source: &str) -> Self { - let message = error.to_string(); + fn new(error: &wgpu_types::CompilationMessage, source: &str) -> Self { + let message = error.message.clone(); - let loc = match error { - pipeline::CreateShaderModuleError::Parsing(e) => e.inner.location(source), - pipeline::CreateShaderModuleError::Validation(e) => { - e.inner.location(source) - } - _ => None, - }; + let loc = error.location.as_ref(); match loc { Some(loc) => { @@ -188,13 +181,14 @@ impl GPUCompilationInfo { impl GPUCompilationInfo { pub fn new<'args, 'scope>( scope: &mut v8::HandleScope<'scope>, - messages: impl ExactSizeIterator< - Item = &'args pipeline::CreateShaderModuleError, - >, + compilation_info: &wgpu_types::CompilationInfo, source: &'args str, ) -> Self { - let array = v8::Array::new(scope, messages.len().try_into().unwrap()); - for (i, message) in messages.enumerate() { + let array = v8::Array::new( + scope, + compilation_info.messages.len().try_into().unwrap(), + ); + for (i, message) in compilation_info.messages.iter().enumerate() { let message_object = make_cppgc_object(scope, GPUCompilationMessage::new(message, source)); array.set_index(scope, i.try_into().unwrap(), message_object.into()); diff --git a/wgpu-core-remote/src/global/device.rs b/wgpu-core-remote/src/global/device.rs index aec646d32..20db3ac95 100644 --- a/wgpu-core-remote/src/global/device.rs +++ b/wgpu-core-remote/src/global/device.rs @@ -654,6 +654,15 @@ impl Global { (id, error) } + pub fn shader_module_compilation_info( + &self, + shader_module_id: id::ShaderModuleId, + ) -> wgt::CompilationInfo { + let hub = self.hub.borrow(); + let shader_module = hub.shader_modules.get(shader_module_id); + shader_module.compilation_info().clone() + } + pub fn shader_module_remove( &self, shader_module_id: id::ShaderModuleId, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index b667a8b54..92336f364 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -46,7 +46,7 @@ use crate::{ }, instance::{Adapter, RequestDeviceError}, lock::{rank, Mutex, RwLock}, - pipeline::{self, ColorStateError}, + pipeline::{self, shader_module_error_into_compilation_info, ColorStateError}, pool::ResourcePool, resource::{ self, Buffer, BufferState, ExternalTexture, ExternalTextureState, Labeled, ParentDevice, @@ -2446,8 +2446,11 @@ impl Device { let (shader, error) = match self.create_shader_module_inner(desc, source) { Ok(shader) => (shader, None), Err(e) => { - let shader = - pipeline::ShaderModule::invalid(Arc::clone(self), desc.label.to_string()); + let shader = pipeline::ShaderModule::invalid( + Arc::clone(self), + desc.label.to_string(), + shader_module_error_into_compilation_info(&e), + ); (shader, Some(e)) } }; @@ -2601,6 +2604,7 @@ impl Device { }), device: self.clone(), label: desc.label.to_string(), + compilation_info: wgt::CompilationInfo::default(), }; let module = Arc::new(module); @@ -2624,8 +2628,11 @@ impl Device { let (shader, error) = match unsafe { self.create_shader_module_passthrough_inner(desc) } { Ok(shader) => (shader, None), Err(e) => { - let shader = - pipeline::ShaderModule::invalid(Arc::clone(self), desc.label.to_string()); + let shader = pipeline::ShaderModule::invalid( + Arc::clone(self), + desc.label.to_string(), + shader_module_error_into_compilation_info(&e), + ); (shader, Some(e)) } }; @@ -2762,6 +2769,7 @@ impl Device { }), device: self.clone(), label: descriptor.label.to_string(), + compilation_info: wgt::CompilationInfo::default(), }; Ok(Arc::new(module)) diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index d11069f45..b21a6560e 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -1,3 +1,4 @@ +use alloc::string::ToString as _; use alloc::{ borrow::{Cow, ToOwned}, boxed::Box, @@ -78,6 +79,7 @@ pub struct ShaderModule { pub(crate) device: Arc, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, + pub(crate) compilation_info: wgt::CompilationInfo, } impl Drop for ShaderModule { @@ -116,14 +118,23 @@ impl ShaderModule { Ok(state) } - pub(crate) fn invalid(device: Arc, label: String) -> Arc { + pub(crate) fn invalid( + device: Arc, + label: String, + compilation_info: wgt::CompilationInfo, + ) -> Arc { Arc::new(Self { state: ResourceState::Invalid, device, label, + compilation_info, }) } + pub fn compilation_info(&self) -> &wgt::CompilationInfo { + &self.compilation_info + } + /// Select an entry point name, given an optional name and a shader stage. /// /// This function takes care of turning the `Option<&str>` @@ -243,6 +254,113 @@ impl WebGpuError for CreateShaderModuleError { } } +#[cfg(feature = "wgsl")] +pub(crate) fn wgsl_to_compilation_info( + value: &ShaderError, +) -> wgt::CompilationInfo { + use alloc::{string::ToString, vec}; + wgt::CompilationInfo { + messages: vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: value + .inner + .location(&value.source) + .as_ref() + .map(naga_to_source_location), + }], + } +} +#[cfg(feature = "glsl")] +pub(crate) fn glsl_to_compilation_info( + value: &ShaderError, +) -> wgt::CompilationInfo { + use alloc::string::ToString; + let messages = value + .inner + .errors + .iter() + .map(|err| wgt::CompilationMessage { + message: err.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: err + .location(&value.source) + .as_ref() + .map(naga_to_source_location), + }) + .collect(); + wgt::CompilationInfo { messages } +} + +#[cfg(feature = "spirv")] +pub(crate) fn spirv_to_compilation_info( + value: &ShaderError, +) -> wgt::CompilationInfo { + use alloc::{string::ToString, vec}; + wgt::CompilationInfo { + messages: vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: None, + }], + } +} + +pub(crate) fn naga_to_compilation_info( + value: &ShaderError>, +) -> wgt::CompilationInfo { + use alloc::{string::ToString, vec}; + wgt::CompilationInfo { + messages: vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: value + .inner + .location(&value.source) + .as_ref() + .map(naga_to_source_location), + }], + } +} + +fn naga_to_source_location(value: &naga::SourceLocation) -> wgt::SourceLocation { + wgt::SourceLocation { + length: value.length, + offset: value.offset, + line_number: value.line_number, + line_position: value.line_position, + } +} + +pub(crate) fn shader_module_error_into_compilation_info( + value: &CreateShaderModuleError, +) -> wgt::CompilationInfo { + match value { + #[cfg(feature = "wgsl")] + CreateShaderModuleError::Parsing(v) => wgsl_to_compilation_info(v), + #[cfg(feature = "glsl")] + CreateShaderModuleError::ParsingGlsl(v) => glsl_to_compilation_info(v), + #[cfg(feature = "spirv")] + CreateShaderModuleError::ParsingSpirV(v) => spirv_to_compilation_info(v), + CreateShaderModuleError::Validation(v) => naga_to_compilation_info(v), + // Device errors are reported through the error sink, and are not compilation errors. + // Same goes for native shader module generation errors. + CreateShaderModuleError::Device(_) | CreateShaderModuleError::Generation => { + wgt::CompilationInfo { + messages: Vec::new(), + } + } + // Everything else is an error message without location information. + _ => wgt::CompilationInfo { + messages: alloc::vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: None, + }], + }, + } +} + /// Describes a programmable pipeline stage. #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/wgpu/src/api/shader_module.rs b/wgpu/src/api/shader_module.rs index 75ac2f513..ce4e799ce 100644 --- a/wgpu/src/api/shader_module.rs +++ b/wgpu/src/api/shader_module.rs @@ -33,83 +33,6 @@ impl ShaderModule { } } -#[cfg(all(feature = "wgsl", wgpu_core))] -pub(crate) fn wgsl_to_compilation_info( - value: crate::naga::error::ShaderError, -) -> CompilationInfo { - use alloc::{string::ToString, vec}; - CompilationInfo { - messages: vec![CompilationMessage { - message: value.to_string(), - message_type: CompilationMessageType::Error, - location: value - .inner - .location(&value.source) - .map(naga_to_source_location), - }], - } -} -#[cfg(feature = "glsl")] -pub(crate) fn glsl_to_compilation_info( - value: naga::error::ShaderError, -) -> CompilationInfo { - use alloc::string::ToString; - let messages = value - .inner - .errors - .into_iter() - .map(|err| CompilationMessage { - message: err.to_string(), - message_type: CompilationMessageType::Error, - location: err.location(&value.source).map(naga_to_source_location), - }) - .collect(); - CompilationInfo { messages } -} - -#[cfg(feature = "spirv")] -pub(crate) fn spirv_to_compilation_info( - value: naga::error::ShaderError, -) -> CompilationInfo { - use alloc::{string::ToString, vec}; - CompilationInfo { - messages: vec![CompilationMessage { - message: value.to_string(), - message_type: CompilationMessageType::Error, - location: None, - }], - } -} - -#[cfg(any(wgpu_core, naga))] -pub(crate) fn naga_to_compilation_info( - value: crate::naga::error::ShaderError< - crate::naga::WithSpan, - >, -) -> CompilationInfo { - use alloc::{string::ToString, vec}; - CompilationInfo { - messages: vec![CompilationMessage { - message: value.to_string(), - message_type: CompilationMessageType::Error, - location: value - .inner - .location(&value.source) - .map(naga_to_source_location), - }], - } -} - -#[cfg(any(wgpu_core, naga))] -fn naga_to_source_location(value: crate::naga::SourceLocation) -> SourceLocation { - SourceLocation { - length: value.length, - offset: value.offset, - line_number: value.line_number, - line_position: value.line_position, - } -} - /// Source of a shader module. /// /// The source will be parsed and validated. diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 36aa446e4..0c256cee2 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1972,6 +1972,67 @@ impl WebDevice { } } +#[cfg(feature = "glsl")] +pub(crate) fn glsl_to_compilation_info( + value: naga::error::ShaderError, +) -> wgt::CompilationInfo { + use alloc::string::ToString; + let messages = value + .inner + .errors + .into_iter() + .map(|err| wgt::CompilationMessage { + message: err.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: err.location(&value.source).map(naga_to_source_location), + }) + .collect(); + wgt::CompilationInfo { messages } +} + +#[cfg(feature = "spirv")] +pub(crate) fn spirv_to_compilation_info( + value: naga::error::ShaderError, +) -> wgt::CompilationInfo { + use alloc::{string::ToString, vec}; + wgt::CompilationInfo { + messages: vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: None, + }], + } +} + +#[cfg(naga)] +pub(crate) fn naga_to_compilation_info( + value: crate::naga::error::ShaderError< + crate::naga::WithSpan, + >, +) -> wgt::CompilationInfo { + use alloc::{string::ToString, vec}; + wgt::CompilationInfo { + messages: vec![wgt::CompilationMessage { + message: value.to_string(), + message_type: wgt::CompilationMessageType::Error, + location: value + .inner + .location(&value.source) + .map(naga_to_source_location), + }], + } +} + +#[cfg(naga)] +fn naga_to_source_location(value: crate::naga::SourceLocation) -> wgt::SourceLocation { + wgt::SourceLocation { + length: value.length, + offset: value.offset, + line_number: value.line_number, + line_position: value.line_position, + } +} + impl dispatch::DeviceInterface for WebDevice { fn features(&self) -> crate::Features { map_wgt_features(self.inner.features()) @@ -2004,7 +2065,7 @@ impl dispatch::DeviceInterface for WebDevice { spv_parser .parse() .map_err(|inner| { - crate::spirv_to_compilation_info(naga::error::ShaderError { + spirv_to_compilation_info(naga::error::ShaderError { source: String::new(), label: desc.label.map(|s| s.to_string()), inner: Box::new(inner), @@ -2041,7 +2102,7 @@ impl dispatch::DeviceInterface for WebDevice { parser .parse(&options, shader) .map_err(|inner| { - crate::glsl_to_compilation_info(naga::error::ShaderError { + glsl_to_compilation_info(naga::error::ShaderError { source: shader.to_string(), label: desc.label.map(|s| s.to_string()), inner: Box::new(inner), @@ -2094,7 +2155,7 @@ impl dispatch::DeviceInterface for WebDevice { let mut validator = valid::Validator::new(valid::ValidationFlags::all(), valid::Capabilities::all()); let module_info = validator.validate(module).map_err(|err| { - crate::naga_to_compilation_info(naga::error::ShaderError { + naga_to_compilation_info(naga::error::ShaderError { source: source.to_string(), label: desc.label.map(|s| s.to_string()), inner: err, diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 09248cb68..782343fac 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -2,9 +2,8 @@ use alloc::borrow::ToOwned; use alloc::{ borrow::Cow::{self, Borrowed}, boxed::Box, - string::{String, ToString as _}, + string::String, sync::Arc, - vec, vec::Vec, }; use core::{ @@ -21,15 +20,14 @@ use wgt::error::WebGpuError; use arrayvec::ArrayVec; use smallvec::SmallVec; -use wgc::{pipeline::CreateShaderModuleError, resource::BlasPrepareCompactResult}; +use wgc::resource::BlasPrepareCompactResult; use wgt::WasmNotSendSync; use crate::{ api, dispatch::{self, BlasCompactCallback, BufferMappedRangeInterface}, - BindingResource, Blas, BufferBinding, BufferDescriptor, CompilationInfo, CompilationMessage, - CompilationMessageType, Features, LoadOp, MapMode, Operations, ShaderSource, - SurfaceTargetUnsafe, TextureDescriptor, Tlas, WriteOnly, + BindingResource, Blas, BufferBinding, BufferDescriptor, Features, LoadOp, MapMode, Operations, + ShaderSource, SurfaceTargetUnsafe, TextureDescriptor, Tlas, WriteOnly, }; use crate::{dispatch::DispatchAdapter, util::Mutex}; @@ -281,7 +279,6 @@ impl CoreBuffer { #[derive(Debug, Clone)] pub struct CoreShaderModule { pub(crate) wgpu_shader_module: Arc, - compilation_info: CompilationInfo, } #[derive(Debug, Clone)] @@ -479,33 +476,6 @@ impl fmt::Debug for CoreSurfaceOutputDetail { } } -fn shader_module_error_into_compilation_info(value: CreateShaderModuleError) -> CompilationInfo { - match value { - #[cfg(feature = "wgsl")] - CreateShaderModuleError::Parsing(v) => crate::wgsl_to_compilation_info(v), - #[cfg(feature = "glsl")] - CreateShaderModuleError::ParsingGlsl(v) => crate::glsl_to_compilation_info(v), - #[cfg(feature = "spirv")] - CreateShaderModuleError::ParsingSpirV(v) => crate::spirv_to_compilation_info(v), - CreateShaderModuleError::Validation(v) => crate::naga_to_compilation_info(v), - // Device errors are reported through the error sink, and are not compilation errors. - // Same goes for native shader module generation errors. - CreateShaderModuleError::Device(_) | CreateShaderModuleError::Generation => { - CompilationInfo { - messages: Vec::new(), - } - } - // Everything else is an error message without location information. - _ => CompilationInfo { - messages: vec![CompilationMessage { - message: value.to_string(), - message_type: CompilationMessageType::Error, - location: None, - }], - }, - } -} - #[derive(Debug)] pub struct CoreQueueWriteBuffer { wgpu_staging_buffer: wgc::resource::StagingBuffer, @@ -827,23 +797,15 @@ impl dispatch::DeviceInterface for CoreDevice { }; let (wgpu_shader_module, error) = self.wgpu_device.create_shader_module(&descriptor, source); - let compilation_info = match error { - Some(cause) => { - self.wgpu_device.handle_error( - cause.clone(), - desc.label, - "Device::create_shader_module", - ); - shader_module_error_into_compilation_info(cause) - } - None => CompilationInfo { messages: vec![] }, - }; - - CoreShaderModule { - wgpu_shader_module, - compilation_info, + if let Some(cause) = error { + self.wgpu_device.handle_error( + cause.clone(), + desc.label, + "Device::create_shader_module", + ); } - .into() + + CoreShaderModule { wgpu_shader_module }.into() } unsafe fn create_shader_module_passthrough( @@ -853,24 +815,15 @@ impl dispatch::DeviceInterface for CoreDevice { let desc = desc.map_label(|l| l.map(Cow::from)); let (wgpu_shader_module, error) = unsafe { self.wgpu_device.create_shader_module_passthrough(&desc) }; - - let compilation_info = match error { - Some(cause) => { - self.wgpu_device.handle_error( - cause.clone(), - desc.label.as_deref(), - "Device::create_shader_module_passthrough", - ); - shader_module_error_into_compilation_info(cause) - } - None => CompilationInfo { messages: vec![] }, - }; - - CoreShaderModule { - wgpu_shader_module, - compilation_info, + if let Some(cause) = error { + self.wgpu_device.handle_error( + cause.clone(), + desc.label.as_deref(), + "Device::create_shader_module_passthrough", + ); } - .into() + + CoreShaderModule { wgpu_shader_module }.into() } fn create_bind_group_layout( @@ -1649,7 +1602,7 @@ impl dispatch::QueueInterface for CoreQueue { impl dispatch::ShaderModuleInterface for CoreShaderModule { fn get_compilation_info(&self) -> Pin> { - Box::pin(ready(self.compilation_info.clone())) + Box::pin(ready(self.wgpu_shader_module.compilation_info().clone())) } }