Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
   Copyright (c) 2015 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _CORE_MATH_MATH_HELPER_INL_
#define _CORE_MATH_MATH_HELPER_INL_

namespace Divide {

template <typename T>
T Random(const T max) {
    return max * rand() * static_cast<T>(INV_RAND_MAX);
}

template <>
inline I32 Random(const I32 max) {
    return rand() % (max + 1);
}

template <typename T>
inline T Random(const T min, const T max) {
    return min + (max - min) * 
           static_cast<T>(INV_RAND_MAX) * 
           static_cast<T>(rand());
}

/// Clamps value n between min and max
template <typename T>
inline void CLAMP(T& n, const T min, const T max) {
    n = std::min(std::max(n, min), max);
}

template <typename T>
inline T CLAMPED(const T& n, const T min, const T max) {
    return std::min(std::max(n, min), max);
}


template<typename T>
inline bool BitCompare(const T bitMask, const T bit) {
    return BitCompare(to_uint(bitMask), to_uint(bit));
}

template<>
inline bool BitCompare<U32>(const U32 bitMask, const U32 bit) {
    return ((bitMask & bit) == bit);
}

inline void SetBit(U32& bitMask, const U32 bit) {
    bitMask |= bit;
}

inline void ClearBit(U32& bitMask, const U32 bit) {
    bitMask &= ~(bit);
}

///(thx sqrt[-1] and canuckle of opengl.org forums)

// Helper method to emulate GLSL
inline F32 FRACT(const F32 floatValue) {
    return to_float(fmod(floatValue, 1.0f));
}

///Helper methods to go from a float [0 ... 1] to packed char and back
inline U8 FLOAT_TO_CHAR_SNORM(const F32 value) {
    return to_ubyte(std::min(255, (I32)(value * 256.0f)));
}

inline U8 FLOAT_TO_CHAR(const F32 value) {
    return to_ubyte(((value + 1.0f) * 0.5f) * 255.0f);
}

inline F32 CHAR_TO_FLOAT(const U8 value) {
    return ((value / 255.0f) * 2.0f) - 1.0f;
}

inline F32 CHAR_TO_FLOAT_SNORM(const U8 value) {
    return value / 256.0f;
}

// Pack 3 values into 1 float
inline F32 PACK_FLOAT(const U8 x, const U8 y, const U8 z) {
    static const D32 offset = to_double(1 << 24);

    U32 packedColor = (x << 16) | (y << 8) | z;
    return to_float(to_double(packedColor) / offset);
}

// UnPack 3 values from 1 float
inline void UNPACK_FLOAT(const F32 src, F32& r, F32& g, F32& b) {
    r = FRACT(src);
    g = FRACT(src * 256.0f);
    b = FRACT(src * 65536.0f);

    // Unpack to the -1..1 range
    r = (r * 2.0f) - 1.0f;
    g = (g * 2.0f) - 1.0f;
    b = (b * 2.0f) - 1.0f;
}

namespace Angle {
template <typename T>
constexpr T DegreesToRadians(const T angleDegrees) {
    return static_cast<T>(angleDegrees * M_PIDIV180);
}

template <typename T>
constexpr T RadiansToDegrees(const T angleRadians) {
    return static_cast<T>(angleRadians * M_180DIVPI);
}

/// Returns the specified value. Used only for emphasis
template <typename T>
constexpr T Degrees(const T degrees) {
    return degrees;
}

/// Returns the specified value. Used only for emphasis
template <typename T>
constexpr T Radians(const T radians) {
    return radians;
}

};  // namespace Angle

namespace Metric {
template <typename T>
constexpr T Tera(const T a) {
    return static_cast<T>(a * 1000000000000.0);
}

template <typename T>
constexpr T Giga(const T a) {
    return static_cast<T>(a * 1000000000.0);
}

template <typename T>
constexpr T Mega(const T a) {
    return static_cast<T>(a * 1000000.0);
}

template <typename T>
constexpr T Kilo(const T a) {
    return static_cast<T>(a * 1000.0);
}

template <typename T>
constexpr T Hecto(const T a) {
    return static_cast<T>(a * 100.0);
}

template <typename T>
constexpr T Deca(const T a) {
    return static_cast<T>(a * 10.0);
}

template <typename T>
constexpr T Base(const T a) {
    return a;
}

template <typename T>
constexpr T Deci(const T a) {
    return static_cast<T>(a * 0.1);
}

template <typename T>
constexpr T Centi(const T a) {
    return static_cast<T>(a * 0.01);
}

template <typename T>
constexpr T Milli(const T a) {
    return static_cast<T>(a * 0.001);
}

template <typename T>
constexpr T Micro(const T a) {
    return static_cast<T>(a * 0.000001);
}

template <typename T>
constexpr T Nano(const T a) {
    return static_cast<T>(a * 0.000000001);
}

template <typename T>
constexpr T Pico(const T a) {
    return static_cast<T>(a * 0.000000000001);
}
};  // namespace Metric

namespace Time {
template <typename T>
T Seconds(const T a) {
    return a;
}

template <typename T>
T Milliseconds(const T a) {
    return a;
}

template <typename T>
U64 Microseconds(const T a) {
    return static_cast<U64>(a);
}

template <typename T>
/*constexpr*/ T MicrosecondsToSeconds(const U64 a) {
    return Metric::Micro(static_cast<T>(a));
}

template <typename T>
/*constexpr*/ T MicrosecondsToMilliseconds(const U64 a) {
    return Metric::Milli(static_cast<T>(a));
}

template <typename T>
/*constexpr*/ T SecondsToMilliseconds(const T a) {
    return Metric::Kilo(a);
}

template <typename T>
/*constexpr*/ U64 SecondsToMicroseconds(const T a) {
    return static_cast<U64>(Metric::Mega(a));
}

template <typename T>
/*constexpr*/ U64 MillisecondsToMicroseconds(const T a) {
    return static_cast<U64>(Metric::Kilo(a));
}

template <typename T>
/*constexpr*/ T MillisecondsToSeconds(const T a) {
    return Metric::Milli(a);
}
};  // namespace Time

namespace Util {
/// a la Boost
template <typename T>
void Hash_combine(U32& seed, const T& v) {
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

inline void ReplaceStringInPlace(stringImpl& subject, 
                                 const stringImpl& search,
                                 const stringImpl& replace) {
    stringAlg::stringSize pos = 0;
    while ((pos = subject.find(search, pos)) != stringImpl::npos) {
        subject.replace(pos, search.length(), replace);
        pos += replace.length();
    }
}

// U = to data type, T = from data type
template <typename U, typename T>
U ConvertData(const T& data) {
    U targetValue;
    std::istringstream iss(data);
    iss >> targetValue;
    DIVIDE_ASSERT(!iss.fail(), "Util::convertData error : invalid conversion!");
    return targetValue;
}

/// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
inline stringImpl& Ltrim(stringImpl& s) {
    s.erase(std::begin(s),
            std::find_if(std::begin(s), std::end(s),
                         std::not1(std::ptr_fun<int, int>(std::isspace))));
    return s;
}

inline stringImpl& Rtrim(stringImpl& s) {
    s.erase(
        std::find_if(std::rbegin(s), std::rend(s),
                     std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
        std::end(s));
    return s;
}

inline stringImpl& Trim(stringImpl& s) {
    return Ltrim(Rtrim(s));
}

namespace Mat4 {

template <typename T>
inline T Det(const T* mat) {
    return ((mat[0] * mat[5] * mat[10]) + (mat[4] * mat[9] * mat[2]) +
            (mat[8] * mat[1] * mat[6]) - (mat[8] * mat[5] * mat[2]) -
            (mat[4] * mat[1] * mat[10]) - (mat[0] * mat[9] * mat[6]));
}

// Copyright 2011 The Closure Library Authors. All Rights Reserved.
template <typename T>
inline void Inverse(const T* in, T* out) {
    T m00 = in[0], m10 = in[1], m20 = in[2], m30 = in[3];
    T m01 = in[4], m11 = in[5], m21 = in[6], m31 = in[7];
    T m02 = in[8], m12 = in[9], m22 = in[10], m32 = in[11];
    T m03 = in[12], m13 = in[13], m23 = in[14], m33 = in[15];

    T a0 = m00 * m11 - m10 * m01;
    T a1 = m00 * m21 - m20 * m01;
    T a2 = m00 * m31 - m30 * m01;
    T a3 = m10 * m21 - m20 * m11;
    T a4 = m10 * m31 - m30 * m11;
    T a5 = m20 * m31 - m30 * m21;
    T b0 = m02 * m13 - m12 * m03;
    T b1 = m02 * m23 - m22 * m03;
    T b2 = m02 * m33 - m32 * m03;
    T b3 = m12 * m23 - m22 * m13;
    T b4 = m12 * m33 - m32 * m13;
    T b5 = m22 * m33 - m32 * m23;

    T idet = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
    assert(!IS_ZERO(idet));

    idet = 1 / idet;

    out[0] = (m11 * b5 - m21 * b4 + m31 * b3) * idet;
    out[1] = (-m10 * b5 + m20 * b4 - m30 * b3) * idet;
    out[2] = (m13 * a5 - m23 * a4 + m33 * a3) * idet;
    out[3] = (-m12 * a5 + m22 * a4 - m32 * a3) * idet;
    out[4] = (-m01 * b5 + m21 * b2 - m31 * b1) * idet;
    out[5] = (m00 * b5 - m20 * b2 + m30 * b1) * idet;
    out[6] = (-m03 * a5 + m23 * a2 - m33 * a1) * idet;
    out[7] = (m02 * a5 - m22 * a2 + m32 * a1) * idet;
    out[8] = (m01 * b4 - m11 * b2 + m31 * b0) * idet;
    out[9] = (-m00 * b4 + m10 * b2 - m30 * b0) * idet;
    out[10] = (m03 * a4 - m13 * a2 + m33 * a0) * idet;
    out[11] = (-m02 * a4 + m12 * a2 - m32 * a0) * idet;
    out[12] = (-m01 * b3 + m11 * b1 - m21 * b0) * idet;
    out[13] = (m00 * b3 - m10 * b1 + m20 * b0) * idet;
    out[14] = (-m03 * a3 + m13 * a1 - m23 * a0) * idet;
    out[15] = (m02 * a3 - m12 * a1 + m22 * a0) * idet;
}

template <typename T>
FORCE_INLINE void Add(const T a[16], const T b[16], T r[16]) {
    T rTemp[] = {
        a[0]  + b[0],  a[1]  + b[1],  a[2]  + b[2],  a[3]  + b[3],
        a[4]  + b[4],  a[5]  + b[5],  a[6]  + b[6],  a[7]  + b[7],
        a[8]  + b[8],  a[9]  + b[9],  a[10] + b[10], a[11] + b[11],
        a[12] + b[12], a[13] + b[13], a[14] + b[14], a[15] + b[15]};

    memcpy(r, rTemp, 16 * sizeof(T));
}

template <typename T>
FORCE_INLINE void Substract(const T a[16], const T b[16], T r[16]) {
    T rTemp[] = {
        a[0]  - b[0],  a[1]  - b[1],  a[2]  - b[2],  a[3]  - b[3],
        a[4]  - b[4],  a[5]  - b[5],  a[6]  - b[6],  a[7]  - b[7],
        a[8]  - b[8],  a[9]  - b[9],  a[10] - b[10], a[11] - b[11],
        a[12] - b[12], a[13] - b[13], a[14] - b[14], a[15] - b[15]};

    memcpy(r, rTemp, 16 * sizeof(T));
}

template <typename T>
inline void Multiply(const T a[16], const T b[16], T r[16]) {
    T rTemp[] = 
        {(a[0]  * b[0]) + (a[1]  * b[4]) + (a[2]  * b[8] ) + (a[3]  * b[12]),
         (a[0]  * b[1]) + (a[1]  * b[5]) + (a[2]  * b[9] ) + (a[3]  * b[13]),
         (a[0]  * b[2]) + (a[1]  * b[6]) + (a[2]  * b[10]) + (a[3]  * b[14]),
         (a[0]  * b[3]) + (a[1]  * b[7]) + (a[2]  * b[11]) + (a[3]  * b[15]),
         (a[4]  * b[0]) + (a[5]  * b[4]) + (a[6]  * b[8] ) + (a[7]  * b[12]),
         (a[4]  * b[1]) + (a[5]  * b[5]) + (a[6]  * b[9] ) + (a[7]  * b[13]),
         (a[4]  * b[2]) + (a[5]  * b[6]) + (a[6]  * b[10]) + (a[7]  * b[14]),
         (a[4]  * b[3]) + (a[5]  * b[7]) + (a[6]  * b[11]) + (a[7]  * b[15]),
         (a[8]  * b[0]) + (a[9]  * b[4]) + (a[10] * b[8] ) + (a[11] * b[12]),
         (a[8]  * b[1]) + (a[9]  * b[5]) + (a[10] * b[9] ) + (a[11] * b[13]),
         (a[8]  * b[2]) + (a[9]  * b[6]) + (a[10] * b[10]) + (a[11] * b[14]),
         (a[8]  * b[3]) + (a[9]  * b[7]) + (a[10] * b[11]) + (a[11] * b[15]),
         (a[12] * b[0]) + (a[13] * b[4]) + (a[14] * b[8] ) + (a[15] * b[12]),
         (a[12] * b[1]) + (a[13] * b[5]) + (a[14] * b[9] ) + (a[15] * b[13]),
         (a[12] * b[2]) + (a[13] * b[6]) + (a[14] * b[10]) + (a[15] * b[14]),
         (a[12] * b[3]) + (a[13] * b[7]) + (a[14] * b[11]) + (a[15] * b[15])};

    memcpy(r, rTemp, 16 * sizeof(T));
}

template <typename T>
FORCE_INLINE void MultiplyScalar(const T a[16], T b, T r[16]){
    T rTemp[] = { (a[0]  * b), (a[1]  * b), (a[2]  * b), (a[3]  * b),
                  (a[4]  * b), (a[5]  * b), (a[6]  * b), (a[7]  * b),
                  (a[8]  * b), (a[9]  * b), (a[10] * b), (a[11] * b),
                  (a[12] * b), (a[13] * b), (a[14] * b), (a[15] * b) };

   memcpy(r, rTemp, 16 * sizeof(T));
}

};  // namespace Mat4
};  // namespace Util
};  // namespace Divide

#endif  //_CORE_MATH_MATH_HELPER_INL_

Commits for Divide-Framework/trunk/Source Code/Core/Math/Headers/MathHelper.inl

Diff revisions: vs.
Revision Author Commited Message
557 Diff Diff IonutCava picture IonutCava Tue 10 Nov, 2015 12:00:20 +0000

[IonutCava]
- Gather all rendering calls from glVertexArray and glGenericVertexData into a single function in glResources: submitRenderCommand.
— This allows both classes to interpret GenericDrawCommands in the same way
- Use unsigned ints for all hasehs instead of size_t to avoid negative number hashes (useful for debugging)

552 Diff Diff IonutCava picture IonutCava Fri 06 Nov, 2015 17:07:22 +0000

[Ionut]
- Use UNIT0 and UNIT1 during depth pass for transparency texture to reduce texture binds between calls
- Allow different VAOs with different attributes per render stage per glVertexArray instance
- Initial work on multiple glVertexArray instances per single VBO (attempt to get VBOs to the 4MB mark) - Currently crashes the build
- Fix a bug on exit if background tasks depended on scene elements. Wait for the main threadpool to clear before destroying the scene.
- Use a different node buffer per rendering stage to avoid waiting too much on fence syncs

520 Diff Diff IonutCava picture IonutCava Tue 06 Oct, 2015 10:53:06 +0000

[IonutCava]
- Reduce VBO memory usage for animated characters by packing bone weights in a single unsigned integer.
- Improve performance by using pre-calculated weight.w factor and by sending normalized color data instead of converting it in the shader.

504 Diff Diff IonutCava picture IonutCava Tue 25 Aug, 2015 16:17:38 +0000

[IonutCava]
- Better caching of draw commands (W.I.P.)
- Remove unused occlusion culling code. Will be replaced with Hi-Z based culling

483 Diff Diff IonutCava picture IonutCava Sat 08 Aug, 2015 19:31:24 +0000

[Ionut]
- Removed OcclusionQueryHelper in preparation for a new occlusion system
— New method will use compute shaders and modify the command buffer directly setting primCount to 0 for occluded objects (No CPU roundtrip)
- Cleaned up the GenericDrawCommand system (usage and format)

480 Diff Diff IonutCava picture IonutCava Fri 07 Aug, 2015 15:51:09 +0000

[Ionut]
- Rework PhysicsAsset <-> PhysicsComponent transform synchronization
- Added better GPU Validation defines

464 Diff Diff IonutCava picture IonutCava Thu 04 Jun, 2015 16:07:22 +0000

[Ionut]
- Linux port work:
— Improved timing code
— Split solution in 3 projects: Divide-Game, Divide-Networking, Divide-Server all in a single working set.
- Fixed OpenGL context creation fail message

457 Diff Diff IonutCava picture IonutCava Mon 01 Jun, 2015 21:45:52 +0000

[Ionut]
- Windows build fix for previous commit

452 Diff Diff IonutCava picture IonutCava Fri 29 May, 2015 16:04:26 +0000

[Ionut]
- Initial work on Linux port:
— Some GCC compilation issues fixed

444 IonutCava picture IonutCava Thu 21 May, 2015 16:06:53 +0000

[Ionut]
- More platform independent code cleanup:
— Moved platform specific defines to their respective header files (e.g. THREAD_LOCAL)
— Moved most preprocessor defines from the VisualC++ projects into source code
— Removed compiler specific code (e.g. warning disables) and fixed most warning resulting from this