[wgsl] restrict type casts to matching container types

This commit is contained in:
Systemcluster
2021-02-14 00:29:08 +01:00
committed by Dzmitry Malyshau
parent 18853ab149
commit dcd269782a
3 changed files with 21 additions and 13 deletions

View File

@@ -844,7 +844,6 @@ impl Parser {
ctx.constants,
)?;
let kind = inner.scalar_kind();
let ty = ctx.types.fetch_or_append(crate::Type { name: None, inner });
lexer.expect(Token::Paren('('))?;
let mut components = Vec::new();
@@ -856,13 +855,24 @@ impl Parser {
}
lexer.expect(Token::Paren(')'))?;
if components.is_empty() {
crate::Expression::As {
expr: last_component,
kind: kind.ok_or(Error::BadTypeCast(word))?,
convert: true,
let last_component_inner = ctx.resolve_type(last_component)?;
match (&inner, last_component_inner) {
(crate::TypeInner::Scalar { .. }, crate::TypeInner::Scalar { .. })
| (crate::TypeInner::Matrix { .. }, crate::TypeInner::Matrix { .. })
| (crate::TypeInner::Vector { .. }, crate::TypeInner::Vector { .. }) => {
crate::Expression::As {
expr: last_component,
kind: kind.ok_or(Error::BadTypeCast(word))?,
convert: true,
}
}
_ => {
return Err(Error::BadTypeCast(word));
}
}
} else {
components.push(last_component);
let ty = ctx.types.fetch_or_append(crate::Type { name: None, inner });
crate::Expression::Compose { ty, components }
}
}

View File

@@ -37,11 +37,9 @@ typedef float type7;
typedef metal::float2 type8;
typedef int type9;
typedef metal::float3 type9;
typedef metal::float3 type10;
typedef bool type11;
typedef bool type10;
type7 fetch_shadow(
type6 light_id,
@@ -57,7 +55,7 @@ type7 fetch_shadow(
}
struct fs_mainInput {
type10 in_normal_fs [[user(loc0)]];
type9 in_normal_fs [[user(loc0)]];
type2 in_position_fs [[user(loc1)]];
};
@@ -73,7 +71,7 @@ fragment fs_mainOutput fs_main(
type5 sampler_shadow [[sampler(0)]]
) {
fs_mainOutput output;
type10 color1 = type10(0.05, 0.05, 0.05);
type9 color1 = type9(0.05, 0.05, 0.05);
type6 i = 0u;
bool loop_init = true;
while(true) {

View File

@@ -20,9 +20,9 @@ struct Data {
typedef int type4;
typedef float type5;
typedef metal::float3x3 type5;
typedef metal::float3x3 type6;
typedef float type6;
typedef metal::texturecube<float, metal::access::sample> type7;