From 7246ad74429e2384adaea67d7d8f23ed3f06be02 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Thu, 21 Jul 2022 12:50:38 +0200 Subject: [PATCH] Wrap `ShaderSource::Naga` in `Cow<'static>` --- Cargo.lock | 3 +-- Cargo.toml | 2 ++ deno_webgpu/Cargo.toml | 4 ++++ wgpu-core/src/device/mod.rs | 2 +- wgpu-core/src/pipeline.rs | 2 +- wgpu-hal/examples/halmark/main.rs | 13 +++++++++++-- wgpu-hal/src/lib.rs | 4 ++-- wgpu/src/backend/direct.rs | 6 ++++-- wgpu/src/lib.rs | 3 ++- 9 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e16d6af4d1e..125f8a95824 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1043,8 +1043,7 @@ dependencies = [ [[package]] name = "naga" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f50357e1167a3ab92d6b3c7f4bf5f7fd13fde3f4b28bf0d5ea07b5100fdb6c0" +source = "git+https://github.com/daxpedda/naga?branch=module-clone#707a76da0a7594f78c1c632cfe1037bfaef490a7" dependencies = [ "bit-set", "bitflags", diff --git a/Cargo.toml b/Cargo.toml index 7d3044ae3d9..38a0d56264e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,5 @@ default-members = ["wgpu", "wgpu-hal", "wgpu-info"] #web-sys = { path = "../wasm-bindgen/crates/web-sys" } #js-sys = { path = "../wasm-bindgen/crates/js-sys" } #wasm-bindgen = { path = "../wasm-bindgen" } +# https://github.com/gfx-rs/naga/pull/2013 +naga = { git = "https://github.com/daxpedda/naga", branch = "module-clone" } diff --git a/deno_webgpu/Cargo.toml b/deno_webgpu/Cargo.toml index 3278cfc7da1..6551abdae20 100644 --- a/deno_webgpu/Cargo.toml +++ b/deno_webgpu/Cargo.toml @@ -16,3 +16,7 @@ serde = { version = "1.0", features = ["derive"] } tokio = { version = "1.17", features = ["full"] } wgpu-core = { path = "../wgpu-core", features = ["trace", "replay", "serde"] } wgpu-types = { path = "../wgpu-types", features = ["trace", "replay", "serde"] } + +# https://github.com/gfx-rs/naga/pull/2013 +[patch.crates-io] +naga = { git = "https://github.com/daxpedda/naga", branch = "module-clone" } diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index bc3553f0bbd..ac1dc48acca 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1202,7 +1202,7 @@ impl Device { inner, }) })?; - (module, code.into_owned()) + (Cow::Owned(module), code.into_owned()) } pipeline::ShaderModuleSource::Naga(module) => (module, String::new()), }; diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index ca30e56a2e7..a9c57fdc7e3 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -21,7 +21,7 @@ pub(crate) struct LateSizedBufferGroup { #[allow(clippy::large_enum_variant)] pub enum ShaderModuleSource<'a> { Wgsl(Cow<'a, str>), - Naga(naga::Module), + Naga(Cow<'static, naga::Module>), } #[derive(Clone, Debug)] diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 383efcdc537..a866ca1d23e 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -6,7 +6,13 @@ use hal::{ Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _, }; -use std::{borrow::Borrow, iter, mem, num::NonZeroU32, ptr, time::Instant}; +use std::{ + borrow::{Borrow, Cow}, + iter, mem, + num::NonZeroU32, + ptr, + time::Instant, +}; const MAX_BUNNIES: usize = 1 << 20; const BUNNY_SIZE: f32 = 0.15 * 256.0; @@ -143,7 +149,10 @@ impl Example { ) .validate(&module) .unwrap(); - hal::NagaShader { module, info } + hal::NagaShader { + module: Cow::Owned(module), + info, + } }; let shader_desc = hal::ShaderModuleDescriptor { label: None, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index a343f111bb1..19344cd7eae 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -83,7 +83,7 @@ pub mod api { pub use vulkan::UpdateAfterBindTypes; use std::{ - borrow::Borrow, + borrow::{Borrow, Cow}, fmt, num::{NonZeroU32, NonZeroU8}, ops::{Range, RangeInclusive}, @@ -939,7 +939,7 @@ pub struct CommandEncoderDescriptor<'a, A: Api> { /// Naga shader module. pub struct NagaShader { /// Shader module IR. - pub module: naga::Module, + pub module: Cow<'static, naga::Module>, /// Analysis information of the module. pub info: naga::valid::ModuleInfo, } diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index cb5ca21b92a..c075e27f14a 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -10,6 +10,8 @@ use crate::{ use arrayvec::ArrayVec; use parking_lot::Mutex; use smallvec::SmallVec; +#[cfg(any(feature = "glsl", feature = "spirv"))] +use std::borrow::Cow; use std::{ borrow::Cow::Borrowed, error::Error, @@ -1104,7 +1106,7 @@ impl crate::Context for Context { }; let parser = naga::front::spv::Parser::new(spv.iter().cloned(), &options); let module = parser.parse().unwrap(); - wgc::pipeline::ShaderModuleSource::Naga(module) + wgc::pipeline::ShaderModuleSource::Naga(Cow::Owned(module)) } #[cfg(feature = "glsl")] ShaderSource::Glsl { @@ -1120,7 +1122,7 @@ impl crate::Context for Context { let mut parser = naga::front::glsl::Parser::default(); let module = parser.parse(&options, shader).unwrap(); - wgc::pipeline::ShaderModuleSource::Naga(module) + wgc::pipeline::ShaderModuleSource::Naga(Cow::Owned(module)) } ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)), #[cfg(feature = "naga")] diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 090d67c1f15..673d9d34740 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -814,6 +814,7 @@ impl Drop for ShaderModule { /// /// Any necessary shader translation (e.g. from WGSL to SPIR-V or vice versa) /// will be done internally by wgpu. +#[cfg_attr(feature = "naga", allow(clippy::large_enum_variant))] #[non_exhaustive] pub enum ShaderSource<'a> { /// SPIR-V module represented as a slice of words. @@ -840,7 +841,7 @@ pub enum ShaderSource<'a> { /// Naga module. #[cfg(feature = "naga")] #[cfg_attr(docsrs, doc(cfg(feature = "naga")))] - Naga(naga::Module), + Naga(Cow<'static, naga::Module>), } /// Descriptor for use with [`Device::create_shader_module`].