xref: /aosp_15_r20/external/skia/tests/sksl/shared/ArrayNarrowingConversions.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
17template <typename T1, typename T2>
18bool operator==(const array_ref<T1> left, const array_ref<T2> right);
19template <typename T1, typename T2>
20bool operator!=(const array_ref<T1> left, const array_ref<T2> right);
21
22template <size_t N>
23array<int, N> array_of_int_from_short(thread const array<short, N>& x) {
24    array<int, N> result;
25    for (int i = 0; i < N; ++i) {
26        result[i] = int(x[i]);
27    }
28    return result;
29}
30
31template <size_t N>
32array<short, N> array_of_short_from_int(thread const array<int, N>& x) {
33    array<short, N> result;
34    for (int i = 0; i < N; ++i) {
35        result[i] = short(x[i]);
36    }
37    return result;
38}
39
40template <size_t N>
41array<float, N> array_of_float_from_half(thread const array<half, N>& x) {
42    array<float, N> result;
43    for (int i = 0; i < N; ++i) {
44        result[i] = float(x[i]);
45    }
46    return result;
47}
48
49template <size_t N>
50array<half, N> array_of_half_from_float(thread const array<float, N>& x) {
51    array<half, N> result;
52    for (int i = 0; i < N; ++i) {
53        result[i] = half(x[i]);
54    }
55    return result;
56}
57
58template <typename T1, typename T2>
59bool operator==(const array_ref<T1> left, const array_ref<T2> right) {
60    if (left.size() != right.size()) {
61        return false;
62    }
63    for (size_t index = 0; index < left.size(); ++index) {
64        if (!all(left[index] == right[index])) {
65            return false;
66        }
67    }
68    return true;
69}
70
71template <typename T1, typename T2>
72bool operator!=(const array_ref<T1> left, const array_ref<T2> right) {
73    return !(left == right);
74}
75fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
76    Outputs _out;
77    (void)_out;
78    array<int, 2> i2 = array<int, 2>{1, 2};
79    array<short, 2> s2 = array<short, 2>{1, 2};
80    array<float, 2> f2 = array<float, 2>{1.0, 2.0};
81    array<half, 2> h2 = array<half, 2>{1.0h, 2.0h};
82    i2 = array_of_int_from_short(s2);
83    s2 = array_of_short_from_int(i2);
84    f2 = array_of_float_from_half(h2);
85    h2 = array_of_half_from_float(f2);
86    const array<float, 2> cf2 = array<float, 2>{1.0, 2.0};
87    _out.sk_FragColor = ((make_array_ref(i2) == make_array_ref(array_of_int_from_short(s2)) && make_array_ref(f2) == make_array_ref(array_of_float_from_half(h2))) && make_array_ref(i2) == make_array_ref(array<int, 2>{1, 2})) && make_array_ref(array_of_float_from_half(h2)) == make_array_ref(cf2) ? _uniforms.colorGreen : _uniforms.colorRed;
88    return _out;
89}
90