cnq3/code/renderer/shaders/crp/common.hlsli

145 lines
2.8 KiB
HLSL
Raw Normal View History

/*
===========================================================================
Copyright (C) 2023 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/>.
===========================================================================
*/
2024-01-13 21:40:13 +00:00
// shared utilities
2024-01-13 21:40:13 +00:00
#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
*/
2024-01-13 21:40:13 +00:00
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)
{
2024-01-13 21:40:13 +00:00
return float2(
(float)(id / 2) * 2.0,
1.0 - (float)(id % 2) * 2.0);
}
2024-01-13 21:40:13 +00:00
uint PackColor(float4 c)
{
2024-01-13 21:40:13 +00:00
uint4 u = uint4(saturate(c) * 255.0);
uint r = u.r | (u.g << 8) | (u.b << 16) | (u.a << 24);
return r;
}
2024-01-13 21:40:13 +00:00
float4 UnpackColor(uint c)
{
2024-01-13 21:40:13 +00:00
uint4 u = uint4(c & 0xFFu, (c >> 8) & 0xFFu, (c >> 16) & 0xFFu, (c >> 24) & 0xFFu);
float4 r = float4(u) / 255.0;
return r;
}
2024-01-13 21:40:13 +00:00
float EaseInCubic(float x)
{
2024-01-13 21:40:13 +00:00
return x * x * x;
}
2024-01-13 21:40:13 +00:00
float EaseOutCubic(float x)
{
float y = 1.0 - x;
return 1.0 - y * y * y;
}
2024-01-13 21:40:13 +00:00
float EaseInOutCubic(float x)
{
2024-01-13 21:40:13 +00:00
if(x < 0.5)
{
return 4 * x * x * x;
}
float y = -2 * x + 2;
2024-01-13 21:40:13 +00:00
return 1 - 0.5 * y * y * y;
}
2024-01-13 21:40:13 +00:00
float EaseInQuad(float x)
{
2024-01-13 21:40:13 +00:00
return x * x;
}
2024-01-13 21:40:13 +00:00
float SmoothStep(float x)
{
2024-01-13 21:40:13 +00:00
return smoothstep(0.0, 1.0, x);
}