1cb93a386Sopenharmony_ci#include <metal_stdlib>
2cb93a386Sopenharmony_ci#include <simd/simd.h>
3cb93a386Sopenharmony_ciusing namespace metal;
4cb93a386Sopenharmony_cistruct Uniforms {
5cb93a386Sopenharmony_ci    half4 src;
6cb93a386Sopenharmony_ci    half4 dst;
7cb93a386Sopenharmony_ci};
8cb93a386Sopenharmony_cistruct Inputs {
9cb93a386Sopenharmony_ci};
10cb93a386Sopenharmony_cistruct Outputs {
11cb93a386Sopenharmony_ci    half4 sk_FragColor [[color(0)]];
12cb93a386Sopenharmony_ci};
13cb93a386Sopenharmony_cihalf3 _blend_set_color_luminance_h3h3hh3(half3 hueSatColor, half alpha, half3 lumColor) {
14cb93a386Sopenharmony_ci    half lum = dot(half3(0.30000001192092896h, 0.5899999737739563h, 0.10999999940395355h), lumColor);
15cb93a386Sopenharmony_ci    half3 result = (lum - dot(half3(0.30000001192092896h, 0.5899999737739563h, 0.10999999940395355h), hueSatColor)) + hueSatColor;
16cb93a386Sopenharmony_ci    half minComp = min(min(result.x, result.y), result.z);
17cb93a386Sopenharmony_ci    half maxComp = max(max(result.x, result.y), result.z);
18cb93a386Sopenharmony_ci    if (minComp < 0.0h && lum != minComp) {
19cb93a386Sopenharmony_ci        result = lum + (result - lum) * (lum / (lum - minComp));
20cb93a386Sopenharmony_ci    }
21cb93a386Sopenharmony_ci    if (maxComp > alpha && maxComp != lum) {
22cb93a386Sopenharmony_ci        return lum + ((result - lum) * (alpha - lum)) / (maxComp - lum);
23cb93a386Sopenharmony_ci    } else {
24cb93a386Sopenharmony_ci        return result;
25cb93a386Sopenharmony_ci    }
26cb93a386Sopenharmony_ci}
27cb93a386Sopenharmony_cihalf3 _blend_set_color_saturation_helper_h3h3h(half3 minMidMax, half sat) {
28cb93a386Sopenharmony_ci    if (minMidMax.x < minMidMax.z) {
29cb93a386Sopenharmony_ci        return half3(0.0h, (sat * (minMidMax.y - minMidMax.x)) / (minMidMax.z - minMidMax.x), sat);
30cb93a386Sopenharmony_ci    } else {
31cb93a386Sopenharmony_ci        return half3(0.0h);
32cb93a386Sopenharmony_ci    }
33cb93a386Sopenharmony_ci}
34cb93a386Sopenharmony_cihalf3 _blend_set_color_saturation_h3h3h3(half3 hueLumColor, half3 satColor) {
35cb93a386Sopenharmony_ci    half sat = max(max(satColor.x, satColor.y), satColor.z) - min(min(satColor.x, satColor.y), satColor.z);
36cb93a386Sopenharmony_ci    if (hueLumColor.x <= hueLumColor.y) {
37cb93a386Sopenharmony_ci        if (hueLumColor.y <= hueLumColor.z) {
38cb93a386Sopenharmony_ci            return _blend_set_color_saturation_helper_h3h3h(hueLumColor, sat);
39cb93a386Sopenharmony_ci        } else if (hueLumColor.x <= hueLumColor.z) {
40cb93a386Sopenharmony_ci            return _blend_set_color_saturation_helper_h3h3h(hueLumColor.xzy, sat).xzy;
41cb93a386Sopenharmony_ci        } else {
42cb93a386Sopenharmony_ci            return _blend_set_color_saturation_helper_h3h3h(hueLumColor.zxy, sat).yzx;
43cb93a386Sopenharmony_ci        }
44cb93a386Sopenharmony_ci    } else if (hueLumColor.x <= hueLumColor.z) {
45cb93a386Sopenharmony_ci        return _blend_set_color_saturation_helper_h3h3h(hueLumColor.yxz, sat).yxz;
46cb93a386Sopenharmony_ci    } else if (hueLumColor.y <= hueLumColor.z) {
47cb93a386Sopenharmony_ci        return _blend_set_color_saturation_helper_h3h3h(hueLumColor.yzx, sat).zxy;
48cb93a386Sopenharmony_ci    } else {
49cb93a386Sopenharmony_ci        return _blend_set_color_saturation_helper_h3h3h(hueLumColor.zyx, sat).zyx;
50cb93a386Sopenharmony_ci    }
51cb93a386Sopenharmony_ci}
52cb93a386Sopenharmony_cifragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
53cb93a386Sopenharmony_ci    Outputs _out;
54cb93a386Sopenharmony_ci    (void)_out;
55cb93a386Sopenharmony_ci    half _0_alpha = _uniforms.dst.w * _uniforms.src.w;
56cb93a386Sopenharmony_ci    half3 _1_sda = _uniforms.src.xyz * _uniforms.dst.w;
57cb93a386Sopenharmony_ci    half3 _2_dsa = _uniforms.dst.xyz * _uniforms.src.w;
58cb93a386Sopenharmony_ci    _out.sk_FragColor = half4((((_blend_set_color_luminance_h3h3hh3(_blend_set_color_saturation_h3h3h3(_2_dsa, _1_sda), _0_alpha, _2_dsa) + _uniforms.dst.xyz) - _2_dsa) + _uniforms.src.xyz) - _1_sda, (_uniforms.src.w + _uniforms.dst.w) - _0_alpha);
59cb93a386Sopenharmony_ci    return _out;
60cb93a386Sopenharmony_ci}
61