270 lines
6.4 KiB
Plaintext
270 lines
6.4 KiB
Plaintext
////////////////////////////////////////////////////////////
|
|
//~ Base math types
|
|
|
|
#define Pi ((f32)3.14159265358979323846)
|
|
#define Tau ((f32)6.28318530717958647693)
|
|
#define GoldenRatio ((f32)1.61803398874989484820)
|
|
|
|
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;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Range types
|
|
|
|
Struct(Rng) { f32 min; f32 max; };
|
|
Struct(RngI32) { i32 min; i32 max; };
|
|
Struct(RngU32) { u32 min; u32 max; };
|
|
|
|
Struct(Rng2) { Vec2 p0; Vec2 p1; };
|
|
Struct(Rng2I32) { Vec2I32 p0; Vec2I32 p1; };
|
|
Struct(Rng2U32) { Vec2U32 p0; Vec2U32 p1; };
|
|
|
|
Struct(Rng3) { Vec3 p0; Vec3 p1; };
|
|
Struct(Rng3I32) { Vec3I32 p0; Vec3I32 p1; };
|
|
Struct(Rng3U32) { Vec3U32 p0; Vec3U32 p1; };
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Countof
|
|
|
|
template<typename T, u32 N>
|
|
u32 countof(T arr[N])
|
|
{
|
|
return N;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Min / max
|
|
|
|
//- Min
|
|
#define MinU8(...) min(__VA_ARGS__)
|
|
#define MinI8(...) min(__VA_ARGS__)
|
|
#define MinU32(...) min(__VA_ARGS__)
|
|
#define MinI32(...) min(__VA_ARGS__)
|
|
#define MinF32(...) min(__VA_ARGS__)
|
|
#define MinU64(...) min(__VA_ARGS__)
|
|
#define MinI64(...) min(__VA_ARGS__)
|
|
#define MinF64(...) min(__VA_ARGS__)
|
|
|
|
//- Max
|
|
#define MaxU8(...) max(__VA_ARGS__)
|
|
#define MaxI8(...) max(__VA_ARGS__)
|
|
#define MaxU32(...) max(__VA_ARGS__)
|
|
#define MaxI32(...) max(__VA_ARGS__)
|
|
#define MaxF32(...) max(__VA_ARGS__)
|
|
#define MaxU64(...) max(__VA_ARGS__)
|
|
#define MaxI64(...) max(__VA_ARGS__)
|
|
#define MaxF64(...) max(__VA_ARGS__)
|
|
|
|
//- Clamp
|
|
#define ClampU32(...) clamp(__VA_ARGS__)
|
|
#define ClampI32(...) clamp(__VA_ARGS__)
|
|
#define ClampF32(...) clamp(__VA_ARGS__)
|
|
#define ClampU64(...) clamp(__VA_ARGS__)
|
|
#define ClampI64(...) clamp(__VA_ARGS__)
|
|
#define ClampF64(...) clamp(__VA_ARGS__)
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Derivative helpers
|
|
|
|
#define fwidth_fine(v) (abs(ddx_fine((v))) + abs(ddx_fine((v))))
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Color helpers
|
|
|
|
Vec4 Vec4FromU32(u32 v)
|
|
{
|
|
Vec4 result;
|
|
result.x = ((v >> 0) & 0xFF) / 255.0;
|
|
result.y = ((v >> 8) & 0xFF) / 255.0;
|
|
result.z = ((v >> 16) & 0xFF) / 255.0;
|
|
result.w = ((v >> 24) & 0xFF) / 255.0;
|
|
return result;
|
|
}
|
|
|
|
u32 U32FromVec4(Vec4 v)
|
|
{
|
|
u32 result;
|
|
result |= (((u32)(v.x * 255.0)) & 0xFF) << 0;
|
|
result |= (((u32)(v.y * 255.0)) & 0xFF) << 8;
|
|
result |= (((u32)(v.z * 255.0)) & 0xFF) << 16;
|
|
result |= (((u32)(v.w * 255.0)) & 0xFF) << 24;
|
|
return result;
|
|
}
|
|
|
|
f32 LinearFromSrgbF32(f32 srgb)
|
|
{
|
|
f32 result;
|
|
if (srgb <= 0.04045f)
|
|
{
|
|
result = srgb / 12.92f;
|
|
}
|
|
else
|
|
{
|
|
result = pow((srgb + 0.055f) / 1.055f, 2.4f);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Vec4 LinearFromSrgb(Vec4 srgb)
|
|
{
|
|
Vec4 result;
|
|
result.x = LinearFromSrgbF32(srgb.x);
|
|
result.y = LinearFromSrgbF32(srgb.y);
|
|
result.z = LinearFromSrgbF32(srgb.z);
|
|
result.w = srgb.w;
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ 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 NdcFromPos(Vec2 pos, Vec2 size)
|
|
{
|
|
Vec2 result;
|
|
result = pos / size;
|
|
result *= Vec2(2, -2);
|
|
result += Vec2(-1, 1);
|
|
return result;
|
|
}
|
|
|
|
Vec2 NdcFromUv(Vec2 uv)
|
|
{
|
|
Vec2 result;
|
|
result = uv;
|
|
result *= Vec2(2, -2);
|
|
result += Vec2(-1, 1);
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ String helpers
|
|
|
|
// https://therealmjp.github.io/posts/hlsl-printf/
|
|
template<typename T>
|
|
u32 U32FromChar(in T c)
|
|
{
|
|
if(c == ' ') return 32;
|
|
if(c == '!') return 33;
|
|
if(c == '\"' || c == '\"') return 34;
|
|
if(c == '#') return 35;
|
|
if(c == '$') return 36;
|
|
if(c == '%') return 37;
|
|
if(c == '&') return 38;
|
|
if(c == '\'') return 39;
|
|
if(c == '(') return 40;
|
|
if(c == ')') return 41;
|
|
if(c == '*') return 42;
|
|
if(c == '+') return 43;
|
|
if(c == ',') return 44;
|
|
if(c == '-') return 45;
|
|
if(c == '.') return 46;
|
|
if(c == '/') return 47;
|
|
if(c == '0') return 48;
|
|
if(c == '1') return 49;
|
|
if(c == '2') return 50;
|
|
if(c == '3') return 51;
|
|
if(c == '4') return 52;
|
|
if(c == '5') return 53;
|
|
if(c == '6') return 54;
|
|
if(c == '7') return 55;
|
|
if(c == '8') return 56;
|
|
if(c == '9') return 57;
|
|
if(c == ':') return 58;
|
|
if(c == ';') return 59;
|
|
if(c == '<') return 60;
|
|
if(c == '=') return 61;
|
|
if(c == '>') return 62;
|
|
if(c == '?') return 63;
|
|
if(c == '@') return 64;
|
|
if(c == 'A') return 65;
|
|
if(c == 'B') return 66;
|
|
if(c == 'C') return 67;
|
|
if(c == 'D') return 68;
|
|
if(c == 'E') return 69;
|
|
if(c == 'F') return 70;
|
|
if(c == 'G') return 71;
|
|
if(c == 'H') return 72;
|
|
if(c == 'I') return 73;
|
|
if(c == 'J') return 74;
|
|
if(c == 'K') return 75;
|
|
if(c == 'L') return 76;
|
|
if(c == 'M') return 77;
|
|
if(c == 'N') return 78;
|
|
if(c == 'O') return 79;
|
|
if(c == 'P') return 80;
|
|
if(c == 'Q') return 81;
|
|
if(c == 'R') return 82;
|
|
if(c == 'S') return 83;
|
|
if(c == 'T') return 84;
|
|
if(c == 'U') return 85;
|
|
if(c == 'V') return 86;
|
|
if(c == 'W') return 87;
|
|
if(c == 'X') return 88;
|
|
if(c == 'Y') return 89;
|
|
if(c == 'Z') return 90;
|
|
if(c == '[') return 91;
|
|
if(c == '\\') return 92;
|
|
if(c == ']') return 93;
|
|
if(c == '^') return 94;
|
|
if(c == '_') return 95;
|
|
if(c == '`') return 96;
|
|
if(c == 'a') return 97;
|
|
if(c == 'b') return 98;
|
|
if(c == 'c') return 99;
|
|
if(c == 'd') return 100;
|
|
if(c == 'e') return 101;
|
|
if(c == 'f') return 102;
|
|
if(c == 'g') return 103;
|
|
if(c == 'h') return 104;
|
|
if(c == 'i') return 105;
|
|
if(c == 'j') return 106;
|
|
if(c == 'k') return 107;
|
|
if(c == 'l') return 108;
|
|
if(c == 'm') return 109;
|
|
if(c == 'n') return 110;
|
|
if(c == 'o') return 111;
|
|
if(c == 'p') return 112;
|
|
if(c == 'q') return 113;
|
|
if(c == 'r') return 114;
|
|
if(c == 's') return 115;
|
|
if(c == 't') return 116;
|
|
if(c == 'u') return 117;
|
|
if(c == 'v') return 118;
|
|
if(c == 'w') return 119;
|
|
if(c == 'x') return 120;
|
|
if(c == 'y') return 121;
|
|
if(c == 'z') return 122;
|
|
if(c == '{') return 123;
|
|
if(c == '|') return 124;
|
|
if(c == '}') return 125;
|
|
if(c == '~') return 126;
|
|
return 0;
|
|
}
|