Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Workaround FXC Bug in Matrix Indexing
Browse files Browse the repository at this point in the history
Fixes #2095
  • Loading branch information
cwfitzgerald committed Oct 20, 2022
1 parent a7193d6 commit fe4b30f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,14 +2085,18 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
index: u32,
) -> BackendResult {
match *resolved {
TypeInner::Vector { .. } => {
// We specifcally lift the ValuePointer to this case. While `[0]` is valid
// HLSL for any vector behind a value pointer, FXC completely miscompiles
// it and generates completely nonsensical DXBC.
//
// See https://github.com/gfx-rs/naga/issues/2095 for more details.
TypeInner::Vector { .. } | TypeInner::ValuePointer { .. } => {
// Write vector access as a swizzle
write!(writer.out, ".{}", back::COMPONENTS[index as usize])?
}
TypeInner::Matrix { .. }
| TypeInner::Array { .. }
| TypeInner::BindingArray { .. }
| TypeInner::ValuePointer { .. } => write!(writer.out, "[{}]", index)?,
| TypeInner::BindingArray { .. } => write!(writer.out, "[{}]", index)?,
TypeInner::Struct { .. } => {
// This will never panic in case the type is a `Struct`, this is not true
// for other types so we can only check while inside this match arm
Expand Down
10 changes: 5 additions & 5 deletions tests/out/hlsl/access.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ void test_matrix_within_struct_accesses()
float2 unnamed_1 = GetMatmOnBaz(baz)[0];
int _expr16 = idx;
float2 unnamed_2 = GetMatmOnBaz(baz)[_expr16];
float unnamed_3 = GetMatmOnBaz(baz)[0][1];
float unnamed_3 = GetMatmOnBaz(baz)[0].y;
int _expr28 = idx;
float unnamed_4 = GetMatmOnBaz(baz)[0][_expr28];
int _expr32 = idx;
float unnamed_5 = GetMatmOnBaz(baz)[_expr32][1];
float unnamed_5 = GetMatmOnBaz(baz)[_expr32].y;
int _expr38 = idx;
int _expr40 = idx;
float unnamed_6 = GetMatmOnBaz(baz)[_expr38][_expr40];
Expand Down Expand Up @@ -191,11 +191,11 @@ void test_matrix_within_array_within_struct_accesses()
float2 unnamed_9 = nested_mat_cx2_.am[0]._0;
int _expr25 = idx_1;
float2 unnamed_10 = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _expr25);
float unnamed_11 = nested_mat_cx2_.am[0]._0[1];
float unnamed_11 = nested_mat_cx2_.am[0]._0.y;
int _expr41 = idx_1;
float unnamed_12 = nested_mat_cx2_.am[0]._0[_expr41];
int _expr47 = idx_1;
float unnamed_13 = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _expr47)[1];
float unnamed_13 = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _expr47).y;
int _expr55 = idx_1;
int _expr57 = idx_1;
float unnamed_14 = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _expr55)[_expr57];
Expand All @@ -207,7 +207,7 @@ void test_matrix_within_array_within_struct_accesses()
t_1.am[0]._0 = (9.0).xx;
int _expr90 = idx_1;
__set_col_of_mat4x2(t_1.am[0], _expr90, (90.0).xx);
t_1.am[0]._0[1] = 10.0;
t_1.am[0]._0.y = 10.0;
int _expr107 = idx_1;
t_1.am[0]._0[_expr107] = 20.0;
int _expr113 = idx_1;
Expand Down

0 comments on commit fe4b30f

Please sign in to comment.