Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hlsl-out] fix matrix not being declared as transposed #1784

Merged
merged 7 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/back/hlsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ becomes `vec * mat`, etc. This acts as the inverse transpose making the results
The only time we don't get this implicit transposition is when reading matrices from Uniforms/Push Constants.
To deal with this, we add `row_major` to all declarations of matrices in Uniforms/Push Constants.

Finally because all of our matrices are transposed, if you use `mat3x4`, it'll become `float4x3` in HLSL.
Finally because all of our matrices are transposed, if you use `mat3x4`, it'll become `float3x4` in HLSL
(HLSL has inverted col/row notation).

[hlsl]: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl
*/
Expand Down
15 changes: 13 additions & 2 deletions src/back/hlsl/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ impl<W: fmt::Write> super::Writer<'_, W> {
rows,
width,
} => {
// we declared the matrix as transposed in HLSL
let (rows, columns) = (columns, rows);
teoxoy marked this conversation as resolved.
Show resolved Hide resolved

write!(
self.out,
"{}{}x{}(",
Expand Down Expand Up @@ -253,6 +256,9 @@ impl<W: fmt::Write> super::Writer<'_, W> {
rows,
width,
} => {
// we declared the matrix as transposed in HLSL
let (rows, columns) = (columns, rows);

// first, assign the value to a temporary
writeln!(self.out, "{}{{", level)?;
let depth = level.0 + 1;
Expand Down Expand Up @@ -401,8 +407,13 @@ impl<W: fmt::Write> super::Writer<'_, W> {
crate::TypeInner::Vector { width, .. } => Parent::Array {
stride: width as u32,
},
crate::TypeInner::Matrix { rows, width, .. } => Parent::Array {
stride: width as u32 * if rows > crate::VectorSize::Bi { 4 } else { 2 },
crate::TypeInner::Matrix { columns, width, .. } => Parent::Array {
stride: width as u32
* if columns > crate::VectorSize::Bi {
4
} else {
2
},
},
_ => unreachable!(),
},
Expand Down
2 changes: 1 addition & 1 deletion src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
self.out,
"{}{}x{}",
crate::ScalarKind::Float.to_hlsl_str(width)?,
back::vector_size_str(rows),
back::vector_size_str(columns),
back::vector_size_str(rows),
)?;
}
TypeInner::Image {
Expand Down
6 changes: 3 additions & 3 deletions tests/in/access.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct AlignedWrapper {
};

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

return matrix * vec4<f32>(vec4<i32>(value));
return vec4<f32>(matrix * vec4<f32>(vec4<i32>(value)), 2.0);
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/out/glsl/access.atomics.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct AlignedWrapper {
int value;
};
layout(std430) buffer Bar_block_0Compute {
mat4x4 matrix;
mat4x3 matrix;
mat2x2 matrix_array[2];
int atom;
uvec2 arr[2];
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/access.foo_frag.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct AlignedWrapper {
int value;
};
layout(std430) buffer Bar_block_0Fragment {
mat4x4 matrix;
mat4x3 matrix;
mat2x2 matrix_array[2];
int atom;
uvec2 arr[2];
Expand All @@ -23,7 +23,7 @@ float read_from_private(inout float foo_1) {

void main() {
_group_0_binding_0_fs.matrix[1][2] = 1.0;
_group_0_binding_0_fs.matrix = mat4x4(vec4(0.0), vec4(1.0), vec4(2.0), vec4(3.0));
_group_0_binding_0_fs.matrix = mat4x3(vec3(0.0), vec3(1.0), vec3(2.0), vec3(3.0));
_group_0_binding_0_fs.arr = uvec2[2](uvec2(0u), uvec2(1u));
_group_0_binding_0_fs.data[1].value = 1;
_fs2p_location0 = vec4(0.0);
Expand Down
6 changes: 3 additions & 3 deletions tests/out/glsl/access.foo_vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct AlignedWrapper {
int value;
};
layout(std430) buffer Bar_block_0Vertex {
mat4x4 matrix;
mat4x3 matrix;
mat2x2 matrix_array[2];
int atom;
uvec2 arr[2];
Expand All @@ -26,15 +26,15 @@ void main() {
int c[5] = int[5](0, 0, 0, 0, 0);
float baz = foo;
foo = 1.0;
mat4x4 matrix = _group_0_binding_0_vs.matrix;
mat4x3 matrix = _group_0_binding_0_vs.matrix;
uvec2 arr[2] = _group_0_binding_0_vs.arr;
float b = _group_0_binding_0_vs.matrix[3][0];
int a = _group_0_binding_0_vs.data[(uint(_group_0_binding_0_vs.data.length()) - 2u)].value;
float _e27 = read_from_private(foo);
c = int[5](a, int(b), 3, 4, 5);
c[(vi + 1u)] = 42;
int value = c[vi];
gl_Position = (matrix * vec4(ivec4(value)));
gl_Position = vec4((matrix * vec4(ivec4(value))), 2.0);
gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
return;
}
Expand Down
14 changes: 7 additions & 7 deletions tests/out/hlsl/access.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ float4 foo_vert(uint vi : SV_VertexID) : SV_Position

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

float4 foo_frag() : SV_Target0
{
bar.Store(8+16+0, asuint(1.0));
{
float4x4 _value2 = float4x4(float4(0.0.xxxx), float4(1.0.xxxx), float4(2.0.xxxx), float4(3.0.xxxx));
bar.Store4(0+0, asuint(_value2[0]));
bar.Store4(0+16, asuint(_value2[1]));
bar.Store4(0+32, asuint(_value2[2]));
bar.Store4(0+48, asuint(_value2[3]));
float4x3 _value2 = float4x3(float3(0.0.xxx), float3(1.0.xxx), float3(2.0.xxx), float3(3.0.xxx));
bar.Store3(0+0, asuint(_value2[0]));
bar.Store3(0+16, asuint(_value2[1]));
bar.Store3(0+32, asuint(_value2[2]));
bar.Store3(0+48, asuint(_value2[3]));
}
{
uint2 _value2[2] = { uint2(0u.xx), uint2(1u.xx) };
Expand Down
8 changes: 4 additions & 4 deletions tests/out/msl/access.msl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct type_6 {
};
typedef AlignedWrapper type_7[1];
struct Bar {
metal::float4x4 matrix;
metal::float4x3 matrix;
type_3 matrix_array;
metal::atomic_int atom;
char _pad3[4];
Expand Down Expand Up @@ -51,15 +51,15 @@ vertex foo_vertOutput foo_vert(
type_13 c;
float baz = foo;
foo = 1.0;
metal::float4x4 matrix = bar.matrix;
metal::float4x3 matrix = bar.matrix;
type_6 arr = bar.arr;
float b = bar.matrix[3].x;
int a = bar.data[(1 + (_buffer_sizes.size0 - 120 - 8) / 8) - 2u].value;
float _e27 = read_from_private(foo);
for(int _i=0; _i<5; ++_i) c.inner[_i] = type_13 {a, static_cast<int>(b), 3, 4, 5}.inner[_i];
c.inner[vi + 1u] = 42;
int value = c.inner[vi];
return foo_vertOutput { matrix * static_cast<metal::float4>(metal::int4(value)) };
return foo_vertOutput { metal::float4(matrix * static_cast<metal::float4>(metal::int4(value)), 2.0) };
}


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