Remove Unsafe in macro.rs (#43)
* remove unsafe slice::from_raw_parts this should be a way to remove the unsafe code in the Spirv Deref impl. Not sure how it affects performance yet. I will take a look at the bytecode and see if maybe the compiler is smart enough to optimize that away. * change renderers to accomodate new Deref Let the deref return a `Vec<u32>` instead of a slice reference. This should not hurt performance too much as this will be done a finite amount of times att startup. * replaced deref with normal impl * fixed endianness, fixed offset + rustfmt * bump version * fixed clippy lints * respect the target endianness * use wgpu::read_spirv() * remove an unneccessary allocation of a vec * move borrow into macro
This commit is contained in:
parent
bb898d78de
commit
c5718cc041
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "pixels"
|
name = "pixels"
|
||||||
description = "A tiny library providing a GPU-powered pixel frame buffer."
|
description = "A tiny library providing a GPU-powered pixel frame buffer."
|
||||||
version = "0.0.2"
|
version = "0.0.3"
|
||||||
authors = ["Jay Oster <jay@kodewerx.org>"]
|
authors = ["Jay Oster <jay@kodewerx.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
repository = "https://github.com/parasyte/pixels"
|
repository = "https://github.com/parasyte/pixels"
|
||||||
|
|
|
@ -7,36 +7,11 @@
|
||||||
//!
|
//!
|
||||||
//! This macro moves all of that complexity to build-time. At least for the SPIR-V part of the
|
//! This macro moves all of that complexity to build-time. At least for the SPIR-V part of the
|
||||||
//! shader pipeline. (`gfx-hal` backends have their own SPIR-V-to-native compilers at runtime.)
|
//! shader pipeline. (`gfx-hal` backends have their own SPIR-V-to-native compilers at runtime.)
|
||||||
//!
|
|
||||||
//! Cribbed with permission from Ralith
|
|
||||||
//! See: https://github.com/MaikKlein/ash/pull/245
|
|
||||||
|
|
||||||
/// Include correctly aligned and typed precompiled SPIR-V
|
|
||||||
///
|
|
||||||
/// Does not account for endianness mismatches between the SPIR-V file and the target. See
|
|
||||||
/// [`wgpu::read_spirv`] for a more general solution.
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! include_spv {
|
macro_rules! include_spv {
|
||||||
($path:expr) => {
|
($path:expr) => {
|
||||||
&$crate::Align4(*include_bytes!($path)) as &$crate::Spirv
|
&wgpu::read_spirv(std::io::Cursor::new(&include_bytes!($path)[..]))
|
||||||
|
.expect(&format!("Invalid SPIR-V shader in file: {}", $path))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type returned by `include_spv`, convertible to `&[u32]`
|
|
||||||
///
|
|
||||||
/// The definition of this type is unstable.
|
|
||||||
pub type Spirv = Align4<[u8]>;
|
|
||||||
|
|
||||||
impl std::ops::Deref for Spirv {
|
|
||||||
type Target = [u32];
|
|
||||||
fn deref(&self) -> &[u32] {
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
unsafe {
|
|
||||||
std::slice::from_raw_parts(self.0.as_ptr() as *const u32, self.0.len() / 4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(align(4))]
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct Align4<T: ?Sized>(pub T);
|
|
||||||
|
|
Loading…
Reference in a new issue