
#ifndef UNITY_POSTFX_BLOOM
#define UNITY_POSTFX_BLOOM



        
//#include "../StdLib.hlsl"
//#include "../Colors.hlsl"

//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

//#include "Sampling.hlsl"
TEXTURE2D_X(_MainTex);  SAMPLER(sampler_MainTex);
TEXTURE2D_X(_BloomTex);  SAMPLER(sampler_BloomTex);
TEXTURE2D_X(_AutoExposureTex);  SAMPLER(sampler_AutoExposureTex);

//TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
//TEXTURE2D_SAMPLER2D(_BloomTex, sampler_BloomTex);
//TEXTURE2D_SAMPLER2D(_AutoExposureTex, sampler_AutoExposureTex);

float4 _MainTex_TexelSize;
float  _SampleScale;
float4 _ColorIntensity;
float4 _Threshold; // x: threshold value (linear), y: threshold - knee, z: knee * 2, w: 0.25 / knee
float4 _Params; // x: clamp, yzw: unused


//
// Quadratic color thresholding
// curve = (threshold - knee, knee * 2, 0.25 / knee)
//
half4 QuadraticThreshold(half4 color, half threshold, half3 curve)
{
    // Pixel brightness
    half br = Max3(color.r, color.g, color.b);

    // Under-threshold part: quadratic curve
    half rq = clamp(br - curve.x, 0.0, curve.y);
    rq = curve.z * rq * rq;

    // Combine and apply the brightness response curve.
    color *= max(rq, br - threshold) / max(br, EPSILON);

    return color;
}


// ----------------------------------------------------------------------------------------
// Prefilter

half4 Prefilter(half4 color, float2 uv)
{
    half autoExposure = SAMPLE_TEXTURE2D(_AutoExposureTex, sampler_AutoExposureTex, uv).r;
    color *= autoExposure;
    color = min(_Params.x, color); // clamp to max
    color = QuadraticThreshold(color, _Threshold.x, _Threshold.yzw);
    return color;
}

half4 FragPrefilter13(VaryingsDefault i) : SV_Target
{
    half4 color = DownsampleBox13Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy);
    return Prefilter(SafeHDR(color), i.texcoord);
}

half4 FragPrefilter4(VaryingsDefault i) : SV_Target
{
    half4 color = DownsampleBox4Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy);
    return Prefilter(SafeHDR(color), i.texcoord);
}

// ----------------------------------------------------------------------------------------
// Downsample

half4 FragDownsample13(VaryingsDefault i) : SV_Target
{
    half4 color = DownsampleBox13Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy);
    return color;
}

half4 FragDownsample4(VaryingsDefault i) : SV_Target
{
    half4 color = DownsampleBox4Tap(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy);
    return color;
}

// ----------------------------------------------------------------------------------------
// Upsample & combine

half4 Combine(half4 bloom, float2 uv)
{
    half4 color = SAMPLE_TEXTURE2D(_BloomTex, sampler_BloomTex, uv);
    return bloom + color;
}

half4 FragUpsampleTent(VaryingsDefault i) : SV_Target
{
    half4 bloom = UpsampleTent(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy, _SampleScale);
    return Combine(bloom, i.texcoordStereo);
}

half4 FragUpsampleBox(VaryingsDefault i) : SV_Target
{
    half4 bloom = UpsampleBox(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy, _SampleScale);
    return Combine(bloom, i.texcoordStereo);
}

// ----------------------------------------------------------------------------------------
// Debug overlays

half4 FragDebugOverlayThreshold(VaryingsDefault i) : SV_Target
{
    half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
    return half4(Prefilter(SafeHDR(color), i.texcoord).rgb, 1.0);
}

half4 FragDebugOverlayTent(VaryingsDefault i) : SV_Target
{
    half4 bloom = UpsampleTent(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy, _SampleScale);
    return half4(bloom.rgb * _ColorIntensity.w * _ColorIntensity.rgb, 1.0);
}

half4 FragDebugOverlayBox(VaryingsDefault i) : SV_Target
{
    half4 bloom = UpsampleBox(TEXTURE2D_PARAM(_MainTex, sampler_MainTex), i.texcoord, UnityStereoAdjustedTexelSize(_MainTex_TexelSize).xy, _SampleScale);
    return half4(bloom.rgb * _ColorIntensity.w * _ColorIntensity.rgb, 1.0);
}

#endif // UNITY_POSTFX_BLOOM