Debian Codegraph @

third_party/spirv-cross/reference/opt/shaders-msl/comp/inverse.comp

raw
1 #pragma clang diagnostic ignored "-Wmissing-prototypes"
2
3 #include <metal_stdlib>
4 #include <simd/simd.h>
5
6 using namespace metal;
7
8 struct MatrixOut
9 {
10 float2x2 m2out;
11 float3x3 m3out;
12 float4x4 m4out;
13 };
14
15 struct MatrixIn
16 {
17 float2x2 m2in;
18 float3x3 m3in;
19 float4x4 m4in;
20 };
21
22 constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(1u);
23
24 // Returns the determinant of a 2x2 matrix.
25 static inline __attribute__((always_inline))
26 float spvDet2x2(float a1, float a2, float b1, float b2)
27 {
28 return a1 * b2 - b1 * a2;
29 }
30
31 // Returns the determinant of a 3x3 matrix.
32 static inline __attribute__((always_inline))
33 float spvDet3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3)
34 {
35 return a1 * spvDet2x2(b2, b3, c2, c3) - b1 * spvDet2x2(a2, a3, c2, c3) + c1 * spvDet2x2(a2, a3, b2, b3);
36 }
37
38 // Returns the inverse of a matrix, by using the algorithm of calculating the classical
39 // adjoint and dividing by the determinant. The contents of the matrix are changed.
40 static inline __attribute__((always_inline))
41 float4x4 spvInverse4x4(float4x4 m)
42 {
43 float4x4 adj; // The adjoint matrix (inverse after dividing by determinant)
44
45 // Create the transpose of the cofactors, as the classical adjoint of the matrix.
46 adj[0][0] = spvDet3x3(m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]);
47 adj[0][1] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3]);
48 adj[0][2] = spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[3][1], m[3][2], m[3][3]);
49 adj[0][3] = -spvDet3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3]);
50
51 adj[1][0] = -spvDet3x3(m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]);
52 adj[1][1] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], m[3][3]);
53 adj[1][2] = -spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[3][0], m[3][2], m[3][3]);
54 adj[1][3] = spvDet3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3]);
55
56 adj[2][0] = spvDet3x3(m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]);
57 adj[2][1] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], m[3][3]);
58 adj[2][2] = spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[3][0], m[3][1], m[3][3]);
59 adj[2][3] = -spvDet3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3]);
60
61 adj[3][0] = -spvDet3x3(m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]);
62 adj[3][1] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], m[3][2]);
63 adj[3][2] = -spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[3][0], m[3][1], m[3][2]);
64 adj[3][3] = spvDet3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2]);
65
66 // Calculate the determinant as a combination of the cofactors of the first row.
67 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] * m[3][0]);
68
69 // Divide the classical adjoint matrix by the determinant.
70 // If determinant is zero, matrix is not invertable, so leave it unchanged.
71 return (det != 0.0f) ? (adj * (1.0f / det)) : m;
72 }
73
74 // Returns the inverse of a matrix, by using the algorithm of calculating the classical
75 // adjoint and dividing by the determinant. The contents of the matrix are changed.
76 static inline __attribute__((always_inline))
77 float3x3 spvInverse3x3(float3x3 m)
78 {
79 float3x3 adj; // The adjoint matrix (inverse after dividing by determinant)
80
81 // Create the transpose of the cofactors, as the classical adjoint of the matrix.
82 adj[0][0] = spvDet2x2(m[1][1], m[1][2], m[2][1], m[2][2]);
83 adj[0][1] = -spvDet2x2(m[0][1], m[0][2], m[2][1], m[2][2]);
84 adj[0][2] = spvDet2x2(m[0][1], m[0][2], m[1][1], m[1][2]);
85
86 adj[1][0] = -spvDet2x2(m[1][0], m[1][2], m[2][0], m[2][2]);
87 adj[1][1] = spvDet2x2(m[0][0], m[0][2], m[2][0], m[2][2]);
88 adj[1][2] = -spvDet2x2(m[0][0], m[0][2], m[1][0], m[1][2]);
89
90 adj[2][0] = spvDet2x2(m[1][0], m[1][1], m[2][0], m[2][1]);
91 adj[2][1] = -spvDet2x2(m[0][0], m[0][1], m[2][0], m[2][1]);
92 adj[2][2] = spvDet2x2(m[0][0], m[0][1], m[1][0], m[1][1]);
93
94 // Calculate the determinant as a combination of the cofactors of the first row.
95 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]);
96
97 // Divide the classical adjoint matrix by the determinant.
98 // If determinant is zero, matrix is not invertable, so leave it unchanged.
99 return (det != 0.0f) ? (adj * (1.0f / det)) : m;
100 }
101
102 // Returns the inverse of a matrix, by using the algorithm of calculating the classical
103 // adjoint and dividing by the determinant. The contents of the matrix are changed.
104 static inline __attribute__((always_inline))
105 float2x2 spvInverse2x2(float2x2 m)
106 {
107 float2x2 adj; // The adjoint matrix (inverse after dividing by determinant)
108
109 // Create the transpose of the cofactors, as the classical adjoint of the matrix.
110 adj[0][0] = m[1][1];
111 adj[0][1] = -m[0][1];
112
113 adj[1][0] = -m[1][0];
114 adj[1][1] = m[0][0];
115
116 // Calculate the determinant as a combination of the cofactors of the first row.
117 float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]);
118
119 // Divide the classical adjoint matrix by the determinant.
120 // If determinant is zero, matrix is not invertable, so leave it unchanged.
121 return (det != 0.0f) ? (adj * (1.0f / det)) : m;
122 }
123
124 kernel void main0(device MatrixOut& _15 [[buffer(0)]], const device MatrixIn& _20 [[buffer(1)]])
125 {
126 _15.m2out = spvInverse2x2(_20.m2in);
127 _15.m3out = spvInverse3x3(_20.m3in);
128 _15.m4out = spvInverse4x4(_20.m4in);
129 }
130