/*
===========================================================================
Copyright (C) 2023-2024 Gian 'myT' Schellenbaum

This file is part of Challenge Quake 3 (CNQ3).

Challenge Quake 3 is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Challenge Quake 3 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Challenge Quake 3. If not, see <https://www.gnu.org/licenses/>.
===========================================================================
*/
// shared utilities


#pragma once


#include "../common/state_bits.h.hlsli"
#include "../common/blend.hlsli"


#define PI    3.1415926535897932384626433832795
#define PI_D2 (PI / 2.0)
#define PI_D4 (PI / 4.0)
#define PI_M2 (PI * 2.0)


float DegToRad(float deg)
{
	return PI * (deg / 180.0);
}

float RadToDeg(float rad)
{
	return 180.0 * (rad / PI);
}

float Brightness(float3 color)
{
	float brightness = dot(color, float3(0.299, 0.587, 0.114));

	return brightness;
}

float4 MakeGreyscale(float4 input, float amount)
{
	float grey = dot(input.rgb, float3(0.299, 0.587, 0.114));
	float4 result = lerp(input, float4(grey, grey, grey, input.a), amount);

	return result;
}

/*
f   = far  clip plane distance
n   = near clip plane distance
exp = exponential depth value (as stored in the Z-buffer)

					 2 * f * n             B
linear(exp) = ----------------------- = -------
			  (f + n) - exp * (f - n)   exp - A

			f + n               -2 * f * n
with    A = -----    and    B = ----------
			f - n                  f - n
*/
float LinearDepth(float zwDepth, float A, float B)
{
	return B / (zwDepth - A);
}

float4 FSTrianglePosFromVertexId(uint id)
{
	return float4(
		(float)(id / 2) * 4.0 - 1.0,
		(float)(id % 2) * 4.0 - 1.0,
		0.0,
		1.0);
}

float2 FSTriangleTCFromVertexId(uint id)
{
	return float2(
		(float)(id / 2) * 2.0,
		1.0 - (float)(id % 2) * 2.0);
}

uint PackColor(float4 c)
{
	uint4 u = uint4(saturate(c) * 255.0);
	uint r = u.r | (u.g << 8) | (u.b << 16) | (u.a << 24);

	return r;
}

float4 UnpackColor(uint c)
{
	uint4 u = uint4(c & 0xFFu, (c >> 8) & 0xFFu, (c >> 16) & 0xFFu, (c >> 24) & 0xFFu);
	float4 r = float4(u) / 255.0;

	return r;
}

float EaseInCubic(float x)
{
	return x * x * x;
}

float EaseOutCubic(float x)
{
	float y = 1.0 - x;

	return 1.0 - y * y * y;
}

float EaseInOutCubic(float x)
{
	if(x < 0.5)
	{
		return 4 * x * x * x;
	}

	float y = -2 * x + 2;

	return 1 - 0.5 * y * y * y;
}

float EaseInQuad(float x)
{
	return x * x;
}

float smoothstep01(float x)
{
	return smoothstep(0.0, 1.0, x);
}

// Oct*: octahedron normal vector encoding
// original code from "A Survey of Efficient Representations for Independent Unit Vectors"
// further improved by Krzysztof Narkowicz and Rune Stubbe

float2 OctWrap(float2 v)
{
	return (1.0 - abs(v.yx)) * select(v.xy >= 0.0, 1.0, -1.0);
}

float2 OctEncode(float3 n)
{
	n /= (abs(n.x) + abs(n.y) + abs(n.z));
	n.xy = n.z >= 0.0 ? n.xy : OctWrap(n.xy);
	n.xy = n.xy * 0.5 + 0.5;

	return n.xy;
}

float3 OctDecode(float2 f)
{
	f = f * 2.0 - 1.0;
	float3 n = float3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y));
	float t = saturate(-n.z);
	n.xy += select(n.xy >= 0.0, -t, t);

	return normalize(n);
}

float3 GetPositionFromDepth(float2 tc01, float depthZW, matrix invMatrix)
{
	float x = tc01.x * 2.0 - 1.0;
	float y = (1.0 - tc01.y) * 2.0 - 1.0;
	float4 position = mul(float4(x, y, depthZW, 1.0), invMatrix);
	float3 result = position.xyz / position.w;

	return result;
}

float3 TransformNormal(float3 normal, matrix transform)
{
	return mul(transform, float4(normal, 0)).xyz;
}

float3 TransformPoint(float3 position, matrix transform)
{
	float4 result = mul(transform, float4(position, 1));
	return result.xyz / result.w;
}

float3 RandomColorFromUInt(uint id)
{
	float r = frac(0.420 + 1.337 * id);
	float g = frac(0.69 + 1.666 * id);
	float b = frac(0.13 + 1.777 * id);

	return float3(r, g, b);
}

float3 BiasPosition(float3 position, float3 normal)
{
	float3 result = position + sign(normal) * abs(position * 0.0000002);

	return result;
}

// from Mauricio Vives, https://gist.github.com/pixnblox/5e64b0724c186313bc7b6ce096b08820
// Projects the specified position (point) onto the plane with the specified origin and normal.
float3 ProjectPointOnPlane(float3 position, float3 planeOrigin, float3 planeNormal)
{
	return position - dot(position - planeOrigin, planeNormal) * planeNormal;
}

