Skip to content

Commit

Permalink
Wrap ShaderSource::Naga in Cow<'static>
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jul 21, 2022
1 parent 537c6be commit 7246ad7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
4 changes: 4 additions & 0 deletions deno_webgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,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
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
6 changes: 4 additions & 2 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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")]
Expand Down
3 changes: 2 additions & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`].
Expand Down

0 comments on commit 7246ad7

Please sign in to comment.