From c5718cc04183d1c5cb51454197f8f7eec68a827c Mon Sep 17 00:00:00 2001 From: fuckwit Date: Wed, 20 Nov 2019 06:27:55 +0100 Subject: [PATCH] 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` 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 --- Cargo.toml | 2 +- src/macros.rs | 29 ++--------------------------- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 775e942..e0585b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pixels" description = "A tiny library providing a GPU-powered pixel frame buffer." -version = "0.0.2" +version = "0.0.3" authors = ["Jay Oster "] edition = "2018" repository = "https://github.com/parasyte/pixels" diff --git a/src/macros.rs b/src/macros.rs index f91746a..d39606b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -7,36 +7,11 @@ //! //! 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.) -//! -//! 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_rules! include_spv { ($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(pub T);