| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Common math |
| 3 |
//------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
/** @public-api */ |
| 6 |
#define PI 3.14159265359 |
| 7 |
/** @public-api */ |
| 8 |
#define HALF_PI 1.570796327 |
| 9 |
|
| 10 |
#define MEDIUMP_FLT_MAX 65504.0 |
| 11 |
#define MEDIUMP_FLT_MIN 0.00006103515625 |
| 12 |
|
| 13 |
#ifdef TARGET_MOBILE |
| 14 |
#define FLT_EPS MEDIUMP_FLT_MIN |
| 15 |
#define saturateMediump(x) min(x, MEDIUMP_FLT_MAX) |
| 16 |
#else |
| 17 |
#define FLT_EPS 1e-5 |
| 18 |
#define saturateMediump(x) x |
| 19 |
#endif |
| 20 |
|
| 21 |
#define saturate(x) clamp(x, 0.0, 1.0) |
| 22 |
#define atan2(x, y) atan(y, x) |
| 23 |
|
| 24 |
//------------------------------------------------------------------------------ |
| 25 |
// Scalar operations |
| 26 |
//------------------------------------------------------------------------------ |
| 27 |
|
| 28 |
/** |
| 29 |
* Computes x^5 using only multiply operations. |
| 30 |
* |
| 31 |
* @public-api |
| 32 |
*/ |
| 33 |
float pow5(float x) { |
| 34 |
float x2 = x * x; |
| 35 |
return x2 * x2 * x; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Computes x^2 as a single multiplication. |
| 40 |
* |
| 41 |
* @public-api |
| 42 |
*/ |
| 43 |
float sq(float x) { |
| 44 |
return x * x; |
| 45 |
} |
| 46 |
|
| 47 |
//------------------------------------------------------------------------------ |
| 48 |
// Vector operations |
| 49 |
//------------------------------------------------------------------------------ |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the maximum component of the specified vector. |
| 53 |
* |
| 54 |
* @public-api |
| 55 |
*/ |
| 56 |
float max3(const vec3 v) { |
| 57 |
return max(v.x, max(v.y, v.z)); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the minimum component of the specified vector. |
| 62 |
* |
| 63 |
* @public-api |
| 64 |
*/ |
| 65 |
float min3(const vec3 v) { |
| 66 |
return min(v.x, min(v.y, v.z)); |
| 67 |
} |
| 68 |
|
| 69 |
//------------------------------------------------------------------------------ |
| 70 |
// Trigonometry |
| 71 |
//------------------------------------------------------------------------------ |
| 72 |
|
| 73 |
/** |
| 74 |
* Approximates acos(x) with a max absolute error of 9.0x10^-3. |
| 75 |
* Valid in the range -1..1. |
| 76 |
*/ |
| 77 |
float acosFast(float x) { |
| 78 |
// Lagarde 2014, "Inverse trigonometric functions GPU optimization for AMD GCN architecture" |
| 79 |
// This is the approximation of degree 1, with a max absolute error of 9.0x10^-3 |
| 80 |
float y = abs(x); |
| 81 |
float p = -0.1565827 * y + 1.570796; |
| 82 |
p *= sqrt(1.0 - y); |
| 83 |
return x >= 0.0 ? p : PI - p; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Approximates acos(x) with a max absolute error of 9.0x10^-3. |
| 88 |
* Valid only in the range 0..1. |
| 89 |
*/ |
| 90 |
float acosFastPositive(float x) { |
| 91 |
float p = -0.1565827 * x + 1.570796; |
| 92 |
return p * sqrt(1.0 - x); |
| 93 |
} |
| 94 |
|
| 95 |
//------------------------------------------------------------------------------ |
| 96 |
// Matrix and quaternion operations |
| 97 |
//------------------------------------------------------------------------------ |
| 98 |
|
| 99 |
/** |
| 100 |
* Multiplies the specified 3-component vector by the 4x4 matrix (m * v) in |
| 101 |
* high precision. |
| 102 |
* |
| 103 |
* @public-api |
| 104 |
*/ |
| 105 |
vec4 mulMat4x4Float3(const highp mat4 m, const highp vec3 v) { |
| 106 |
return v.x * m[0] + (v.y * m[1] + (v.z * m[2] + m[3])); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Multiplies the specified 3-component vector by the 3x3 matrix (m * v) in |
| 111 |
* high precision. |
| 112 |
* |
| 113 |
* @public-api |
| 114 |
*/ |
| 115 |
vec3 mulMat3x3Float3(const highp mat4 m, const highp vec3 v) { |
| 116 |
return v.x * m[0].xyz + (v.y * m[1].xyz + (v.z * m[2].xyz)); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Extracts the normal vector of the tangent frame encoded in the specified quaternion. |
| 121 |
*/ |
| 122 |
void toTangentFrame(const highp vec4 q, out highp vec3 n) { |
| 123 |
n = vec3( 0.0, 0.0, 1.0) + |
| 124 |
vec3( 2.0, -2.0, -2.0) * q.x * q.zwx + |
| 125 |
vec3( 2.0, 2.0, -2.0) * q.y * q.wzy; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Extracts the normal and tangent vectors of the tangent frame encoded in the |
| 130 |
* specified quaternion. |
| 131 |
*/ |
| 132 |
void toTangentFrame(const highp vec4 q, out highp vec3 n, out highp vec3 t) { |
| 133 |
toTangentFrame(q, n); |
| 134 |
t = vec3( 1.0, 0.0, 0.0) + |
| 135 |
vec3(-2.0, 2.0, -2.0) * q.y * q.yxw + |
| 136 |
vec3(-2.0, 2.0, 2.0) * q.z * q.zwx; |
| 137 |
} |