xref: /aosp_15_r20/external/skia/tests/sksl/shared/StructComparison.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 S {
8    int x;
9    int y;
10    half2x2 m;
11    array<float, 5> a;
12};
13struct Uniforms {
14    half4 colorGreen;
15    half4 colorRed;
16    array<float, 5> testArray;
17};
18struct Inputs {
19};
20struct Outputs {
21    half4 sk_FragColor [[color(0)]];
22};
23
24thread bool operator==(const half2x2 left, const half2x2 right);
25thread bool operator!=(const half2x2 left, const half2x2 right);
26
27template <typename T1, typename T2>
28bool operator==(const array_ref<T1> left, const array_ref<T2> right);
29template <typename T1, typename T2>
30bool operator!=(const array_ref<T1> left, const array_ref<T2> right);
31
32thread bool operator==(thread const S& left, thread const S& right);
33thread bool operator!=(thread const S& left, thread const S& right);
34thread bool operator==(const half2x2 left, const half2x2 right) {
35    return all(left[0] == right[0]) &&
36           all(left[1] == right[1]);
37}
38thread bool operator!=(const half2x2 left, const half2x2 right) {
39    return !(left == right);
40}
41
42template <typename T1, typename T2>
43bool operator==(const array_ref<T1> left, const array_ref<T2> right) {
44    if (left.size() != right.size()) {
45        return false;
46    }
47    for (size_t index = 0; index < left.size(); ++index) {
48        if (!all(left[index] == right[index])) {
49            return false;
50        }
51    }
52    return true;
53}
54
55template <typename T1, typename T2>
56bool operator!=(const array_ref<T1> left, const array_ref<T2> right) {
57    return !(left == right);
58}
59thread bool operator==(thread const S& left, thread const S& right) {
60    return all(left.x == right.x) &&
61           all(left.y == right.y) &&
62           all(left.m == right.m) &&
63           (make_array_ref(left.a) == make_array_ref(right.a));
64}
65thread bool operator!=(thread const S& left, thread const S& right) {
66    return !(left == right);
67}
68fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
69    Outputs _out;
70    (void)_out;
71    array<float, 5> array = array<float, 5>{1.0, 2.0, 3.0, 4.0, 5.0};
72    S s1 = S{1, 2, half2x2(1.0h), array};
73    S s2 = S{1, 2, half2x2(1.0h), _uniforms.testArray};
74    S s3 = S{1, 2, half2x2(2.0h), array<float, 5>{1.0, 2.0, 3.0, 4.0, 5.0}};
75    _out.sk_FragColor = s1 == s2 && s1 != s3 ? _uniforms.colorGreen : _uniforms.colorRed;
76    return _out;
77}
78