From bd9efe5915e548b9a2ec1f3d3e9296c90b31e69a Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 27 Aug 2020 01:31:32 -0400 Subject: [PATCH] Allow struct members without an origin --- src/back/msl.rs | 2 ++ src/front/spv/convert.rs | 10 +++------- src/front/spv/mod.rs | 24 ++++++++++++++++++------ src/lib.rs | 2 ++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/back/msl.rs b/src/back/msl.rs index 4bf57ba0b..3950b6960 100644 --- a/src/back/msl.rs +++ b/src/back/msl.rs @@ -930,6 +930,7 @@ impl Writer { let base_name = module.types[member.ty].name.or_index(member.ty); write!(self.out, "\t{} {}", base_name, name)?; match member.origin { + crate::MemberOrigin::Empty => {} crate::MemberOrigin::BuiltIn(built_in) => { ResolvedBinding::BuiltIn(built_in) .try_fmt_decorated(&mut self.out, "")?; @@ -1105,6 +1106,7 @@ impl Writer { let name = member.name.or_index(MemberIndex(index)); let ty_name = module.types[member.ty].name.or_index(member.ty); match member.origin { + crate::MemberOrigin::Empty => {} crate::MemberOrigin::BuiltIn(built_in) => { write!(self.out, "\t{} {}", ty_name, name)?; ResolvedBinding::BuiltIn(built_in) diff --git a/src/front/spv/convert.rs b/src/front/spv/convert.rs index cc20ff5d4..fe3845a14 100644 --- a/src/front/spv/convert.rs +++ b/src/front/spv/convert.rs @@ -83,9 +83,9 @@ pub fn map_width(word: spirv::Word) -> Result { .map_err(|_| Error::InvalidTypeWidth(word)) } -pub fn map_builtin(word: spirv::Word) -> Result, Error> { +pub fn map_builtin(word: spirv::Word) -> Result { use spirv::BuiltIn as Bi; - Ok(Some(match spirv::BuiltIn::from_u32(word) { + Ok(match spirv::BuiltIn::from_u32(word) { Some(Bi::BaseInstance) => crate::BuiltIn::BaseInstance, Some(Bi::BaseVertex) => crate::BuiltIn::BaseVertex, Some(Bi::ClipDistance) => crate::BuiltIn::ClipDistance, @@ -103,10 +103,6 @@ pub fn map_builtin(word: spirv::Word) -> Result, Error> { Some(Bi::LocalInvocationId) => crate::BuiltIn::LocalInvocationId, Some(Bi::LocalInvocationIndex) => crate::BuiltIn::LocalInvocationIndex, Some(Bi::WorkgroupId) => crate::BuiltIn::WorkGroupId, - Some(Bi::WorkgroupSize) => { - log::warn!("gl_WorkgroupSize builtin is not supported"); - return Ok(None); - } _ => return Err(Error::UnsupportedBuiltIn(word)), - })) + }) } diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index 307d9309f..44cfedbd0 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -147,6 +147,13 @@ struct Decoration { } impl Decoration { + fn debug_name(&self) -> &str { + match self.name { + Some(ref name) => name.as_str(), + None => "?", + } + } + fn get_binding(&self) -> Option { //TODO: validate this better match *self { @@ -197,7 +204,7 @@ impl Decoration { offset: Some(offset), .. } => Ok(crate::MemberOrigin::Offset(offset)), - _ => Err(Error::MissingDecoration(spirv::Decoration::Offset)), + _ => Ok(crate::MemberOrigin::Empty), } } } @@ -353,12 +360,15 @@ impl> Parser { ) -> Result<(), Error> { let raw = self.next()?; let dec_typed = spirv::Decoration::from_u32(raw).ok_or(Error::InvalidDecoration(raw))?; - log::trace!("\t\t{:?}", dec_typed); + log::trace!("\t\t{}: {:?}", dec.debug_name(), dec_typed); match dec_typed { spirv::Decoration::BuiltIn => { inst.expect(base_words + 2)?; let raw = self.next()?; - dec.built_in = map_builtin(raw)?; + match map_builtin(raw) { + Ok(built_in) => dec.built_in = Some(built_in), + Err(_e) => log::warn!("Unsupported builtin {}", raw), + }; } spirv::Decoration::Location => { inst.expect(base_words + 2)?; @@ -1573,6 +1583,7 @@ impl> Parser { if left != 0 { return Err(Error::InvalidOperand); } + self.future_member_decor .entry((id, member)) .or_default() @@ -1595,6 +1606,7 @@ impl> Parser { inst.expect_at_least(4)?; let id = self.next()?; let member = self.next()?; + let mut dec = self .future_member_decor .remove(&(id, member)) @@ -1870,8 +1882,8 @@ impl> Parser { self.switch(ModuleState::Type, inst.op)?; inst.expect_at_least(2)?; let id = self.next()?; - let decor = self.future_decor.remove(&id); - if let Some(ref decor) = decor { + let parent_decor = self.future_decor.remove(&id); + if let Some(ref decor) = parent_decor { if decor.block.is_some() { // do nothing } @@ -1897,7 +1909,7 @@ impl> Parser { id, LookupType { handle: module.types.append(crate::Type { - name: decor.and_then(|dec| dec.name), + name: parent_decor.and_then(|dec| dec.name), inner, }), base_id: None, diff --git a/src/lib.rs b/src/lib.rs index 8dec8ff0d..3315108e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,6 +147,8 @@ pub enum ArraySize { #[cfg_attr(feature = "serialize", derive(Serialize))] #[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum MemberOrigin { + /// Member is local to the shader. + Empty, /// Built-in shader variable. BuiltIn(BuiltIn), /// Offset within the struct.