Skip to content

Commit 505b66a

Browse files
39alislyedoc
authored andcommitted
fixed incorrect packed_int3 conversion generated for Metal (gfx-rs#9355)
1 parent d16c723 commit 505b66a

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
@@ -2284,6 +2284,7 @@ impl<W: Write> Writer<W> {
22842284
// to signed.
22852285
self.put_bitcasted_expression(
22862286
context.resolve_type(expr_handle),
2287+
expr_handle,
22872288
context,
22882289
&|writer, context, is_scoped| {
22892290
writer.put_binop(
@@ -2295,6 +2296,7 @@ impl<W: Write> Writer<W> {
22952296
&|writer, expr, context, _is_scoped| {
22962297
writer.put_bitcasted_expression(
22972298
&to_unsigned(context.resolve_type(expr))?,
2299+
expr,
22982300
context,
22992301
&|writer, context, is_scoped| {
23002302
writer.put_expression(expr, context, is_scoped)
@@ -3036,6 +3038,7 @@ impl<W: Write> Writer<W> {
30363038
fn put_bitcasted_expression<F>(
30373039
&mut self,
30383040
cast_to: &crate::TypeInner,
3041+
inner_expr: Handle<crate::Expression>,
30393042
context: &ExpressionContext,
30403043
put_expression: &F,
30413044
) -> BackendResult
@@ -3051,9 +3054,18 @@ impl<W: Write> Writer<W> {
30513054
_ => return Err(Error::UnsupportedBitCast(cast_to.clone())),
30523055
};
30533056
write!(self.out, ">(")?;
3054-
put_expression(self, context, true)?;
3055-
write!(self.out, ")")?;
30563057

3058+
// if it's packed, we must unpack it (e.g., float3(val)) before the bitcast.
3059+
if let Some(scalar) = context.get_packed_vec_kind(inner_expr) {
3060+
put_numeric_type(&mut self.out, scalar, &[crate::VectorSize::Tri])?;
3061+
write!(self.out, "(")?;
3062+
put_expression(self, context, true)?;
3063+
write!(self.out, ")")?;
3064+
} else {
3065+
put_expression(self, context, true)?;
3066+
}
3067+
3068+
write!(self.out, ")")?;
30573069
Ok(())
30583070
}
30593071

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)