xref: /aosp_15_r20/external/skia/tests/sksl/intrinsics/Inverse.metal (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker#include <metal_stdlib>
2*c8dee2aaSAndroid Build Coastguard Worker#include <simd/simd.h>
3*c8dee2aaSAndroid Build Coastguard Worker#ifdef __clang__
4*c8dee2aaSAndroid Build Coastguard Worker#pragma clang diagnostic ignored "-Wall"
5*c8dee2aaSAndroid Build Coastguard Worker#endif
6*c8dee2aaSAndroid Build Coastguard Workerusing namespace metal;
7*c8dee2aaSAndroid Build Coastguard Workerstruct Uniforms {
8*c8dee2aaSAndroid Build Coastguard Worker    half4 colorGreen;
9*c8dee2aaSAndroid Build Coastguard Worker    half4 colorRed;
10*c8dee2aaSAndroid Build Coastguard Worker};
11*c8dee2aaSAndroid Build Coastguard Workerstruct Inputs {
12*c8dee2aaSAndroid Build Coastguard Worker};
13*c8dee2aaSAndroid Build Coastguard Workerstruct Outputs {
14*c8dee2aaSAndroid Build Coastguard Worker    half4 sk_FragColor [[color(0)]];
15*c8dee2aaSAndroid Build Coastguard Worker};
16*c8dee2aaSAndroid Build Coastguard Worker
17*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float2x2 left, const float2x2 right);
18*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float2x2 left, const float2x2 right);
19*c8dee2aaSAndroid Build Coastguard Worker
20*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float3x3 left, const float3x3 right);
21*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float3x3 left, const float3x3 right);
22*c8dee2aaSAndroid Build Coastguard Worker
23*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float4x4 left, const float4x4 right);
24*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float4x4 left, const float4x4 right);
25*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float2x2 left, const float2x2 right) {
26*c8dee2aaSAndroid Build Coastguard Worker    return all(left[0] == right[0]) &&
27*c8dee2aaSAndroid Build Coastguard Worker           all(left[1] == right[1]);
28*c8dee2aaSAndroid Build Coastguard Worker}
29*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float2x2 left, const float2x2 right) {
30*c8dee2aaSAndroid Build Coastguard Worker    return !(left == right);
31*c8dee2aaSAndroid Build Coastguard Worker}
32*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float3x3 left, const float3x3 right) {
33*c8dee2aaSAndroid Build Coastguard Worker    return all(left[0] == right[0]) &&
34*c8dee2aaSAndroid Build Coastguard Worker           all(left[1] == right[1]) &&
35*c8dee2aaSAndroid Build Coastguard Worker           all(left[2] == right[2]);
36*c8dee2aaSAndroid Build Coastguard Worker}
37*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float3x3 left, const float3x3 right) {
38*c8dee2aaSAndroid Build Coastguard Worker    return !(left == right);
39*c8dee2aaSAndroid Build Coastguard Worker}
40*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float4x4 left, const float4x4 right) {
41*c8dee2aaSAndroid Build Coastguard Worker    return all(left[0] == right[0]) &&
42*c8dee2aaSAndroid Build Coastguard Worker           all(left[1] == right[1]) &&
43*c8dee2aaSAndroid Build Coastguard Worker           all(left[2] == right[2]) &&
44*c8dee2aaSAndroid Build Coastguard Worker           all(left[3] == right[3]);
45*c8dee2aaSAndroid Build Coastguard Worker}
46*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float4x4 left, const float4x4 right) {
47*c8dee2aaSAndroid Build Coastguard Worker    return !(left == right);
48*c8dee2aaSAndroid Build Coastguard Worker}
49*c8dee2aaSAndroid Build Coastguard Worker
50*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T>
51*c8dee2aaSAndroid Build Coastguard Workermatrix<T, 3, 3> mat3_inverse(matrix<T, 3, 3> m) {
52*c8dee2aaSAndroid Build Coastguard WorkerT
53*c8dee2aaSAndroid Build Coastguard Worker a00 = m[0].x, a01 = m[0].y, a02 = m[0].z,
54*c8dee2aaSAndroid Build Coastguard Worker a10 = m[1].x, a11 = m[1].y, a12 = m[1].z,
55*c8dee2aaSAndroid Build Coastguard Worker a20 = m[2].x, a21 = m[2].y, a22 = m[2].z,
56*c8dee2aaSAndroid Build Coastguard Worker b01 =  a22*a11 - a12*a21,
57*c8dee2aaSAndroid Build Coastguard Worker b11 = -a22*a10 + a12*a20,
58*c8dee2aaSAndroid Build Coastguard Worker b21 =  a21*a10 - a11*a20,
59*c8dee2aaSAndroid Build Coastguard Worker det = a00*b01 + a01*b11 + a02*b21;
60*c8dee2aaSAndroid Build Coastguard Workerreturn matrix<T, 3, 3>(
61*c8dee2aaSAndroid Build Coastguard Worker b01, (-a22*a01 + a02*a21), ( a12*a01 - a02*a11),
62*c8dee2aaSAndroid Build Coastguard Worker b11, ( a22*a00 - a02*a20), (-a12*a00 + a02*a10),
63*c8dee2aaSAndroid Build Coastguard Worker b21, (-a21*a00 + a01*a20), ( a11*a00 - a01*a10)) * (1/det);
64*c8dee2aaSAndroid Build Coastguard Worker}
65*c8dee2aaSAndroid Build Coastguard Worker
66*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T>
67*c8dee2aaSAndroid Build Coastguard Workermatrix<T, 2, 2> mat2_inverse(matrix<T, 2, 2> m) {
68*c8dee2aaSAndroid Build Coastguard Workerreturn matrix<T, 2, 2>(m[1].y, -m[0].y, -m[1].x, m[0].x) * (1/determinant(m));
69*c8dee2aaSAndroid Build Coastguard Worker}
70*c8dee2aaSAndroid Build Coastguard Worker
71*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T>
72*c8dee2aaSAndroid Build Coastguard Workermatrix<T, 4, 4> mat4_inverse(matrix<T, 4, 4> m) {
73*c8dee2aaSAndroid Build Coastguard WorkerT
74*c8dee2aaSAndroid Build Coastguard Worker a00 = m[0].x, a01 = m[0].y, a02 = m[0].z, a03 = m[0].w,
75*c8dee2aaSAndroid Build Coastguard Worker a10 = m[1].x, a11 = m[1].y, a12 = m[1].z, a13 = m[1].w,
76*c8dee2aaSAndroid Build Coastguard Worker a20 = m[2].x, a21 = m[2].y, a22 = m[2].z, a23 = m[2].w,
77*c8dee2aaSAndroid Build Coastguard Worker a30 = m[3].x, a31 = m[3].y, a32 = m[3].z, a33 = m[3].w,
78*c8dee2aaSAndroid Build Coastguard Worker b00 = a00*a11 - a01*a10,
79*c8dee2aaSAndroid Build Coastguard Worker b01 = a00*a12 - a02*a10,
80*c8dee2aaSAndroid Build Coastguard Worker b02 = a00*a13 - a03*a10,
81*c8dee2aaSAndroid Build Coastguard Worker b03 = a01*a12 - a02*a11,
82*c8dee2aaSAndroid Build Coastguard Worker b04 = a01*a13 - a03*a11,
83*c8dee2aaSAndroid Build Coastguard Worker b05 = a02*a13 - a03*a12,
84*c8dee2aaSAndroid Build Coastguard Worker b06 = a20*a31 - a21*a30,
85*c8dee2aaSAndroid Build Coastguard Worker b07 = a20*a32 - a22*a30,
86*c8dee2aaSAndroid Build Coastguard Worker b08 = a20*a33 - a23*a30,
87*c8dee2aaSAndroid Build Coastguard Worker b09 = a21*a32 - a22*a31,
88*c8dee2aaSAndroid Build Coastguard Worker b10 = a21*a33 - a23*a31,
89*c8dee2aaSAndroid Build Coastguard Worker b11 = a22*a33 - a23*a32,
90*c8dee2aaSAndroid Build Coastguard Worker det = b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06;
91*c8dee2aaSAndroid Build Coastguard Workerreturn matrix<T, 4, 4>(
92*c8dee2aaSAndroid Build Coastguard Worker a11*b11 - a12*b10 + a13*b09,
93*c8dee2aaSAndroid Build Coastguard Worker a02*b10 - a01*b11 - a03*b09,
94*c8dee2aaSAndroid Build Coastguard Worker a31*b05 - a32*b04 + a33*b03,
95*c8dee2aaSAndroid Build Coastguard Worker a22*b04 - a21*b05 - a23*b03,
96*c8dee2aaSAndroid Build Coastguard Worker a12*b08 - a10*b11 - a13*b07,
97*c8dee2aaSAndroid Build Coastguard Worker a00*b11 - a02*b08 + a03*b07,
98*c8dee2aaSAndroid Build Coastguard Worker a32*b02 - a30*b05 - a33*b01,
99*c8dee2aaSAndroid Build Coastguard Worker a20*b05 - a22*b02 + a23*b01,
100*c8dee2aaSAndroid Build Coastguard Worker a10*b10 - a11*b08 + a13*b06,
101*c8dee2aaSAndroid Build Coastguard Worker a01*b08 - a00*b10 - a03*b06,
102*c8dee2aaSAndroid Build Coastguard Worker a30*b04 - a31*b02 + a33*b00,
103*c8dee2aaSAndroid Build Coastguard Worker a21*b02 - a20*b04 - a23*b00,
104*c8dee2aaSAndroid Build Coastguard Worker a11*b07 - a10*b09 - a12*b06,
105*c8dee2aaSAndroid Build Coastguard Worker a00*b09 - a01*b07 + a02*b06,
106*c8dee2aaSAndroid Build Coastguard Worker a31*b01 - a30*b03 - a32*b00,
107*c8dee2aaSAndroid Build Coastguard Worker a20*b03 - a21*b01 + a22*b00) * (1/det);
108*c8dee2aaSAndroid Build Coastguard Worker}
109*c8dee2aaSAndroid Build Coastguard Workerfragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
110*c8dee2aaSAndroid Build Coastguard Worker    Outputs _out;
111*c8dee2aaSAndroid Build Coastguard Worker    (void)_out;
112*c8dee2aaSAndroid Build Coastguard Worker    const float2x2 matrix2x2 = float2x2(float2(1.0, 2.0), float2(3.0, 4.0));
113*c8dee2aaSAndroid Build Coastguard Worker    float2x2 inv2x2 = float2x2(float2(-2.0, 1.0), float2(1.5, -0.5));
114*c8dee2aaSAndroid Build Coastguard Worker    float3x3 inv3x3 = float3x3(float3(-24.0, 18.0, 5.0), float3(20.0, -15.0, -4.0), float3(-5.0, 4.0, 1.0));
115*c8dee2aaSAndroid Build Coastguard Worker    float4x4 inv4x4 = float4x4(float4(-2.0, -0.5, 1.0, 0.5), float4(1.0, 0.5, 0.0, -0.5), float4(-8.0, -1.0, 2.0, 2.0), float4(3.0, 0.5, -1.0, -0.5));
116*c8dee2aaSAndroid Build Coastguard Worker    float Zero = float(_uniforms.colorGreen.z);
117*c8dee2aaSAndroid Build Coastguard Worker    _out.sk_FragColor = (((((float2x2(float2(-2.0, 1.0), float2(1.5, -0.5)) == inv2x2 && float3x3(float3(-24.0, 18.0, 5.0), float3(20.0, -15.0, -4.0), float3(-5.0, 4.0, 1.0)) == inv3x3) && float4x4(float4(-2.0, -0.5, 1.0, 0.5), float4(1.0, 0.5, 0.0, -0.5), float4(-8.0, -1.0, 2.0, 2.0), float4(3.0, 0.5, -1.0, -0.5)) == inv4x4) && mat3_inverse(float3x3(float3(1.0, 2.0, 3.0), float3(4.0, 5.0, 6.0), float3(7.0, 8.0, 9.0))) != inv3x3) && mat2_inverse(matrix2x2 + (float2x2(1.0, 1.0, 1.0, 1.0) * Zero)) == inv2x2) && mat3_inverse(float3x3(float3(1.0, 2.0, 3.0), float3(0.0, 1.0, 4.0), float3(5.0, 6.0, 0.0)) + (float3x3(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) * Zero)) == inv3x3) && mat4_inverse(float4x4(float4(1.0, 0.0, 0.0, 1.0), float4(0.0, 2.0, 1.0, 2.0), float4(2.0, 1.0, 0.0, 1.0), float4(2.0, 0.0, 1.0, 4.0)) + (float4x4(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) * Zero)) == inv4x4 ? _uniforms.colorGreen : _uniforms.colorRed;
118*c8dee2aaSAndroid Build Coastguard Worker    return _out;
119*c8dee2aaSAndroid Build Coastguard Worker}
120