From 231ea5c531803e02d8f394beefe0ddca89d710a0 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 16 Jun 2016 09:20:33 +0200 Subject: [PATCH] Fix width of integers and floating-points reflected from SPIR-V --- vulkano-shaders/src/lib.rs | 36 ++++++++++++++++--- vulkano-shaders/src/structs.rs | 64 +++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index 37d76352..6a2b5726 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -198,11 +198,25 @@ fn format_from_id(doc: &parse::Spirv, searched: u32, ignore_first_array: bool) - match instruction { &parse::Instruction::TypeInt { result_id, width, signedness } if result_id == searched => { assert!(!ignore_first_array); - return ("R32Sint".to_owned(), 1); + return (match (width, signedness) { + (8, true) => "R8Sint", + (8, false) => "R8Uint", + (16, true) => "R16Sint", + (16, false) => "R16Uint", + (32, true) => "R32Sint", + (32, false) => "R32Uint", + (64, true) => "R64Sint", + (64, false) => "R64Uint", + _ => panic!() + }.to_owned(), 1); }, &parse::Instruction::TypeFloat { result_id, width } if result_id == searched => { assert!(!ignore_first_array); - return ("R32Sfloat".to_owned(), 1); + return (match width { + 32 => "R32Sfloat", + 64 => "R64Sfloat", + _ => panic!() + }.to_owned(), 1); }, &parse::Instruction::TypeVector { result_id, component_id, count } if result_id == searched => { assert!(!ignore_first_array); @@ -261,10 +275,24 @@ fn type_from_id(doc: &parse::Spirv, searched: u32) -> String { return "bool".to_owned() }, &parse::Instruction::TypeInt { result_id, width, signedness } if result_id == searched => { - return "i32".to_owned() + return match (width, signedness) { + (8, true) => "i8", + (8, false) => "u8", + (16, true) => "i16", + (16, false) => "u16", + (32, true) => "i32", + (32, false) => "u32", + (64, true) => "i64", + (64, false) => "u64", + _ => panic!() + }.to_owned(); }, &parse::Instruction::TypeFloat { result_id, width } if result_id == searched => { - return "f32".to_owned() + return match width { + 32 => "f32", + 64 => "f64", + _ => panic!() + }.to_owned(); }, &parse::Instruction::TypeVector { result_id, component_id, count } if result_id == searched => { let t = type_from_id(doc, component_id); diff --git a/vulkano-shaders/src/structs.rs b/vulkano-shaders/src/structs.rs index 81c5ff8a..0d6c7b28 100644 --- a/vulkano-shaders/src/structs.rs +++ b/vulkano-shaders/src/structs.rs @@ -185,16 +185,64 @@ fn type_from_id(doc: &parse::Spirv, searched: u32) -> (String, Option, us return ("bool".to_owned(), Some(size), mem::align_of::()) }, &parse::Instruction::TypeInt { result_id, width, signedness } if result_id == searched => { - // FIXME: width - #[repr(C)] struct Foo { data: i32, after: u8 } - let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; - return ("i32".to_owned(), Some(size), mem::align_of::()) + match (width, signedness) { + (8, true) => { + #[repr(C)] struct Foo { data: i8, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("i8".to_owned(), Some(size), mem::align_of::()) + }, + (8, false) => { + #[repr(C)] struct Foo { data: u8, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("u8".to_owned(), Some(size), mem::align_of::()) + }, + (16, true) => { + #[repr(C)] struct Foo { data: i16, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("i16".to_owned(), Some(size), mem::align_of::()) + }, + (16, false) => { + #[repr(C)] struct Foo { data: u16, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("u16".to_owned(), Some(size), mem::align_of::()) + }, + (32, true) => { + #[repr(C)] struct Foo { data: i32, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("i32".to_owned(), Some(size), mem::align_of::()) + }, + (32, false) => { + #[repr(C)] struct Foo { data: u32, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("u32".to_owned(), Some(size), mem::align_of::()) + }, + (64, true) => { + #[repr(C)] struct Foo { data: i64, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("i64".to_owned(), Some(size), mem::align_of::()) + }, + (64, false) => { + #[repr(C)] struct Foo { data: u64, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("u64".to_owned(), Some(size), mem::align_of::()) + }, + _ => panic!("No Rust equivalent for an integer of width {}", width) + } }, &parse::Instruction::TypeFloat { result_id, width } if result_id == searched => { - // FIXME: width - #[repr(C)] struct Foo { data: f32, after: u8 } - let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; - return ("f32".to_owned(), Some(size), mem::align_of::()) + match width { + 32 => { + #[repr(C)] struct Foo { data: f32, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("f32".to_owned(), Some(size), mem::align_of::()) + }, + 64 => { + #[repr(C)] struct Foo { data: f64, after: u8 } + let size = unsafe { (&(&*(0 as *const Foo)).after) as *const u8 as usize }; + return ("f64".to_owned(), Some(size), mem::align_of::()) + }, + _ => panic!("No Rust equivalent for a floating-point of width {}", width) + } }, &parse::Instruction::TypeVector { result_id, component_id, count } if result_id == searched => { debug_assert_eq!(mem::align_of::<[u32; 3]>(), mem::align_of::());