-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[naga/wgsl-out]: polyfill
inverse
function (#6385)
- Loading branch information
Showing
12 changed files
with
367 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn _naga_inverse_2x2_f16(m: mat2x2<f16>) -> mat2x2<f16> { | ||
var adj: mat2x2<f16>; | ||
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: f16 = m[0][0] * m[1][1] - m[1][0] * m[0][1]; | ||
return adj * (1 / det); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
fn _naga_inverse_3x3_f16(m: mat3x3<f16>) -> mat3x3<f16> { | ||
var adj: mat3x3<f16>; | ||
|
||
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: f16 = (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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
fn _naga_inverse_4x4_f16(m: mat4x4<f16>) -> mat4x4<f16> { | ||
let sub_factor00: f16 = m[2][2] * m[3][3] - m[3][2] * m[2][3]; | ||
let sub_factor01: f16 = m[2][1] * m[3][3] - m[3][1] * m[2][3]; | ||
let sub_factor02: f16 = m[2][1] * m[3][2] - m[3][1] * m[2][2]; | ||
let sub_factor03: f16 = m[2][0] * m[3][3] - m[3][0] * m[2][3]; | ||
let sub_factor04: f16 = m[2][0] * m[3][2] - m[3][0] * m[2][2]; | ||
let sub_factor05: f16 = m[2][0] * m[3][1] - m[3][0] * m[2][1]; | ||
let sub_factor06: f16 = m[1][2] * m[3][3] - m[3][2] * m[1][3]; | ||
let sub_factor07: f16 = m[1][1] * m[3][3] - m[3][1] * m[1][3]; | ||
let sub_factor08: f16 = m[1][1] * m[3][2] - m[3][1] * m[1][2]; | ||
let sub_factor09: f16 = m[1][0] * m[3][3] - m[3][0] * m[1][3]; | ||
let sub_factor10: f16 = m[1][0] * m[3][2] - m[3][0] * m[1][2]; | ||
let sub_factor11: f16 = m[1][1] * m[3][3] - m[3][1] * m[1][3]; | ||
let sub_factor12: f16 = m[1][0] * m[3][1] - m[3][0] * m[1][1]; | ||
let sub_factor13: f16 = m[1][2] * m[2][3] - m[2][2] * m[1][3]; | ||
let sub_factor14: f16 = m[1][1] * m[2][3] - m[2][1] * m[1][3]; | ||
let sub_factor15: f16 = m[1][1] * m[2][2] - m[2][1] * m[1][2]; | ||
let sub_factor16: f16 = m[1][0] * m[2][3] - m[2][0] * m[1][3]; | ||
let sub_factor17: f16 = m[1][0] * m[2][2] - m[2][0] * m[1][2]; | ||
let sub_factor18: f16 = m[1][0] * m[2][1] - m[2][0] * m[1][1]; | ||
|
||
var adj: mat4x4<f16>; | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::{ScalarKind, TypeInner, VectorSize}; | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct InversePolyfill { | ||
pub fun_name: &'static str, | ||
pub source: &'static str, | ||
} | ||
|
||
impl InversePolyfill { | ||
pub fn find_overload(ty: &TypeInner) -> Option<InversePolyfill> { | ||
let &TypeInner::Matrix { | ||
columns, | ||
rows, | ||
scalar, | ||
} = ty | ||
else { | ||
return None; | ||
}; | ||
|
||
if columns != rows || scalar.kind != ScalarKind::Float { | ||
return None; | ||
}; | ||
|
||
Self::polyfill_overload(columns, scalar.width) | ||
} | ||
|
||
const fn polyfill_overload( | ||
dimension: VectorSize, | ||
width: crate::Bytes, | ||
) -> Option<InversePolyfill> { | ||
const INVERSE_2X2_F32: &str = include_str!("inverse/inverse_2x2_f32.wgsl"); | ||
const INVERSE_3X3_F32: &str = include_str!("inverse/inverse_3x3_f32.wgsl"); | ||
const INVERSE_4X4_F32: &str = include_str!("inverse/inverse_4x4_f32.wgsl"); | ||
const INVERSE_2X2_F16: &str = include_str!("inverse/inverse_2x2_f16.wgsl"); | ||
const INVERSE_3X3_F16: &str = include_str!("inverse/inverse_3x3_f16.wgsl"); | ||
const INVERSE_4X4_F16: &str = include_str!("inverse/inverse_4x4_f16.wgsl"); | ||
|
||
match (dimension, width) { | ||
(VectorSize::Bi, 4) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_2x2_f32", | ||
source: INVERSE_2X2_F32, | ||
}), | ||
(VectorSize::Tri, 4) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_3x3_f32", | ||
source: INVERSE_3X3_F32, | ||
}), | ||
(VectorSize::Quad, 4) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_4x4_f32", | ||
source: INVERSE_4X4_F32, | ||
}), | ||
(VectorSize::Bi, 2) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_2x2_f16", | ||
source: INVERSE_2X2_F16, | ||
}), | ||
(VectorSize::Tri, 2) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_3x3_f16", | ||
source: INVERSE_3X3_F16, | ||
}), | ||
(VectorSize::Quad, 2) => Some(InversePolyfill { | ||
fun_name: "_naga_inverse_4x4_f16", | ||
source: INVERSE_4X4_F16, | ||
}), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.