blob: b27426504f7974d27b17d0ca7b215df3556780b9 [file] [log] [blame]
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Inputs {
float4 src;
float4 dst;
};
struct Outputs {
float4 sk_FragColor [[color(0)]];
};
float3 _blend_set_color_luminance(float3 hueSatColor, float alpha, float3 lumColor) {
float lum = dot(float3(0.30000001192092896, 0.5899999737739563, 0.10999999940395355), lumColor);
float3 result = (lum - dot(float3(0.30000001192092896, 0.5899999737739563, 0.10999999940395355), hueSatColor)) + hueSatColor;
float minComp = min(min(result.x, result.y), result.z);
float maxComp = max(max(result.x, result.y), result.z);
if (minComp < 0.0 && lum != minComp) {
float _4_d = lum - minComp;
result = lum + (result - lum) * (lum / _4_d);
}
if (maxComp > alpha && maxComp != lum) {
float3 _5_n = (result - lum) * (alpha - lum);
float _6_d = maxComp - lum;
return lum + _5_n / _6_d;
} else {
return result;
}
}
float3 _blend_set_color_saturation_helper(float3 minMidMax, float sat) {
if (minMidMax.x < minMidMax.z) {
float _7_n = sat * (minMidMax.y - minMidMax.x);
float _8_d = minMidMax.z - minMidMax.x;
return float3(0.0, _7_n / _8_d, sat);
} else {
return float3(0.0);
}
}
float3 _blend_set_color_saturation(float3 hueLumColor, float3 satColor) {
float sat = max(max(satColor.x, satColor.y), satColor.z) - min(min(satColor.x, satColor.y), satColor.z);
if (hueLumColor.x <= hueLumColor.y) {
if (hueLumColor.y <= hueLumColor.z) {
return _blend_set_color_saturation_helper(hueLumColor, sat);
} else if (hueLumColor.x <= hueLumColor.z) {
return _blend_set_color_saturation_helper(hueLumColor.xzy, sat).xzy;
} else {
return _blend_set_color_saturation_helper(hueLumColor.zxy, sat).yzx;
}
} else if (hueLumColor.x <= hueLumColor.z) {
return _blend_set_color_saturation_helper(hueLumColor.yxz, sat).yxz;
} else if (hueLumColor.y <= hueLumColor.z) {
return _blend_set_color_saturation_helper(hueLumColor.yzx, sat).zxy;
} else {
return _blend_set_color_saturation_helper(hueLumColor.zyx, sat).zyx;
}
}
float4 blend_hue(float4 src, float4 dst) {
float alpha = dst.w * src.w;
float3 sda = src.xyz * dst.w;
float3 dsa = dst.xyz * src.w;
return float4((((_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) + dst.xyz) - dsa) + src.xyz) - sda, (src.w + dst.w) - alpha);
}
fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Outputs _out;
(void)_out;
_out.sk_FragColor = blend_hue(_in.src, _in.dst);
return _out;
}