Skip to content

Commit f0cb500

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

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

naga/src/back/msl/writer.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,7 @@ impl<W: Write> Writer<W> {
22742274
// to signed.
22752275
self.put_bitcasted_expression(
22762276
context.resolve_type(expr_handle),
2277+
expr_handle,
22772278
context,
22782279
&|writer, context, is_scoped| {
22792280
writer.put_binop(
@@ -2285,6 +2286,7 @@ impl<W: Write> Writer<W> {
22852286
&|writer, expr, context, _is_scoped| {
22862287
writer.put_bitcasted_expression(
22872288
&to_unsigned(context.resolve_type(expr))?,
2289+
expr,
22882290
context,
22892291
&|writer, context, is_scoped| {
22902292
writer.put_expression(expr, context, is_scoped)
@@ -3026,6 +3028,7 @@ impl<W: Write> Writer<W> {
30263028
fn put_bitcasted_expression<F>(
30273029
&mut self,
30283030
cast_to: &crate::TypeInner,
3031+
inner_expr: Handle<crate::Expression>,
30293032
context: &ExpressionContext,
30303033
put_expression: &F,
30313034
) -> BackendResult
@@ -3041,9 +3044,18 @@ impl<W: Write> Writer<W> {
30413044
_ => return Err(Error::UnsupportedBitCast(cast_to.clone())),
30423045
};
30433046
write!(self.out, ">(")?;
3044-
put_expression(self, context, true)?;
3045-
write!(self.out, ")")?;
30463047

3048+
// if it's packed, we must unpack it (e.g., float3(val)) before the bitcast.
3049+
if let Some(scalar) = context.get_packed_vec_kind(inner_expr) {
3050+
put_numeric_type(&mut self.out, scalar, &[crate::VectorSize::Tri])?;
3051+
write!(self.out, "(")?;
3052+
put_expression(self, context, true)?;
3053+
write!(self.out, ")")?;
3054+
} else {
3055+
put_expression(self, context, true)?;
3056+
}
3057+
3058+
write!(self.out, ")")?;
30473059
Ok(())
30483060
}
30493061

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+
}
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)