#include using namespace metal; struct Uniforms { float invert; float mode; // 0 = alpha, 1 = binary float2 padding; }; struct FragmentIn { float4 position [[position]]; float2 uv; }; fragment float4 fragment_main( FragmentIn in [[stage_in]], constant Uniforms &uniforms [[buffer(0)]], texture2d content_tex [[texture(0)]], texture2d mask_tex [[texture(1)]], sampler samp [[sampler(0)]] ) { float4 content = content_tex.sample(samp, in.uv); float4 mask = mask_tex.sample(samp, in.uv); // Get mask value (use alpha channel) float mask_value = mask.a; // Binary mode: threshold at 0.5 if (uniforms.mode > 0.5) { mask_value = mask_value > 0.5 ? 1.0 : 0.0; } // Invert if requested if (uniforms.invert > 0.5) { mask_value = 1.0 - mask_value; } // Apply mask to content alpha return float4(content.rgb, content.a * mask_value); }