From 95408cd48b809044b26696abdf34e16a4d25ba80 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Mon, 19 Sep 2022 06:50:46 +0200 Subject: [PATCH] [BREAKING] Wrap `ShaderSource::Naga` in `Cow<'static>` (#2903) --- CHANGELOG.md | 1 + wgpu-core/Cargo.toml | 2 +- wgpu-core/src/device/mod.rs | 2 +- wgpu-core/src/pipeline.rs | 2 +- wgpu-hal/Cargo.toml | 1 + wgpu-hal/examples/halmark/main.rs | 13 +++++++++++-- wgpu-hal/src/lib.rs | 4 ++-- wgpu/Cargo.toml | 1 + wgpu/src/backend/direct.rs | 4 ++-- wgpu/src/lib.rs | 3 ++- 10 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d88698cc3..dd1493c31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,7 @@ SurfaceConfiguration { - Update Winit to version 0.27 and raw-window-handle to 0.5 by @wyatt-herkamp in [#2918](https://github.com/gfx-rs/wgpu/pull/2918) - Address Clippy 0.1.63 complaints. By @jimblandy in [#2977](https://github.com/gfx-rs/wgpu/pull/2977) - Don't use `PhantomData` for `IdentityManager`'s `Input` type. By @jimblandy in [#2972](https://github.com/gfx-rs/wgpu/pull/2972) +- Changed Naga variant in ShaderSource to `Cow<'static, Module>`, to allow loading global variables by @daxpedda in [#2903](https://github.com/gfx-rs/wgpu/pull/2903) #### Metal - Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826) diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 57db15e47..ddadf433a 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -47,7 +47,7 @@ thiserror = "1" git = "https://github.com/gfx-rs/naga" rev = "b209d911" version = "0.9" -features = ["span", "validate", "wgsl-in"] +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 0142265fb..1f2363e21 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1216,7 +1216,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 f9d500178..2b0e0804a 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 49e05cb2b..a8cfe5e4a 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -98,6 +98,7 @@ android_system_properties = "0.1.1" git = "https://github.com/gfx-rs/naga" rev = "b209d911" version = "0.9" +features = ["clone"] # DEV dependencies diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index ac88bc86d..b023a8641 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -7,7 +7,13 @@ use hal::{ }; use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; -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; @@ -148,7 +154,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 90f358ccb..2d5806323 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}, @@ -940,7 +940,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 01e242566..36754a868 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -143,6 +143,7 @@ env_logger = "0.9" git = "https://github.com/gfx-rs/naga" rev = "b209d911" version = "0.9" +features = ["clone"] optional = true # used to test all the example shaders diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 4fe977fdc..8ec8bc6f6 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1116,7 +1116,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 { @@ -1132,7 +1132,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 f79367857..9bdcd9d32 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -835,6 +835,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. @@ -861,7 +862,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>), } static_assertions::assert_impl_all!(ShaderSource: Send, Sync);