From bb54ae4c5a039b3ae4120d78a7674fe69fc6e41b Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Dec 2018 08:00:55 -0600 Subject: [PATCH] refactored to use util::read_spv --- examples/Cargo.toml | 1 - examples/src/bin/texture.rs | 15 ++++----------- examples/src/bin/triangle.rs | 18 +++++------------- examples/src/lib.rs | 35 ----------------------------------- 4 files changed, 9 insertions(+), 60 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 7cfb9ec..1ff2abf 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -7,7 +7,6 @@ authors = ["maik klein "] winit = "0.16" image = "0.10.4" ash = { path = "../ash" } -byteorder = "1.2.7" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.4", features = ["windef", "winuser"] } diff --git a/examples/src/bin/texture.rs b/examples/src/bin/texture.rs index 89df311..ba1a6f6 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -5,11 +5,9 @@ extern crate image; use std::default::Default; use std::ffi::CString; use std::fs::File; -use std::io::Read; use std::mem::{self, align_of}; use std::os::raw::c_void; use std::path::Path; -use std::ptr; use ash::util::*; use ash::vk; @@ -548,20 +546,15 @@ fn main() { ]; base.device.update_descriptor_sets(&write_desc_sets, &[]); - let vertex_spv_file = + let mut vertex_spv_file = File::open(Path::new("shader/texture/vert.spv")).expect("Could not find vert.spv."); - let frag_spv_file = + let mut frag_spv_file = File::open(Path::new("shader/texture/frag.spv")).expect("Could not find frag.spv."); - let vertex_bytes: Vec = vertex_spv_file - .bytes() - .filter_map(|byte| byte.ok()) - .collect(); - let vertex_code = bytes_to_u32_vec(&vertex_bytes); + 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); - let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_code = bytes_to_u32_vec(&frag_bytes); + 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); let vertex_shader_module = base diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 13f1d84..09a4eb2 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -7,11 +7,9 @@ use examples::*; use std::default::Default; use std::ffi::CString; use std::fs::File; -use std::io::Read; use std::mem; use std::mem::align_of; use std::path::Path; -use std::ptr; #[derive(Clone, Debug, Copy)] struct Vertex { @@ -204,23 +202,17 @@ fn main() { base.device .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .unwrap(); - let vertex_spv_file = + let mut vertex_spv_file = File::open(Path::new("shader/triangle/vert.spv")).expect("Could not find vert.spv."); - let frag_spv_file = + let mut frag_spv_file = File::open(Path::new("shader/triangle/frag.spv")).expect("Could not find frag.spv."); - let vertex_bytes: Vec = vertex_spv_file - .bytes() - .filter_map(|byte| byte.ok()) - .collect(); - - let vertex_code = bytes_to_u32_vec(&vertex_bytes); - + 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); - let frag_bytes: Vec = frag_spv_file.bytes().filter_map(|byte| byte.ok()).collect(); - let frag_code = bytes_to_u32_vec(&frag_bytes); + 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); + let vertex_shader_module = base .device .create_shader_module(&vertex_shader_info, None) diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 7b4d165..daa1ba1 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -1,6 +1,5 @@ #[macro_use] extern crate ash; -extern crate byteorder; #[cfg(target_os = "windows")] extern crate winapi; @@ -36,47 +35,13 @@ use ash::extensions::khr::Win32Surface; use ash::extensions::mvk::MacOSSurface; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; use ash::{vk, Device, Entry, Instance}; -use byteorder::{BigEndian, LittleEndian, ReadBytesExt}; use std::cell::RefCell; use std::default::Default; use std::ffi::{CStr, CString}; -use std::io::Cursor; use std::ops::Drop; use std::os::raw::{c_char, c_void}; use std::ptr; - -const MAGIC_NUMBER: u32 = 0x07230203; - -fn spirv_is_little_endian(bytes: &[u8]) -> bool { - let buffer: [u8; 4] = [bytes[0], bytes[1], bytes[2], bytes[3]]; - let number: u32 = Cursor::new(buffer).read_u32::().unwrap(); - number == MAGIC_NUMBER -} - -pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { - let mut output = vec![]; - let mut buffer: [u8; 4] = [0, 0, 0, 0]; - let is_little_endian = spirv_is_little_endian(bytes); - for (i, b) in bytes.iter().enumerate() { - let idx = i % 4; - buffer[idx] = *b; - if idx == 3 { - let new_word = if is_little_endian { - Cursor::new(buffer).read_u32::().unwrap() - } else { - Cursor::new(buffer).read_u32::().unwrap() - }; - output.push(new_word); - buffer = [0, 0, 0, 0]; - } - } - if bytes.len() % 4 != 0 { - output.push(Cursor::new(buffer).read_u32::().unwrap()); - } - output -} - // Simple offset_of macro akin to C++ offsetof #[macro_export] macro_rules! offset_of {