Skip to content

Commit

Permalink
Add member_types to Type::Struct
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Feb 9, 2018
1 parent f3ba2c4 commit f7e07fa
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
29 changes: 21 additions & 8 deletions spirv_cross/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1938,18 +1938,30 @@ pub mod root {
#[derive(Debug, Copy)]
pub struct ScType {
pub type_: root::spirv_cross::SPIRType_BaseType,
pub member_types: *mut u32,
pub member_types_size: usize,
}
#[test]
fn bindgen_test_layout_ScType() {
assert_eq!(::std::mem::size_of::<ScType>() , 4usize , concat ! (
assert_eq!(::std::mem::size_of::<ScType>() , 24usize , concat ! (
"Size of: " , stringify ! ( ScType ) ));
assert_eq! (::std::mem::align_of::<ScType>() , 4usize , concat ! (
assert_eq! (::std::mem::align_of::<ScType>() , 8usize , concat ! (
"Alignment of " , stringify ! ( ScType ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const ScType ) ) . type_ as * const _ as
usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( ScType ) , "::" ,
stringify ! ( type_ ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const ScType ) ) . member_types as * const
_ as usize } , 8usize , concat ! (
"Alignment of field: " , stringify ! ( ScType ) , "::" ,
stringify ! ( member_types ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const ScType ) ) . member_types_size as *
const _ as usize } , 16usize , concat ! (
"Alignment of field: " , stringify ! ( ScType ) , "::" ,
stringify ! ( member_types_size ) ));
}
impl Clone for ScType {
fn clone(&self) -> Self { *self }
Expand All @@ -1973,12 +1985,13 @@ pub mod root {
-> root::ScInternalResult;
}
extern "C" {
pub fn sc_internal_compiler_hlsl_set_root_constant_layout(
compiler: *const root::ScInternalCompilerHlsl,
constants: *const root::ScHlslRootConstant,
count: usize,
) -> root::ScInternalResult;

pub fn sc_internal_compiler_hlsl_set_root_constant_layout(compiler:
*const root::ScInternalCompilerHlsl,
constants:
*const root::ScHlslRootConstant,
count:
usize)
-> root::ScInternalResult;
}
extern "C" {
pub fn sc_internal_compiler_msl_new(compiler:
Expand Down
15 changes: 12 additions & 3 deletions spirv_cross/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl spirv::Decoration {
}

impl spirv::Type {
pub fn from_raw(ty: spirv_cross::SPIRType_BaseType) -> Type {
pub fn from_raw(ty: spirv_cross::SPIRType_BaseType, member_types: Vec<u32>) -> Type {
use bindings::root::spirv_cross::SPIRType_BaseType as b;
use spirv::Type::*;
match ty {
Expand All @@ -109,7 +109,7 @@ impl spirv::Type {
b::AtomicCounter => AtomicCounter,
b::Float => Float,
b::Double => Double,
b::Struct => Struct,
b::Struct => Struct { member_types },
b::Image => Image,
b::SampledImage => SampledImage,
b::Sampler => Sampler,
Expand Down Expand Up @@ -292,13 +292,22 @@ impl<TTargetData> Compiler<TTargetData> {
pub fn get_type(&self, id: u32) -> Result<spirv::Type, ErrorCode> {
unsafe {
let mut type_ptr = ptr::null();

check!(sc_internal_compiler_get_type(
self.sc_compiler,
id,
&mut type_ptr,
));
let result = Type::from_raw((*type_ptr).type_);

let raw = *type_ptr;

let member_types =
slice::from_raw_parts(raw.member_types, raw.member_types_size).to_vec();
let result = Type::from_raw(raw.type_, member_types);

check!(sc_internal_free_pointer(raw.member_types as *mut c_void));
check!(sc_internal_free_pointer(type_ptr as *mut c_void));

Ok(result)
}
}
Expand Down
2 changes: 1 addition & 1 deletion spirv_cross/src/spirv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub enum Type {
AtomicCounter,
Float,
Double,
Struct,
Struct { member_types: Vec<u32> },
Image,
SampledImage,
Sampler,
Expand Down
20 changes: 18 additions & 2 deletions spirv_cross/src/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ ScInternalResult sc_internal_compiler_hlsl_set_root_constant_layout(const ScInte
for (int i = 0; i < count; i++)
{
root_constants.push_back(
spirv_cross::RootConstants {
spirv_cross::RootConstants{
constants[i].start,
constants[i].end,
constants[i].binding,
constants[i].space });
constants[i].space});
}

auto compiler_hlsl = (spirv_cross::CompilerHLSL *)compiler;
Expand Down Expand Up @@ -270,8 +270,24 @@ ScInternalResult sc_internal_compiler_get_type(const ScInternalCompilerBase *com
INTERNAL_RESULT(
do {
auto const &type = ((spirv_cross::Compiler *)compiler)->get_type(id);
auto const member_types_size = type.member_types.size();

auto ty = (ScType *)malloc(sizeof(ScType));
ty->type = type.basetype;
ty->member_types_size = member_types_size;

if (member_types_size > 0)
{
auto const &member_types = (uint32_t *)malloc(member_types_size * sizeof(uint32_t));

for (auto i = 0; i < member_types_size; i++)
{
member_types[i] = type.member_types[i];
}

ty->member_types = member_types;
}

*spirv_type = ty;
} while (0);)
}
Expand Down
2 changes: 2 additions & 0 deletions spirv_cross/src/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ typedef struct ScSpecializationConstant
typedef struct ScType
{
spirv_cross::SPIRType::BaseType type;
uint32_t *member_types;
size_t member_types_size;
} ScType;

ScInternalResult sc_internal_get_latest_exception_message(const char **message);
Expand Down
21 changes: 12 additions & 9 deletions spirv_cross/tests/spirv_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ fn ast_gets_shader_resources() {
assert!(
stage_inputs
.iter()
.any(|stage_input| { stage_input.name == "a_normal" })
.any(|stage_input| stage_input.name == "a_normal")
);
assert!(
stage_inputs
.iter()
.any(|stage_input| { stage_input.name == "a_position" })
.any(|stage_input| stage_input.name == "a_position")
);
assert_eq!(stage_outputs.len(), 1);
assert!(
stage_outputs
.iter()
.any(|stage_output| { stage_output.name == "v_normal" })
.any(|stage_output| stage_output.name == "v_normal")
);
assert_eq!(shader_resources.subpass_inputs.len(), 0);
assert_eq!(shader_resources.storage_images.len(), 0);
Expand Down Expand Up @@ -97,12 +97,15 @@ fn ast_gets_type() {

let uniform_buffers = ast.get_shader_resources().unwrap().uniform_buffers;

assert!(
match ast.get_type(uniform_buffers[0].base_type_id).unwrap() {
spirv::Type::Struct => true,
_ => false,
let is_struct = match ast.get_type(uniform_buffers[0].base_type_id).unwrap() {
spirv::Type::Struct { member_types } => {
assert_eq!(member_types.len(), 2);
true
}
);
_ => false,
};

assert!(is_struct);
}

#[test]
Expand Down Expand Up @@ -149,7 +152,7 @@ fn ast_sets_member_decoration() {
uniform_buffers[0].base_type_id,
1,
spirv::Decoration::Offset,
new_offset
new_offset,
).unwrap();

assert_eq!(
Expand Down

0 comments on commit f7e07fa

Please sign in to comment.