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

144 lines
2.8 KiB
HLSL

/*
===========================================================================
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/>.
===========================================================================
*/
// 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 SmoothStep(float x)
{
return smoothstep(0.0, 1.0, x);
}