xref: /aosp_15_r20/external/skia/tests/sksl/shared/ArrayCast.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 Workertemplate <typename T1, typename T2>
18*c8dee2aaSAndroid Build Coastguard Workerbool operator==(const array_ref<T1> left, const array_ref<T2> right);
19*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T1, typename T2>
20*c8dee2aaSAndroid Build Coastguard Workerbool operator!=(const array_ref<T1> left, const array_ref<T2> right);
21*c8dee2aaSAndroid Build Coastguard Worker
22*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float2x2 left, const float2x2 right);
23*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float2x2 left, const float2x2 right);
24*c8dee2aaSAndroid Build Coastguard Worker
25*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
26*c8dee2aaSAndroid Build Coastguard Workerarray<half, N> array_of_half_from_float(thread const array<float, N>& x) {
27*c8dee2aaSAndroid Build Coastguard Worker    array<half, N> result;
28*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
29*c8dee2aaSAndroid Build Coastguard Worker        result[i] = half(x[i]);
30*c8dee2aaSAndroid Build Coastguard Worker    }
31*c8dee2aaSAndroid Build Coastguard Worker    return result;
32*c8dee2aaSAndroid Build Coastguard Worker}
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
35*c8dee2aaSAndroid Build Coastguard Workerarray<float, N> array_of_float_from_half(thread const array<half, N>& x) {
36*c8dee2aaSAndroid Build Coastguard Worker    array<float, N> result;
37*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
38*c8dee2aaSAndroid Build Coastguard Worker        result[i] = float(x[i]);
39*c8dee2aaSAndroid Build Coastguard Worker    }
40*c8dee2aaSAndroid Build Coastguard Worker    return result;
41*c8dee2aaSAndroid Build Coastguard Worker}
42*c8dee2aaSAndroid Build Coastguard Worker
43*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
44*c8dee2aaSAndroid Build Coastguard Workerarray<short3, N> array_of_short3_from_int3(thread const array<int3, N>& x) {
45*c8dee2aaSAndroid Build Coastguard Worker    array<short3, N> result;
46*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
47*c8dee2aaSAndroid Build Coastguard Worker        result[i] = short3(x[i]);
48*c8dee2aaSAndroid Build Coastguard Worker    }
49*c8dee2aaSAndroid Build Coastguard Worker    return result;
50*c8dee2aaSAndroid Build Coastguard Worker}
51*c8dee2aaSAndroid Build Coastguard Worker
52*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
53*c8dee2aaSAndroid Build Coastguard Workerarray<int3, N> array_of_int3_from_short3(thread const array<short3, N>& x) {
54*c8dee2aaSAndroid Build Coastguard Worker    array<int3, N> result;
55*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
56*c8dee2aaSAndroid Build Coastguard Worker        result[i] = int3(x[i]);
57*c8dee2aaSAndroid Build Coastguard Worker    }
58*c8dee2aaSAndroid Build Coastguard Worker    return result;
59*c8dee2aaSAndroid Build Coastguard Worker}
60*c8dee2aaSAndroid Build Coastguard Worker
61*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
62*c8dee2aaSAndroid Build Coastguard Workerarray<float2x2, N> array_of_float2x2_from_half2x2(thread const array<half2x2, N>& x) {
63*c8dee2aaSAndroid Build Coastguard Worker    array<float2x2, N> result;
64*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
65*c8dee2aaSAndroid Build Coastguard Worker        result[i] = float2x2(x[i]);
66*c8dee2aaSAndroid Build Coastguard Worker    }
67*c8dee2aaSAndroid Build Coastguard Worker    return result;
68*c8dee2aaSAndroid Build Coastguard Worker}
69*c8dee2aaSAndroid Build Coastguard Worker
70*c8dee2aaSAndroid Build Coastguard Workertemplate <size_t N>
71*c8dee2aaSAndroid Build Coastguard Workerarray<half2x2, N> array_of_half2x2_from_float2x2(thread const array<float2x2, N>& x) {
72*c8dee2aaSAndroid Build Coastguard Worker    array<half2x2, N> result;
73*c8dee2aaSAndroid Build Coastguard Worker    for (int i = 0; i < N; ++i) {
74*c8dee2aaSAndroid Build Coastguard Worker        result[i] = half2x2(x[i]);
75*c8dee2aaSAndroid Build Coastguard Worker    }
76*c8dee2aaSAndroid Build Coastguard Worker    return result;
77*c8dee2aaSAndroid Build Coastguard Worker}
78*c8dee2aaSAndroid Build Coastguard Worker
79*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T1, typename T2>
80*c8dee2aaSAndroid Build Coastguard Workerbool operator==(const array_ref<T1> left, const array_ref<T2> right) {
81*c8dee2aaSAndroid Build Coastguard Worker    if (left.size() != right.size()) {
82*c8dee2aaSAndroid Build Coastguard Worker        return false;
83*c8dee2aaSAndroid Build Coastguard Worker    }
84*c8dee2aaSAndroid Build Coastguard Worker    for (size_t index = 0; index < left.size(); ++index) {
85*c8dee2aaSAndroid Build Coastguard Worker        if (!all(left[index] == right[index])) {
86*c8dee2aaSAndroid Build Coastguard Worker            return false;
87*c8dee2aaSAndroid Build Coastguard Worker        }
88*c8dee2aaSAndroid Build Coastguard Worker    }
89*c8dee2aaSAndroid Build Coastguard Worker    return true;
90*c8dee2aaSAndroid Build Coastguard Worker}
91*c8dee2aaSAndroid Build Coastguard Worker
92*c8dee2aaSAndroid Build Coastguard Workertemplate <typename T1, typename T2>
93*c8dee2aaSAndroid Build Coastguard Workerbool operator!=(const array_ref<T1> left, const array_ref<T2> right) {
94*c8dee2aaSAndroid Build Coastguard Worker    return !(left == right);
95*c8dee2aaSAndroid Build Coastguard Worker}
96*c8dee2aaSAndroid Build Coastguard Workerthread bool operator==(const float2x2 left, const float2x2 right) {
97*c8dee2aaSAndroid Build Coastguard Worker    return all(left[0] == right[0]) &&
98*c8dee2aaSAndroid Build Coastguard Worker           all(left[1] == right[1]);
99*c8dee2aaSAndroid Build Coastguard Worker}
100*c8dee2aaSAndroid Build Coastguard Workerthread bool operator!=(const float2x2 left, const float2x2 right) {
101*c8dee2aaSAndroid Build Coastguard Worker    return !(left == right);
102*c8dee2aaSAndroid Build Coastguard Worker}
103*c8dee2aaSAndroid Build Coastguard Workerfragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
104*c8dee2aaSAndroid Build Coastguard Worker    Outputs _out;
105*c8dee2aaSAndroid Build Coastguard Worker    (void)_out;
106*c8dee2aaSAndroid Build Coastguard Worker    array<float, 4> f = array<float, 4>{1.0, 2.0, 3.0, 4.0};
107*c8dee2aaSAndroid Build Coastguard Worker    array<half, 4> h = array_of_half_from_float(f);
108*c8dee2aaSAndroid Build Coastguard Worker    f = array_of_float_from_half(h);
109*c8dee2aaSAndroid Build Coastguard Worker    h = array_of_half_from_float(f);
110*c8dee2aaSAndroid Build Coastguard Worker    array<int3, 3> i3 = array<int3, 3>{int3(1), int3(2), int3(3)};
111*c8dee2aaSAndroid Build Coastguard Worker    array<short3, 3> s3 = array_of_short3_from_int3(i3);
112*c8dee2aaSAndroid Build Coastguard Worker    i3 = array_of_int3_from_short3(s3);
113*c8dee2aaSAndroid Build Coastguard Worker    s3 = array_of_short3_from_int3(i3);
114*c8dee2aaSAndroid Build Coastguard Worker    array<half2x2, 2> h2x2 = array<half2x2, 2>{half2x2(half2(1.0h, 2.0h), half2(3.0h, 4.0h)), half2x2(half2(5.0h, 6.0h), half2(7.0h, 8.0h))};
115*c8dee2aaSAndroid Build Coastguard Worker    array<float2x2, 2> f2x2 = array_of_float2x2_from_half2x2(h2x2);
116*c8dee2aaSAndroid Build Coastguard Worker    f2x2 = array_of_float2x2_from_half2x2(h2x2);
117*c8dee2aaSAndroid Build Coastguard Worker    h2x2 = array_of_half2x2_from_float2x2(f2x2);
118*c8dee2aaSAndroid Build Coastguard Worker    _out.sk_FragColor = (make_array_ref(f) == make_array_ref(array_of_float_from_half(h)) && make_array_ref(i3) == make_array_ref(array_of_int3_from_short3(s3))) && make_array_ref(f2x2) == make_array_ref(array_of_float2x2_from_half2x2(h2x2)) ? _uniforms.colorGreen : _uniforms.colorRed;
119*c8dee2aaSAndroid Build Coastguard Worker    return _out;
120*c8dee2aaSAndroid Build Coastguard Worker}
121