xref: /aosp_15_r20/external/skia/tests/sksl/shared/MatrixOpEqualsES2.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 colorRed;
9    half4 colorGreen;
10};
11struct Inputs {
12};
13struct Outputs {
14    half4 sk_FragColor [[color(0)]];
15};
16
17thread bool operator==(const half3x3 left, const half3x3 right);
18thread bool operator!=(const half3x3 left, const half3x3 right);
19
20thread bool operator==(const half4x4 left, const half4x4 right);
21thread bool operator!=(const half4x4 left, const half4x4 right);
22
23thread bool operator==(const half2x2 left, const half2x2 right);
24thread bool operator!=(const half2x2 left, const half2x2 right);
25
26thread bool operator==(const float3x3 left, const float3x3 right);
27thread bool operator!=(const float3x3 left, const float3x3 right);
28
29thread bool operator==(const float4x4 left, const float4x4 right);
30thread bool operator!=(const float4x4 left, const float4x4 right);
31
32thread bool operator==(const float2x2 left, const float2x2 right);
33thread bool operator!=(const float2x2 left, const float2x2 right);
34thread bool operator==(const half3x3 left, const half3x3 right) {
35    return all(left[0] == right[0]) &&
36           all(left[1] == right[1]) &&
37           all(left[2] == right[2]);
38}
39thread bool operator!=(const half3x3 left, const half3x3 right) {
40    return !(left == right);
41}
42thread half3x3 operator/(const half3x3 left, const half3x3 right) {
43    return half3x3(left[0] / right[0], left[1] / right[1], left[2] / right[2]);
44}
45thread half3x3& operator/=(thread half3x3& left, thread const half3x3& right) {
46    left = left / right;
47    return left;
48}
49thread bool operator==(const half4x4 left, const half4x4 right) {
50    return all(left[0] == right[0]) &&
51           all(left[1] == right[1]) &&
52           all(left[2] == right[2]) &&
53           all(left[3] == right[3]);
54}
55thread bool operator!=(const half4x4 left, const half4x4 right) {
56    return !(left == right);
57}
58thread bool operator==(const half2x2 left, const half2x2 right) {
59    return all(left[0] == right[0]) &&
60           all(left[1] == right[1]);
61}
62thread bool operator!=(const half2x2 left, const half2x2 right) {
63    return !(left == right);
64}
65thread half2x2 operator/(const half2x2 left, const half2x2 right) {
66    return half2x2(left[0] / right[0], left[1] / right[1]);
67}
68thread half2x2& operator/=(thread half2x2& left, thread const half2x2& right) {
69    left = left / right;
70    return left;
71}
72thread half2x2& operator*=(thread half2x2& left, thread const half2x2& right) {
73    left = left * right;
74    return left;
75}
76thread half3x3& operator*=(thread half3x3& left, thread const half3x3& right) {
77    left = left * right;
78    return left;
79}
80thread bool operator==(const float3x3 left, const float3x3 right) {
81    return all(left[0] == right[0]) &&
82           all(left[1] == right[1]) &&
83           all(left[2] == right[2]);
84}
85thread bool operator!=(const float3x3 left, const float3x3 right) {
86    return !(left == right);
87}
88thread float3x3 operator/(const float3x3 left, const float3x3 right) {
89    return float3x3(left[0] / right[0], left[1] / right[1], left[2] / right[2]);
90}
91thread float3x3& operator/=(thread float3x3& left, thread const float3x3& right) {
92    left = left / right;
93    return left;
94}
95thread bool operator==(const float4x4 left, const float4x4 right) {
96    return all(left[0] == right[0]) &&
97           all(left[1] == right[1]) &&
98           all(left[2] == right[2]) &&
99           all(left[3] == right[3]);
100}
101thread bool operator!=(const float4x4 left, const float4x4 right) {
102    return !(left == right);
103}
104thread bool operator==(const float2x2 left, const float2x2 right) {
105    return all(left[0] == right[0]) &&
106           all(left[1] == right[1]);
107}
108thread bool operator!=(const float2x2 left, const float2x2 right) {
109    return !(left == right);
110}
111thread float2x2 operator/(const float2x2 left, const float2x2 right) {
112    return float2x2(left[0] / right[0], left[1] / right[1]);
113}
114thread float2x2& operator/=(thread float2x2& left, thread const float2x2& right) {
115    left = left / right;
116    return left;
117}
118thread float2x2& operator*=(thread float2x2& left, thread const float2x2& right) {
119    left = left * right;
120    return left;
121}
122thread float3x3& operator*=(thread float3x3& left, thread const float3x3& right) {
123    left = left * right;
124    return left;
125}
126bool test_matrix_op_matrix_half_b() {
127    bool ok = true;
128    {
129        const half3x3 splat_4 = half3x3(half3(4.0h, 4.0h, 4.0h), half3(4.0h, 4.0h, 4.0h), half3(4.0h, 4.0h, 4.0h));
130        const half3x3 splat_2 = half3x3(half3(2.0h, 2.0h, 2.0h), half3(2.0h, 2.0h, 2.0h), half3(2.0h, 2.0h, 2.0h));
131        half3x3 m = half3x3(2.0h);
132        m += splat_4;
133        ok = ok && m == half3x3(half3(6.0h, 4.0h, 4.0h), half3(4.0h, 6.0h, 4.0h), half3(4.0h, 4.0h, 6.0h));
134        m = half3x3(2.0h);
135        m -= splat_4;
136        ok = ok && m == half3x3(half3(-2.0h, -4.0h, -4.0h), half3(-4.0h, -2.0h, -4.0h), half3(-4.0h, -4.0h, -2.0h));
137        m = half3x3(2.0h);
138        m /= splat_4;
139        ok = ok && m == half3x3(0.5h);
140        m = splat_4;
141        m += half3x3(2.0h);
142        ok = ok && m == half3x3(half3(6.0h, 4.0h, 4.0h), half3(4.0h, 6.0h, 4.0h), half3(4.0h, 4.0h, 6.0h));
143        m = splat_4;
144        m -= half3x3(2.0h);
145        ok = ok && m == half3x3(half3(2.0h, 4.0h, 4.0h), half3(4.0h, 2.0h, 4.0h), half3(4.0h, 4.0h, 2.0h));
146        m = splat_4;
147        m /= splat_2;
148        ok = ok && m == half3x3(half3(2.0h, 2.0h, 2.0h), half3(2.0h, 2.0h, 2.0h), half3(2.0h, 2.0h, 2.0h));
149    }
150    {
151        half4x4 m = half4x4(half4(1.0h, 2.0h, 3.0h, 4.0h), half4(5.0h, 6.0h, 7.0h, 8.0h), half4(9.0h, 10.0h, 11.0h, 12.0h), half4(13.0h, 14.0h, 15.0h, 16.0h));
152        m += half4x4(half4(16.0h, 15.0h, 14.0h, 13.0h), half4(12.0h, 11.0h, 10.0h, 9.0h), half4(8.0h, 7.0h, 6.0h, 5.0h), half4(4.0h, 3.0h, 2.0h, 1.0h));
153        ok = ok && m == half4x4(half4(17.0h, 17.0h, 17.0h, 17.0h), half4(17.0h, 17.0h, 17.0h, 17.0h), half4(17.0h, 17.0h, 17.0h, 17.0h), half4(17.0h, 17.0h, 17.0h, 17.0h));
154    }
155    {
156        half2x2 m = half2x2(half2(10.0h, 20.0h), half2(30.0h, 40.0h));
157        m -= half2x2(half2(1.0h, 2.0h), half2(3.0h, 4.0h));
158        ok = ok && m == half2x2(half2(9.0h, 18.0h), half2(27.0h, 36.0h));
159    }
160    {
161        half2x2 m = half2x2(half2(2.0h, 4.0h), half2(6.0h, 8.0h));
162        m /= half2x2(half2(2.0h, 2.0h), half2(2.0h, 4.0h));
163        ok = ok && m == half2x2(half2(1.0h, 2.0h), half2(3.0h, 2.0h));
164    }
165    {
166        half2x2 m = half2x2(half2(1.0h, 2.0h), half2(7.0h, 4.0h));
167        m *= half2x2(half2(3.0h, 5.0h), half2(3.0h, 2.0h));
168        ok = ok && m == half2x2(half2(38.0h, 26.0h), half2(17.0h, 14.0h));
169    }
170    {
171        half3x3 m = half3x3(half3(10.0h, 4.0h, 2.0h), half3(20.0h, 5.0h, 3.0h), half3(10.0h, 6.0h, 5.0h));
172        m *= half3x3(half3(3.0h, 3.0h, 4.0h), half3(2.0h, 3.0h, 4.0h), half3(4.0h, 9.0h, 2.0h));
173        ok = ok && m == half3x3(half3(130.0h, 51.0h, 35.0h), half3(120.0h, 47.0h, 33.0h), half3(240.0h, 73.0h, 45.0h));
174    }
175    return ok;
176}
177fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
178    Outputs _out;
179    (void)_out;
180    bool _0_ok = true;
181    {
182        const float3x3 _1_splat_4 = float3x3(float3(4.0, 4.0, 4.0), float3(4.0, 4.0, 4.0), float3(4.0, 4.0, 4.0));
183        const float3x3 _2_splat_2 = float3x3(float3(2.0, 2.0, 2.0), float3(2.0, 2.0, 2.0), float3(2.0, 2.0, 2.0));
184        float3x3 _3_m = float3x3(2.0);
185        _3_m += _1_splat_4;
186        _0_ok = _0_ok && _3_m == float3x3(float3(6.0, 4.0, 4.0), float3(4.0, 6.0, 4.0), float3(4.0, 4.0, 6.0));
187        _3_m = float3x3(2.0);
188        _3_m -= _1_splat_4;
189        _0_ok = _0_ok && _3_m == float3x3(float3(-2.0, -4.0, -4.0), float3(-4.0, -2.0, -4.0), float3(-4.0, -4.0, -2.0));
190        _3_m = float3x3(2.0);
191        _3_m /= _1_splat_4;
192        _0_ok = _0_ok && _3_m == float3x3(0.5);
193        _3_m = _1_splat_4;
194        _3_m += float3x3(2.0);
195        _0_ok = _0_ok && _3_m == float3x3(float3(6.0, 4.0, 4.0), float3(4.0, 6.0, 4.0), float3(4.0, 4.0, 6.0));
196        _3_m = _1_splat_4;
197        _3_m -= float3x3(2.0);
198        _0_ok = _0_ok && _3_m == float3x3(float3(2.0, 4.0, 4.0), float3(4.0, 2.0, 4.0), float3(4.0, 4.0, 2.0));
199        _3_m = _1_splat_4;
200        _3_m /= _2_splat_2;
201        _0_ok = _0_ok && _3_m == float3x3(float3(2.0, 2.0, 2.0), float3(2.0, 2.0, 2.0), float3(2.0, 2.0, 2.0));
202    }
203    {
204        float4x4 _4_m = float4x4(float4(1.0, 2.0, 3.0, 4.0), float4(5.0, 6.0, 7.0, 8.0), float4(9.0, 10.0, 11.0, 12.0), float4(13.0, 14.0, 15.0, 16.0));
205        _4_m += float4x4(float4(16.0, 15.0, 14.0, 13.0), float4(12.0, 11.0, 10.0, 9.0), float4(8.0, 7.0, 6.0, 5.0), float4(4.0, 3.0, 2.0, 1.0));
206        _0_ok = _0_ok && _4_m == float4x4(float4(17.0, 17.0, 17.0, 17.0), float4(17.0, 17.0, 17.0, 17.0), float4(17.0, 17.0, 17.0, 17.0), float4(17.0, 17.0, 17.0, 17.0));
207    }
208    {
209        float2x2 _5_m = float2x2(float2(10.0, 20.0), float2(30.0, 40.0));
210        _5_m -= float2x2(float2(1.0, 2.0), float2(3.0, 4.0));
211        _0_ok = _0_ok && _5_m == float2x2(float2(9.0, 18.0), float2(27.0, 36.0));
212    }
213    {
214        float2x2 _6_m = float2x2(float2(2.0, 4.0), float2(6.0, 8.0));
215        _6_m /= float2x2(float2(2.0, 2.0), float2(2.0, 4.0));
216        _0_ok = _0_ok && _6_m == float2x2(float2(1.0, 2.0), float2(3.0, 2.0));
217    }
218    {
219        float2x2 _7_m = float2x2(float2(1.0, 2.0), float2(7.0, 4.0));
220        _7_m *= float2x2(float2(3.0, 5.0), float2(3.0, 2.0));
221        _0_ok = _0_ok && _7_m == float2x2(float2(38.0, 26.0), float2(17.0, 14.0));
222    }
223    {
224        float3x3 _8_m = float3x3(float3(10.0, 4.0, 2.0), float3(20.0, 5.0, 3.0), float3(10.0, 6.0, 5.0));
225        _8_m *= float3x3(float3(3.0, 3.0, 4.0), float3(2.0, 3.0, 4.0), float3(4.0, 9.0, 2.0));
226        _0_ok = _0_ok && _8_m == float3x3(float3(130.0, 51.0, 35.0), float3(120.0, 47.0, 33.0), float3(240.0, 73.0, 45.0));
227    }
228    _out.sk_FragColor = _0_ok && test_matrix_op_matrix_half_b() ? _uniforms.colorGreen : _uniforms.colorRed;
229    return _out;
230}
231