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

Commit 012f2a6

Browse files
authored
Merge pull request #1784 from teoxoy/patch-2
[hlsl-out] fix matrix not being declared as transposed
1 parent 4146cb2 commit 012f2a6

File tree

11 files changed

+188
-180
lines changed

11 files changed

+188
-180
lines changed

src/back/hlsl/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ becomes `vec * mat`, etc. This acts as the inverse transpose making the results
1616
The only time we don't get this implicit transposition is when reading matrices from Uniforms/Push Constants.
1717
To deal with this, we add `row_major` to all declarations of matrices in Uniforms/Push Constants.
1818
19-
Finally because all of our matrices are transposed, if you use `mat3x4`, it'll become `float4x3` in HLSL.
19+
Finally because all of our matrices are transposed, if you use `mat3x4`, it'll become `float3x4` in HLSL
20+
(HLSL has inverted col/row notation).
2021
2122
[hlsl]: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl
2223
*/

src/back/hlsl/storage.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ impl<W: fmt::Write> super::Writer<'_, W> {
122122
self.out,
123123
"{}{}x{}(",
124124
crate::ScalarKind::Float.to_hlsl_str(width)?,
125-
rows as u8,
126125
columns as u8,
126+
rows as u8,
127127
)?;
128128

129129
// Note: Matrices containing vec3s, due to padding, act like they contain vec4s.
130-
let padded_columns = match columns {
130+
let padded_rows = match rows {
131131
crate::VectorSize::Tri => 4,
132-
columns => columns as u32,
132+
rows => rows as u32,
133133
};
134-
let row_stride = width as u32 * padded_columns;
135-
let iter = (0..rows as u32).map(|i| {
134+
let row_stride = width as u32 * padded_rows;
135+
let iter = (0..columns as u32).map(|i| {
136136
let ty_inner = crate::TypeInner::Vector {
137-
size: columns,
137+
size: rows,
138138
kind: crate::ScalarKind::Float,
139139
width,
140140
};
@@ -261,27 +261,27 @@ impl<W: fmt::Write> super::Writer<'_, W> {
261261
"{}{}{}x{} {}{} = ",
262262
level.next(),
263263
crate::ScalarKind::Float.to_hlsl_str(width)?,
264-
rows as u8,
265264
columns as u8,
265+
rows as u8,
266266
STORE_TEMP_NAME,
267267
depth,
268268
)?;
269269
self.write_store_value(module, &value, func_ctx)?;
270270
writeln!(self.out, ";")?;
271271

272272
// Note: Matrices containing vec3s, due to padding, act like they contain vec4s.
273-
let padded_columns = match columns {
273+
let padded_rows = match rows {
274274
crate::VectorSize::Tri => 4,
275-
columns => columns as u32,
275+
rows => rows as u32,
276276
};
277-
let row_stride = width as u32 * padded_columns;
277+
let row_stride = width as u32 * padded_rows;
278278

279279
// then iterate the stores
280-
for i in 0..rows as u32 {
280+
for i in 0..columns as u32 {
281281
self.temp_access_chain
282282
.push(SubAccess::Offset(i * row_stride));
283283
let ty_inner = crate::TypeInner::Vector {
284-
size: columns,
284+
size: rows,
285285
kind: crate::ScalarKind::Float,
286286
width,
287287
};
@@ -401,8 +401,13 @@ impl<W: fmt::Write> super::Writer<'_, W> {
401401
crate::TypeInner::Vector { width, .. } => Parent::Array {
402402
stride: width as u32,
403403
},
404-
crate::TypeInner::Matrix { rows, width, .. } => Parent::Array {
405-
stride: width as u32 * if rows > crate::VectorSize::Bi { 4 } else { 2 },
404+
crate::TypeInner::Matrix { columns, width, .. } => Parent::Array {
405+
stride: width as u32
406+
* if columns > crate::VectorSize::Bi {
407+
4
408+
} else {
409+
2
410+
},
406411
},
407412
_ => unreachable!(),
408413
},

src/back/hlsl/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
838838
self.out,
839839
"{}{}x{}",
840840
crate::ScalarKind::Float.to_hlsl_str(width)?,
841-
back::vector_size_str(rows),
842841
back::vector_size_str(columns),
842+
back::vector_size_str(rows),
843843
)?;
844844
}
845845
TypeInner::Image {

tests/in/access.wgsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct AlignedWrapper {
55
}
66

77
struct Bar {
8-
matrix: mat4x4<f32>,
8+
matrix: mat4x3<f32>,
99
matrix_array: array<mat2x2<f32>, 2>,
1010
atom: atomic<i32>,
1111
arr: array<vec2<u32>, 2>,
@@ -42,14 +42,14 @@ fn foo_vert(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4<f32> {
4242
c[vi + 1u] = 42;
4343
let value = c[vi];
4444

45-
return matrix * vec4<f32>(vec4<i32>(value));
45+
return vec4<f32>(matrix * vec4<f32>(vec4<i32>(value)), 2.0);
4646
}
4747

4848
@stage(fragment)
4949
fn foo_frag() -> @location(0) vec4<f32> {
5050
// test storage stores
5151
bar.matrix[1].z = 1.0;
52-
bar.matrix = mat4x4<f32>(vec4<f32>(0.0), vec4<f32>(1.0), vec4<f32>(2.0), vec4<f32>(3.0));
52+
bar.matrix = mat4x3<f32>(vec3<f32>(0.0), vec3<f32>(1.0), vec3<f32>(2.0), vec3<f32>(3.0));
5353
bar.arr = array<vec2<u32>, 2>(vec2<u32>(0u), vec2<u32>(1u));
5454
bar.data[1].value = 1;
5555

tests/out/glsl/access.atomics.Compute.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct AlignedWrapper {
99
int value;
1010
};
1111
layout(std430) buffer Bar_block_0Compute {
12-
mat4x4 matrix;
12+
mat4x3 matrix;
1313
mat2x2 matrix_array[2];
1414
int atom;
1515
uvec2 arr[2];

tests/out/glsl/access.foo_frag.Fragment.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct AlignedWrapper {
77
int value;
88
};
99
layout(std430) buffer Bar_block_0Fragment {
10-
mat4x4 matrix;
10+
mat4x3 matrix;
1111
mat2x2 matrix_array[2];
1212
int atom;
1313
uvec2 arr[2];
@@ -23,7 +23,7 @@ float read_from_private(inout float foo_1) {
2323

2424
void main() {
2525
_group_0_binding_0_fs.matrix[1][2] = 1.0;
26-
_group_0_binding_0_fs.matrix = mat4x4(vec4(0.0), vec4(1.0), vec4(2.0), vec4(3.0));
26+
_group_0_binding_0_fs.matrix = mat4x3(vec3(0.0), vec3(1.0), vec3(2.0), vec3(3.0));
2727
_group_0_binding_0_fs.arr = uvec2[2](uvec2(0u), uvec2(1u));
2828
_group_0_binding_0_fs.data[1].value = 1;
2929
_fs2p_location0 = vec4(0.0);

tests/out/glsl/access.foo_vert.Vertex.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct AlignedWrapper {
77
int value;
88
};
99
layout(std430) buffer Bar_block_0Vertex {
10-
mat4x4 matrix;
10+
mat4x3 matrix;
1111
mat2x2 matrix_array[2];
1212
int atom;
1313
uvec2 arr[2];
@@ -26,15 +26,15 @@ void main() {
2626
int c[5] = int[5](0, 0, 0, 0, 0);
2727
float baz = foo;
2828
foo = 1.0;
29-
mat4x4 matrix = _group_0_binding_0_vs.matrix;
29+
mat4x3 matrix = _group_0_binding_0_vs.matrix;
3030
uvec2 arr[2] = _group_0_binding_0_vs.arr;
3131
float b = _group_0_binding_0_vs.matrix[3][0];
3232
int a = _group_0_binding_0_vs.data[(uint(_group_0_binding_0_vs.data.length()) - 2u)].value;
3333
float _e27 = read_from_private(foo);
3434
c = int[5](a, int(b), 3, 4, 5);
3535
c[(vi + 1u)] = 42;
3636
int value = c[vi];
37-
gl_Position = (matrix * vec4(ivec4(value)));
37+
gl_Position = vec4((matrix * vec4(ivec4(value))), 2.0);
3838
gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
3939
return;
4040
}

tests/out/hlsl/access.hlsl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
2525

2626
float baz = foo;
2727
foo = 1.0;
28-
float4x4 matrix_ = float4x4(asfloat(bar.Load4(0+0)), asfloat(bar.Load4(0+16)), asfloat(bar.Load4(0+32)), asfloat(bar.Load4(0+48)));
28+
float4x3 matrix_ = float4x3(asfloat(bar.Load3(0+0)), asfloat(bar.Load3(0+16)), asfloat(bar.Load3(0+32)), asfloat(bar.Load3(0+48)));
2929
uint2 arr[2] = {asuint(bar.Load2(104+0)), asuint(bar.Load2(104+8))};
3030
float b = asfloat(bar.Load(0+48+0));
3131
int a = asint(bar.Load(0+(((NagaBufferLengthRW(bar) - 120) / 8) - 2u)*8+120));
@@ -36,18 +36,18 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position
3636
}
3737
c[(vi + 1u)] = 42;
3838
int value = c[vi];
39-
return mul(float4(int4(value.xxxx)), matrix_);
39+
return float4(mul(float4(int4(value.xxxx)), matrix_), 2.0);
4040
}
4141

4242
float4 foo_frag() : SV_Target0
4343
{
4444
bar.Store(8+16+0, asuint(1.0));
4545
{
46-
float4x4 _value2 = float4x4(float4(0.0.xxxx), float4(1.0.xxxx), float4(2.0.xxxx), float4(3.0.xxxx));
47-
bar.Store4(0+0, asuint(_value2[0]));
48-
bar.Store4(0+16, asuint(_value2[1]));
49-
bar.Store4(0+32, asuint(_value2[2]));
50-
bar.Store4(0+48, asuint(_value2[3]));
46+
float4x3 _value2 = float4x3(float3(0.0.xxx), float3(1.0.xxx), float3(2.0.xxx), float3(3.0.xxx));
47+
bar.Store3(0+0, asuint(_value2[0]));
48+
bar.Store3(0+16, asuint(_value2[1]));
49+
bar.Store3(0+32, asuint(_value2[2]));
50+
bar.Store3(0+48, asuint(_value2[3]));
5151
}
5252
{
5353
uint2 _value2[2] = { uint2(0u.xx), uint2(1u.xx) };

tests/out/msl/access.msl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct type_6 {
1919
};
2020
typedef AlignedWrapper type_7[1];
2121
struct Bar {
22-
metal::float4x4 matrix;
22+
metal::float4x3 matrix;
2323
type_3 matrix_array;
2424
metal::atomic_int atom;
2525
char _pad3[4];
@@ -51,15 +51,15 @@ vertex foo_vertOutput foo_vert(
5151
type_13 c;
5252
float baz = foo;
5353
foo = 1.0;
54-
metal::float4x4 matrix = bar.matrix;
54+
metal::float4x3 matrix = bar.matrix;
5555
type_6 arr = bar.arr;
5656
float b = bar.matrix[3].x;
5757
int a = bar.data[(1 + (_buffer_sizes.size0 - 120 - 8) / 8) - 2u].value;
5858
float _e27 = read_from_private(foo);
5959
for(int _i=0; _i<5; ++_i) c.inner[_i] = type_13 {a, static_cast<int>(b), 3, 4, 5}.inner[_i];
6060
c.inner[vi + 1u] = 42;
6161
int value = c.inner[vi];
62-
return foo_vertOutput { matrix * static_cast<metal::float4>(metal::int4(value)) };
62+
return foo_vertOutput { metal::float4(matrix * static_cast<metal::float4>(metal::int4(value)), 2.0) };
6363
}
6464

6565

@@ -71,7 +71,7 @@ fragment foo_fragOutput foo_frag(
7171
, constant _mslBufferSizes& _buffer_sizes [[buffer(24)]]
7272
) {
7373
bar.matrix[1].z = 1.0;
74-
bar.matrix = metal::float4x4(metal::float4(0.0), metal::float4(1.0), metal::float4(2.0), metal::float4(3.0));
74+
bar.matrix = metal::float4x3(metal::float3(0.0), metal::float3(1.0), metal::float3(2.0), metal::float3(3.0));
7575
for(int _i=0; _i<2; ++_i) bar.arr.inner[_i] = type_6 {metal::uint2(0u), metal::uint2(1u)}.inner[_i];
7676
bar.data[1].value = 1;
7777
return foo_fragOutput { metal::float4(0.0) };

0 commit comments

Comments
 (0)