xref: /aosp_15_r20/external/skia/tests/sksl/shared/Matrices.metal (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#include <metal_stdlib>
2#include <simd/simd.h>
3#ifdef __clang__
4#pragma clang diagnostic ignored "-Wall"
5#endif
6using namespace metal;
7struct Uniforms {
8    half4 colorGreen;
9    half4 colorRed;
10};
11struct Inputs {
12};
13struct Outputs {
14    half4 sk_FragColor [[color(0)]];
15};
16
17thread bool operator==(const half2x2 left, const half2x2 right);
18thread bool operator!=(const half2x2 left, const half2x2 right);
19
20thread bool operator==(const half3x3 left, const half3x3 right);
21thread bool operator!=(const half3x3 left, const half3x3 right);
22
23thread bool operator==(const half4x4 left, const half4x4 right);
24thread bool operator!=(const half4x4 left, const half4x4 right);
25
26thread bool operator==(const float2x2 left, const float2x2 right);
27thread bool operator!=(const float2x2 left, const float2x2 right);
28
29thread bool operator==(const float4x4 left, const float4x4 right);
30thread bool operator!=(const float4x4 left, const float4x4 right);
31thread bool operator==(const half2x2 left, const half2x2 right) {
32    return all(left[0] == right[0]) &&
33           all(left[1] == right[1]);
34}
35thread bool operator!=(const half2x2 left, const half2x2 right) {
36    return !(left == right);
37}
38thread half2x2& operator*=(thread half2x2& left, thread const half2x2& right) {
39    left = left * right;
40    return left;
41}
42thread bool operator==(const half3x3 left, const half3x3 right) {
43    return all(left[0] == right[0]) &&
44           all(left[1] == right[1]) &&
45           all(left[2] == right[2]);
46}
47thread bool operator!=(const half3x3 left, const half3x3 right) {
48    return !(left == right);
49}
50thread bool operator==(const half4x4 left, const half4x4 right) {
51    return all(left[0] == right[0]) &&
52           all(left[1] == right[1]) &&
53           all(left[2] == right[2]) &&
54           all(left[3] == right[3]);
55}
56thread bool operator!=(const half4x4 left, const half4x4 right) {
57    return !(left == right);
58}
59thread bool operator==(const float2x2 left, const float2x2 right) {
60    return all(left[0] == right[0]) &&
61           all(left[1] == right[1]);
62}
63thread bool operator!=(const float2x2 left, const float2x2 right) {
64    return !(left == right);
65}
66thread float2x2& operator*=(thread float2x2& left, thread const float2x2& right) {
67    left = left * right;
68    return left;
69}
70thread bool operator==(const float4x4 left, const float4x4 right) {
71    return all(left[0] == right[0]) &&
72           all(left[1] == right[1]) &&
73           all(left[2] == right[2]) &&
74           all(left[3] == right[3]);
75}
76thread bool operator!=(const float4x4 left, const float4x4 right) {
77    return !(left == right);
78}
79bool test_half_b() {
80    bool ok = true;
81    half2x2 m1 = half2x2(half2(1.0h, 2.0h), half2(3.0h, 4.0h));
82    ok = ok && m1 == half2x2(half2(1.0h, 2.0h), half2(3.0h, 4.0h));
83    half2x2 m3 = m1;
84    ok = ok && m3 == half2x2(half2(1.0h, 2.0h), half2(3.0h, 4.0h));
85    half2x2 m4 = half2x2(6.0h);
86    ok = ok && m4 == half2x2(half2(6.0h, 0.0h), half2(0.0h, 6.0h));
87    m3 *= m4;
88    ok = ok && m3 == half2x2(half2(6.0h, 12.0h), half2(18.0h, 24.0h));
89    half2x2 m5 = half2x2(m1[1].y);
90    ok = ok && m5 == half2x2(half2(4.0h, 0.0h), half2(0.0h, 4.0h));
91    m1 += m5;
92    ok = ok && m1 == half2x2(half2(5.0h, 2.0h), half2(3.0h, 8.0h));
93    half2x2 m7 = half2x2(half2(5.0h, 6.0h), half2(7.0h, 8.0h));
94    ok = ok && m7 == half2x2(half2(5.0h, 6.0h), half2(7.0h, 8.0h));
95    half3x3 m9 = half3x3(9.0h);
96    ok = ok && m9 == half3x3(half3(9.0h, 0.0h, 0.0h), half3(0.0h, 9.0h, 0.0h), half3(0.0h, 0.0h, 9.0h));
97    half4x4 m10 = half4x4(11.0h);
98    ok = ok && m10 == half4x4(half4(11.0h, 0.0h, 0.0h, 0.0h), half4(0.0h, 11.0h, 0.0h, 0.0h), half4(0.0h, 0.0h, 11.0h, 0.0h), half4(0.0h, 0.0h, 0.0h, 11.0h));
99    half4x4 m11 = half4x4(half4(20.0h, 20.0h, 20.0h, 20.0h), half4(20.0h, 20.0h, 20.0h, 20.0h), half4(20.0h, 20.0h, 20.0h, 20.0h), half4(20.0h, 20.0h, 20.0h, 20.0h));
100    m11 -= m10;
101    ok = ok && m11 == half4x4(half4(9.0h, 20.0h, 20.0h, 20.0h), half4(20.0h, 9.0h, 20.0h, 20.0h), half4(20.0h, 20.0h, 9.0h, 20.0h), half4(20.0h, 20.0h, 20.0h, 9.0h));
102    return ok;
103}
104bool test_comma_b() {
105    float2x2 x;
106    float2x2 y;
107    return ((x = float2x2(float2(1.0, 2.0), float2(3.0, 4.0)), y = float2x2(float2(1.0, 2.0), float2(3.0, 4.0))), x == y);
108}
109fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
110    Outputs _out;
111    (void)_out;
112    bool _0_ok = true;
113    float2x2 _1_m1 = float2x2(float2(1.0, 2.0), float2(3.0, 4.0));
114    _0_ok = _0_ok && _1_m1 == float2x2(float2(1.0, 2.0), float2(3.0, 4.0));
115    float2x2 _2_m3 = _1_m1;
116    _0_ok = _0_ok && _2_m3 == float2x2(float2(1.0, 2.0), float2(3.0, 4.0));
117    const float2x2 _3_m4 = float2x2(6.0);
118    _2_m3 *= _3_m4;
119    _0_ok = _0_ok && _2_m3 == float2x2(float2(6.0, 12.0), float2(18.0, 24.0));
120    float2x2 _4_m5 = float2x2(_1_m1[1].y);
121    _0_ok = _0_ok && _4_m5 == float2x2(float2(4.0, 0.0), float2(0.0, 4.0));
122    _1_m1 += _4_m5;
123    _0_ok = _0_ok && _1_m1 == float2x2(float2(5.0, 2.0), float2(3.0, 8.0));
124    const float4x4 _7_m10 = float4x4(11.0);
125    float4x4 _8_m11 = float4x4(float4(20.0, 20.0, 20.0, 20.0), float4(20.0, 20.0, 20.0, 20.0), float4(20.0, 20.0, 20.0, 20.0), float4(20.0, 20.0, 20.0, 20.0));
126    _8_m11 -= _7_m10;
127    _0_ok = _0_ok && _8_m11 == float4x4(float4(9.0, 20.0, 20.0, 20.0), float4(20.0, 9.0, 20.0, 20.0), float4(20.0, 20.0, 9.0, 20.0), float4(20.0, 20.0, 20.0, 9.0));
128    _out.sk_FragColor = (_0_ok && test_half_b()) && test_comma_b() ? _uniforms.colorGreen : _uniforms.colorRed;
129    return _out;
130}
131