-
Notifications
You must be signed in to change notification settings - Fork 3
/
voodoo.h
6588 lines (5756 loc) · 233 KB
/
voodoo.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
#ifndef VOODOO_INCLUDED
/*
█ ██ ███ ██████████████████████████████████████▀▀▀▀▀▀▀█████████████████████████
▀ ▀▀ ▀▀▀ ▀▀▀▀▀▀▀ ▄▄▄▄▄▄▄ ▀▀▀▀ ▄▄▄▄▄▄▄ ▀▀▀▀ ▄▄▄▄▄█████ ▀ ▄▄▄▄▄▄▄ ▀▀▀▀ ▄▄▄▄▄▄▄ ▀▀
█████ ██████████ ██████████ █████▐████▌ ██████████ ██████████ █████
█████ ██████████ ██████████ ██████████ ██████████ ██████████ █████
▐████▌ ▐████▌▐████▌ ▐████▌▐████▌ ▐████▌▐████▌ ▐████▌▐████▌ ▐████▌▐████▌ ▐████▌
▀▀███ ███▀▀ ▀▀███ ███▀▀ ▀▀███ ███▀▀ ▀▀███ ███▀▀ ▀▀███ ███▀▀ ▀▀███ ███▀▀
▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀
▄■
▐▌
▀▄▄
▀▄▄ ▄█▀▀▄▄ ▄▄▀
▀▀██▄▄▄▄ ▐██▌ ▀ ▄▄▄▄██▀▀
▀▀▀███████▄██▄███████▀▀▀
▀▀▀██████▀▀▀
▄▄▀ ▐████▌
▀▄ ████
▀▀▀▄▄▄▄▄▄▄
▄▄▄▄▄▄ ▀▀▀▄
▐██▌▐██▌ ▄▄▀
▄▄▐██ ██▌▀▀
▄▀ ██▌ ▐██
▀▀▄▄▄▄▄ ▀▀
▄▄ ▀▀▀▀▀▄▄
▐██ ██▌ ▄▀
▀▀ ▄▀ ▀▀
▐▌
▀▀■
voodoo.h -- 3D game programming library
*/
#define VOODOO_INCLUDED (1)
#include <dirent.h>
#include "janet.h"
extern void jsfun_vd_app_update(void);
static Janet cfun_vd_app_width(int32_t argc, Janet *argv);
static Janet cfun_vd_app_height(int32_t argc, Janet *argv);
static Janet cfun_vd_asset_load(int32_t argc, Janet *argv);
static Janet cfun_vd_cam_orbit(int32_t argc, Janet *argv);
static Janet cfun_vd_cam_handle_event(int32_t argc, Janet *argv);
static Janet cfun_vd_cam_update(int32_t argc, Janet *argv);
static Janet cfun_vd_cam_lookat(int32_t argc, Janet *argv);
static Janet cfun_vd_dbg_draw_camera(int32_t argc, Janet *argv);
static Janet cfun_vd_dbg_draw_cube(int32_t argc, Janet *argv);
static Janet cfun_vd_dbg_draw_grid(int32_t argc, Janet *argv);
static Janet cfun_vd_game_object(int32_t argc, Janet *argv);
static Janet cfun_vd_game_object_get(int32_t argc, Janet *argv);
static Janet cfun_vd_game_object_set(int32_t argc, Janet *argv);
static Janet cfun_vd_gfx_img_get(int32_t argc, Janet *argv);
static Janet cfun_vd_input_bind(int32_t argc, Janet *argv);
static Janet cfun_vd_input_state(int32_t argc, Janet *argv);
static Janet cfun_vd_math_add_v3(int32_t argc, Janet *argv);
static Janet cfun_vd_math_lerp_v3(int32_t argc, Janet *argv);
static Janet cfun_vd_math_atan2(int32_t argc, Janet *argv);
static Janet cfun_vd_math_cross(int32_t argc, Janet *argv);
static Janet cfun_vd_math_is_nan(int32_t argc, Janet *argv);
static Janet cfun_vd_math_mul_v3f(int32_t argc, Janet *argv);
static Janet cfun_vd_math_norm_v3(int32_t argc, Janet *argv);
static Janet cfun_vd_math_mul_q(int32_t argc, Janet *argv);
static Janet cfun_vd_math_axis_angle_q(int32_t argc, Janet *argv);
static Janet cfun_vd_math_qv3(int32_t argc, Janet *argv);
static Janet cfun_vd_math_rotate_v3q(int32_t argc, Janet *argv);
static Janet cfun_vd_math_sub_v3(int32_t argc, Janet *argv);
static Janet cfun_vd_math_to_deg(int32_t argc, Janet *argv);
static Janet cfun_vd_math_to_rad(int32_t argc, Janet *argv);
static Janet cfun_vd_v3d_cube(int32_t argc, Janet *argv);
static Janet cfun_vd_v3d_doll(int32_t argc, Janet *argv);
static Janet cfun_vd_v3d_draw(int32_t argc, Janet *argv);
// static Janet cfun_vd_ecs_component(int32_t argc, Janet *argv);
static const JanetReg vd__cfuns[] = {
{"app/width", cfun_vd_app_width, "(voodoo/app/width)\n\nWidth of the current application."},
{"app/height", cfun_vd_app_height, "(voodoo/app/height)\n\nHeight of the current application."},
{"asset/load", cfun_vd_asset_load, "(voodoo/asset/load)\n\nLoad an asset."},
{"cam/orbit", cfun_vd_cam_orbit, "(voodoo/cam/orbit)\n\nCreate an orbit camera."},
{"cam/handle-event", cfun_vd_cam_handle_event,
"(voodoo/cam/handle-event)\n\nHandle an event with an existing camera."},
{"cam/update", cfun_vd_cam_update, "(voodoo/cam/update)\n\nUpdate an existing camera."},
{"cam/lookat", cfun_vd_cam_lookat, "(voodoo/cam/lookat)\n\nMake camera look at target."},
{"dbg/draw/camera", cfun_vd_dbg_draw_camera,
"(voodoo/dbg/draw/camera)\n\nSet camera matricies for debug draw operations."},
{"dbg/draw/cube", cfun_vd_dbg_draw_cube, "(voodoo/dbg/draw/cube)\n\nDraw a debug cube."},
{"dbg/draw/grid", cfun_vd_dbg_draw_grid, "(voodoo/dbg/draw/grid)\n\nDraw a debug grid."},
{"game/object", cfun_vd_game_object, "(voodoo/game/object)\n\nCreate a new game object."},
{"game/object/get", cfun_vd_game_object_get,
"(voodoo/game/object/get)\n\nGets the value of a component for an existing game object."},
{"game/object/set", cfun_vd_game_object_set,
"(voodoo/game/object/set)\n\nSets the value of a component for an existing game object."},
{"gfx/img/get", cfun_vd_gfx_img_get, "(voodoo/gfx/img/get)\n\nRetrieves image data using asset handle."},
{"input/bind", cfun_vd_input_bind, "(voodoo/input/bind)\n\nBind an action to an input."},
{"input/state", cfun_vd_input_state, "(voodoo/input/state)\n\nGet the state of an input."},
{"math/add-v3", cfun_vd_math_add_v3, "(voodoo/math/add-v3\n\nAdd two 3d vectors together.)"},
{"math/lerp-v3", cfun_vd_math_lerp_v3, "(voodoo/math/lerp-v3\n\nLerp two 3d vectors by time.)"},
{"math/atan2", cfun_vd_math_atan2, "(voodoo/math/atan2\n\nCompute arctangent of two floats.)"},
{"math/cross", cfun_vd_math_cross, "(voodoo/math/cross\n\nCompute cross product of two 3d vectors.)"},
{"math/is-nan", cfun_vd_math_is_nan, "(voodoo/math/is-nan\n\nDetermine if argument is a number.)"},
{"math/mul-v3f", cfun_vd_math_mul_v3f, "(voodoo/math/mul-v3f\n\nMultiply a 3d vector by a float.)"},
{"math/norm-v3", cfun_vd_math_norm_v3, "(voodoo/math/norm-v3\n\nNormalize a 3d vector.)"},
{"math/mul-q", cfun_vd_math_mul_q, "(voodoo/math/mul-q\n\nMultiply two quaternions.)"},
{"math/axis-angle-q", cfun_vd_math_axis_angle_q,
"(voodoo/math/axis-angle-q\n\nCreate a quaternion from an axis and angle amount.)"},
{"math/qv3", cfun_vd_math_qv3, "(voodoo/math/qv3\n\nCreate a quaternion from a pitch, yaw, and roll vector.)"},
{"math/rotate-v3q", cfun_vd_math_rotate_v3q, "(voodoo/math/rotate-v3q\n\nRotate a 3d vector by a quaternion.)"},
{"math/sub-v3", cfun_vd_math_sub_v3, "(voodoo/math/sub-v3\n\nSubtract two 3d vectors from one another.)"},
{"math/to-deg", cfun_vd_math_to_deg, "(voodoo/math/to-deg\n\nConvert radians to degrees.)"},
{"math/to-rad", cfun_vd_math_to_rad, "(voodoo/math/to-rad\n\nConvert degrees to radians.)"},
{"v3d/cube", cfun_vd_v3d_cube, "(voodoo/v3d/cube)\n\nCreate a cube."},
{"v3d/draw", cfun_vd_v3d_draw, "(voodoo/v3d/draw)\n\nDraw a frame."},
{"v3d/doll/create", cfun_vd_v3d_doll, "(voodoo/v3d/doll/create)\n\nCreate a doll."},
// {"ecs/component", cfun_vd_ecs_component, "(voodoo/ecs/component)\n\nCreate a component."},
{NULL, NULL, NULL}};
#endif // VOODOO_INCLUDED
// ______ _______ __ ________ __________ ___________ ______________ _ __
// / _/ |/ / __ \/ / / ____/ |/ / ____/ | / /_ __/ |/_ __/ _/ __ \/ | / /
// / // /|_/ / /_/ / / / __/ / /|_/ / __/ / |/ / / / / /| | / / / // / / / |/ /
// _/ // / / / ____/ /___/ /___/ / / / /___/ /| / / / / ___ |/ / _/ // /_/ / /| /
// /___/_/ /_/_/ /_____/_____/_/ /_/_____/_/ |_/ /_/ /_/ |_/_/ /___/\____/_/ |_/
//
// >>implementation
#define VOODOO_IMPL
#ifdef VOODOO_IMPL
#define VOODOO_IMPL_INCLUDED (1)
#ifdef _MSC_VER
#pragma warning(push)
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include "sx/allocator.h"
#include "sx/array.h"
#include "sx/atomic.h"
#include "sx/handle.h"
#include "sx/hash.h"
#include "sx/io.h"
#include "sx/jobs.h"
#include "sx/lin-alloc.h"
#include "sx/lockless.h"
#include "sx/os.h"
#include "sx/platform.h"
#include "sx/pool.h"
#include "sx/string.h"
#include "sx/threads.h"
#include "sx/vmem.h"
#include "stackwalkerc.h"
#define CJ5_IMPLEMENT
#include "cj5.h"
#define FLECS_CUSTOM_BUILD
#define FLECS_MODULE
#define FLECS_SYSTEM
#define FLECS_META
#define FLECS_META_C
#include "flecs.h"
#define HANDMADE_MATH_IMPLEMENTATION
#include "hmm.h"
#include "ozz_util.h"
#define SOKOL_IMPL
#define SOKOL_WGPU
#include "sokol_app.h"
#include "sokol_args.h"
#include "sokol_fetch.h"
#include "sokol_gfx.h"
#include "sokol_gl.h"
#include "sokol_glue.h"
#include "sokol_log.h"
#include "sokol_shape.h"
#include "sokol_time.h"
// shaders
#include "depth.glsl.h"
#include "display.glsl.h"
#include "doll.glsl.h"
#include "scene.glsl.h"
#include "shadows.glsl.h"
#include "shadows_doll.glsl.h"
#include "shapes.glsl.h"
#define vd__to_id(index) ((uint32_t)(index) + 1)
#define vd__to_index(id) ((int)(id) - 1)
static const sx_alloc *vd__core_heap_alloc(void);
int64_t vd__core_frame_index();
int vd__app_width();
int vd__app_height();
// _________ _ _______________
// / ___/ __ \/ |/ / __/ _/ ___/
// / /__/ /_/ / / _/_/ // (_ /
// \___/\____/_/|_/_/ /___/\___/
//
// >>config
#ifndef VD__MAX_PATH
#define VD__MAX_PATH 256
#endif
#ifndef VD__CONFIG_ASSET_POOL_SIZE
#define VD__CONFIG_ASSET_POOL_SIZE 256
#endif
#ifndef VD__CONFIG_SHADOW_MAP_SIZE
#define VD__CONFIG_SHADOW_MAP_SIZE 4096
#endif
#define VD__INPUT_ACTION_COMMAND 31
#define VD__INPUT_ACTION_MAX 32
#define VD__INPUT_DEADZONE 0.1
#define VD__INPUT_DEADZONE_CAPTURE 0.5
#define VD__INPUT_ACTION_NONE 255
#define VD__INPUT_BUTTON_NONE 0
// __ ____ _______________ _______
// / / / __ \/ ___/ ___/ _/ |/ / ___/
// / /__/ /_/ / (_ / (_ // // / (_ /
// /____/\____/\___/\___/___/_/|_/\___/
//
// >>logging
typedef enum
{
VD__LOG_LEVEL_ERROR = 0,
VD__LOG_LEVEL_WARNING,
VD__LOG_LEVEL_INFO,
VD__LOG_LEVEL_VERBOSE,
VD__LOG_LEVEL_DEBUG,
VD__LOG_LEVEL_COUNT
} vd__log_level;
void (*vd__core_print_info)(uint32_t channels, const char *source_file, int line, const char *fmt, ...);
void (*vd__core_print_debug)(uint32_t channels, const char *source_file, int line, const char *fmt, ...);
void (*vd__core_print_verbose)(uint32_t channels, const char *source_file, int line, const char *fmt, ...);
void (*vd__core_print_error)(uint32_t channels, const char *source_file, int line, const char *fmt, ...);
void (*vd__core_print_warning)(uint32_t channels, const char *source_file, int line, const char *fmt, ...);
void (*vd__core_set_log_level)(vd__log_level level);
#define vd__log_info(_text, ...) vd__core_print_info(0, __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_debug(_text, ...) vd__core_print_debug(0, __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_verbose(_text, ...) vd__core_print_verbose(0, __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_error(_text, ...) vd__core_print_error(0, __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_warn(_text, ...) vd__core_print_warning(0, __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_info_channels(_channels, _text, ...) \
vd__core_print_info((_channels), __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_debug_channels(_channels, _text, ...) \
vd__core_print_debug((_channels), __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_verbose_channels(_channels, _text, ...) \
vd__core_print_verbose((_channels), __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_error_channels(_channels, _text, ...) \
vd__core_print_error((_channels), __FILE__, __LINE__, _text, ##__VA_ARGS__)
#define vd__log_warn_channels(_channels, _text, ...) \
vd__core_print_warning((_channels), __FILE__, __LINE__, _text, ##__VA_ARGS__)
// _____________ __ _________________
// / __/_ __/ _ \/ / / / ___/_ __/ __/
// _\ \ / / / , _/ /_/ / /__ / / _\ \
// /___/ /_/ /_/|_|\____/\___/ /_/ /___/
//
// >>structs
typedef union {
uintptr_t id;
void *ptr;
} vd__asset_obj;
typedef struct
{
uint32_t id;
} vd__asset_handle;
typedef struct
{
uint32_t id;
} vd__asset_group_handle;
enum
{
VD__ASSET_LOAD_FLAG_NONE = 0x0,
VD__ASSET_LOAD_FLAG_ABSOLUTE_PATH = 0x1,
VD__ASSET_LOAD_FLAG_WAIT_ON_LOAD = 0x2,
VD__ASSET_LOAD_FLAG_RELOAD = 0x4,
VD__ASSET_LOAD_FLAG_TEXT_FILE = 0x8
};
typedef uint32_t vd__asset_load_flags;
typedef struct
{
char key[32];
char value[32];
} vd__asset_meta_keyval;
typedef struct
{
const char *path; // path to asset file
const void *params; // must cast to asset-specific implementation type
const sx_alloc *alloc; // allocator that is user sends for loading asset data
uint32_t tags; // user-defined tag bits
vd__asset_load_flags flags; // flags that are used for loading
uint32_t num_meta; // meta key-value pairs, embedded in custom _vd__ assets
const vd__asset_meta_keyval *metas;
} vd__asset_load_params;
typedef struct
{
vd__asset_obj obj; // valid internal object
void *user1; // user-data can be allocated and filled with anything specific to asset loader
void *user2; // same as user1
} vd__asset_load_data;
typedef enum
{
VD__ASSET_STATE_ZOMBIE = 0,
VD__ASSET_STATE_OK,
VD__ASSET_STATE_FAILED,
VD__ASSET_STATE_LOADING
} vd__asset_state;
typedef struct
{
vd__asset_load_data (*on_prepare)(const vd__asset_load_params *params, const sx_mem_block *mem);
bool (*on_load)(vd__asset_load_data *data, const vd__asset_load_params *params, const sx_mem_block *mem);
void (*on_finalize)(vd__asset_load_data *data, const vd__asset_load_params *params, const sx_mem_block *mem);
void (*on_reload)(vd__asset_handle handle, vd__asset_obj prev_obj, const sx_alloc *alloc);
void (*on_release)(vd__asset_obj obj, const sx_alloc *alloc);
} vd__asset_callbacks;
// fourcc code for embedded asset meta data
static uint32_t VD__ASSET_FLAG = sx_makefourcc('R', 'I', 'Z', 'Z');
// Asset managers are managers for each type of asset
// For example, 'texture' has it's own manager, 'model' has it's manager, ...
// They handle loading, unloading, reloading asset objects
// They also have metdata and parameters memory containers
typedef struct vd__asset_mgr
{
char name[32];
vd__asset_callbacks callbacks;
uint32_t name_hash;
int params_size;
char params_type_name[32];
vd__asset_obj failed_obj;
vd__asset_obj async_obj;
uint8_t *SX_ARRAY params_buff; // (byte-array, item-size: params_size)
vd__asset_load_flags forced_flags; // these flags are foced upon every load-call
bool unreg;
} vd__asset_mgr;
// Assets consist of files (resource) on disk and load params combination
// One file may be loaded with different parameters (different allocator, mip-map, LOD, etc.)
// Each asset is binded to a resource and params data
typedef struct
{
const sx_alloc *alloc;
sx_handle_t handle;
uint32_t params_id; // id-to: vd__asset_mgr.params_buff
uint32_t resource_id; // id-to: vd__asset_lib.resources
int asset_mgr_id; // index-to: vd__asset_lib.asset_mgrs
int ref_count;
vd__asset_obj obj;
vd__asset_obj dead_obj;
uint32_t hash;
uint32_t tags;
vd__asset_load_flags load_flags;
vd__asset_state state;
} vd__asset;
// Resources are the actual files on the file-system
// Each resource has a metadata
// Likely to be populated by asset-db, but can grow on run-time
typedef struct
{
char path[VD__MAX_PATH]; // path that is referenced in database and code
char real_path[VD__MAX_PATH]; // real path on disk, resolved by asset-db and variation
uint32_t path_hash; // hash of 'real_path'
uint64_t last_modified; // last-modified time-stamp
int asset_mgr_id; // index-to: vd__asset_lib.asset_mgrs
bool used;
} vd__asset_resource;
// Async loads are queued for each new async file loads
// To track which file points to which asset
typedef struct
{
uint32_t path_hash; // hash of real_path
vd__asset_handle asset;
} vd__asset_async_load_req;
typedef enum
{
VD__ASSET_JOB_STATE_SPAWN = 0,
VD__ASSET_JOB_STATE_LOAD_FAILED,
VD__ASSET_JOB_STATE_SUCCESS,
VD__ASSET_JOB_STATE_COUNT = INT32_MAX
} vd__asset_job_state;
typedef struct vd__asset_async_job
{
vd__asset_load_data load_data;
sx_mem_block *mem;
vd__asset_mgr *amgr;
vd__asset_load_params lparams;
sx_job_t job;
vd__asset_job_state state;
vd__asset_handle asset;
struct vd__asset_async_job *next;
struct vd__asset_async_job *prev;
} vd__asset_async_job;
typedef struct vd__asset_group
{
vd__asset_handle *SX_ARRAY assets;
} vd__asset_group;
typedef struct
{
float min_dist;
float max_dist;
float min_lat;
float max_lat;
float distance;
float latitude;
float longitude;
float aspect;
float nearz;
float farz;
HMM_Vec3 center;
} vd__camera_desc;
/*typedef struct
{
float min_dist;
float max_dist;
float min_lat;
float max_lat;
float distance;
float latitude;
float longitude;
float aspect;
float nearz;
float farz;
HMM_Vec3 center;
HMM_Vec3 eye_pos;
HMM_Vec3 eye_dir;
HMM_Mat4 view;
HMM_Mat4 proj;
HMM_Mat4 view_proj;
} vd__camera;*/
enum
{
VD__CORE_FLAG_LOG_TO_FILE = 0x01, // log to file defined by `app_name.log`
VD__CORE_FLAG_LOG_TO_PROFILER = 0x02, // log to remote profiler
VD__CORE_FLAG_PROFILE_GPU = 0x04, // enable GPU profiling
VD__CORE_FLAG_DUMP_UNUSED_ASSETS = 0x08, // write `unused-assets.json` on exit
VD__CORE_FLAG_DETECT_LEAKS = 0x10, // Detect memory leaks (default on in _DEBUG builds)
VD__CORE_FLAG_HEAP_TEMP_ALLOCATOR =
0x20, // Replace temp allocator backends with heap, so we can better trace out-of-bounds and corruption
VD__CORE_FLAG_HOT_RELOAD_PLUGINS =
0x40, // Enables hot reloading for all modules and plugins including the game itself
VD__CORE_FLAG_TRACE_TEMP_ALLOCATOR =
0x80 // Enable memory tracing on temp allocators, slows them down, but provides more insight on temp allocations
};
typedef uint32_t vd__core_flags;
typedef struct
{
vd__core_flags core_flags;
vd__log_level log_level; // default = VD__LOG_LEVEL_INFO
int job_num_threads;
int job_max_fibers;
int job_stack_size;
int tmp_mem_max;
} vd__config;
typedef enum
{
VD__BOX = 0,
VD__PLANE,
VD__SPHERE,
VD__CYLINDER,
VD__TORUS,
VD__NUM_SHAPES
} vd__dbg_shapes;
typedef struct
{
HMM_Vec3 pos;
sshape_element_range_t draw;
} vd__dbg_shape;
typedef struct
{
union {
int x, w;
};
union {
int y, h;
};
union {
int n, comps;
};
union {
void *pixels;
uint8_t *pixels8;
uint16_t *pixels16;
uint32_t *pixels32;
float *pixelsf;
};
} vd__image;
typedef struct
{
uint32_t name_hdl; // get name with the_core->str_cstr();
sg_image_type type;
sg_pixel_format format;
int mem_size_bytes;
int width;
int height;
union {
int depth;
int layers;
};
int mips;
int bpp;
} vd__texture_info;
typedef struct
{
sg_image img;
sg_sampler smp;
vd__texture_info info;
} vd__texture;
typedef enum
{
VD__INPUT_INVALID = 0,
VD__INPUT_KEY_A = 4,
VD__INPUT_KEY_B = 5,
VD__INPUT_KEY_C = 6,
VD__INPUT_KEY_D = 7,
VD__INPUT_KEY_E = 8,
VD__INPUT_KEY_F = 9,
VD__INPUT_KEY_G = 10,
VD__INPUT_KEY_H = 11,
VD__INPUT_KEY_I = 12,
VD__INPUT_KEY_J = 13,
VD__INPUT_KEY_K = 14,
VD__INPUT_KEY_L = 15,
VD__INPUT_KEY_M = 16,
VD__INPUT_KEY_N = 17,
VD__INPUT_KEY_O = 18,
VD__INPUT_KEY_P = 19,
VD__INPUT_KEY_Q = 20,
VD__INPUT_KEY_R = 21,
VD__INPUT_KEY_S = 22,
VD__INPUT_KEY_T = 23,
VD__INPUT_KEY_U = 24,
VD__INPUT_KEY_V = 25,
VD__INPUT_KEY_W = 26,
VD__INPUT_KEY_X = 27,
VD__INPUT_KEY_Y = 28,
VD__INPUT_KEY_Z = 29,
VD__INPUT_KEY_1 = 30,
VD__INPUT_KEY_2 = 31,
VD__INPUT_KEY_3 = 32,
VD__INPUT_KEY_4 = 33,
VD__INPUT_KEY_5 = 34,
VD__INPUT_KEY_6 = 35,
VD__INPUT_KEY_7 = 36,
VD__INPUT_KEY_8 = 37,
VD__INPUT_KEY_9 = 38,
VD__INPUT_KEY_0 = 39,
VD__INPUT_KEY_RETURN = 40,
VD__INPUT_KEY_ESCAPE = 41,
VD__INPUT_KEY_BACKSPACE = 42,
VD__INPUT_KEY_TAB = 43,
VD__INPUT_KEY_SPACE = 44,
VD__INPUT_KEY_MINUS = 45,
VD__INPUT_KEY_EQUALS = 46,
VD__INPUT_KEY_LEFTBRACKET = 47,
VD__INPUT_KEY_RIGHTBRACKET = 48,
VD__INPUT_KEY_BACKSLASH = 49,
VD__INPUT_KEY_HASH = 50,
VD__INPUT_KEY_SEMICOLON = 51,
VD__INPUT_KEY_APOSTROPHE = 52,
VD__INPUT_KEY_TILDE = 53,
VD__INPUT_KEY_COMMA = 54,
VD__INPUT_KEY_PERIOD = 55,
VD__INPUT_KEY_SLASH = 56,
VD__INPUT_KEY_CAPSLOCK = 57,
VD__INPUT_KEY_F1 = 58,
VD__INPUT_KEY_F2 = 59,
VD__INPUT_KEY_F3 = 60,
VD__INPUT_KEY_F4 = 61,
VD__INPUT_KEY_F5 = 62,
VD__INPUT_KEY_F6 = 63,
VD__INPUT_KEY_F7 = 64,
VD__INPUT_KEY_F8 = 65,
VD__INPUT_KEY_F9 = 66,
VD__INPUT_KEY_F10 = 67,
VD__INPUT_KEY_F11 = 68,
VD__INPUT_KEY_F12 = 69,
VD__INPUT_KEY_PRINTSCREEN = 70,
VD__INPUT_KEY_SCROLLLOCK = 71,
VD__INPUT_KEY_PAUSE = 72,
VD__INPUT_KEY_INSERT = 73,
VD__INPUT_KEY_HOME = 74,
VD__INPUT_KEY_PAGEUP = 75,
VD__INPUT_KEY_DELETE = 76,
VD__INPUT_KEY_END = 77,
VD__INPUT_KEY_PAGEDOWN = 78,
VD__INPUT_KEY_RIGHT = 79,
VD__INPUT_KEY_LEFT = 80,
VD__INPUT_KEY_DOWN = 81,
VD__INPUT_KEY_UP = 82,
VD__INPUT_KEY_NUMLOCK = 83,
VD__INPUT_KEY_KP_DIVIDE = 84,
VD__INPUT_KEY_KP_MULTIPLY = 85,
VD__INPUT_KEY_KP_MINUS = 86,
VD__INPUT_KEY_KP_PLUS = 87,
VD__INPUT_KEY_KP_ENTER = 88,
VD__INPUT_KEY_KP_1 = 89,
VD__INPUT_KEY_KP_2 = 90,
VD__INPUT_KEY_KP_3 = 91,
VD__INPUT_KEY_KP_4 = 92,
VD__INPUT_KEY_KP_5 = 93,
VD__INPUT_KEY_KP_6 = 94,
VD__INPUT_KEY_KP_7 = 95,
VD__INPUT_KEY_KP_8 = 96,
VD__INPUT_KEY_KP_9 = 97,
VD__INPUT_KEY_KP_0 = 98,
VD__INPUT_KEY_KP_PERIOD = 99,
VD__INPUT_KEY_LCTRL = 100,
VD__INPUT_KEY_LSHIFT = 101,
VD__INPUT_KEY_LALT = 102,
VD__INPUT_KEY_LGUI = 103,
VD__INPUT_KEY_RCTRL = 104,
VD__INPUT_KEY_RSHIFT = 105,
VD__INPUT_KEY_RALT = 106,
VD__INPUT_KEY_MAX = 107,
VD__INPUT_GAMEPAD_A = 108,
VD__INPUT_GAMEPAD_Y = 109,
VD__INPUT_GAMEPAD_B = 110,
VD__INPUT_GAMEPAD_X = 111,
VD__INPUT_GAMEPAD_L_SHOULDER = 112,
VD__INPUT_GAMEPAD_R_SHOULDER = 113,
VD__INPUT_GAMEPAD_L_TRIGGER = 114,
VD__INPUT_GAMEPAD_R_TRIGGER = 115,
VD__INPUT_GAMEPAD_SELECT = 116,
VD__INPUT_GAMEPAD_START = 117,
VD__INPUT_GAMEPAD_L_STICK_PRESS = 118,
VD__INPUT_GAMEPAD_R_STICK_PRESS = 119,
VD__INPUT_GAMEPAD_DPAD_UP = 120,
VD__INPUT_GAMEPAD_DPAD_DOWN = 121,
VD__INPUT_GAMEPAD_DPAD_LEFT = 122,
VD__INPUT_GAMEPAD_DPAD_RIGHT = 123,
VD__INPUT_GAMEPAD_HOME = 124,
VD__INPUT_GAMEPAD_L_STICK_UP = 125,
VD__INPUT_GAMEPAD_L_STICK_DOWN = 126,
VD__INPUT_GAMEPAD_L_STICK_LEFT = 127,
VD__INPUT_GAMEPAD_L_STICK_RIGHT = 128,
VD__INPUT_GAMEPAD_R_STICK_UP = 129,
VD__INPUT_GAMEPAD_R_STICK_DOWN = 130,
VD__INPUT_GAMEPAD_R_STICK_LEFT = 131,
VD__INPUT_GAMEPAD_R_STICK_RIGHT = 132,
VD__INPUT_MOUSE_LEFT = 134,
VD__INPUT_MOUSE_MIDDLE = 135,
VD__INPUT_MOUSE_RIGHT = 136,
VD__INPUT_MOUSE_WHEEL_UP = 137,
VD__INPUT_MOUSE_WHEEL_DOWN = 138,
VD__INPUT_BUTTON_MAX = 139
} vd__button;
typedef enum
{
VD__INPUT_LAYER_SYSTEM = 0,
VD__INPUT_LAYER_USER = 1,
VD__INPUT_LAYER_MAX = 2
} vd__input_layer;
typedef void (*vd__input_capture_callback)(void *user, vd__button button, int32_t ascii_char);
typedef struct vd__tmp_alloc vd__tmp_alloc;
typedef struct
{
sx_alloc alloc;
vd__tmp_alloc *owner;
size_t end_offset;
size_t start_offset;
size_t start_lastptr_offset;
uint32_t depth;
const char *file;
uint32_t line;
} vd__tmp_alloc_inst;
typedef struct vd__tmp_alloc
{
sx_alloc *tracer;
sx_vmem_context vmem;
vd__tmp_alloc_inst *SX_ARRAY alloc_stack;
sx_atomic_uint32 stack_depth;
float wait_time;
size_t peak;
size_t frame_peak;
};
typedef struct
{
void *ptr;
size_t size;
char file[32];
uint32_t line;
} vd__tmp_alloc_heapmode_item;
typedef struct vd__tmp_alloc_heapmode vd__tmp_alloc_heapmode;
typedef struct
{
sx_alloc alloc;
vd__tmp_alloc_heapmode *owner;
int item_idx;
const char *file;
uint32_t line;
} vd__tmp_alloc_heapmode_inst;
typedef struct vd__tmp_alloc_heapmode
{
size_t offset;
size_t max_size;
size_t peak;
size_t frame_peak;
sx_atomic_uint32 stack_depth;
float wait_time;
vd__tmp_alloc_heapmode_item *SX_ARRAY items;
vd__tmp_alloc_heapmode_inst *SX_ARRAY alloc_stack;
};
typedef struct
{
bool init;
uint32_t tid;
float idle_tm;
union {
vd__tmp_alloc alloc;
vd__tmp_alloc_heapmode heap_alloc;
};
sx_alloc *tracer_front; // This is the trace-allocator that is filled with trace data during
// the frame exec
sx_alloc *tracer_back; // This is the trace-allocator that information is saved from the
// previous frame and can be viewed in imgui
} vd__tmp_alloc_tls;
typedef struct vd__mem_trace_context vd__mem_trace_context;
typedef enum
{
VD__MEM_ACTION_MALLOC = 0,
VD__MEM_ACTION_FREE,
VD__MEM_ACTION_REALLOC
} vd__mem_action;
typedef struct vd__mem_item
{
vd__mem_trace_context *owner;
uint16_t num_callstack_items; // = 0, then try to read 'source_file' and 'source_func'
vd__mem_action action;
size_t size;
void *ptr;
union {
void *callstack[SW_MAX_FRAMES];
struct
{
char source_file[128];
char source_func[32];
uint32_t source_line;
};
};
uint32_t callstack_hash;
struct vd__mem_item *next;
struct vd__mem_item *prev;
int64_t frame; // record frame number
bool freed; // for mallocs, reallocs that got freed, but we want to keep the record
} vd__mem_item;
typedef struct
{
int item_idx;
vd__mem_item *item;
uint32_t count;
char entry_symbol[64];
size_t size;
} vd__mem_item_collapsed;
typedef struct vd__mem_trace_context
{
char name[32];
char name_view[32];
sx_alloc my_alloc; // all allocations are redirected from this to redirect_alloc
sx_alloc redirect_alloc; // receives all alloc calls and performs main allocations
uint32_t name_hash;
uint32_t options;
bool disabled;
bool viewDisabled;
sx_atomic_uint32 num_items;
sx_atomic_uint64 alloc_size;
sx_atomic_uint64 peak_size;
sx_pool *item_pool; // item_size = sizeof(vd__mem_item)
vd__mem_item *items_list; // first node
sx_mutex mtx;
vd__mem_item_collapsed *SX_ARRAY cached; // keep sorted cached data
struct vd__mem_trace_context *parent;
struct vd__mem_trace_context *child;
struct vd__mem_trace_context *next;
struct vd__mem_trace_context *prev;
} vd__mem_trace_context;
typedef struct
{
char name[32];
uint64_t start_tm;
sx_mutex mtx;
vd__mem_item **SX_ARRAY items;
} vd__mem_capture_context;
typedef struct
{
sg_pass_action pass_action;
sg_attachments atts;
sg_pipeline pip;
sg_pipeline pip_2;
sg_image depth_target;
sg_image color_target;
sg_sampler sampler;
int32_t sample_count;
} vd__v3d_offscreen_pass;
typedef struct
{
sg_pass_action pass_action;
sg_pipeline pip;
sg_pipeline pip_2;
} vd__v3d_display_pass;
typedef struct
{
ozz_instance_t *ozz;
} vd__v3d_doll;
enum
{
VD__VFS_FLAG_NONE = 0x01,
VD__VFS_FLAG_ABSOLUTE_PATH = 0x02,
VD__VFS_FLAG_TEXT_FILE = 0x04,
VD__VFS_FLAG_APPEND = 0x08,
};
typedef uint32_t vd__vfs_flags;
typedef enum
{
VD__VFS_CMD_READ,
VD__VFS_CMD_WRITE,
} vd__vfs_async_command;
typedef enum
{
VD__VFS_RESPONSE_READ_FAILED,
VD__VFS_RESPONSE_READ_OK,
VD__VFS_RESPONSE_WRITE_FAILED,
VD__VFS_RESPONSE_WRITE_OK,
} vd__vfs_response_code;
typedef void(vd__vfs_async_read_cb)(const char *path, sx_mem_block *mem, void *user_data);
typedef void(vd__vfs_async_write_cb)(const char *path, int64_t bytes_written, sx_mem_block *mem, void *user_data);
typedef struct
{
vd__vfs_async_command cmd;
vd__vfs_flags flags;
char path[VD__MAX_PATH];
sx_mem_block *write_mem;
const sx_alloc *alloc;
union {
vd__vfs_async_read_cb *read_fn;
vd__vfs_async_write_cb *write_fn;
};
void *user_data;
} vd__vfs_async_request;
typedef struct
{
vd__vfs_response_code code;
union {
sx_mem_block *read_mem;
sx_mem_block *write_mem;
};
union {
vd__vfs_async_read_cb *read_fn;
vd__vfs_async_write_cb *write_fn;
};
void *user_data;
int64_t bytes_written;
char path[VD__MAX_PATH];
} vd__vfs_async_response;
typedef struct
{
char path[VD__MAX_PATH];
char alias[VD__MAX_PATH];
int alias_len;
} vd__vfs_mount_point;
static struct
{
struct
{
bool active;
int width;
int height;
JanetFiber *main_fiber;
JanetFunction *mod_init_cb;
JanetFunction *mod_event_cb;
JanetFunction *mod_update_cb;
JanetFunction *mod_shutdown_cb;
} app;
struct
{
sx_alloc *alloc; // allocator passed on init
char asset_db_file[VD__MAX_PATH];
char variation[32];
vd__asset_mgr *SX_ARRAY asset_mgrs;
uint32_t *SX_ARRAY asset_name_hashes; // (count = count(asset_mgrs))
vd__asset *assets; // loaded assets
sx_handle_pool *asset_handles;
sx_hashtbl *asset_tbl; // key: hash(path+params), value: handle (asset_handles)
sx_hashtbl *resource_tbl; // key hash(path), value: index-to resources
vd__asset_resource *resources; // resource database
sx_hash_xxh32_t *hasher;
vd__asset_async_load_req *async_reqs;
vd__asset_async_job *async_job_list;
vd__asset_async_job *async_job_list_last;
vd__asset_group *groups;
sx_handle_pool *group_handles;
vd__asset_group_handle cur_group;
sx_lock_t assets_lk; // used for locking assets-array
} asset;
struct
{
const sx_alloc *heap_alloc;
sx_alloc *alloc;
sx_job_context *jobs;
uint32_t flags;
int tmp_mem_max;
int num_threads;
sx_alloc *temp_alloc_dummy;
int64_t frame_idx;
uint64_t delta_tick;
uint64_t elapsed_tick;
uint64_t last_tick;
float fps_mean;
float fps_frame;
sx_mutex tmp_allocs_mtx;