1cb93a386Sopenharmony_ci#include <metal_stdlib> 2cb93a386Sopenharmony_ci#include <simd/simd.h> 3cb93a386Sopenharmony_ciusing namespace metal; 4cb93a386Sopenharmony_cistruct Uniforms { 5cb93a386Sopenharmony_ci half4 colorGreen; 6cb93a386Sopenharmony_ci half4 colorRed; 7cb93a386Sopenharmony_ci}; 8cb93a386Sopenharmony_cistruct Inputs { 9cb93a386Sopenharmony_ci}; 10cb93a386Sopenharmony_cistruct Outputs { 11cb93a386Sopenharmony_ci half4 sk_FragColor [[color(0)]]; 12cb93a386Sopenharmony_ci}; 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_citemplate <typename T1, typename T2, size_t N> 15cb93a386Sopenharmony_cibool operator==(thread const array<T1, N>& left, thread const array<T2, N>& right); 16cb93a386Sopenharmony_citemplate <typename T1, typename T2, size_t N> 17cb93a386Sopenharmony_cibool operator!=(thread const array<T1, N>& left, thread const array<T2, N>& right); 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_cithread bool operator==(const float2x2 left, const float2x2 right); 20cb93a386Sopenharmony_cithread bool operator!=(const float2x2 left, const float2x2 right); 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_citemplate <size_t N> 23cb93a386Sopenharmony_ciarray<half, N> array_of_half_from_float(thread const array<float, N>& x) { 24cb93a386Sopenharmony_ci array<half, N> result; 25cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 26cb93a386Sopenharmony_ci result[i] = half(x[i]); 27cb93a386Sopenharmony_ci } 28cb93a386Sopenharmony_ci return result; 29cb93a386Sopenharmony_ci} 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_citemplate <size_t N> 32cb93a386Sopenharmony_ciarray<float, N> array_of_float_from_half(thread const array<half, N>& x) { 33cb93a386Sopenharmony_ci array<float, N> result; 34cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 35cb93a386Sopenharmony_ci result[i] = float(x[i]); 36cb93a386Sopenharmony_ci } 37cb93a386Sopenharmony_ci return result; 38cb93a386Sopenharmony_ci} 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_citemplate <size_t N> 41cb93a386Sopenharmony_ciarray<short3, N> array_of_short3_from_int3(thread const array<int3, N>& x) { 42cb93a386Sopenharmony_ci array<short3, N> result; 43cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 44cb93a386Sopenharmony_ci result[i] = short3(x[i]); 45cb93a386Sopenharmony_ci } 46cb93a386Sopenharmony_ci return result; 47cb93a386Sopenharmony_ci} 48cb93a386Sopenharmony_ci 49cb93a386Sopenharmony_citemplate <size_t N> 50cb93a386Sopenharmony_ciarray<int3, N> array_of_int3_from_short3(thread const array<short3, N>& x) { 51cb93a386Sopenharmony_ci array<int3, N> result; 52cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 53cb93a386Sopenharmony_ci result[i] = int3(x[i]); 54cb93a386Sopenharmony_ci } 55cb93a386Sopenharmony_ci return result; 56cb93a386Sopenharmony_ci} 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_citemplate <size_t N> 59cb93a386Sopenharmony_ciarray<float2x2, N> array_of_float2x2_from_half2x2(thread const array<half2x2, N>& x) { 60cb93a386Sopenharmony_ci array<float2x2, N> result; 61cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 62cb93a386Sopenharmony_ci result[i] = float2x2(x[i]); 63cb93a386Sopenharmony_ci } 64cb93a386Sopenharmony_ci return result; 65cb93a386Sopenharmony_ci} 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_citemplate <size_t N> 68cb93a386Sopenharmony_ciarray<half2x2, N> array_of_half2x2_from_float2x2(thread const array<float2x2, N>& x) { 69cb93a386Sopenharmony_ci array<half2x2, N> result; 70cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 71cb93a386Sopenharmony_ci result[i] = half2x2(x[i]); 72cb93a386Sopenharmony_ci } 73cb93a386Sopenharmony_ci return result; 74cb93a386Sopenharmony_ci} 75cb93a386Sopenharmony_ci 76cb93a386Sopenharmony_citemplate <typename T1, typename T2, size_t N> 77cb93a386Sopenharmony_cibool operator==(thread const array<T1, N>& left, thread const array<T2, N>& right) { 78cb93a386Sopenharmony_ci for (size_t index = 0; index < N; ++index) { 79cb93a386Sopenharmony_ci if (!all(left[index] == right[index])) { 80cb93a386Sopenharmony_ci return false; 81cb93a386Sopenharmony_ci } 82cb93a386Sopenharmony_ci } 83cb93a386Sopenharmony_ci return true; 84cb93a386Sopenharmony_ci} 85cb93a386Sopenharmony_ci 86cb93a386Sopenharmony_citemplate <typename T1, typename T2, size_t N> 87cb93a386Sopenharmony_cibool operator!=(thread const array<T1, N>& left, thread const array<T2, N>& right) { 88cb93a386Sopenharmony_ci return !(left == right); 89cb93a386Sopenharmony_ci} 90cb93a386Sopenharmony_cithread bool operator==(const float2x2 left, const float2x2 right) { 91cb93a386Sopenharmony_ci return all(left[0] == right[0]) && 92cb93a386Sopenharmony_ci all(left[1] == right[1]); 93cb93a386Sopenharmony_ci} 94cb93a386Sopenharmony_cithread bool operator!=(const float2x2 left, const float2x2 right) { 95cb93a386Sopenharmony_ci return !(left == right); 96cb93a386Sopenharmony_ci} 97cb93a386Sopenharmony_cifragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { 98cb93a386Sopenharmony_ci Outputs _out; 99cb93a386Sopenharmony_ci (void)_out; 100cb93a386Sopenharmony_ci array<float, 4> f = array<float, 4>{1.0, 2.0, 3.0, 4.0}; 101cb93a386Sopenharmony_ci array<half, 4> h = array_of_half_from_float(f); 102cb93a386Sopenharmony_ci f = array_of_float_from_half(h); 103cb93a386Sopenharmony_ci h = array_of_half_from_float(f); 104cb93a386Sopenharmony_ci array<int3, 3> i3 = array<int3, 3>{int3(1), int3(2), int3(3)}; 105cb93a386Sopenharmony_ci array<short3, 3> s3 = array_of_short3_from_int3(i3); 106cb93a386Sopenharmony_ci i3 = array_of_int3_from_short3(s3); 107cb93a386Sopenharmony_ci s3 = array_of_short3_from_int3(i3); 108cb93a386Sopenharmony_ci 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))}; 109cb93a386Sopenharmony_ci array<float2x2, 2> f2x2 = array_of_float2x2_from_half2x2(h2x2); 110cb93a386Sopenharmony_ci f2x2 = array_of_float2x2_from_half2x2(h2x2); 111cb93a386Sopenharmony_ci h2x2 = array_of_half2x2_from_float2x2(f2x2); 112cb93a386Sopenharmony_ci _out.sk_FragColor = (f == array_of_float_from_half(h) && i3 == array_of_int3_from_short3(s3)) && f2x2 == array_of_float2x2_from_half2x2(h2x2) ? _uniforms.colorGreen : _uniforms.colorRed; 113cb93a386Sopenharmony_ci return _out; 114cb93a386Sopenharmony_ci} 115