Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper macro for including precompiled SPIR-V #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ impl<'r, T> RawPtr<T> for Option<&'r T> {
}
}

/// Include correctly aligned and typed precompiled SPIR-V
///
/// Does not account for endianness mismatches between the SPIR-V file and the target. See
/// `util::read_spv` for a more general solution.
#[macro_export]
macro_rules! include_spv {
($path:expr) => {
&$crate::util::Align4(*include_bytes!($path)) as &$crate::Spirv
MaikKlein marked this conversation as resolved.
Show resolved Hide resolved
};
}

/// Type returned by `include_spv`, convertible to `&[u32]`
///
/// The definition of this type is unstable.
pub type Spirv = util::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)
}
}
}

#[cfg(test)]
mod tests {
use super::vk;
Expand Down
4 changes: 4 additions & 0 deletions ash/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ pub fn read_spv<R: io::Read + io::Seek>(x: &mut R) -> io::Result<Vec<u32>> {
}
Ok(result)
}

#[repr(align(4))]
#[doc(hidden)]
pub struct Align4<T: ?Sized>(pub T);
16 changes: 5 additions & 11 deletions examples/src/bin/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use ash::util::*;
use ash::vk;
use ash::{include_spv, vk, Spirv};
use examples::*;
use std::default::Default;
use std::ffi::CString;
use std::io::Cursor;
use std::mem;
use std::mem::align_of;

Expand Down Expand Up @@ -198,17 +197,12 @@ fn main() {
base.device
.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0)
.unwrap();
let mut vertex_spv_file =
Cursor::new(&include_bytes!("../../shader/triangle/vert.spv")[..]);
let mut frag_spv_file = Cursor::new(&include_bytes!("../../shader/triangle/frag.spv")[..]);

let vertex_code =
read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file");
let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code);
const VERTEX_CODE: &Spirv = include_spv!("../../shader/triangle/vert.spv");
let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(VERTEX_CODE);

let frag_code =
read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file");
let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code);
const FRAG_CODE: &Spirv = include_spv!("../../shader/triangle/frag.spv");
let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(FRAG_CODE);

let vertex_shader_module = base
.device
Expand Down