-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathETC1.hpp
More file actions
91 lines (79 loc) · 1.52 KB
/
MathETC1.hpp
File metadata and controls
91 lines (79 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// https://bitbucket.org/wolfpld/etcpak
// Bartosz Taudul <wolf.pld@gmail.com>
#ifndef __DARKRL__MATH_HPP__
#define __DARKRL__MATH_HPP__
#include <algorithm>
#include <cmath>
template<typename T>
inline T AlignPOT(T val)
{
if (val == 0) return 1;
val--;
for (unsigned int i = 1; i < sizeof(T) * 8; i <<= 1)
{
val |= val >> i;
}
return val + 1;
}
inline int CountSetBits(unsigned int val)
{
val -= (val >> 1) & 0x55555555;
val = ((val >> 2) & 0x33333333) + (val & 0x33333333);
val = ((val >> 4) + val) & 0x0f0f0f0f;
val += val >> 8;
val += val >> 16;
return val & 0x0000003f;
}
inline int CountLeadingZeros(unsigned int val)
{
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
return 32 - CountSetBits(val);
}
inline float sRGB2linear(float v)
{
const float a = 0.055f;
if (v <= 0.04045f)
{
return v / 12.92f;
}
else
{
return pow((v + a) / (1 + a), 2.4f);
}
}
inline float linear2sRGB(float v)
{
const float a = 0.055f;
if (v <= 0.0031308f)
{
return 12.92f * v;
}
else
{
return (1 + a) * pow(v, 1 / 2.4f) - a;
}
}
template<class T>
inline T SmoothStep(T x)
{
return x*x*(3 - 2 * x);
}
inline unsigned char clampu8(int val)
{
return std::min(std::max(0, val), 255);
}
template<class T>
inline T sq(T val)
{
return val * val;
}
static inline int mul8bit(int a, int b)
{
int t = a*b + 128;
return (t + (t >> 8)) >> 8;
}
#endif