added bytes_to_u32_vec function using byteorder crate

This commit is contained in:
colin 2018-12-06 16:07:53 -06:00
parent 6f4259ee36
commit cf60417ecc
4 changed files with 30 additions and 20 deletions

View file

@ -7,6 +7,7 @@ authors = ["maik klein <maikklein@googlemail.com>"]
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"] }

View file

@ -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<u8> = 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

View file

@ -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<u8> = 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)

View file

@ -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<u32> {
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::<LittleEndian>().unwrap());
buffer = [0, 0, 0, 0];
}
}
if bytes.len() % 4 != 0 {
output.push(Cursor::new(buffer).read_u32::<LittleEndian>().unwrap());
}
output
}
// Simple offset_of macro akin to C++ offsetof
#[macro_export]
macro_rules! offset_of {