// ============================================================
//  CinematicVanilla V2 - tonemap.glsl
//  Reinhard-Jodie Extended + cinematic film grade.
//  V2 improvements:
//   - Stronger but subtler split-tone (shadows cool, highs warm)
//   - Filmic shadow lift (crushed blacks → print emulsion look)
//   - Slightly desaturated midtones for analog film character
//   - Warmer overall temperature (+amber cast)
// ============================================================

const float oneMinusShoulder         = 1.0 - SHOULDER_STRENGTH;
const float shoulderFactor           = oneMinusShoulder * 3.0;
const float shoulderWhitePointFactor = oneMinusShoulder / (WHITE_POINT * WHITE_POINT);

float getLuminance(in vec3 col){
    return dot(col, vec3(0.2126, 0.7152, 0.0722));
}

vec3 saturation(in vec3 col, in float a){
    float luma = getLuminance(col);
    return (col - luma) * a + luma;
}

vec3 contrast(in vec3 col, in float a){
    return (col - 0.5) * a + 0.5;
}

// Reinhard Extended Luminance
vec3 modifiedReinhardExtended(in vec3 color){
    float s = sumOf(color);
    return color * ((3.0 + s * shoulderWhitePointFactor) / (shoulderFactor + s));
}

// Reinhard Jodie Extended (per-channel + luminance blend)
vec3 modifiedReinhardJodieExtended(in vec3 color){
    float s = sumOf(color);
    vec3 rC = color * ((1.0 + color * shoulderWhitePointFactor) / (oneMinusShoulder + color));
    vec3 rL = color * ((3.0 + s * shoulderWhitePointFactor) / (shoulderFactor + s));
    return (rC - rL) * rC + rL;
}

// ─── Cinematic Film Grade ────────────────────────────────────────────────────
// Applied in sRGB space after tonemapping. Pure math — zero texture reads.
vec3 cinematicGrade(in vec3 col){
    float luma = getLuminance(col);

    // 1. Shadow lift — prevents pure black, gives film-print look
    //    0.018 is one stop above digital black, very subtle
    col = max(col, vec3(0.018));

    // 2. Split-tone:
    //    Shadows → cool teal-blue  (like cold shadow fill light)
    //    Highlights → warm amber   (golden hour warmth)
    float shadowW    = saturate(1.0 - luma * 2.8);   // peaks in deep darks
    float highlightW = saturate(luma * 2.0 - 0.60);  // peaks in bright areas

    col += vec3(-0.012, -0.004, 0.018) * shadowW;    // cool shadow push
    col += vec3( 0.018,  0.010, -0.008) * highlightW;// warm highlight push

    // 3. Midtone warmth (slight amber overall temperature shift)
    col *= vec3(1.015, 1.006, 0.988);

    // 4. Subtle midtone desaturation — analog film is never hyper-saturated
    float midW = saturate(1.0 - abs(luma * 2.0 - 1.0));
    col = mix(col, saturation(col, 0.88), midW * 0.3);

    return col;
}
