Skip to content

Commit

Permalink
[naga/wgsl-out]: add tests for polyfilled inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Oct 9, 2024
1 parent 496e5b9 commit 0cb32f3
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
17 changes: 17 additions & 0 deletions naga/tests/in/glsl/inverse-polyfill.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 450

void main() {
vec4 a4 = vec4(1.0);
vec4 b4 = vec4(2.0);
mat4 m4 = mat4(a4, b4, a4, b4);

vec3 a3 = vec3(1.0);
vec3 b3 = vec3(2.0);
mat3 m3 = mat3(a3, b3, a3);

mat2 m2 = mat2(1.0, 2.0, 3.0, 4.0);

mat4 m4_inverse = inverse(m4);
mat3 m3_inverse = inverse(m3);
mat2 m2_inverse = inverse(m2);
}
110 changes: 110 additions & 0 deletions naga/tests/out/wgsl/inverse-polyfill.frag.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
fn main_1() {
var a4_: vec4<f32> = vec4(1f);
var b4_: vec4<f32> = vec4(2f);
var m4_: mat4x4<f32>;
var a3_: vec3<f32> = vec3(1f);
var b3_: vec3<f32> = vec3(2f);
var m3_: mat3x3<f32>;
var m2_: mat2x2<f32> = mat2x2<f32>(vec2<f32>(1f, 2f), vec2<f32>(3f, 4f));
var m4_inverse: mat4x4<f32>;
var m3_inverse: mat3x3<f32>;
var m2_inverse: mat2x2<f32>;

let _e6 = a4_;
let _e7 = b4_;
let _e8 = a4_;
let _e9 = b4_;
m4_ = mat4x4<f32>(vec4<f32>(_e6.x, _e6.y, _e6.z, _e6.w), vec4<f32>(_e7.x, _e7.y, _e7.z, _e7.w), vec4<f32>(_e8.x, _e8.y, _e8.z, _e8.w), vec4<f32>(_e9.x, _e9.y, _e9.z, _e9.w));
let _e38 = a3_;
let _e39 = b3_;
let _e40 = a3_;
m3_ = mat3x3<f32>(vec3<f32>(_e38.x, _e38.y, _e38.z), vec3<f32>(_e39.x, _e39.y, _e39.z), vec3<f32>(_e40.x, _e40.y, _e40.z));
let _e64 = m4_;
m4_inverse = _naga_inverse_4x4_f32(_e64);
let _e68 = m3_;
m3_inverse = _naga_inverse_3x3_f32(_e68);
let _e72 = m2_;
m2_inverse = _naga_inverse_2x2_f32(_e72);
return;
}

@fragment
fn main() {
main_1();
return;
}

