power_play/src/base/base_math_gpu.h
2025-10-23 03:32:43 -05:00

80 lines
1.8 KiB
C

////////////////////////////////////////////////////////////
//~ Gpu math constants
#define Pi ((f32)3.14159265358979323846)
#define Tau ((f32)6.28318530717958647693)
#define GoldenRatio ((f32)1.61803398874989484820)
////////////////////////////////////////////////////////////
//~ Gpu math types
typedef float2 Vec2;
typedef float3 Vec3;
typedef float4 Vec4;
typedef int2 Vec2I32;
typedef int3 Vec3I32;
typedef int4 Vec4I32;
typedef uint2 Vec2U32;
typedef uint3 Vec3U32;
typedef uint4 Vec4U32;
typedef float2x3 Xform;
typedef float4 Rect;
typedef float4 ClipRect;
typedef float4 Aabb;
typedef float4 Quad;
typedef float4x4 Mat4x4;
////////////////////////////////////////////////////////////
//~ Integer -> float
Vec4 Vec4NormFromU32(u32 v)
{
Vec4 result;
result.r = ((v >> 0) & 0xFF) / 255.0;
result.g = ((v >> 8) & 0xFF) / 255.0;
result.b = ((v >> 16) & 0xFF) / 255.0;
result.a = ((v >> 24) & 0xFF) / 255.0;
return result;
}
////////////////////////////////////////////////////////////
//~ Srgb -> linear
/* Linear color from normalized sRGB */
Vec4 LinearFromSrgbVec4(Vec4 srgb)
{
return Vec4(pow(abs(srgb.rgb), 2.2), srgb.a);
}
/* Linear color from R8G8B8A8 sRGB */
Vec4 LinearFromSrgbU32(u32 srgb32)
{
return LinearFromSrgbVec4(Vec4NormFromU32(srgb32));
}
////////////////////////////////////////////////////////////
//~ Vertex ID helpers
Vec2 RectUvFromVertexId(u32 id)
{
static const Vec2 uvs[4] = {
Vec2(0, 0),
Vec2(1, 0),
Vec2(1, 1),
Vec2(0, 1)
};
return uvs[id];
}
////////////////////////////////////////////////////////////
//~ Ndc helpers
Vec2 NdcFromViewport(Vec2 viewport_size, Vec2 viewport_coords)
{
Vec2 result;
result = viewport_coords / viewport_size;
result *= Vec2(2, -2);
result += Vec2(-1, 1);
return result;
}