cnq3/code/renderer/shaders/crp/common.hlsli
2024-01-19 23:57:40 +01:00

182 lines
3.7 KiB
HLSL

/*
===========================================================================
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)) * (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 += n.xy >= 0.0 ? -t : t;
return normalize(n);
}
float3 GetPositionFromDepth(float2 tc01, float depthZW, float4x4 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;
}