From 4420258001e5be7820c6eee7e0ee6394624aaa52 Mon Sep 17 00:00:00 2001 From: Timo de Kort Date: Tue, 6 Oct 2020 23:19:29 +0200 Subject: [PATCH] [ir] Remove Matrix Kind from the IR (#230) --- src/back/glsl.rs | 12 +++-------- src/back/msl.rs | 9 ++------ src/back/spv/test_framework.rs | 5 ++++- src/back/spv/writer.rs | 6 ++---- src/front/glsl/types.rs | 3 +-- src/front/spv/mod.rs | 3 +-- src/front/wgsl/mod.rs | 39 +++++++++++++++------------------- src/lib.rs | 3 +-- src/proc/typifier.rs | 17 ++++++--------- src/proc/validator.rs | 16 +++++++++----- 10 files changed, 49 insertions(+), 64 deletions(-) diff --git a/src/back/glsl.rs b/src/back/glsl.rs index 23187ffae..3884282a5 100644 --- a/src/back/glsl.rs +++ b/src/back/glsl.rs @@ -940,11 +940,10 @@ fn write_expression<'a, 'b>( TypeInner::Matrix { columns, rows, - kind, width, } => format!( "{}mat{}x{}", - map_scalar(kind, width, builder.manager)?.prefix, + map_scalar(crate::ScalarKind::Float, width, builder.manager)?.prefix, columns as u8, rows as u8, ), @@ -1405,20 +1404,15 @@ fn write_type<'a>( TypeInner::Matrix { columns, rows, - kind, width, } => { - if kind != ScalarKind::Float { - return Err(Error::Custom(String::from( - "Non float matrices aren't allowed", - ))); - } else if width == 8 { + if width == 8 { manager.request(Features::DOUBLE_TYPE); } Cow::Owned(format!( "{}mat{}x{}", - map_scalar(kind, width, manager)?.prefix, + map_scalar(crate::ScalarKind::Float, width, manager)?.prefix, columns as u8, rows as u8 )) diff --git a/src/back/msl.rs b/src/back/msl.rs index 78e091f87..dd5bb0f44 100644 --- a/src/back/msl.rs +++ b/src/back/msl.rs @@ -814,16 +814,11 @@ impl Writer { name )?; } - crate::TypeInner::Matrix { - columns, - rows, - kind, - .. - } => { + crate::TypeInner::Matrix { columns, rows, .. } => { write!( self.out, "typedef {}{}x{} {}", - scalar_kind_string(kind), + scalar_kind_string(crate::ScalarKind::Float), vector_size_string(columns), vector_size_string(rows), name diff --git a/src/back/spv/test_framework.rs b/src/back/spv/test_framework.rs index a21aeb0f0..be2fa74fe 100644 --- a/src/back/spv/test_framework.rs +++ b/src/back/spv/test_framework.rs @@ -1,4 +1,7 @@ -pub(super) fn validate_instruction(words: &[spirv::Word], instruction: &crate::back::spv::Instruction) { +pub(super) fn validate_instruction( + words: &[spirv::Word], + instruction: &crate::back::spv::Instruction, +) { let mut inst_index = 0; let (wc, op) = ((words[inst_index] >> 16) as u16, words[inst_index] as u16); inst_index += 1; diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index 174b01764..c164a03ca 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -465,14 +465,13 @@ impl Writer { crate::TypeInner::Matrix { columns, rows: _, - kind, width, } => { let vector_id = self.get_type_id( arena, LookupType::Local(LocalType::Vector { size: columns, - kind, + kind: crate::ScalarKind::Float, width, }), ); @@ -836,14 +835,13 @@ impl Writer { crate::TypeInner::Matrix { rows, columns, - kind, width, } => { let vector_type_id = self.get_type_id( &ir_module.types, LookupType::Local(LocalType::Vector { width, - kind, + kind: crate::ScalarKind::Float, size: columns, }), ); diff --git a/src/front/glsl/types.rs b/src/front/glsl/types.rs index 25845ef46..dceb230b8 100644 --- a/src/front/glsl/types.rs +++ b/src/front/glsl/types.rs @@ -77,7 +77,7 @@ pub fn parse_type(type_name: &str) -> Option { let kind = iter.next()?; let size = iter.next()?; - let (kind, width) = kind_width_parse(kind)?; + let (_, width) = kind_width_parse(kind)?; let (columns, rows) = if let Some(size) = size_parse(size) { (size, size) @@ -94,7 +94,6 @@ pub fn parse_type(type_name: &str) -> Option { inner: TypeInner::Matrix { columns, rows, - kind, width, }, }) diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index 3b42f1092..c7458c1ec 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -1846,10 +1846,9 @@ impl> Parser { let num_columns = self.next()?; let vector_type_lookup = self.lookup_type.lookup(vector_type_id)?; let inner = match module.types[vector_type_lookup.handle].inner { - crate::TypeInner::Vector { size, kind, width } => crate::TypeInner::Matrix { + crate::TypeInner::Vector { size, width, .. } => crate::TypeInner::Matrix { columns: map_vector_size(num_columns)?, rows: size, - kind, width, }, _ => return Err(Error::InvalidInnerType(vector_type_id)), diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index 7a4fc2b36..3d7b4826b 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -298,11 +298,17 @@ impl Parser { index: usize, ) -> Result, Error<'static>> { match type_arena[ty].inner { - crate::TypeInner::Vector { kind, width, .. } - | crate::TypeInner::Matrix { kind, width, .. } => { + crate::TypeInner::Vector { kind, width, .. } => { let inner = crate::TypeInner::Scalar { kind, width }; Ok(type_arena.fetch_or_append(crate::Type { name: None, inner })) } + crate::TypeInner::Matrix { width, .. } => { + let inner = crate::TypeInner::Scalar { + kind: crate::ScalarKind::Float, + width, + }; + Ok(type_arena.fetch_or_append(crate::Type { name: None, inner })) + } crate::TypeInner::Array { base, .. } => Ok(base), crate::TypeInner::Struct { ref members } => Ok(members[index].ty), _ => Err(Error::NotCompositeType(ty)), @@ -543,14 +549,12 @@ impl Parser { crate::TypeInner::Matrix { rows, columns, - kind, width, } => match Composition::make(handle, columns, name, ctx.expressions)? { Composition::Multi(columns, components) => { let inner = crate::TypeInner::Matrix { columns, rows, - kind, width, }; crate::Expression::Compose { @@ -1049,83 +1053,74 @@ impl Parser { } } Token::Word("mat2x2") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Bi, rows: crate::VectorSize::Bi, - kind, width, } } Token::Word("mat2x3") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Bi, rows: crate::VectorSize::Tri, - kind, width, } } Token::Word("mat2x4") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Bi, rows: crate::VectorSize::Quad, - kind, width, } } Token::Word("mat3x2") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Tri, rows: crate::VectorSize::Bi, - kind, width, } } Token::Word("mat3x3") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Tri, rows: crate::VectorSize::Tri, - kind, width, } } Token::Word("mat3x4") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Tri, rows: crate::VectorSize::Quad, - kind, width, } } Token::Word("mat4x2") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Quad, rows: crate::VectorSize::Bi, - kind, width, } } Token::Word("mat4x3") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Quad, rows: crate::VectorSize::Tri, - kind, width, } } Token::Word("mat4x4") => { - let (kind, width) = lexer.next_scalar_generic()?; + let (_, width) = lexer.next_scalar_generic()?; crate::TypeInner::Matrix { columns: crate::VectorSize::Quad, rows: crate::VectorSize::Quad, - kind, width, } } diff --git a/src/lib.rs b/src/lib.rs index a05391cb3..2354c8c5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -351,11 +351,10 @@ pub enum TypeInner { kind: ScalarKind, width: Bytes, }, - /// Matrix of numbers. + /// Matrix of floats. Matrix { columns: VectorSize, rows: VectorSize, - kind: ScalarKind, width: Bytes, }, /// Pointer to a value. diff --git a/src/proc/typifier.rs b/src/proc/typifier.rs index 721c92bef..64b59203b 100644 --- a/src/proc/typifier.rs +++ b/src/proc/typifier.rs @@ -23,12 +23,10 @@ impl Clone for Resolution { crate::TypeInner::Matrix { rows, columns, - kind, width, } => crate::TypeInner::Matrix { rows, columns, - kind, width, }, _ => panic!("Unepxected clone type: {:?}", v), @@ -101,9 +99,12 @@ impl Typifier { crate::TypeInner::Matrix { rows: size, columns: _, - kind, width, - } => Resolution::Value(crate::TypeInner::Vector { size, kind, width }), + } => Resolution::Value(crate::TypeInner::Vector { + size, + kind: crate::ScalarKind::Float, + width, + }), ref other => panic!("Can't access into {:?}", other), }, crate::Expression::AccessIndex { base, index } => match *self.get(base, types) { @@ -116,7 +117,6 @@ impl Typifier { crate::TypeInner::Matrix { columns, rows, - kind, width, } => { if index >= columns as u32 { @@ -124,7 +124,7 @@ impl Typifier { } Resolution::Value(crate::TypeInner::Vector { size: rows, - kind, + kind: crate::ScalarKind::Float, width, }) } @@ -186,11 +186,10 @@ impl Typifier { crate::TypeInner::Matrix { columns, rows: _, - kind, width, } => Resolution::Value(crate::TypeInner::Vector { size: columns, - kind, + kind: crate::ScalarKind::Float, width, }), _ => panic!("Incompatible arguments {:?} x {:?}", ty_left, ty_right), @@ -219,12 +218,10 @@ impl Typifier { crate::TypeInner::Matrix { columns, rows, - kind, width, } => Resolution::Value(crate::TypeInner::Matrix { columns: rows, rows: columns, - kind, width, }), ref other => panic!("incompatible transpose of {:?}", other), diff --git a/src/proc/validator.rs b/src/proc/validator.rs index a730db74b..450df01e2 100644 --- a/src/proc/validator.rs +++ b/src/proc/validator.rs @@ -228,14 +228,14 @@ impl Validator { | (crate::ShaderStage::Fragment, crate::StorageClass::Input) => { match module.types[var.ty].inner { crate::TypeInner::Scalar { kind, .. } - | crate::TypeInner::Vector { kind, .. } - | crate::TypeInner::Matrix { kind, .. } => { + | crate::TypeInner::Vector { kind, .. } => { if kind != crate::ScalarKind::Float && var.interpolation != Some(crate::Interpolation::Flat) { return Err(EntryPointError::InvalidIntegerInterpolation); } } + crate::TypeInner::Matrix { .. } => {} _ => unreachable!(), } } @@ -342,9 +342,7 @@ impl Validator { for (handle, ty) in module.types.iter() { use crate::TypeInner as Ti; match ty.inner { - Ti::Scalar { kind, width } - | Ti::Vector { kind, width, .. } - | Ti::Matrix { kind, width, .. } => { + Ti::Scalar { kind, width } | Ti::Vector { kind, width, .. } => { let expected = match kind { crate::ScalarKind::Bool => 1, _ => 4, @@ -353,6 +351,14 @@ impl Validator { return Err(ValidationError::InvalidTypeWidth(kind, width)); } } + Ti::Matrix { width, .. } => { + if width != 4 { + return Err(ValidationError::InvalidTypeWidth( + crate::ScalarKind::Float, + width, + )); + } + } Ti::Pointer { base, class: _ } => { if base >= handle { return Err(ValidationError::UnresolvedType(base));