#include <metal_stdlib>
using namespace metal;

struct Uniforms {
  float threshold;
  float intensity;
  float2 padding;
};

struct FragmentIn {
  float4 position [[position]];
  float2 uv;
};

fragment float4 fragment_main(
  FragmentIn in [[stage_in]],
  constant Uniforms &uniforms [[buffer(0)]],
  texture2d<float> tex [[texture(0)]],
  sampler samp [[sampler(0)]]
) {
  float4 color = tex.sample(samp, in.uv);
  
  // Calculate luminance
  float luma = dot(color.rgb, float3(0.299, 0.587, 0.114));
  
  // Extract bright pixels above threshold
  float brightness = max(0.0, luma - uniforms.threshold);
  
  // Return bright pixels multiplied by intensity
  return float4(color.rgb * brightness * uniforms.intensity, color.a);
}
