-
Notifications
You must be signed in to change notification settings - Fork 2
/
fbx.h
3740 lines (2938 loc) · 114 KB
/
fbx.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
//===-- fbx.h ---------------------------------------------*- mode: C99 -*-===//
//
// _____ _____ __ __
// | __| __ | | |
// | __| __ -|- -|
// |__| |_____|__|__|
//
// The contents of this of this file is released into the public domain.
//
//===----------------------------------------------------------------------===//
#ifndef _FBX_H_
#define _FBX_H_
#if defined(_MSC_VER)
#define FBX_ON_MSVC 1
#define FBX_ON_CLANG 0
#define FBX_ON_GCC 0
#elif defined(__GNUC__)
#if defined(__clang__)
#define FBX_ON_MSVC 0
#define FBX_ON_CLANG 1
#define FBX_ON_GCC 1
#else
/* HACK(mtwilliams): We assume that we're being compiled with GCC. */
#define FBX_ON_MSVC 0
#define FBX_ON_CLANG 0
#define FBX_ON_GCC 1
#endif
#endif
#if defined(_WIN32) || defined(_WIN64)
#define FBX_ON_WINDOWS 1
#define FBX_ON_MAC 0
#define FBX_ON_LINUX 0
#define FBX_ON_ANDROID 0
#define FBX_ON_IOS 0
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_MAC
#define FBX_ON_WINDOWS 0
#define FBX_ON_MAC 1
#define FBX_ON_LINUX 0
#define FBX_ON_ANDROID 0
#define FBX_ON_IOS 0
#elif TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#define FBX_ON_WINDOWS 0
#define FBX_ON_MAC 0
#define FBX_ON_LINUX 0
#define FBX_ON_ANDROID 0
#define FBX_ON_IOS 1
#elif TARGET_OS_TVOS
/* Realistically, we could support tvOS... */
#error ("We don't support tvOS!")
#else
#error ("We don't support this platform!")
#endif
#elif defined(__linux__)
#if defined(ANDROID)
#define FBX_ON_WINDOWS 0
#define FBX_ON_MAC 0
#define FBX_ON_LINUX 0
#define FBX_ON_ANDROID 1
#define FBX_ON_IOS 0
#else
#define FBX_ON_WINDOWS 0
#define FBX_ON_MAC 0
#define FBX_ON_LINUX 1
#define FBX_ON_ANDROID 0
#define FBX_ON_IOS 0
#endif
#else
#error ("We don't support this platform!")
#endif
#if FBX_ON_WINDOWS
#define FBX_ON_POSIX 0
#elif FBX_ON_MAC || FBX_ON_LINUX
#define FBX_ON_POSIX 1
#elif FBX_ON_ANDROID || FBX_ON_IOS
/* Ostensibly... */
#define FBX_ON_POSIX 1
#endif
#if defined(_M_IX86) || defined(__i386__)
#define FBX_ON_X86 1
#define FBX_ON_X86_64 0
#elif defined(_M_X64) || defined(__x86_64__)
#define FBX_ON_X86 0
#define FBX_ON_X86_64 1
#endif
#if FBX_ON_WINDOWS
#define FBX_EXPORT(Return) Return __stdcall
#define FBX_CALLBACK(Return) Return __cdecl
#elif FBX_ON_MAC
#define FBX_EXPORT(Return) Return
#define FBX_CALLBACK(Return) Return
#elif FBX_ON_LINUX
#define FBX_EXPORT(Return) Return
#define FBX_CALLBACK(Return) Return
#elif FBX_ON_ANDROID
#define FBX_EXPORT(Return) Return
#define FBX_CALLBACK(Return) Return
#elif FBX_ON_IOS
#define FBX_EXPORT(Return) Return
#define FBX_CALLBACK(Return) Return
#endif
#if defined(__cplusplus)
#define FBX_BEGIN_EXTERN_C extern "C" {
#define FBX_END_EXTERN_C }
#else
#define FBX_BEGIN_EXTERN_C
#define FBX_END_EXTERN_C
#endif
FBX_BEGIN_EXTERN_C
typedef signed char fbx_int8_t;
typedef unsigned char fbx_uint8_t;
typedef signed short fbx_int16_t;
typedef unsigned short fbx_uint16_t;
typedef signed int fbx_int32_t;
typedef unsigned int fbx_uint32_t;
#if defined(_MSC_VER)
typedef signed __int64 fbx_int64_t;
typedef unsigned __int64 fbx_uint64_t;
#else
typedef signed long long fbx_int64_t;
typedef unsigned long long fbx_uint64_t;
#endif
typedef float fbx_real32_t;
typedef double fbx_real64_t;
#if (_MSC_VER >= 1300 && _Wp64)
#if FBX_ON_X86
typedef __w64 signed long fbx_intptr_t;
typedef __w64 unsigned long fbx_uintptr_t;
typedef __w64 unsigned long fbx_size_t;
#elif FBX_ON_X86_64
typedef __w64 signed __int64 fbx_intptr_t;
typedef __w64 unsigned __int64 fbx_uintptr_t;
typedef __w64 unsigned __int64 fbx_size_t;
#endif
#else
#if FBX_ON_X86
typedef fbx_int32_t fbx_intptr_t;
typedef fbx_uint32_t fbx_uintptr_t;
typedef fbx_uint32_t fbx_size_t;
#elif FBX_ON_X86_64
typedef fbx_int64_t fbx_intptr_t;
typedef fbx_uint64_t fbx_uintptr_t;
typedef fbx_uint64_t fbx_size_t;
#endif
#endif
/* We (have to) define our own boolean type regardless of compiler support
to prevent its width from being changed on us. We use this throughout the
codebase. Be careful not mix our type with others, since it's not
compatible! */
typedef fbx_uint32_t fbx_bool_t;
#define FBX_TRUE ((fbx_bool_t)1)
#define FBX_FALSE ((fbx_bool_t)0)
/* This will cause a negative subscript error if the sizes of our types don't
match our expectations. */
#define FBX_CHECK_SIZE_OF_TYPE(expression) \
typedef char fbx__size_matches_expectation[(expression) ? 1 : -1]
/* Ensure fixed-width types match our expectations. */
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_int8_t) == 1);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_uint8_t) == 1);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_int16_t) == 2);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_uint16_t) == 2);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_int32_t) == 4);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_uint32_t) == 4);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_int64_t) == 8);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_uint64_t) == 8);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_real32_t) == 4);
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_real64_t) == 8);
/* Ensure pointer-width integer types can actually hold pointers. */
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_intptr_t) == sizeof(void *));
FBX_CHECK_SIZE_OF_TYPE(sizeof(fbx_uintptr_t) == sizeof(void *));
#undef FBX_CHECK_SIZE_OF_TYPE
typedef enum fbx_status {
FBX_OK = 0,
FBX_EMEMORY = -1, /* Out of memory. */
FBX_ESPACE = -2, /* Ran out of space. */
FBX_EFORMAT = -3, /* Malformed. */
FBX_EVERSION = -4, /* Unsupported version. */
FBX_EFEATURE = -5, /* Unsupported feature. */
FBX_EDATA = -6, /* Bad or insufficent data. */
} fbx_status_t;
/* TODO(mtwilliams): Free-standing. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
/* _____ ___ _
* | __|___| _|___| |_ _ _
* |__ | .'| _| -_| _| | |
* |_____|__,|_| |___|_| |_ |
* |___|
*/
/* This library will panic if something goes horribly wrong. This will seldom
happen but you should still install a handler as it will allow you to get
some information about what went wrong. */
typedef struct fbx_panic_info {
/* File where the panic occured. */
const char *file;
/* Line in file where the panic occured. */
unsigned line;
} fbx_panic_info_t;
typedef FBX_CALLBACK(void)
fbx_panic_handler_fn(const char *reason,
const fbx_panic_info_t *info);
extern FBX_EXPORT(void)
fbx_get_panic_handler(fbx_panic_handler_fn **handler);
extern FBX_EXPORT(void)
fbx_set_panic_handler(fbx_panic_handler_fn *handler);
/* _____
* | |___ _____ ___ ___ _ _
* | | | | -_| | . | _| | |
* |_|_|_|___|_|_|_|___|_| |_ |
* |___|
*/
typedef struct fbx_allocation_info {
/* File where allocation was requested. */
const char *file;
/* Line in file where allocation was requested. */
unsigned line;
/* Optional tag indicating type or reason for allocation. */
const char *tag;
} fbx_allocation_info_t;
typedef FBX_CALLBACK(void *)
fbx_allocate_callback_fn(fbx_size_t size,
fbx_size_t alignment,
const fbx_allocation_info_t *info);
typedef FBX_CALLBACK(void)
fbx_deallocate_callback_fn(void *ptr);
extern FBX_EXPORT(void)
fbx_get_allocator(fbx_allocate_callback_fn **allocate,
fbx_deallocate_callback_fn **deallocate);
extern FBX_EXPORT(void)
fbx_set_allocator(fbx_allocate_callback_fn *allocate,
fbx_deallocate_callback_fn *deallocate);
/* _____ _ _____
* | | / | |
* |- -|/ /| | |
* |_____|_/ |_____|
*/
/* A stream of bytes. */
typedef struct fbx_stream fbx_stream_t;
/* Wraps a buffer as a stream. */
extern FBX_EXPORT(fbx_stream_t *)
fbx_stream_open_from_memory(void *buffer,
fbx_size_t size,
const char *flags);
#ifndef FBX_NO_STANDARD_LIBRARY
/* Wraps file in a stream. */
extern FBX_EXPORT(fbx_stream_t *)
fbx_stream_open_from_file(FILE *file);
/* Opens a file at a given path and wraps it in a stream. */
extern FBX_EXPORT(fbx_stream_t *)
fbx_stream_open_from_path(const char *path,
const char *flags);
#endif
/* Closes a stream. */
extern FBX_EXPORT(void)
fbx_stream_close(fbx_stream_t *stream);
/* Returns absolute position in a stream. */
extern FBX_EXPORT(fbx_size_t)
fbx_stream_tell(fbx_stream_t *stream);
/* Moves to an absolute offset in a stream. Returns `FBX_TRUE` if seek was
succesful or `FBX_FALSE` if not. */
extern FBX_EXPORT(fbx_bool_t)
fbx_stream_seek(fbx_stream_t *stream,
fbx_size_t offset);
/* Skips through a stream. Returns `FBX_TRUE` if skip was successful, or
`FBX_FALSE` otherwise if not. */
extern FBX_EXPORT(fbx_bool_t)
fbx_stream_skip(fbx_stream_t *stream,
fbx_size_t amount);
#ifdef FBX_NO_IMPORT
/* Reads up to some number of bytes into a buffer. Returns the number of
bytes actually read. May be less than requested if the stream is
exhausted prior to completion. */
extern FBX_EXPORT(fbx_size_t)
fbx_stream_read(fbx_stream_t *stream,
void *buffer,
fbx_size_t count);
#endif
#ifdef FBX_NO_EXPORT
/* Writes up to some number of bytes from a buffer. Returns the number of
bytes actually written. May be less than requested if the stream is
filled prior to completion. */
extern FBX_EXPORT(fbx_size_t)
fbx_stream_write(fbx_stream_t *stream,
const void *buffer,
fbx_size_t count);
#endif
/* Returns `FBX_TRUE` if a stream is exhausted, or `FBX_FALSE` if not. */
extern FBX_EXPORT(fbx_bool_t)
fbx_stream_exhausted(fbx_stream_t *stream);
/* _____ _ _
* | |___| |_| |_
* | | | | .'| _| |
* |_|_|_|__,|_| |_|_|
*/
/* Ratio of a circle's circumference to its diameter. */
#define FBX_PI 3.14159265358979323846264338327950288f
/* Twice as good as Pi */
#define FBX_TAU (2.f * FBX_PI)
/* Converts degrees to radians. */
static fbx_real32_t fbx_degrees_to_radians(const fbx_real32_t degrees) {
return degrees * (180.f / FBX_PI);
}
/* Converts radians to degrees. */
static fbx_real32_t fbx_radians_to_degrees(const fbx_real32_t radians) {
return radians * (FBX_PI / 180.f);
}
/* Represents a point or a direction in two-dimensional space. */
typedef struct fbx_vec2 { fbx_real32_t x, y; } fbx_vec2_t;
/* Represents a point or a direction in three-dimensional space. */
typedef struct fbx_vec3 { fbx_real32_t x, y, z; } fbx_vec3_t;
/* Represents a point or a direction in four-dimensional space. */
typedef struct fbx_vec4 { fbx_real32_t x, y, z, w; } fbx_vec4_t;
/* Represents a direction (and optionally, a magnitude). */
typedef struct fbx_quaternion {
fbx_real32_t x, y, z, w;
} fbx_quaternion_t;
/* Identity quaternion. */
extern const fbx_quaternion_t FBX_IDENTITY_QUATERNION;
/* Represents a linear transformation of three-dimensional space.
*
* Note that we store matrices in column major order. This means columns are
* laid our contiguously in memory. We also uses columns vectors when
* multiplying matrices and vectors, meaning transformations are applied
* right-to-left.
*/
typedef struct fbx_mat4 {
fbx_real32_t v[4][4];
} fbx_mat4_t;
/* Zero matrix. */
extern const fbx_mat4_t FBX_ZERO_MATRIX;
/* Identity matrix. */
extern const fbx_mat4_t FBX_IDENTITY_MATRIX;
/* _____ _
* | |___| |___ ___
* | --| . | | . | _|
* |_____|___|_|___|_|
*/
/* TODO(mtwilliams): HSL, HSV, and YUV? */
/* Represents a color. */
typedef struct fbx_color {
fbx_real32_t r, g, b, a;
} fbx_color_t;
static fbx_color_t fbx_rgb(fbx_uint8_t r, fbx_uint8_t g, fbx_uint8_t b) {
return { r * (1.f/255.f), g * (1.f/255.f), b * (1.f/255.f), 1.f };
}
static fbx_color_t fbx_rgba(fbx_uint8_t r, fbx_uint8_t g, fbx_uint8_t b, fbx_uint8_t a) {
return { r * (1.f/255.f), g * (1.f/255.f), b * (1.f/255.f), a * (1.f/255.f) };
}
static fbx_color_t fbx_rgbf(fbx_real32_t r, fbx_real32_t g, fbx_real32_t b) {
return { r, g, b, 1.f };
}
static fbx_color_t fbx_rgbaf(fbx_real32_t r, fbx_real32_t g, fbx_real32_t b, fbx_real32_t a) {
return { r, g, b, a };
}
/* ____ _
* | \ ___| |_ ___
* | | | .'| _| .'|
* |____/|__,|_| |__,|
*/
/* Bindings? */
/* Custom properties? */
typedef enum fbx_object_type {
FBX_UNKNOWN = 0,
FBX_EMPTY = 1,
FBX_MODEL = 2,
FBX_MESH = 3
} fbx_object_type_t;
typedef struct fbx_object {
fbx_uint64_t id;
const char *name;
fbx_object_type_t type;
void *reified;
struct fbx_object *parent;
struct fbx_object **children;
fbx_uint32_t num_of_children;
} fbx_object_t;
typedef struct fbx_material fbx_material_t;
typedef struct fbx_texture fbx_texture_t;
typedef struct fbx_transform fbx_transform_t;
/* TODO(mtwilliams): Deal with geometric transformations to support 3DS Max. */
typedef struct fbx_transform {
/* Position relative to parent. */
fbx_vec3_t position;
/* Rotation relative to parent. */
fbx_quaternion_t rotation;
/* Center of rotation. */
fbx_vec3_t center_of_rotation;
/* Point rotated around. */
fbx_vec3_t point_of_rotation;
/* Scale relative to parent. */
fbx_vec3_t scale;
/* Center of scaling. */
fbx_vec3_t center_of_scaling;
/* Point scaled around. */
fbx_vec3_t point_of_scaling;
} fbx_transform_t;
/* Scaled, then rotated, then translated. */
typedef struct fbx_model {
fbx_transform_t transform;
} fbx_model_t;
typedef enum fbx_topology {
FBX_POINT = 1,
FBX_TRIANGLE = 3,
FBX_QUADS = 4
} fbx_topology_t;
typedef struct fbx_mesh {
/* TODO(mtwilliams): Provide vertex format. */
void * const* streams;
/* De facto... */
const void *positions;
const fbx_uint32_t *indices;
/* Number of vertices composing geometry. */
fbx_uint32_t num_of_vertices;
/* Number of edges in geometry. */
fbx_uint32_t num_of_edges;
/* Number of triangles composing geometry. */
fbx_uint32_t num_of_faces;
} fbx_mesh_t;
typedef struct fbx_camera fbx_camera_t;
typedef struct fbx_light fbx_light_t;
typedef struct fbx_bone fbx_bone_t;
typedef struct fbx_skeleton fbx_skeleton_t;
typedef struct fbx_pose fbx_pose_t;
typedef struct fbx_animation fbx_animation_t;
typedef struct fbx_metadata {
const char *title;
const char *subject;
const char *author;
const char *comment;
const char *keywords;
const char *version;
const char *revision;
} fbx_metadata_t;
typedef struct fbx_tool_info {
const char *vendor;
const char *name;
const char *version;
} fbx_tool_info_t;
typedef struct fbx_exporter_info {
const char *name;
} fbx_exporter_info_t;
typedef struct fbx_timestamp {
fbx_uint32_t year;
fbx_uint32_t month;
fbx_uint32_t day;
fbx_uint32_t hour;
fbx_uint32_t minute;
fbx_uint32_t second;
fbx_uint32_t millisecond;
} fbx_timestamp_t;
typedef struct fbx_basis {
fbx_vec3_t up;
fbx_vec3_t forward;
fbx_vec3_t right;
} fbx_basis_t;
/* TODO(mtwilliams): Units per meter? */
typedef struct fbx_scene {
const fbx_object_t *root;
fbx_object_t * const *objects;
fbx_uint32_t num_of_objects;
} fbx_scene_t;
typedef struct fbx {
fbx_uint32_t version;
fbx_metadata_t metadata;
fbx_tool_info_t tool;
fbx_exporter_info_t exporter;
fbx_timestamp_t timestamp;
fbx_vec3_t origin;
fbx_basis_t basis;
fbx_scene_t scene;
} fbx_t;
/* _____ _
* | |_____ ___ ___ ___| |_
* |- -| | . | . | _| _|
* |_____|_|_|_| _|___|_| |_|
* |_|
*/
/* TODO(mtwilliams): Coordinate spaces. */
/* TODO(mtwilliams): Normal smoothing. */
/* TODO(mtwilliams): Triangulation. */
typedef struct fbx_import_options {
/* The amount of memory to dedicate to permanent allocations. The permanent
pool is only freed when the imported scene is freed. */
fbx_size_t permanent_memory_pool;
/* The amount of memory to dedicate to transient allocations. The transient
pool is freed after importing. */
fbx_size_t transient_memory_pool;
/* The amount of memory to dedicate to string allocations. The string pool
is only freed when the imported document is freed. */
fbx_size_t strings_memory_pool;
} fbx_import_options_t;
typedef struct fbx_importer fbx_importer_t;
/* _____ _
* | __|_ _ ___ ___ ___| |_
* | __|_'_| . | . | _| _|
* |_____|_,_| _|___|_| |_|
* |_|
*/
/* TODO(mtwilliams): Export functionality. */
FBX_END_EXTERN_C
#endif
#ifdef FBX_IMPLEMENTATION
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#if FBX_ON_WINDOWS || FBX_ON_POSIX
#include <memory.h>
#endif
/* We rely on setjmp and longjmp as it drastically reduces the complexity of
error handling. This, unfortunately, ties us to the standard library.
Theoretically you can provide your own implementation, but doing so will
likely be hair pulling. This shouldn't affect you too much because
interchange formats should be well away from your runtime. */
#include <setjmp.h>
/* Default to safer inflation and deflation that checks for errors. Error
handling can affect performance but is better than crashing! */
#ifndef FBX__ZLIB_SAFE
#define FBX__ZLIB_SAFE 1
#endif
/* _____ _ _ _ _ _
* | | | |_|_| |_| |_ _ _
* | | | _| | | | _| | |
* |_____|_| |_|_|_|_| |_ |
* |___|
*/
#define FBX_MIN(a, b) \
(((a) < (b)) ? (a) : (b))
#define FBX_MAX(a, b) \
(((a) > (b)) ? (a) : (b))
#define fbx_copy(source, destination, size_in_bytes) \
memcpy((void *)(destination), (void *)(source), size_in_bytes)
#define fbx_copy_s(source, destination, size_in_bytes) \
memmove((void *)(destination), (void *)(source), size_in_bytes)
#define fbx_zero(ptr, size_in_bytes) \
memset((void *)(ptr), 0, size_in_bytes)
FBX_BEGIN_EXTERN_C
/* _____ ___ _
* | __|___| _|___| |_ _ _
* |__ | .'| _| -_| _| | |
* |_____|__,|_| |___|_| |_ |
* |___|
*/
static void fbx__default_panic_handler(const char *reason,
const fbx_panic_info_t *info)
{
fputs(reason, stderr);
abort();
}
static fbx_panic_handler_fn *fbx__panic_handler = &fbx__default_panic_handler;
void fbx_get_panic_handler(fbx_panic_handler_fn **handler)
{
*handler = fbx__panic_handler;
}
void fbx_set_panic_handler(fbx_panic_handler_fn *handler)
{
fbx__panic_handler = handler;
}
#define FBX_PANIC(Format, ...) \
fbx_panic({ __FILE__, __LINE__ }, Format, ##__VA_ARGS__)
void fbx_panic(const fbx_panic_info_t info, const char *format, ...)
{
char reason[4096 + 1];
va_list ap;
va_start(ap, format);
vsprintf(reason, format, ap);
va_end(ap);
fbx__panic_handler(reason, &info);
}
/* _____
* | |___ _____ ___ ___ _ _
* | | | | -_| | . | _| | |
* |_|_|_|___|_|_|_|___|_| |_ |
* |___|
*/
static void *fbx__default_allocate_callback(fbx_size_t size,
fbx_size_t alignment,
const fbx_allocation_info_t *info)
{
(void)info;
#if FBX_ON_MSVC
return _aligned_malloc(size, alignment);
#else
#if FBX_ON_POSIX
#endif
#endif
}
static void fbx__default_deallocate_callback(void *ptr)
{
#if FBX_ON_MSVC
_aligned_free(ptr);
#else
#if FBX_ON_POSIX
#endif
#endif
}
static fbx_allocate_callback_fn *fbx__allocate_callback = &fbx__default_allocate_callback;
static fbx_deallocate_callback_fn *fbx__deallocate_callback = &fbx__default_deallocate_callback;
void fbx_get_allocator(fbx_allocate_callback_fn **allocate,
fbx_deallocate_callback_fn **deallocate)
{
*allocate = fbx__allocate_callback;
*deallocate = fbx__deallocate_callback;
}
void fbx_set_allocator(fbx_allocate_callback_fn *allocate,
fbx_deallocate_callback_fn *deallocate)
{
fbx__allocate_callback = allocate;
fbx__deallocate_callback = deallocate;
}
#define FBX_ALLOCATE(Size, Alignment) \
fbx_allocate(Size, Alignment, { __FILE__, __LINE__, NULL })
#define FBX_ALLOCATE_S(Size, Alignment) \
fbx_allocate_s(Size, Alignment, { __FILE__, __LINE__, NULL })
#define FBX_ALLOCATE_TAGGED(Tag, Size, Alignment) \
fbx_allocate(Size, Alignment, { __FILE__, __LINE__, Tag })
#define FBX_ALLOCATE_TAGGED_S(Tag, Size, Alignment) \
fbx_allocate_s(Size, Alignment, { __FILE__, __LINE__, Tag })
void *fbx_allocate(fbx_size_t size,
fbx_size_t alignment,
const fbx_allocation_info_t info)
{
void *ptr;
if (alignment <= 16)
alignment = 16;
ptr = fbx__allocate_callback(size, alignment, &info);
if (ptr)
/* We always zero memory, as it prevents an entire class of errors. */
fbx_zero(ptr, size);
return ptr;
}
void *fbx_allocate_s(fbx_size_t size,
fbx_size_t alignment,
const fbx_allocation_info_t info)
{
void *ptr;
ptr = fbx_allocate(size, alignment, info);
if (ptr == NULL)
/* We can get `NULL` if out of memory. */
FBX_PANIC("Out of memory!\n");
return ptr;
}
void fbx_free(void *ptr)
{
fbx__deallocate_callback(ptr);
}
/* TODO(mtwilliams): Bookmarking. Maintain a linked list of positions that can
be rewound to. If rewinding to bookmark prior to the last bookmark, remove
it from the list to let rewinds cascade. */
/* A simple bump allocator. */
typedef struct fbx_block {
fbx_uintptr_t base;
fbx_size_t offset;
fbx_size_t size;
} fbx_block_t;
void *fbx_block_allocate(fbx_block_t *block,
fbx_size_t size)
{
void *ptr;
/* Allocations are rounded up to the closest multiple of 16, thereby ensuring
16-byte alignment of every allocation (assuming base is aligned.) */
size = (((size + 15) / 16) * 16);
if ((block->offset + size) > block->size)
/* Not enough space. */
return NULL;
ptr = (void *)(block->base + block->offset);
block->offset += size;
return ptr;
}
void *fbx_block_allocate_s(fbx_block_t *block,
fbx_size_t size)
{
void *ptr;
ptr = fbx_block_allocate(block, size);
if (ptr == NULL)
/* We can get `NULL` if out of memory. */
FBX_PANIC("Out of memory!\n");
return ptr;
}
void fbx_block_reset(fbx_block_t *block)
{
block->offset = 0;
/* Rezero. Again, to prevent an entire class of errors. */
fbx_zero(block->base, block->size);
}
/* _____ _ _____
* | | / | |
* |- -|/ /| | |
* |_____|_/ |_____|
*/
/* TODO(mtwilliams): Support large files. */
typedef fbx_size_t (fbx_stream_read_callback_fn)(fbx_stream_t *stream,
void *buffer,
fbx_size_t count);
typedef fbx_size_t (fbx_stream_write_callback_fn)(fbx_stream_t *stream,
const void *buffer,
fbx_size_t count);
typedef fbx_size_t (fbx_stream_tell_callback_fn)(fbx_stream_t *stream);
typedef fbx_bool_t (fbx_stream_seek_callback_fn)(fbx_stream_t *stream,
fbx_size_t offset);
typedef fbx_bool_t (fbx_stream_skip_callback_fn)(fbx_stream_t *stream,
fbx_size_t amount);
typedef fbx_bool_t (fbx_stream_exhausted_callback_fn)(fbx_stream_t *stream);
typedef void (fbx_stream_close_callback_fn)(fbx_stream_t *stream);
typedef struct fbx_stream_dispatch {
fbx_stream_read_callback_fn *read;
fbx_stream_write_callback_fn *write;
fbx_stream_tell_callback_fn *tell;
fbx_stream_seek_callback_fn *seek;
fbx_stream_skip_callback_fn *skip;
fbx_stream_exhausted_callback_fn *exhausted;
fbx_stream_close_callback_fn *close;
} fbx_stream_dispatch_t;
struct fbx_stream {
const fbx_stream_dispatch_t *dispatch;
};
typedef struct fbx_memory_stream {
fbx_stream_t container;
fbx_uint8_t *buffer;
fbx_size_t size;
fbx_size_t cursor;
fbx_bool_t readable;
fbx_bool_t writeable;
} fbx_memory_stream_t;
extern const fbx_stream_dispatch_t fbx_memory_stream__dispatch;
static fbx_stream_t *fbx_memory_stream_open(void *buffer,
fbx_size_t size,
const char *flags)
{
fbx_memory_stream_t *stream =
(fbx_memory_stream_t *)FBX_ALLOCATE_TAGGED_S("stream", sizeof(fbx_memory_stream_t), 16);
stream->container.dispatch = &fbx_memory_stream__dispatch;
stream->buffer = (fbx_uint8_t *)buffer;
stream->size = size;
stream->cursor = 0;
while (const char flag = *flags++) {
switch (flag) {
case 'r': case 'R': stream->readable = FBX_TRUE; break;
case 'w': case 'W': stream->writeable = FBX_TRUE; break;
}
}
return &stream->container;
}
static void fbx_memory_stream_close(fbx_memory_stream_t *stream)
{
fbx_free((void *)stream);
}
static fbx_size_t fbx_memory_stream_read(fbx_memory_stream_t *stream,
void *buffer,
fbx_size_t count)
{
if (stream->readable) {
const fbx_size_t read = FBX_MIN(stream->cursor + count, stream->size);
fbx_copy(&stream->buffer[stream->cursor], buffer, count);
stream->cursor += read;
return read;
}
return 0;
}
static fbx_size_t fbx_memory_stream_write(fbx_memory_stream_t *stream,
const void *buffer,
fbx_size_t count)
{
if (stream->writeable) {
const fbx_size_t write = FBX_MIN(stream->cursor + count, stream->size);
fbx_copy(buffer, &stream->buffer[stream->cursor], count);
stream->cursor += write;
return write;
}
return 0;
}
static fbx_size_t fbx_memory_stream_tell(fbx_memory_stream_t *stream)
{
return stream->cursor;
}
static fbx_bool_t fbx_memory_stream_seek(fbx_memory_stream_t *stream,
fbx_size_t offset)
{
if (offset > stream->size)
return FBX_FALSE;
stream->cursor = offset;
return FBX_TRUE;
}
static fbx_bool_t fbx_memory_stream_skip(fbx_memory_stream_t *stream,
fbx_size_t amount)
{
if (stream->size - stream->cursor < amount)
return FBX_FALSE;