From 0e531a5d5e39e1d4764cea1aba69f429628d20de 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 +-- wgpu-core/Cargo.toml | 8 ++++---- wgpu-core/src/device/mod.rs | 2 +- wgpu-core/src/pipeline.rs | 2 +- wgpu-hal/Cargo.toml | 13 +++++++------ wgpu-hal/examples/halmark/main.rs | 13 +++++++++++-- wgpu-hal/src/lib.rs | 4 ++-- wgpu/Cargo.toml | 19 ++++++++++--------- wgpu/src/backend/direct.rs | 4 ++-- wgpu/src/lib.rs | 3 ++- 10 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e16d6af4d1e..5f8190f7dd8 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/gfx-rs/naga?rev=48e79388#48e79388b506535d668df4f6c7be4e681812ab81" dependencies = [ "bit-set", "bitflags", diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 7b5b5e4b67b..ab9e0e44c6f 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -41,10 +41,10 @@ smallvec = "1" thiserror = "1" [dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" -features = ["span", "validate", "wgsl-in"] +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" +features = ["clone", "span", "validate", "wgsl-in"] [dependencies.wgt] path = "../wgpu-types" 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/Cargo.toml b/wgpu-hal/Cargo.toml index 8fe4aef58e0..18647b1b5d8 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -95,16 +95,17 @@ js-sys = { version = "0.3" } android_system_properties = "0.1.1" [dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" +features = ["clone"] # DEV dependencies [dev-dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" features = ["wgsl-in"] [dev-dependencies] 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/Cargo.toml b/wgpu/Cargo.toml index 0e52132f7be..38fea26c684 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -139,22 +139,23 @@ pollster = "0.2" env_logger = "0.9" [dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" +features = ["clone"] optional = true # used to test all the example shaders [dev-dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" features = ["wgsl-in"] [target.'cfg(target_arch = "wasm32")'.dependencies.naga] -#git = "https://github.com/gfx-rs/naga" -#rev = "27d38aae" -version = "0.9" +git = "https://github.com/gfx-rs/naga" +rev = "48e79388" +#version = "0.9" features = ["wgsl-out"] [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index cb5ca21b92a..8d664d55f1d 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1104,7 +1104,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(std::borrow::Cow::Owned(module)) } #[cfg(feature = "glsl")] ShaderSource::Glsl { @@ -1120,7 +1120,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(std::borrow::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`].