From cf60417ecc7a873ad3334be4fb6e76584441aee2 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 6 Dec 2018 16:07:53 -0600 Subject: [PATCH] added bytes_to_u32_vec function using byteorder crate --- examples/Cargo.toml | 1 + examples/src/bin/texture.rs | 15 ++++----------- examples/src/bin/triangle.rs | 14 +++++--------- examples/src/lib.rs | 20 ++++++++++++++++++++ 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 1ff2abf..7cfb9ec 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -7,6 +7,7 @@ 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 f8bc221..6201180 100644 --- a/examples/src/bin/texture.rs +++ b/examples/src/bin/texture.rs @@ -549,19 +549,12 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); - - let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&vertex_bytes) - .build(); + let vertex_code = bytes_to_u32_vec(&vertex_bytes); + 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_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); - - let frag_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&frag_bytes) - .build(); + let frag_code = bytes_to_u32_vec(&frag_bytes); + let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let vertex_shader_module = base .device diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index 612f664..7c79c0f 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -197,18 +197,14 @@ fn main() { .bytes() .filter_map(|byte| byte.ok()) .collect(); - let vertex_bytes = examples::bytes_to_u32_vec(vertex_bytes.iter().cloned()); - let vertex_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&vertex_bytes) - .build(); + let vertex_code = bytes_to_u32_vec(&vertex_bytes); + + 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_bytes = examples::bytes_to_u32_vec(frag_bytes.iter().cloned()); - - let frag_shader_info = vk::ShaderModuleCreateInfo::builder() - .code(&frag_bytes) - .build(); + let frag_code = bytes_to_u32_vec(&frag_bytes); + 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 4c494cd..643421c 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate ash; +extern crate byteorder; #[cfg(target_os = "windows")] extern crate winapi; @@ -35,13 +36,32 @@ 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::{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; +pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec { + let mut output = vec![]; + let mut buffer: [u8; 4] = [0, 0, 0, 0]; + for (i, b) in bytes.iter().enumerate() { + let idx = i % 4; + buffer[idx] = *b; + if idx == 3 { + output.push(Cursor::new(buffer).read_u32::().unwrap()); + 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 {