From 91ca727eb9412012d2a38c1a9c870415b34d989b Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:01:15 +0200 Subject: [PATCH] webgpu: Divide message code into separate files (#32700) * Spilt webgpu ipc messages even more * Add license to mod.rs * file docs --- components/webgpu/ipc_messages/mod.rs | 7 ++ .../{dom_messages.rs => ipc_messages/recv.rs} | 71 +---------------- components/webgpu/ipc_messages/to_dom.rs | 78 +++++++++++++++++++ .../to_script.rs} | 2 +- components/webgpu/lib.rs | 8 +- 5 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 components/webgpu/ipc_messages/mod.rs rename components/webgpu/{dom_messages.rs => ipc_messages/recv.rs} (79%) create mode 100644 components/webgpu/ipc_messages/to_dom.rs rename components/webgpu/{script_messages.rs => ipc_messages/to_script.rs} (96%) diff --git a/components/webgpu/ipc_messages/mod.rs b/components/webgpu/ipc_messages/mod.rs new file mode 100644 index 00000000000..017749d4807 --- /dev/null +++ b/components/webgpu/ipc_messages/mod.rs @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +pub mod recv; +pub mod to_dom; +pub mod to_script; diff --git a/components/webgpu/dom_messages.rs b/components/webgpu/ipc_messages/recv.rs similarity index 79% rename from components/webgpu/dom_messages.rs rename to components/webgpu/ipc_messages/recv.rs index a79aeea4b3d..1d7daa5a976 100644 --- a/components/webgpu/dom_messages.rs +++ b/components/webgpu/ipc_messages/recv.rs @@ -2,7 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -//! IPC massages that are send/received from GPU DOM objects. +//! IPC messages that are received in wgpu thread +//! (usually from script thread more specifically from dom objects) use std::borrow::Cow; @@ -26,77 +27,11 @@ use wgc::resource::{ BufferDescriptor, SamplerDescriptor, TextureDescriptor, TextureViewDescriptor, }; use wgpu_core::command::{RenderPassColorAttachment, RenderPassDepthStencilAttachment}; -use wgpu_core::pipeline::CreateShaderModuleError; pub use {wgpu_core as wgc, wgpu_types as wgt}; use crate::identity::*; use crate::render_commands::RenderCommand; -use crate::{Error, ErrorFilter, PopError, WebGPU, PRESENTATION_BUFFER_COUNT}; - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct ShaderCompilationInfo { - pub line_number: u64, - pub line_pos: u64, - pub offset: u64, - pub length: u64, - pub message: String, -} - -impl ShaderCompilationInfo { - pub fn from(error: &CreateShaderModuleError, source: &str) -> Self { - let location = match error { - CreateShaderModuleError::Parsing(e) => e.inner.location(source), - CreateShaderModuleError::Validation(e) => e.inner.location(source), - _ => None, - }; - - if let Some(location) = location { - // Naga reports locations in UTF-8 code units, but spec requires location in UTF-16 code units - // Based on https://searchfox.org/mozilla-central/rev/5b037d9c6ecdb0729f39ad519f0b867d80a92aad/gfx/wgpu_bindings/src/server.rs#353 - fn len_utf16(s: &str) -> u64 { - s.chars().map(|c| c.len_utf16() as u64).sum() - } - let start = location.offset as usize; - let end = start + location.length as usize; - let line_start = source[0..start].rfind('\n').map(|pos| pos + 1).unwrap_or(0); - Self { - line_number: location.line_number as u64, - line_pos: len_utf16(&source[line_start..start]) + 1, - offset: len_utf16(&source[0..start]), - length: len_utf16(&source[start..end]), - message: error.to_string(), - } - } else { - Self { - message: error.to_string(), - ..Default::default() - } - } - } -} - -#[derive(Debug, Deserialize, Serialize)] -#[allow(clippy::large_enum_variant)] -pub enum WebGPUResponse { - RequestAdapter { - adapter_info: wgt::AdapterInfo, - adapter_id: WebGPUAdapter, - features: wgt::Features, - limits: wgt::Limits, - channel: WebGPU, - }, - RequestDevice { - device_id: WebGPUDevice, - queue_id: WebGPUQueue, - descriptor: wgt::DeviceDescriptor>, - }, - BufferMapAsync(IpcSharedMemory), - SubmittedWorkDone, - PoppedErrorScope(Result, PopError>), - CompilationInfo(Option), -} - -pub type WebGPUResponseResult = Result; +use crate::{Error, ErrorFilter, WebGPUResponseResult, PRESENTATION_BUFFER_COUNT}; #[derive(Debug, Deserialize, Serialize)] pub enum WebGPURequest { diff --git a/components/webgpu/ipc_messages/to_dom.rs b/components/webgpu/ipc_messages/to_dom.rs new file mode 100644 index 00000000000..b43c912cd04 --- /dev/null +++ b/components/webgpu/ipc_messages/to_dom.rs @@ -0,0 +1,78 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! IPC messages that are send to WebGPU DOM objects. + +use ipc_channel::ipc::IpcSharedMemory; +use serde::{Deserialize, Serialize}; +use wgc::pipeline::CreateShaderModuleError; +pub use {wgpu_core as wgc, wgpu_types as wgt}; + +use crate::identity::*; +use crate::{Error, PopError, WebGPU}; + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct ShaderCompilationInfo { + pub line_number: u64, + pub line_pos: u64, + pub offset: u64, + pub length: u64, + pub message: String, +} + +impl ShaderCompilationInfo { + pub fn from(error: &CreateShaderModuleError, source: &str) -> Self { + let location = match error { + CreateShaderModuleError::Parsing(e) => e.inner.location(source), + CreateShaderModuleError::Validation(e) => e.inner.location(source), + _ => None, + }; + + if let Some(location) = location { + // Naga reports locations in UTF-8 code units, but spec requires location in UTF-16 code units + // Based on https://searchfox.org/mozilla-central/rev/5b037d9c6ecdb0729f39ad519f0b867d80a92aad/gfx/wgpu_bindings/src/server.rs#353 + fn len_utf16(s: &str) -> u64 { + s.chars().map(|c| c.len_utf16() as u64).sum() + } + let start = location.offset as usize; + let end = start + location.length as usize; + let line_start = source[0..start].rfind('\n').map(|pos| pos + 1).unwrap_or(0); + Self { + line_number: location.line_number as u64, + line_pos: len_utf16(&source[line_start..start]) + 1, + offset: len_utf16(&source[0..start]), + length: len_utf16(&source[start..end]), + message: error.to_string(), + } + } else { + Self { + message: error.to_string(), + ..Default::default() + } + } + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[allow(clippy::large_enum_variant)] +pub enum WebGPUResponse { + RequestAdapter { + adapter_info: wgt::AdapterInfo, + adapter_id: WebGPUAdapter, + features: wgt::Features, + limits: wgt::Limits, + channel: WebGPU, + }, + RequestDevice { + device_id: WebGPUDevice, + queue_id: WebGPUQueue, + descriptor: wgt::DeviceDescriptor>, + }, + BufferMapAsync(IpcSharedMemory), + SubmittedWorkDone, + PoppedErrorScope(Result, PopError>), + CompilationInfo(Option), +} + +pub type WebGPUResponseResult = Result; diff --git a/components/webgpu/script_messages.rs b/components/webgpu/ipc_messages/to_script.rs similarity index 96% rename from components/webgpu/script_messages.rs rename to components/webgpu/ipc_messages/to_script.rs index 35571bb9242..1f0cd56cf83 100644 --- a/components/webgpu/script_messages.rs +++ b/components/webgpu/ipc_messages/to_script.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -//! IPC massages that are send to script thread (global scope). +//! IPC messages that are send to script thread. use base::id::PipelineId; use serde::{Deserialize, Serialize}; diff --git a/components/webgpu/lib.rs b/components/webgpu/lib.rs index cb2e4f7ebe5..5def9100d1e 100644 --- a/components/webgpu/lib.rs +++ b/components/webgpu/lib.rs @@ -28,13 +28,13 @@ use webrender_traits::{ }; use wgc::id; -mod dom_messages; mod gpu_error; +mod ipc_messages; mod render_commands; -mod script_messages; -pub use dom_messages::*; pub use identity::*; -pub use script_messages::*; +pub use ipc_messages::recv::*; +pub use ipc_messages::to_dom::*; +pub use ipc_messages::to_script::*; pub use wgpu_thread::PRESENTATION_BUFFER_COUNT; #[derive(Clone, Debug, Deserialize, Serialize)]