fn _naga_inverse_4x4_f32(m: mat4x4<f32>) -> mat4x4<f32> {
let sub_factor00: f32 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
let sub_factor01: f32 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
let sub_factor02: f32 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
let sub_factor03: f32 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
let sub_factor04: f32 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
let sub_factor05: f32 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
let sub_factor06: f32 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
let sub_factor07: f32 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
let sub_factor08: f32 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
let sub_factor09: f32 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
let sub_factor10: f32 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
let sub_factor11: f32 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
let sub_factor12: f32 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
let sub_factor13: f32 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
let sub_factor14: f32 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
let sub_factor15: f32 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
let sub_factor16: f32 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
let sub_factor17: f32 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
let sub_factor18: f32 = m[1][0] * m[2][1] - m[2][0] * m[1][1];

var adj: mat4x4<f32>;
adj[0][0] = (m[1][1] * sub_factor00 - m[1][2] * sub_factor01 + m[1][3] * sub_factor02);
adj[1][0] = - (m[1][0] * sub_factor00 - m[1][2] * sub_factor03 + m[1][3] * sub_factor04);
adj[2][0] = (m[1][0] * sub_factor01 - m[1][1] * sub_factor03 + m[1][3] * sub_factor05);
adj[3][0] = - (m[1][0] * sub_factor02 - m[1][1] * sub_factor04 + m[1][2] * sub_factor05);
adj[0][1] = - (m[0][1] * sub_factor00 - m[0][2] * sub_factor01 + m[0][3] * sub_factor02);
adj[1][1] = (m[0][0] * sub_factor00 - m[0][2] * sub_factor03 + m[0][3] * sub_factor04);
adj[2][1] = - (m[0][0] * sub_factor01 - m[0][1] * sub_factor03 + m[0][3] * sub_factor05);
adj[3][1] = (m[0][0] * sub_factor02 - m[0][1] * sub_factor04 + m[0][2] * sub_factor05);
adj[0][2] = (m[0][1] * sub_factor06 - m[0][2] * sub_factor07 + m[0][3] * sub_factor08);
adj[1][2] = - (m[0][0] * sub_factor06 - m[0][2] * sub_factor09 + m[0][3] * sub_factor10);
adj[2][2] = (m[0][0] * sub_factor11 - m[0][1] * sub_factor09 + m[0][3] * sub_factor12);
adj[3][2] = - (m[0][0] * sub_factor08 - m[0][1] * sub_factor10 + m[0][2] * sub_factor12);
adj[0][3] = - (m[0][1] * sub_factor13 - m[0][2] * sub_factor14 + m[0][3] * sub_factor15);
adj[1][3] = (m[0][0] * sub_factor13 - m[0][2] * sub_factor16 + m[0][3] * sub_factor17);
adj[2][3] = - (m[0][0] * sub_factor14 - m[0][1] * sub_factor16 + m[0][3] * sub_factor18);
adj[3][3] = (m[0][0] * sub_factor15 - m[0][1] * sub_factor17 + m[0][2] * sub_factor18);

let det = (m[0][0] * adj[0][0] + m[0][1] * adj[1][0] + m[0][2] * adj[2][0] + m[0][3] * adj[3][0]);

return adj * (1 / det);
}

fn _naga_inverse_3x3_f32(m: mat3x3<f32>) -> mat3x3<f32> {
var adj: mat3x3<f32>;

adj[0][0] = (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
adj[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
adj[2][0] = (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
adj[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
adj[1][1] = (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
adj[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
adj[0][2] = (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
adj[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
adj[2][2] = (m[0][0] * m[1][1] - m[1][0] * m[0][1]);

let det: f32 = (m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]));

return adj * (1 / det);
}

fn _naga_inverse_2x2_f32(m: mat2x2<f32>) -> mat2x2<f32> {
var adj: mat2x2<f32>;
adj[0][0] = m[1][1];
adj[0][1] = -m[0][1];
adj[1][0] = -m[1][0];
adj[1][1] = m[0][0];

let det: f32 = m[0][0] * m[1][1] - m[1][0] * m[0][1];
return adj * (1 / det);
}
12 changes: 11 additions & 1 deletion naga/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct SpirvOutParameters {
struct WgslOutParameters {
#[serde(default)]
explicit_types: bool,
polyfill_inverse: bool,
}

#[derive(Default, serde::Deserialize)]
Expand Down Expand Up @@ -665,6 +666,7 @@ fn write_output_wgsl(

let mut flags = wgsl::WriterFlags::empty();
flags.set(wgsl::WriterFlags::EXPLICIT_TYPES, params.explicit_types);
flags.set(wgsl::WriterFlags::POLYFILL_INVERSE, params.polyfill_inverse);

let string = wgsl::write_string(module, info, flags).expect("WGSL write failed");

Expand Down Expand Up @@ -1127,7 +1129,15 @@ fn convert_glsl_folder() {

#[cfg(wgsl_out)]
{
write_output_wgsl(&input, &module, &info, &WgslOutParameters::default());
write_output_wgsl(
&input,
&module,
&info,
&WgslOutParameters {
polyfill_inverse: true,
..Default::default()
},
);
}
}
}

0 comments on commit 0cb32f3

Please sign in to comment.