Skip to content

Commit 0928ab9

Browse files
committed
fixed incorrect packed_int3 conversion generated for Metal
1 parent 438ed2a commit 0928ab9

5 files changed

Lines changed: 88 additions & 9 deletions

File tree

naga/src/back/msl/writer.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,34 @@ impl<W: Write> Writer<W> {
19311931
Ok(())
19321932
}
19331933

1934+
fn put_unpacked_access_chain(
1935+
&mut self,
1936+
expr_handle: Handle<crate::Expression>,
1937+
policy: index::BoundsCheckPolicy,
1938+
context: &ExpressionContext,
1939+
) -> Result<(), Error> {
1940+
let packed_kind = context.get_packed_vec_kind(expr_handle);
1941+
let ty = context.resolve_type(expr_handle);
1942+
1943+
// handle packed_type3 like packed_int3, we should unpack by using metal::type3()
1944+
if let (
1945+
Some(_),
1946+
&crate::TypeInner::Vector {
1947+
size: crate::VectorSize::Tri,
1948+
scalar,
1949+
},
1950+
) = (packed_kind, ty)
1951+
{
1952+
put_numeric_type(&mut self.out, scalar, &[crate::VectorSize::Tri])?;
1953+
write!(self.out, "(")?;
1954+
self.put_access_chain(expr_handle, policy, context)?;
1955+
write!(self.out, ")")?;
1956+
} else {
1957+
self.put_access_chain(expr_handle, policy, context)?;
1958+
}
1959+
Ok(())
1960+
}
1961+
19341962
/// Emit code for the expression `expr_handle`.
19351963
///
19361964
/// The `is_scoped` argument is true if the surrounding operators have the
@@ -1993,7 +2021,7 @@ impl<W: Write> Writer<W> {
19932021
)?
19942022
{
19952023
write!(self.out, " ? ")?;
1996-
self.put_access_chain(expr_handle, policy, context)?;
2024+
self.put_unpacked_access_chain(expr_handle, policy, context)?;
19972025
write!(self.out, " : ")?;
19982026

19992027
if context.resolve_type(base).pointer_space().is_some() {
@@ -2035,7 +2063,7 @@ impl<W: Write> Writer<W> {
20352063
write!(self.out, ")")?;
20362064
}
20372065
} else {
2038-
self.put_access_chain(expr_handle, policy, context)?;
2066+
self.put_unpacked_access_chain(expr_handle, policy, context)?;
20392067
}
20402068
}
20412069
crate::Expression::Swizzle {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
targets = "METAL"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct VertexInput {
2+
@location(0) chunk : vec3<i32>,
3+
@location(1) texture_index : u32,
4+
}
5+
6+
struct VertexOutput {
7+
@builtin(position) clip_position : vec4<f32>,
8+
};
9+
10+
@vertex
11+
fn vs_main(
12+
in : VertexInput,
13+
) -> VertexOutput {
14+
var out : VertexOutput;
15+
16+
let position = vec3<f32> (in.chunk - vec3<i32>(5));
17+
18+
return out;
19+
}

naga/tests/out/msl/wgsl-globals.metal

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ void test_msl_packed_vec3_(
4949
int _e16 = idx;
5050
alignment.v3_[_e16] = 3.0;
5151
FooStruct data = alignment;
52-
metal::float3 l0_ = data.v3_;
53-
metal::float2 l1_ = metal::float3(data.v3_).zx;
54-
test_msl_packed_vec3_as_arg(data.v3_);
55-
metal::float3 mvm0_ = metal::float3(data.v3_) * metal::float3x3 {};
56-
metal::float3 mvm1_ = metal::float3x3 {} * metal::float3(data.v3_);
57-
metal::float3 svm0_ = data.v3_ * 2.0;
58-
metal::float3 svm1_ = 2.0 * data.v3_;
52+
metal::float3 l0_ = metal::float3(data.v3_);
53+
metal::float2 l1_ = metal::float3(metal::float3(data.v3_)).zx;
54+
test_msl_packed_vec3_as_arg(metal::float3(data.v3_));
55+
metal::float3 mvm0_ = metal::float3(metal::float3(data.v3_)) * metal::float3x3 {};
56+
metal::float3 mvm1_ = metal::float3x3 {} * metal::float3(metal::float3(data.v3_));
57+
metal::float3 svm0_ = metal::float3(data.v3_) * 2.0;
58+
metal::float3 svm1_ = 2.0 * metal::float3(data.v3_);
5959
return;
6060
}
6161

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// language: metal1.0
2+
#include <metal_stdlib>
3+
#include <simd/simd.h>
4+
5+
using metal::uint;
6+
7+
struct VertexInput {
8+
metal::packed_int3 chunk;
9+
uint texture_index;
10+
};
11+
struct VertexOutput {
12+
metal::float4 clip_position;
13+
};
14+
15+
struct vs_mainInput {
16+
metal::int3 chunk [[attribute(0)]];
17+
uint texture_index [[attribute(1)]];
18+
};
19+
struct vs_mainOutput {
20+
metal::float4 clip_position [[position]];
21+
};
22+
vertex vs_mainOutput vs_main(
23+
vs_mainInput varyings [[stage_in]]
24+
) {
25+
const VertexInput in = { varyings.chunk, varyings.texture_index };
26+
VertexOutput out = {};
27+
metal::float3 position = static_cast<metal::float3>(as_type<metal::int3>(as_type<metal::uint3>(metal::int3(in.chunk)) - as_type<metal::uint3>(metal::int3(5))));
28+
VertexOutput _e7 = out;
29+
const auto _tmp = _e7;
30+
return vs_mainOutput { _tmp.clip_position };
31+
}

0 commit comments

Comments
 (0)