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 Aug 29, 2022
1 parent 537c6be commit 0e531a5
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 30 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.

8 changes: 4 additions & 4 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 @@ -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: 7 additions & 6 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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
19 changes: 10 additions & 9 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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 @@ -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 {
Expand All @@ -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")]
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 0e531a5

Please sign in to comment.