// -*-C++-*-
#version 120

/* ------------------------------------------------------------------ */
/* Utility                                                             */
/* ------------------------------------------------------------------ */

// Construct a tangent-space basis from screen-space position and UV
// derivatives so normal maps can be used without stored mesh tangents.
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv)
{
    vec3 dp1 = dFdx(p);
    vec3 dp2 = dFdy(p);

    vec2 duv1 = dFdx(uv);
    vec2 duv2 = dFdy(uv);

    vec3 dp2perp = cross(dp2, N);
    vec3 dp1perp = cross(N, dp1);

    vec3 T =
        dp2perp * duv1.x +
        dp1perp * duv2.x;

    vec3 B =
        dp2perp * duv1.y +
        dp1perp * duv2.y;

    float maxsqr = max(dot(T, T), dot(B, B));

    if (maxsqr > 0.0)
    {
        float invmax = inversesqrt(maxsqr);
        T *= invmax;
        B *= invmax;
    }

    return mat3(T, B, N);
}

// Convert an sRGB texture color to linear space for lighting calculations.
vec3 srgbToLinear(vec3 c)
{
    vec3 lo = c / 12.92;
    vec3 hi = pow((c + 0.055) / 1.055, vec3(2.4));

    return mix(lo, hi, step(vec3(0.04045), c));
}

// Convert the final linear lighting result back to sRGB for output.
vec3 linearToSrgb(vec3 c)
{
    c = clamp(c, vec3(0.0), vec3(1.0));

    vec3 lo = c * 12.92;
    vec3 hi = 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055;

    return mix(lo, hi, step(vec3(0.0031308), c));
}

// Compute x^5 using multiplications; used by the Schlick Fresnel term.
float pow5(float x)
{
    float x2 = x * x;
    return x2 * x2 * x;
}

/* ------------------------------------------------------------------ */
/* Fresnel                                                             */
/* ------------------------------------------------------------------ */

// Schlick approximation of the Fresnel reflectance term.
// F0 is the surface reflectance at normal incidence.
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
    cosTheta = clamp(cosTheta, 0.0, 1.0);

    float f = pow5(1.0 - cosTheta);

    return F0 + (vec3(1.0) - F0) * f;
}


/* ------------------------------------------------------------------ */
/* GGX normal distribution                                             */
/* ------------------------------------------------------------------ */

// GGX/Trowbridge-Reitz normal distribution function for the
// Cook-Torrance specular BRDF.
float distributionGGX(
    vec3 N,
    vec3 H,
    float roughness
)
{
    float a  = roughness * roughness;
    float a2 = a * a;

    float NdotH = max(dot(N, H), 0.0);

    float NdotH2 = NdotH * NdotH;

    float denom =
        NdotH2 * (a2 - 1.0) + 1.0;

    denom = denom * denom * 3.14159;//PI;

    return a2 / max(denom, 0.000001);
}


/* ------------------------------------------------------------------ */
/* Smith geometry                                                       */
/* ------------------------------------------------------------------ */

// Schlick-GGX approximation of the geometric shadowing/masking term
// for a single view or light direction.
float geometrySchlickGGX(
    float NdotX,
    float roughness
)
{
    float r = roughness + 1.0;
    float k = (r * r) / 8.0;

    return NdotX /
           max(
               NdotX * (1.0 - k) + k,
               0.000001
           );
}

// Combine the view and light Schlick-GGX terms to obtain the Smith
// geometric attenuation term used by the Cook-Torrance BRDF.
float geometrySmith(
    float NdotV,
    float NdotL,
    float roughness
)
{
    float ggxV =
        geometrySchlickGGX(NdotV, roughness);

    float ggxL =
        geometrySchlickGGX(NdotL, roughness);

    return ggxV * ggxL;
}


/* ------------------------------------------------------------------ */
/* Tone mapping                                                        */
/* ------------------------------------------------------------------ */

// Apply simple Reinhard tone mapping after exposure scaling.
vec3 toneMapReinhard(vec3 hdr, float exposure)
{
    hdr *= exposure;
    return hdr / (vec3(1.0) + hdr);
}
