From dcd5b4675dce1cc778153f6cb49aa4c32ee2c74e Mon Sep 17 00:00:00 2001 From: Andrea Nardi Date: Sun, 19 Jul 2020 18:10:59 +0200 Subject: [PATCH] Added Static Lifetime to Statically Loaded SPIR-V Modules Signed-off-by: Andrea Nardi --- src/backend/direct.rs | 4 ++-- src/backend/web.rs | 2 +- src/lib.rs | 5 +++-- src/macros.rs | 2 +- src/util/mod.rs | 35 +++++++++++++++++++++++++++-------- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/backend/direct.rs b/src/backend/direct.rs index 2bf23e81e..bd9a2ae03 100644 --- a/src/backend/direct.rs +++ b/src/backend/direct.rs @@ -550,8 +550,8 @@ impl crate::Context for Context { source: ShaderModuleSource, ) -> Self::ShaderModuleId { let desc = match source { - ShaderModuleSource::SpirV(spv) => wgc::pipeline::ShaderModuleSource::SpirV(spv), - ShaderModuleSource::Wgsl(code) => wgc::pipeline::ShaderModuleSource::Wgsl(code), + ShaderModuleSource::SpirV(ref spv) => wgc::pipeline::ShaderModuleSource::SpirV(spv), + ShaderModuleSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(code), }; gfx_select!(*device => self.device_create_shader_module(*device, desc, PhantomData)) } diff --git a/src/backend/web.rs b/src/backend/web.rs index b3df3023b..c353452d6 100644 --- a/src/backend/web.rs +++ b/src/backend/web.rs @@ -815,7 +815,7 @@ impl crate::Context for Context { ) -> Self::ShaderModuleId { let desc = match source { ShaderModuleSource::SpirV(spv) => { - web_sys::GpuShaderModuleDescriptor::new(&js_sys::Uint32Array::from(spv)) + web_sys::GpuShaderModuleDescriptor::new(&js_sys::Uint32Array::from(&*spv)) } ShaderModuleSource::Wgsl(_code) => { panic!("WGSL is not yet supported by the Web backend") diff --git a/src/lib.rs b/src/lib.rs index 5076b453b..be6e9bf2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod util; mod macros; use std::{ + borrow::Cow, error::Error, fmt::{Debug, Display}, future::Future, @@ -620,14 +621,14 @@ pub enum ShaderModuleSource<'a> { /// /// wgpu will attempt to parse and validate it, but the original binary /// is passed to `gfx-rs` and `spirv_cross` for translation. - SpirV(&'a [u32]), + SpirV(Cow<'a, [u32]>), /// WGSL module as a string slice. /// /// wgpu-rs will parse it and use for validation. It will attempt /// to build a SPIR-V module internally and panic otherwise. /// /// Note: WGSL is not yet supported on the Web. - Wgsl(&'a str), + Wgsl(Cow<'a, str>), } /// Handle to a pipeline layout. diff --git a/src/macros.rs b/src/macros.rs index 17bb4e8fd..0009fd64a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -144,6 +144,6 @@ fn test_vertex_attr_array() { #[macro_export] macro_rules! include_spirv { ($($token:tt)*) => { - $crate::util::make_spirv(&$crate::util::WordAligned(*include_bytes!($($token)*)).0) + $crate::util::make_spirv(include_bytes!($($token)*)) }; } diff --git a/src/util/mod.rs b/src/util/mod.rs index 7a8ef5bb1..caf9eba17 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -2,31 +2,50 @@ mod belt; +use std::{ + borrow::Cow, + mem::{align_of, size_of}, + ptr::copy_nonoverlapping, +}; + #[cfg(all(not(target_arch = "wasm32"), feature = "subscriber"))] pub use wgc::logging::subscriber::{initialize_default_subscriber, ChromeTracingLayer}; pub use belt::StagingBelt; -/// Wrapper aligning contents to at least 4. -#[repr(align(4))] -pub struct WordAligned(pub Bytes); - /// Treat the given byte slice as a SPIR-V module. /// /// # Panic /// /// This function panics if: /// -/// - Input isn't aligned to 4 bytes /// - Input length isn't multiple of 4 /// - Input is longer than [`usize::max_value`] /// - SPIR-V magic number is missing from beginning of stream pub fn make_spirv<'a>(data: &'a [u8]) -> super::ShaderModuleSource<'a> { const MAGIC_NUMBER: u32 = 0x0723_0203; - let (pre, words, post) = unsafe { data.align_to::() }; - assert_eq!(pre, &[], "data offset is not aligned to words!"); - assert_eq!(post, &[], "data size is not aligned to words!"); + assert_eq!( + data.len() % size_of::(), + 0, + "data size is not a multiple of 4" + ); + + //If the data happens to be aligned, directly use the byte array, + // otherwise copy the byte array in an owned vector and use that instead. + let words = if data.as_ptr().align_offset(align_of::()) == 0 { + let (pre, words, post) = unsafe { data.align_to::() }; + debug_assert_eq!(pre, &[]); + debug_assert_eq!(post, &[]); + Cow::from(words) + } else { + let mut words = vec![0u32; data.len() / size_of::()]; + unsafe { + copy_nonoverlapping(data.as_ptr(), words.as_mut_ptr() as *mut u8, data.len()); + } + Cow::from(words) + }; + assert_eq!( words[0], MAGIC_NUMBER, "wrong magic word {:x}. Make sure you are using a binary SPIRV file.",