Skip to content

Commit 78e35c4

Browse files
jamienicolteoxoy
authored andcommitted
[naga wgsl-in] Disallow named component expression for matrix types
The WGSL spec only allows named component expressions when the base type is a vector or a structure, so this patch removes support for it for matrices. Additionally tests which used this for matrices have been updated to use indexing expressions instead, and a test has been added to ensure a named component expression on a matrix results in an error.
1 parent f6f9233 commit 78e35c4

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

naga/src/front/wgsl/lower/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,11 +2060,9 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
20602060

20612061
lowered_base.map(|base| crate::Expression::AccessIndex { base, index })
20622062
}
2063-
crate::TypeInner::Vector { .. } | crate::TypeInner::Matrix { .. } => {
2063+
crate::TypeInner::Vector { .. } => {
20642064
match Components::new(field.name, field.span)? {
20652065
Components::Swizzle { size, pattern } => {
2066-
// Swizzles aren't allowed on matrices, but
2067-
// validation will catch that.
20682066
Typed::Plain(crate::Expression::Swizzle {
20692067
size,
20702068
vector: ctx.apply_load_rule(lowered_base)?,

naga/src/front/wgsl/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ fn parse_postfix() {
399399
}",
400400
)
401401
.unwrap();
402+
403+
let err = parse_str(
404+
"fn foo() {
405+
let v = mat4x4<f32>().x;
406+
}",
407+
)
408+
.unwrap_err();
409+
assert_eq!(err.message(), "invalid field accessor `x`");
402410
}
403411

404412
#[test]

naga/tests/in/shadow.wgsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn vs_main(
3737
let w = u_entity.world;
3838
let world_pos = u_entity.world * vec4<f32>(position);
3939
var out: VertexOutput;
40-
out.world_normal = mat3x3<f32>(w.x.xyz, w.y.xyz, w.z.xyz) * vec3<f32>(normal.xyz);
40+
out.world_normal = mat3x3<f32>(w[0].xyz, w[1].xyz, w[2].xyz) * vec3<f32>(normal.xyz);
4141
out.world_position = world_pos;
4242
out.proj_position = u_globals.view_proj * world_pos;
4343
return out;

naga/tests/in/skybox.wgsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
2222
1.0,
2323
);
2424

25-
let inv_model_view = transpose(mat3x3<f32>(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz));
25+
let inv_model_view = transpose(mat3x3<f32>(r_data.view[0].xyz, r_data.view[1].xyz, r_data.view[2].xyz));
2626
let unprojected = r_data.proj_inv * pos;
2727
return VertexOutput(pos, inv_model_view * unprojected.xyz);
2828
}

naga/tests/wgsl_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,8 @@ fn pointer_type_equivalence() {
12381238
12391239
fn g() {
12401240
var m: mat2x2<f32>;
1241-
let pv: ptr<function, vec2<f32>> = &m.x;
1242-
let pf: ptr<function, f32> = &m.x.x;
1241+
let pv: ptr<function, vec2<f32>> = &m[0];
1242+
let pf: ptr<function, f32> = &m[0].x;
12431243
12441244
f(pv, pf);
12451245
}

0 commit comments

Comments
 (0)