Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Wrap ShaderSource::Naga in Cow<'static> #2903

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ the same every time it is rendered, we now warn if it is missing.
- 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)
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ impl<A: HalApi> Device<A> {
inner,
})
})?;
(module, code.into_owned())
(Cow::Owned(module), code.into_owned())
}
pipeline::ShaderModuleSource::Naga(module) => (module, String::new()),
};
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,7 +149,10 @@ impl<A: hal::Api> Example<A> {
)
.validate(&module)
.unwrap();
hal::NagaShader { module, info }
hal::NagaShader {
module: Cow::Owned(module),
info,
}
};
let shader_desc = hal::ShaderModuleDescriptor {
label: None,
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,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 {
Expand All @@ -1113,7 +1113,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")]
Expand Down
3 changes: 2 additions & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,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.
Expand All @@ -841,7 +842,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`].
Expand Down