-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcrn_decomp.h
4833 lines (3855 loc) · 146 KB
/
crn_decomp.h
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File: crn_decomp.h - Fast CRN->DXTc texture transcoder header file library
// Copyright (c) 2010-2012 Rich Geldreich and Tenacious Software LLC
// See Copyright Notice and license at the end of this file.
//
// This single header file contains *all* of the code necessary to unpack .CRN files to raw DXTn bits.
// It does NOT depend on the crn compression library.
//
// Note: This is a single file, stand-alone C++ library which is controlled by the use of two macros:
// If CRND_INCLUDE_CRND_H is NOT defined, the header is included.
// If CRND_HEADER_FILE_ONLY is NOT defined, the implementation is included.
//
// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
//
#ifndef CRND_INCLUDE_CRND_H
#define CRND_INCLUDE_CRND_H
// Include crnlib.h (only to bring in some basic CRN-related types).
#include <stdint.h>
#include "crnlib.h"
// changelog:
// v1.05 - bugfixed errors and warnings on (g++/mingw) (@r-lyeh)
// using stdint.h types now (@r-lyeh)
// v1.04 - svn checkout
#define CRND_LIB_VERSION 105
#define CRND_VERSION_STRING "01.05"
#ifdef _DEBUG
#define CRND_BUILD_DEBUG
#else
#define CRND_BUILD_RELEASE
#endif
// CRN decompression API
namespace crnd
{
typedef uint8_t uint8;
typedef int8_t int8;
typedef uint16_t uint16;
typedef int16_t int16;
typedef uint32_t uint32;
typedef int32_t int32;
typedef unsigned int uint;
typedef uint64_t uint64;
typedef int64_t int64;
// The crnd library assumes all allocation blocks have at least CRND_MIN_ALLOC_ALIGNMENT alignment.
const uint32 CRND_MIN_ALLOC_ALIGNMENT = sizeof(uint32) * 2U;
// realloc callback:
// Used to allocate, resize, or free memory blocks.
// If p is NULL, the realloc function attempts to allocate a block of at least size bytes. Returns NULL on out of memory.
// *pActual_size must be set to the actual size of the allocated block, which must be greater than or equal to the requested size.
// If p is not NULL, and size is 0, the realloc function frees the specified block, and always returns NULL. *pActual_size should be set to 0.
// If p is not NULL, and size is non-zero, the realloc function attempts to resize the specified block:
// If movable is false, the realloc function attempts to shrink or expand the block in-place. NULL is returned if the block cannot be resized in place, or if the
// underlying heap implementation doesn't support in-place resizing. Otherwise, the pointer to the original block is returned.
// If movable is true, it is permissible to move the block's contents if it cannot be resized in place. NULL is returned if the block cannot be resized in place, and there
// is not enough memory to relocate the block.
// In all cases, *pActual_size must be set to the actual size of the allocated block, whether it was successfully resized or not.
typedef void* (*crnd_realloc_func)(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data);
// msize callback: Returns the size of the memory block in bytes, or 0 if the pointer or block is invalid.
typedef size_t (*crnd_msize_func)(void* p, void* pUser_data);
// crnd_set_memory_callbacks() - Use to override the crnd library's memory allocation functions.
// If any input parameters are NULL, the memory callback functions are reset to the default functions.
// The default functions call malloc(), free(), _msize(), _expand(), etc.
void crnd_set_memory_callbacks(crnd_realloc_func pRealloc, crnd_msize_func pMSize, void* pUser_data);
struct crn_file_info
{
inline crn_file_info() : m_struct_size(sizeof(crn_file_info)) { }
uint32 m_struct_size;
uint32 m_actual_data_size;
uint32 m_header_size;
uint32 m_total_palette_size;
uint32 m_tables_size;
uint32 m_levels;
uint32 m_level_compressed_size[cCRNMaxLevels];
uint32 m_color_endpoint_palette_entries;
uint32 m_color_selector_palette_entries;
uint32 m_alpha_endpoint_palette_entries;
uint32 m_alpha_selector_palette_entries;
};
struct crn_texture_info
{
inline crn_texture_info() : m_struct_size(sizeof(crn_texture_info)) { }
uint32 m_struct_size;
uint32 m_width;
uint32 m_height;
uint32 m_levels;
uint32 m_faces;
uint32 m_bytes_per_block;
uint32 m_userdata0;
uint32 m_userdata1;
crn_format m_format;
};
struct crn_level_info
{
inline crn_level_info() : m_struct_size(sizeof(crn_level_info)) { }
uint32 m_struct_size;
uint32 m_width;
uint32 m_height;
uint32 m_faces;
uint32 m_blocks_x;
uint32 m_blocks_y;
uint32 m_bytes_per_block;
crn_format m_format;
};
// Returns the FOURCC format code corresponding to the specified CRN format.
uint32 crnd_crn_format_to_fourcc(crn_format fmt);
// Returns the fundamental GPU format given a potentially swizzled DXT5 crn_format.
crn_format crnd_get_fundamental_dxt_format(crn_format fmt);
// Returns the size of the crn_format in bits/texel (either 4 or 8).
uint32 crnd_get_crn_format_bits_per_texel(crn_format fmt);
// Returns the number of bytes per DXTn block (8 or 16).
uint32 crnd_get_bytes_per_dxt_block(crn_format fmt);
// Validates the entire file by checking the header and data CRC's.
// This is not something you want to be doing much!
// The crn_file_info.m_struct_size field must be set before calling this function.
bool crnd_validate_file(const void* pData, uint32 data_size, crn_file_info* pFile_info);
// Retrieves texture information from the CRN file.
// The crn_texture_info.m_struct_size field must be set before calling this function.
bool crnd_get_texture_info(const void* pData, uint32 data_size, crn_texture_info* pTexture_info);
// Retrieves mipmap level specific information from the CRN file.
// The crn_level_info.m_struct_size field must be set before calling this function.
bool crnd_get_level_info(const void* pData, uint32 data_size, uint32 level_index, crn_level_info* pLevel_info);
// Transcode/unpack context handle.
typedef void* crnd_unpack_context;
// crnd_unpack_begin() - Decompresses the texture's decoder tables and endpoint/selector palettes.
// Once you call this function, you may call crnd_unpack_level() to unpack one or more mip levels.
// Don't call this once per mip level (unless you absolutely must)!
// This function allocates enough memory to hold: Huffman decompression tables, and the endpoint/selector palettes (color and/or alpha).
// Worst case allocation is approx. 200k, assuming all palettes contain 8192 entries.
// pData must point to a buffer holding all of the compressed .CRN file data.
// This buffer must be stable until crnd_unpack_end() is called.
// Returns NULL if out of memory, or if any of the input parameters are invalid.
crnd_unpack_context crnd_unpack_begin(const void* pData, uint32 data_size);
// Returns a pointer to the compressed .CRN data associated with a crnd_unpack_context.
// Returns false if any of the input parameters are invalid.
bool crnd_get_data(crnd_unpack_context pContext, const void** ppData, uint32* pData_size);
// crnd_unpack_level() - Transcodes the specified mipmap level to a destination buffer in cached or write combined memory.
// pContext - Context created by a call to crnd_unpack_begin().
// ppDst - A pointer to an array of 1 or 6 destination buffer pointers. Cubemaps require an array of 6 pointers, 2D textures require an array of 1 pointer.
// dst_size_in_bytes - Optional size of each destination buffer. Only used for debugging - OK to set to UINT32_MAX.
// row_pitch_in_bytes - The pitch in bytes from one row of DXT blocks to the next. Must be a multiple of 4.
// level_index - mipmap level index, where 0 is the largest/first level.
// Returns false if any of the input parameters, or the compressed stream, are invalid.
// This function does not allocate any memory.
bool crnd_unpack_level(
crnd_unpack_context pContext,
void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes,
uint32 level_index);
// crnd_unpack_level_segmented() - Unpacks the specified mipmap level from a "segmented" CRN file.
// See the crnd_create_segmented_file() API below.
// Segmented files allow the user to control where the compressed mipmap data is stored.
bool crnd_unpack_level_segmented(
crnd_unpack_context pContext,
const void* pSrc, uint32 src_size_in_bytes,
void** ppDst, uint32 dst_size_in_bytes, uint32 row_pitch_in_bytes,
uint32 level_index);
// crnd_unpack_end() - Frees the decompress tables and unpacked palettes associated with the specified unpack context.
// Returns false if the context is NULL, or if it points to an invalid context.
// This function frees all memory associated with the context.
bool crnd_unpack_end(crnd_unpack_context pContext);
// The following API's allow the user to create "segmented" CRN files. A segmented file contains multiple pieces:
// - Base data: Header + compression tables
// - Level data: Individual mipmap levels
// This allows mipmap levels from multiple CRN files to be tightly packed together into single files.
// Returns a pointer to the level's compressed data, and optionally returns the level's compressed data size if pSize is not NULL.
const void* crnd_get_level_data(const void* pData, uint32 data_size, uint32 level_index, uint32* pSize);
// Returns the compressed size of the texture's header and compression tables (but no levels).
uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size);
// Creates a "segmented" CRN texture from a normal CRN texture. The new texture will be created at pBase_data, and will be crnd_get_base_data_size() bytes long.
// base_data_size must be >= crnd_get_base_data_size().
// The base data will contain the CRN header and compression tables, but no mipmap data.
bool crnd_create_segmented_file(const void* pData, uint32 data_size, void* pBase_data, uint base_data_size);
} // namespace crnd
// Low-level CRN file header cracking.
namespace crnd
{
template<unsigned int N>
struct crn_packed_uint
{
inline crn_packed_uint() { }
inline crn_packed_uint(unsigned int val) { *this = val; }
inline crn_packed_uint(const crn_packed_uint& other) { *this = other; }
inline crn_packed_uint& operator= (const crn_packed_uint& rhs)
{
if (this != &rhs)
memcpy(m_buf, rhs.m_buf, sizeof(m_buf));
return *this;
}
inline crn_packed_uint& operator= (unsigned int val)
{
//CRND_ASSERT((N == 4U) || (val < (1U << (N * 8U))));
val <<= (8U * (4U - N));
for (unsigned int i = 0; i < N; i++)
{
m_buf[i] = static_cast<unsigned char>(val >> 24U);
val <<= 8U;
}
return *this;
}
inline operator unsigned int() const
{
switch (N)
{
case 1: return m_buf[0];
case 2: return (m_buf[0] << 8U) | m_buf[1];
case 3: return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]);
default: return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]);
}
}
unsigned char m_buf[N];
};
#pragma pack(push)
#pragma pack(1)
struct crn_palette
{
crn_packed_uint<3> m_ofs;
crn_packed_uint<3> m_size;
crn_packed_uint<2> m_num;
};
enum crn_header_flags
{
// If set, the compressed mipmap level data is not located after the file's base data - it will be separately managed by the user instead.
cCRNHeaderFlagSegmented = 1
};
struct crn_header
{
enum { cCRNSigValue = ('H' << 8) | 'x' };
crn_packed_uint<2> m_sig;
crn_packed_uint<2> m_header_size;
crn_packed_uint<2> m_header_crc16;
crn_packed_uint<4> m_data_size;
crn_packed_uint<2> m_data_crc16;
crn_packed_uint<2> m_width;
crn_packed_uint<2> m_height;
crn_packed_uint<1> m_levels;
crn_packed_uint<1> m_faces;
crn_packed_uint<1> m_format;
crn_packed_uint<2> m_flags;
crn_packed_uint<4> m_reserved;
crn_packed_uint<4> m_userdata0;
crn_packed_uint<4> m_userdata1;
crn_palette m_color_endpoints;
crn_palette m_color_selectors;
crn_palette m_alpha_endpoints;
crn_palette m_alpha_selectors;
crn_packed_uint<2> m_tables_size;
crn_packed_uint<3> m_tables_ofs;
// m_level_ofs[] is actually an array of offsets: m_level_ofs[m_levels]
crn_packed_uint<4> m_level_ofs[1];
};
const unsigned int cCRNHeaderMinSize = 62U;
#pragma pack(pop)
} // namespace crnd
#endif // CRND_INCLUDE_CRND_H
// Internal library source follows this line.
#ifndef CRND_HEADER_FILE_ONLY
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#include <memory.h>
#else
#include <malloc.h>
#endif
#include <stdarg.h>
#include <new> // needed for placement new, _msize, _expand
#define CRND_RESTRICT __restrict
#ifdef _MSC_VER
#include <intrin.h>
#pragma intrinsic(_WriteBarrier)
#pragma intrinsic(_ReadWriteBarrier)
#define CRND_WRITE_BARRIER _WriteBarrier();
#define CRND_FULL_BARRIER _ReadWriteBarrier();
#else
#define CRND_WRITE_BARRIER
#define CRND_FULL_BARRIER
#endif
#ifdef _MSC_VER
#pragma warning(disable:4127) // warning C4127: conditional expression is constant
#endif
#ifdef CRND_DEVEL
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x500
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef
#define NOMINMAX
#endif
#include "windows.h" // only for IsDebuggerPresent(), DebugBreak(), and OutputDebugStringA()
#endif
// File: crnd_types.h
namespace crnd
{
const crn_uint8 cUINT8_MIN = 0;
const crn_uint8 cUINT8_MAX = 0xFFU;
const uint16 cUINT16_MIN = 0;
const uint16 cUINT16_MAX = 0xFFFFU;
const uint32 cUINT32_MIN = 0;
const uint32 cUINT32_MAX = 0xFFFFFFFFU;
const int8 cINT8_MIN = -128;
const int8 cINT8_MAX = 127;
const int16 cINT16_MIN = -32768;
const int16 cINT16_MAX = 32767;
const int32 cINT32_MIN = (-2147483647 - 1);
const int32 cINT32_MAX = 2147483647;
enum eClear { cClear };
const uint32 cIntBits = 32U;
#ifdef _WIN64
typedef uint64 ptr_bits;
#else
typedef uint32 ptr_bits;
#endif
template<typename T> struct int_traits { enum { cMin = crnd::cINT32_MIN, cMax = crnd::cINT32_MAX, cSigned = true }; };
template<> struct int_traits<int8> { enum { cMin = crnd::cINT8_MIN, cMax = crnd::cINT8_MAX, cSigned = true }; };
template<> struct int_traits<int16> { enum { cMin = crnd::cINT16_MIN, cMax = crnd::cINT16_MAX, cSigned = true }; };
template<> struct int_traits<int32> { enum { cMin = crnd::cINT32_MIN, cMax = crnd::cINT32_MAX, cSigned = true }; };
template<> struct int_traits<uint8> { enum { cMin = 0, cMax = crnd::cUINT8_MAX, cSigned = false }; };
template<> struct int_traits<uint16> { enum { cMin = 0, cMax = crnd::cUINT16_MAX, cSigned = false }; };
template<> struct int_traits<uint32> { enum { cMin = 0, cMax = crnd::cUINT32_MAX, cSigned = false }; };
struct empty_type { };
} // namespace crnd
// File: crnd_platform.h
namespace crnd
{
#ifdef _XBOX
const bool c_crnd_little_endian_platform = false;
const bool c_crnd_big_endian_platform = true;
#define CRND_BIG_ENDIAN_PLATFORM 1
#else
const bool c_crnd_little_endian_platform = true;
const bool c_crnd_big_endian_platform = false;
#endif
bool crnd_is_debugger_present();
void crnd_debug_break();
void crnd_output_debug_string(const char* p);
// actually in crnd_assert.cpp
void crnd_assert(const char* pExp, const char* pFile, unsigned line);
void crnd_fail(const char* pExp, const char* pFile, unsigned line);
} // namespace crnd
// File: crnd_assert.h
namespace crnd
{
void crnd_assert(const char* pExp, const char* pFile, unsigned line);
#ifdef NDEBUG
#define CRND_ASSERT(x) ((void)0)
#undef CRND_ASSERTS_ENABLED
#else
#define CRND_ASSERT(_exp) (void)( (!!(_exp)) || (crnd::crnd_assert(#_exp, __FILE__, __LINE__), 0) )
#define CRND_ASSERTS_ENABLED
#endif
void crnd_trace(const char* pFmt, va_list args);
void crnd_trace(const char* pFmt, ...);
} // namespace crnd
// File: crnd_helpers.h
namespace crnd
{
namespace helpers
{
template<typename T> struct rel_ops
{
friend bool operator!= (const T& x, const T& y) { return (!(x == y)); }
friend bool operator> (const T& x, const T& y) { return (y < x); }
friend bool operator<= (const T& x, const T& y) { return (!(y < x)); }
friend bool operator>= (const T& x, const T& y) { return (!(x < y)); }
};
template <typename T>
inline T* construct(T* p)
{
return new (static_cast<void*>(p)) T;
}
template <typename T, typename U>
inline T* construct(T* p, const U& init)
{
return new (static_cast<void*>(p)) T(init);
}
template <typename T>
void construct_array(T* p, uint32 n)
{
T* q = p + n;
for ( ; p != q; ++p)
new (static_cast<void*>(p)) T;
}
template <typename T, typename U>
void construct_array(T* p, uint32 n, const U& init)
{
T* q = p + n;
for ( ; p != q; ++p)
new (static_cast<void*>(p)) T(init);
}
template <typename T>
inline void destruct(T* p)
{
p;
p->~T();
}
template <typename T> inline void destruct_array(T* p, uint32 n)
{
T* q = p + n;
for ( ; p != q; ++p)
p->~T();
}
} // namespace helpers
} // namespace crnd
// File: crnd_traits.h
namespace crnd
{
template<typename T>
struct scalar_type
{
enum { cFlag = false };
static inline void construct(T* p) { helpers::construct(p); }
static inline void construct(T* p, const T& init) { helpers::construct(p, init); }
static inline void construct_array(T* p, uint32 n) { helpers::construct_array(p, n); }
static inline void destruct(T* p) { helpers::destruct(p); }
static inline void destruct_array(T* p, uint32 n) { helpers::destruct_array(p, n); }
};
template<typename T> struct scalar_type<T*>
{
enum { cFlag = true };
static inline void construct(T** p) { memset(p, 0, sizeof(T*)); }
static inline void construct(T** p, T* init) { *p = init; }
static inline void construct_array(T** p, uint32 n) { memset(p, 0, sizeof(T*) * n); }
static inline void destruct(T** p) { p; }
static inline void destruct_array(T** p, uint32 n) { p, n; }
};
#define CRND_DEFINE_BUILT_IN_TYPE(X) \
template<> struct scalar_type<X> { \
enum { cFlag = true }; \
static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \
static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \
static inline void construct_array(X* p, uint32 n) { memset(p, 0, sizeof(X) * n); } \
static inline void destruct(X* p) { p; } \
static inline void destruct_array(X* p, uint32 n) { p, n; } };
CRND_DEFINE_BUILT_IN_TYPE(bool)
CRND_DEFINE_BUILT_IN_TYPE(int8_t)
CRND_DEFINE_BUILT_IN_TYPE(uint8_t)
CRND_DEFINE_BUILT_IN_TYPE(int16_t)
CRND_DEFINE_BUILT_IN_TYPE(uint16_t)
CRND_DEFINE_BUILT_IN_TYPE(int32_t)
CRND_DEFINE_BUILT_IN_TYPE(uint32_t)
CRND_DEFINE_BUILT_IN_TYPE(int64_t)
CRND_DEFINE_BUILT_IN_TYPE(uint64_t)
CRND_DEFINE_BUILT_IN_TYPE(float)
CRND_DEFINE_BUILT_IN_TYPE(double)
CRND_DEFINE_BUILT_IN_TYPE(long double)
#undef CRND_DEFINE_BUILT_IN_TYPE
// See: http://erdani.org/publications/cuj-2004-06.pdf
template<typename T>
struct bitwise_movable { enum { cFlag = false }; };
// Defines type Q as bitwise movable.
#define CRND_DEFINE_BITWISE_MOVABLE(Q) template<> struct bitwise_movable<Q> { enum { cFlag = true }; };
// From yasli_traits.h:
// Credit goes to Boost;
// also found in the C++ Templates book by Vandevoorde and Josuttis
typedef char (&yes_t)[1];
typedef char (&no_t)[2];
template <class U> yes_t class_test(int U::*);
template <class U> no_t class_test(...);
template <class T> struct is_class
{
enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) };
};
template <typename T> struct is_pointer
{
enum { value = false };
};
template <typename T> struct is_pointer<T*>
{
enum { value = true };
};
#define CRND_IS_POD(T) __is_pod(T)
} // namespace crnd
// File: crnd_mem.h
namespace crnd
{
void* crnd_malloc(size_t size, size_t* pActual_size = NULL);
void* crnd_realloc(void* p, size_t size, size_t* pActual_size = NULL, bool movable = true);
void crnd_free(void* p);
size_t crnd_msize(void* p);
template<typename T>
inline T* crnd_new()
{
T* p = static_cast<T*>(crnd_malloc(sizeof(T)));
if (!p)
return NULL;
return helpers::construct(p);
}
template<typename T>
inline T* crnd_new(const T& init)
{
T* p = static_cast<T*>(crnd_malloc(sizeof(T)));
if (!p)
return NULL;
return helpers::construct(p, init);
}
template<typename T>
inline T* crnd_new_array(uint32 num)
{
if (!num) num = 1;
uint8* q = static_cast<uint8*>(crnd_malloc(CRND_MIN_ALLOC_ALIGNMENT + sizeof(T) * num));
if (!q)
return NULL;
T* p = reinterpret_cast<T*>(q + CRND_MIN_ALLOC_ALIGNMENT);
reinterpret_cast<uint32*>(p)[-1] = num;
reinterpret_cast<uint32*>(p)[-2] = ~num;
helpers::construct_array(p, num);
return p;
}
template<typename T>
inline void crnd_delete(T* p)
{
if (p)
{
helpers::destruct(p);
crnd_free(p);
}
}
template<typename T>
inline void crnd_delete_array(T* p)
{
if (p)
{
const uint32 num = reinterpret_cast<uint32*>(p)[-1];
const uint32 num_check = reinterpret_cast<uint32*>(p)[-2];
num_check;
CRND_ASSERT(num && (num == ~num_check));
helpers::destruct_array(p, num);
crnd_free(reinterpret_cast<uint8*>(p) - CRND_MIN_ALLOC_ALIGNMENT);
}
}
} // namespace crnd
// File: crnd_math.h
namespace crnd
{
namespace math
{
const float cNearlyInfinite = 1.0e+37f;
const float cDegToRad = 0.01745329252f;
const float cRadToDeg = 57.29577951f;
extern uint32 g_bitmasks[32];
// Yes I know these should probably be pass by ref, not val:
// http://www.stepanovpapers.com/notes.pdf
// Just don't use them on non-simple (non built-in) types!
template<typename T> inline T minimum(T a, T b)
{
return (a < b) ? a : b;
}
template<typename T> inline T minimum(T a, T b, T c)
{
return minimum(minimum(a, b), c);
}
template<typename T> inline T maximum(T a, T b)
{
return (a > b) ? a : b;
}
template<typename T> inline T maximum(T a, T b, T c)
{
return maximum(maximum(a, b), c);
}
template<typename T> inline T clamp(T value, T low, T high)
{
return (value < low) ? low : ((value > high) ? high : value);
}
template<typename T> inline T square(T value)
{
return value * value;
}
inline bool is_power_of_2(uint32 x)
{
return x && ((x & (x - 1U)) == 0U);
}
// From "Hackers Delight"
inline int next_pow2(uint32 val)
{
val--;
val |= val >> 16;
val |= val >> 8;
val |= val >> 4;
val |= val >> 2;
val |= val >> 1;
return val + 1;
}
// Returns the total number of bits needed to encode v.
inline uint32 total_bits(uint32 v)
{
uint32 l = 0;
while (v > 0U)
{
v >>= 1;
l++;
}
return l;
}
inline uint floor_log2i(uint v)
{
uint l = 0;
while (v > 1U)
{
v >>= 1;
l++;
}
return l;
}
inline uint ceil_log2i(uint v)
{
uint l = floor_log2i(v);
if ((l != cIntBits) && (v > (1U << l)))
l++;
return l;
}
}
}
// File: crnd_utils.h
namespace crnd
{
namespace utils
{
template<typename T> inline void zero_object(T& obj)
{
memset(&obj, 0, sizeof(obj));
}
template<typename T> inline void zero_this(T* pObj)
{
memset(pObj, 0, sizeof(*pObj));
}
template <typename T>
inline void swap(T& left, T& right)
{
T temp(left);
left = right;
right = temp;
}
inline void invert_buf(void* pBuf, uint32 size)
{
uint8* p = static_cast<uint8*>(pBuf);
const uint32 half_size = size >> 1;
for (uint32 i = 0; i < half_size; i++)
swap(p[i], p[size - 1U - i]);
}
static inline uint16 swap16(uint16 x) { return static_cast<uint16>((x << 8) | (x >> 8)); }
static inline uint32 swap32(uint32 x) { return ((x << 24) | ((x << 8) & 0x00FF0000) | (( x >> 8) & 0x0000FF00) | (x >> 24)); }
uint32 compute_max_mips(uint32 width, uint32 height);
} // namespace utils
} // namespace crnd
// File: crnd_vector.h
namespace crnd
{
struct elemental_vector
{
void* m_p;
uint32 m_size;
uint32 m_capacity;
typedef void (*object_mover)(void* pDst, void* pSrc, uint32 num);
bool increase_capacity(uint32 min_new_capacity, bool grow_hint, uint32 element_size, object_mover pRelocate);
};
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127) // warning C4127: conditional expression is constant
#endif
template<typename T>
class vector : public helpers::rel_ops< vector<T> >
{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
inline vector() :
m_p(NULL),
m_size(0),
m_capacity(0),
m_alloc_failed(false)
{
}
inline vector(const vector& other) :
m_p(NULL),
m_size(0),
m_capacity(0),
m_alloc_failed(false)
{
*this = other;
}
inline vector(uint32 size) :
m_p(NULL),
m_size(0),
m_capacity(0),
m_alloc_failed(false)
{
resize(size);
}
inline ~vector()
{
clear();
}
// I don't like this. Not at all. But exceptions, or just failing suck worse.
inline bool get_alloc_failed() const { return m_alloc_failed; }
inline void clear_alloc_failed() { m_alloc_failed = false; }
inline bool assign(const vector& other)
{
if (this == &other)
return true;
if (m_capacity == other.m_size)
resize(0);
else
{
clear();
if (!increase_capacity(other.m_size, false))
return false;
}
if (scalar_type<T>::cFlag)
memcpy(m_p, other.m_p, other.m_size * sizeof(T));
else
{
T* pDst = m_p;
const T* pSrc = other.m_p;
for (uint32 i = other.m_size; i > 0; i--)
helpers::construct(pDst++, *pSrc++);
}
m_size = other.m_size;
return true;
}
inline vector& operator= (const vector& other)
{
assign(other);
return *this;
}
inline const T* begin() const { return m_p; }
T* begin() { return m_p; }
inline const T* end() const { return m_p + m_size; }
T* end() { return m_p + m_size; }
inline bool empty() const { return !m_size; }
inline uint32 size() const { return m_size; }
inline uint32 capacity() const { return m_capacity; }
inline const T& operator[] (uint32 i) const { CRND_ASSERT(i < m_size); return m_p[i]; }
inline T& operator[] (uint32 i) { CRND_ASSERT(i < m_size); return m_p[i]; }
inline const T& front() const { CRND_ASSERT(m_size); return m_p[0]; }
inline T& front() { CRND_ASSERT(m_size); return m_p[0]; }
inline const T& back() const { CRND_ASSERT(m_size); return m_p[m_size - 1]; }
inline T& back() { CRND_ASSERT(m_size); return m_p[m_size - 1]; }
inline void clear()
{
if (m_p)
{
scalar_type<T>::destruct_array(m_p, m_size);
crnd_free(m_p);
m_p = NULL;
m_size = 0;
m_capacity = 0;
}
m_alloc_failed = false;
}
inline bool reserve(uint32 new_capacity)
{
if (!increase_capacity(new_capacity, false))
return false;
return true;
}
inline bool resize(uint32 new_size)
{
if (m_size != new_size)
{
if (new_size < m_size)
scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
else
{
if (new_size > m_capacity)
{
if (!increase_capacity(new_size, new_size == (m_size + 1)))
return false;
}
scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
}
m_size = new_size;
}
return true;
}
inline bool push_back(const T& obj)
{
CRND_ASSERT(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
if (m_size >= m_capacity)
{
if (!increase_capacity(m_size + 1, true))
return false;
}
scalar_type<T>::construct(m_p + m_size, obj);
m_size++;
return true;
}
inline void pop_back()
{
CRND_ASSERT(m_size);
if (m_size)
{
m_size--;
scalar_type<T>::destruct(&m_p[m_size]);
}
}
inline void insert(uint32 index, const T* p, uint32 n)
{
CRND_ASSERT(index <= m_size);
if (!n)
return;
const uint32 orig_size = m_size;
resize(m_size + n);
const T* pSrc = m_p + orig_size - 1;
T* pDst = const_cast<T*>(pSrc) + n;
const uint32 num_to_move = orig_size - index;
for (uint32 i = 0; i < num_to_move; i++)
{
CRND_ASSERT((pDst - m_p) < (int)m_size);