Added Static Lifetime to Statically Loaded SPIR-V Modules

Signed-off-by: Andrea Nardi <buongiorno19972@gmail.com>
This commit is contained in:
Andrea Nardi committed 2020-07-20 19:35:34 +02:00
1 parent 8e4d001586
commit dcd5b4675d
5 files changed
+34 -14

No files matched your search

+2 -2
View File
@@ -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))
}
+1 -1
View File
@@ -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")
+3 -2
View File
@@ -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.
+1 -1
View File
@@ -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)*))
};
}
+27 -8
View File
@@ -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<Bytes: ?Sized>(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::<u32>() };
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::<u32>(),
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::<u32>()) == 0 {
let (pre, words, post) = unsafe { data.align_to::<u32>() };
debug_assert_eq!(pre, &[]);
debug_assert_eq!(post, &[]);
Cow::from(words)
} else {
let mut words = vec![0u32; data.len() / size_of::<u32>()];
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.",