// from Mauricio Vives, https://gist.github.com/pixnblox/5e64b0724c186313bc7b6ce096b08820
// Computes the shading position of the specified geometric position and vertex positions and
// normals. For a triangle with normals describing a convex surface, this point will be slightly
// above the surface. For a concave surface, the geometry position is used directly.
// NOTE: The difference between the shading position and geometry position is significant when
// casting shadow rays. If the geometric position is used, a triangle may fully shadow itself when
// it should be partly lit based on the shading normals; this is the "shadow terminator" problem.
float3 GetShadingPosition(
	float3 geomPosition, float3 shadingNormal,
	float3 positions[3], float3 normals[3], float3 barycentrics)
{
	// Project the geometric position (inside the triangle) to the planes defined by the vertex
	// positions and normals.
	float3 p0 = ProjectPointOnPlane(geomPosition, positions[0], normals[0]);
	float3 p1 = ProjectPointOnPlane(geomPosition, positions[1], normals[1]);
	float3 p2 = ProjectPointOnPlane(geomPosition, positions[2], normals[2]);

	// Interpolate the projected positions using the barycentric coordinates, which gives the
	// shading position.
	float3 shadingPosition = p0 * barycentrics.x + p1 * barycentrics.y + p2 * barycentrics.z;

	// Return the shading position for a convex triangle, where the shading point is above the
	// triangle based on the shading normal. Otherwise use the geometric position.
	bool convex = dot(shadingPosition - geomPosition, shadingNormal) > 0.0;
	float3 result = convex ? shadingPosition : BiasPosition(geomPosition, shadingNormal);

	return result;
}

// based on "Hacking the Shadow Terminator" by Johannes Hanika in "Ray Tracing Gems II"
float3 GetShadingPositionV2(float3 geomPosition, float3 positions[3], float3 normals[3], float3 barycentrics)
{
	float3 tmpu = geomPosition - positions[0];
	float3 tmpv = geomPosition - positions[1];
	float3 tmpw = geomPosition - positions[2];
	float dotu = min(0.0, dot(tmpu, normals[0]));
	float dotv = min(0.0, dot(tmpv, normals[1]));
	float dotw = min(0.0, dot(tmpw, normals[2]));
	tmpu -= dotu * normals[0];
	tmpv -= dotv * normals[1];
	tmpw -= dotw * normals[2];
	float3 shadingPosition = geomPosition + 1.0 * (barycentrics.x * tmpu + barycentrics.y * tmpv + barycentrics.z * tmpw);

	return shadingPosition;
}

template<typename T>
T trilerp(T v0, T v1, T v2, float3 barycentrics)
{
	return
		barycentrics.x * v0 +
		barycentrics.y * v1 +
		barycentrics.z * v2;
}

template<>
float trilerp(float v0, float v1, float v2, float3 barycentrics)
{
	return dot(float3(v0, v1, v2), barycentrics);
}

// Interleaved Gradient Noise by Jorge Jimenez
// from "Next Generation Post Processing in Call of Duty: Advanced Warfare"
float InterleavedGradientNoise(float2 uv)
{
	float3 magic = float3(0.06711056, 0.00583715, 52.9829189);
	return frac(magic.z * frac(dot(uv, magic.xy)));
}

template<typename T>
bool IsValueInRange(T p, T min, T max)
{
	return all(p >= min) && all(p <= max);
}

bool IsValue01(float2 p)
{
	return IsValueInRange(p, float2(0, 0), float2(1, 1));
}

bool IsValue01(float3 p)
{
	return IsValueInRange(p, float3(0, 0, 0), float3(1, 1, 1));
}

bool IsValue01(float4 p)
{
	return IsValueInRange(p, float4(0, 0, 0, 0), float4(1, 1, 1, 1));
}

template<typename T>
uint2 GetTextureSize(Texture2D<T> texture0)
{
	uint2 size;
	texture0.GetDimensions(size.x, size.y);

	return size;
}

template<typename T>
uint2 GetTextureSize(RWTexture2D<T> texture0)
{
	uint2 size;
	texture0.GetDimensions(size.x, size.y);

	return size;
}

// by Sakib Saikia, https://sakibsaikia.github.io/graphics/2022/01/04/Nan-Checks-In-HLSL.html
bool IsNan(float x)
{
	return (asuint(x) & 0x7FFFFFFFu) > 0x7F800000u;
}

bool isnan(float x)
{
	return IsNan(x);
}

// from "Using Blue Noise For Raytraced Soft Shadows" by Alan Wolfe in "Ray Tracing Gems II"
// this turns the blue noise into a low discrepancy additive recurrence
float AnimateBlueNoise(float blueNoise, uint frameIndex)
{
	return frac(blueNoise + float(frameIndex % 32) * 0.61803399);
}

float2 NDCToTC(float2 ndc)
{
	float2 tc = ndc * float2(0.5, -0.5) + 0.5;

	return tc;
}

float2 TCToNDC(float2 tc)
{
	float2 ndc = (2.0 * tc - 1.0) * float2(1, -1);

	return ndc;
}

// returns the longest vector
float2 vmax(float2 a, float2 b)
{
	float2 result = dot(a, a) > dot(b, b) ? a : b;

	return result;
}

uint PackHalf2(float2 input)
{
	uint2 d = f32tof16(input);
	uint result = d.x | (d.y << 16u);

	return result;
}

float2 UnpackHalf2(uint input)
{
	uint2 d = uint2(input & 0xFFFFu, input >> 16u);
	float2 result = f16tof32(d);

	return result;
}

float2 CartesianToPolar(float2 cartesian)
{
	float radius = length(cartesian);
	float angle = atan2(cartesian.y, cartesian.x);
	float2 polar = float2(radius, angle);

	return polar;
}

float2 PolarToCartesian(float2 polar)
{
	float sinAngle, cosAngle;
	sincos(polar.y, sinAngle, cosAngle);
	float2 cartesian = polar.x * float2(cosAngle, sinAngle);

	return cartesian;
}
