refactored to use util::read_spv

This commit is contained in:
colin 2018-12-09 08:00:55 -06:00
parent 59dfc6e216
commit bb54ae4c5a
4 changed files with 9 additions and 60 deletions

View file

@ -7,7 +7,6 @@ authors = ["maik klein <maikklein@googlemail.com>"]
winit = "0.16" winit = "0.16"
image = "0.10.4" image = "0.10.4"
ash = { path = "../ash" } ash = { path = "../ash" }
byteorder = "1.2.7"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.4", features = ["windef", "winuser"] } winapi = { version = "0.3.4", features = ["windef", "winuser"] }

View file

@ -5,11 +5,9 @@ extern crate image;
use std::default::Default; use std::default::Default;
use std::ffi::CString; use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::io::Read;
use std::mem::{self, align_of}; use std::mem::{self, align_of};
use std::os::raw::c_void; use std::os::raw::c_void;
use std::path::Path; use std::path::Path;
use std::ptr;
use ash::util::*; use ash::util::*;
use ash::vk; use ash::vk;
@ -548,20 +546,15 @@ fn main() {
]; ];
base.device.update_descriptor_sets(&write_desc_sets, &[]); 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."); 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."); File::open(Path::new("shader/texture/frag.spv")).expect("Could not find frag.spv.");
let vertex_bytes: Vec<u8> = vertex_spv_file let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file");
.bytes()
.filter_map(|byte| byte.ok())
.collect();
let vertex_code = bytes_to_u32_vec(&vertex_bytes);
let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); 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_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file");
let frag_code = bytes_to_u32_vec(&frag_bytes);
let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code);
let vertex_shader_module = base let vertex_shader_module = base

View file

@ -7,11 +7,9 @@ use examples::*;
use std::default::Default; use std::default::Default;
use std::ffi::CString; use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::io::Read;
use std::mem; use std::mem;
use std::mem::align_of; use std::mem::align_of;
use std::path::Path; use std::path::Path;
use std::ptr;
#[derive(Clone, Debug, Copy)] #[derive(Clone, Debug, Copy)]
struct Vertex { struct Vertex {
@ -204,23 +202,17 @@ fn main() {
base.device base.device
.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0) .bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0)
.unwrap(); .unwrap();
let vertex_spv_file = let mut vertex_spv_file =
File::open(Path::new("shader/triangle/vert.spv")).expect("Could not find vert.spv."); 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."); File::open(Path::new("shader/triangle/frag.spv")).expect("Could not find frag.spv.");
let vertex_bytes: Vec<u8> = vertex_spv_file let vertex_code = read_spv(&mut vertex_spv_file).expect("Failed to read vertex shader spv file");
.bytes()
.filter_map(|byte| byte.ok())
.collect();
let vertex_code = bytes_to_u32_vec(&vertex_bytes);
let vertex_shader_info = vk::ShaderModuleCreateInfo::builder().code(&vertex_code); 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_code = read_spv(&mut frag_spv_file).expect("Failed to read fragment shader spv file");
let frag_code = bytes_to_u32_vec(&frag_bytes);
let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code); let frag_shader_info = vk::ShaderModuleCreateInfo::builder().code(&frag_code);
let vertex_shader_module = base let vertex_shader_module = base
.device .device
.create_shader_module(&vertex_shader_info, None) .create_shader_module(&vertex_shader_info, None)

View file

@ -1,6 +1,5 @@
#[macro_use] #[macro_use]
extern crate ash; extern crate ash;
extern crate byteorder;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
extern crate winapi; extern crate winapi;
@ -36,47 +35,13 @@ use ash::extensions::khr::Win32Surface;
use ash::extensions::mvk::MacOSSurface; use ash::extensions::mvk::MacOSSurface;
pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0}; pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0};
use ash::{vk, Device, Entry, Instance}; use ash::{vk, Device, Entry, Instance};
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use std::cell::RefCell; use std::cell::RefCell;
use std::default::Default; use std::default::Default;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::io::Cursor;
use std::ops::Drop; use std::ops::Drop;
use std::os::raw::{c_char, c_void}; use std::os::raw::{c_char, c_void};
use std::ptr; 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::<LittleEndian>().unwrap();
number == MAGIC_NUMBER
}
pub fn bytes_to_u32_vec(bytes: &[u8]) -> Vec<u32> {
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::<LittleEndian>().unwrap()
} else {
Cursor::new(buffer).read_u32::<BigEndian>().unwrap()
};
output.push(new_word);
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 // Simple offset_of macro akin to C++ offsetof
#[macro_export] #[macro_export]
macro_rules! offset_of { macro_rules! offset_of {