From 70e5e115c99836c538f37bcefc060884db7f154b Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 28 Jul 2025 15:58:03 -0500 Subject: [PATCH] codebase layers progress --- build.c | 37 ++++--- src/app.c | 18 ++-- src/ase/ase.c | 3 + src/ase/ase.h | 8 ++ src/{ase.c => ase/ase_core.c} | 11 --- src/{ase.h => ase/ase_core.h} | 5 - src/asset_cache.c | 9 +- src/asset_cache.h | 4 +- src/base/base.c | 12 +++ src/base/base.h | 21 ++++ src/{arena.c => base/base_arena.c} | 35 +++---- src/{arena.h => base/base_arena.h} | 54 +++++++---- src/{atomic.h => base/base_atomic.h} | 7 +- src/{bitbuff.c => base/base_bitbuff.c} | 6 -- src/{bitbuff.h => base/base_bitbuff.h} | 5 - src/{buddy.c => base/base_buddy.c} | 3 - src/{buddy.h => base/base_buddy.h} | 5 - src/base/base_fiber.c | 14 +++ src/base/base_fiber.h | 4 + src/{incbin.c => base/base_incbin.c} | 14 +-- src/{incbin.h => base/base_incbin.h} | 5 - src/{intrinsics.h => base/base_intrinsics.h} | 5 - src/{math.h => base/base_math.h} | 7 -- src/base/base_memory.c | 86 +++++++++++++++++ src/{memory.h => base/base_memory.h} | 16 +++- src/{rand.c => base/base_rand.c} | 17 +++- src/{rand.h => base/base_rand.h} | 7 +- src/{string.c => base/base_string.c} | 6 -- src/{string.h => base/base_string.h} | 5 - src/{uid.c => base/base_uid.c} | 6 +- src/{uid.h => base/base_uid.h} | 6 +- src/{uni.c => base/base_uni.c} | 2 - src/{uni.h => base/base_uni.h} | 5 - src/{util.h => base/base_util.h} | 57 ----------- src/collider.c | 3 +- src/common.h | 40 ++++---- src/draw.c | 3 +- src/draw.h | 2 +- src/dxc/dxc.cpp | 10 ++ src/dxc/dxc.h | 8 ++ src/{dxc.h => dxc/dxc_core.h} | 5 - src/{dxc.cpp => dxc/dxc_core_win32.cpp} | 39 +++++--- src/font.c | 8 +- src/font.h | 2 +- src/gp/gp.c | 7 ++ src/gp/gp.h | 12 +++ src/{gp.h => gp/gp_core.h} | 5 - src/{gp_dx12.c => gp/gp_core_dx12.c} | 26 ++--- src/gstat.h | 2 +- src/host.c | 9 +- src/host.h | 4 +- src/inc.c | 2 +- src/json/json.c | 3 + src/json/json.h | 8 ++ src/{json.c => json/json_core.c} | 6 -- src/{json.h => json/json_core.h} | 5 - src/memory.c | 36 ------- src/mixer.c | 6 +- src/mp3/mp3.c | 7 ++ src/mp3/mp3.h | 8 ++ src/{mp3.h => mp3/mp3_core.h} | 7 +- src/{mp3_mmf.c => mp3/mp3_core_mmf.c} | 10 +- src/playback.h | 2 - src/playback_wasapi.c | 8 +- src/prof_tracy.h | 2 +- src/resource.c | 7 +- src/resource.h | 2 +- src/settings.c | 7 +- src/sh/blit.hlsl_rs | 2 +- src/sh/common.hlsl | 2 +- src/sh/flood.hlsl_cs | 2 +- src/sh/material.hlsl_rs | 2 +- src/sh/shade.hlsl_cs | 2 +- src/sh/shape.hlsl_rs | 2 +- src/sh/ui.hlsl_rs | 2 +- src/sim/sim.c | 6 ++ src/sim/sim.h | 16 ++++ src/{sim.c => sim/sim_core.c} | 8 -- src/{sim.h => sim/sim_core.h} | 12 --- src/{ => sim}/sim_ent.c | 7 -- src/{ => sim}/sim_ent.h | 10 -- src/{phys.c => sim/sim_phys.c} | 8 -- src/{phys.h => sim/sim_phys.h} | 8 -- src/{ => sim}/sim_step.c | 18 ---- src/{ => sim}/sim_step.h | 7 -- src/sound.c | 11 ++- src/space.c | 3 +- src/sprite.c | 14 +-- src/sprite.h | 2 +- src/sys/sys.c | 6 ++ src/sys/sys.h | 13 +++ src/sys/sys_core.c | 5 + src/{sys.h => sys/sys_core.h} | 69 ++----------- src/{sys_win32.c => sys/sys_core_win32.c} | 96 ++----------------- src/{log.c => sys/sys_log.c} | 70 +++++++------- src/{log.h => sys/sys_log.h} | 8 -- src/sys/sys_sleep.c | 39 ++++++++ src/sys/sys_sleep.h | 3 + src/{snc.c => sys/sys_snc.c} | 8 +- src/{snc.h => sys/sys_snc.h} | 5 - src/tar/tar.c | 3 + src/tar/tar.h | 9 ++ src/{tar.c => tar/tar_core.c} | 5 - src/{tar.h => tar/tar_core.h} | 7 -- src/ttf/ttf.cpp | 10 ++ src/ttf/ttf.h | 8 ++ src/{ttf.h => ttf/ttf_core.h} | 7 -- .../ttf_core_dwrite.cpp} | 11 +-- src/user/user.c | 3 + src/user/user.h | 9 ++ src/{user.c => user/user_core.c} | 20 ++-- src/{user.h => user/user_core.h} | 8 +- src/util.c | 36 ------- src/watch.c | 8 +- 114 files changed, 629 insertions(+), 797 deletions(-) create mode 100644 src/ase/ase.c create mode 100644 src/ase/ase.h rename src/{ase.c => ase/ase_core.c} (99%) rename src/{ase.h => ase/ase_core.h} (97%) create mode 100644 src/base/base.c create mode 100644 src/base/base.h rename src/{arena.c => base/base_arena.c} (79%) rename src/{arena.h => base/base_arena.h} (79%) rename src/{atomic.h => base/base_atomic.h} (96%) rename src/{bitbuff.c => base/base_bitbuff.c} (99%) rename src/{bitbuff.h => base/base_bitbuff.h} (98%) rename src/{buddy.c => base/base_buddy.c} (99%) rename src/{buddy.h => base/base_buddy.h} (95%) create mode 100644 src/base/base_fiber.c create mode 100644 src/base/base_fiber.h rename src/{incbin.c => base/base_incbin.c} (88%) rename src/{incbin.h => base/base_incbin.h} (97%) rename src/{intrinsics.h => base/base_intrinsics.h} (98%) rename src/{math.h => base/base_math.h} (99%) create mode 100644 src/base/base_memory.c rename src/{memory.h => base/base_memory.h} (62%) rename src/{rand.c => base/base_rand.c} (77%) rename src/{rand.h => base/base_rand.h} (92%) rename src/{string.c => base/base_string.c} (99%) rename src/{string.h => base/base_string.h} (99%) rename src/{uid.c => base/base_uid.c} (83%) rename src/{uid.h => base/base_uid.h} (70%) rename src/{uni.c => base/base_uni.c} (99%) rename src/{uni.h => base/base_uni.h} (96%) rename src/{util.h => base/base_util.h} (85%) create mode 100644 src/dxc/dxc.cpp create mode 100644 src/dxc/dxc.h rename src/{dxc.h => dxc/dxc_core.h} (85%) rename src/{dxc.cpp => dxc/dxc_core_win32.cpp} (71%) create mode 100644 src/gp/gp.c create mode 100644 src/gp/gp.h rename src/{gp.h => gp/gp_core.h} (99%) rename src/{gp_dx12.c => gp/gp_core_dx12.c} (99%) create mode 100644 src/json/json.c create mode 100644 src/json/json.h rename src/{json.c => json/json_core.c} (99%) rename src/{json.h => json/json_core.h} (95%) delete mode 100644 src/memory.c create mode 100644 src/mp3/mp3.c create mode 100644 src/mp3/mp3.h rename src/{mp3.h => mp3/mp3_core.h} (77%) rename src/{mp3_mmf.c => mp3/mp3_core_mmf.c} (96%) create mode 100644 src/sim/sim.c create mode 100644 src/sim/sim.h rename src/{sim.c => sim/sim_core.c} (99%) rename src/{sim.h => sim/sim_core.h} (99%) rename src/{ => sim}/sim_ent.c (99%) rename src/{ => sim}/sim_ent.h (99%) rename src/{phys.c => sim/sim_phys.c} (99%) rename src/{phys.h => sim/sim_phys.h} (98%) rename src/{ => sim}/sim_step.c (99%) rename src/{ => sim}/sim_step.h (95%) create mode 100644 src/sys/sys.c create mode 100644 src/sys/sys.h create mode 100644 src/sys/sys_core.c rename src/{sys.h => sys/sys_core.h} (85%) rename src/{sys_win32.c => sys/sys_core_win32.c} (98%) rename src/{log.c => sys/sys_log.c} (72%) rename src/{log.h => sys/sys_log.h} (98%) create mode 100644 src/sys/sys_sleep.c create mode 100644 src/sys/sys_sleep.h rename src/{snc.c => sys/sys_snc.c} (96%) rename src/{snc.h => sys/sys_snc.h} (98%) create mode 100644 src/tar/tar.c create mode 100644 src/tar/tar.h rename src/{tar.c => tar/tar_core.c} (98%) rename src/{tar.h => tar/tar_core.h} (89%) create mode 100644 src/ttf/ttf.cpp create mode 100644 src/ttf/ttf.h rename src/{ttf.h => ttf/ttf_core.h} (89%) rename src/{ttf_dwrite.cpp => ttf/ttf_core_dwrite.cpp} (98%) create mode 100644 src/user/user.c create mode 100644 src/user/user.h rename src/{user.c => user/user_core.c} (99%) rename src/{user.h => user/user_core.h} (97%) delete mode 100644 src/util.c diff --git a/build.c b/build.c index 10f23b45..f1105827 100644 --- a/build.c +++ b/build.c @@ -1,5 +1,5 @@ -#include "buildit.h" +#include "buildit.h" typedef struct StepList StepList; /* ========================== * @@ -475,7 +475,7 @@ void OnBuild(StringList cli_args) String warnings = Lit("/WX /Wall " "/options:strict " - "/wd4820 /wd4201 /wd5220 /wd4514 /wd4244 /wd5045 /wd4242 /wd4061 /wd4189 /wd4723 /wd5246 /wd4324"); + "/wd4820 /wd4201 /wd5220 /wd4514 /wd4244 /wd5045 /wd4242 /wd4061 /wd4189 /wd4723 /wd5246 /wd4324 /wd4464 /wd4577"); StringListAppend(&perm, &compile_warnings, warnings); StringListAppend(&perm, &link_warnings, Lit("/WX")); @@ -496,6 +496,7 @@ void OnBuild(StringList cli_args) "-fno-strict-aliasing " "-fno-finite-loops " "-fwrapv " + "-I src/ " "-msse4.1 " "-msse4.2 ")); @@ -521,6 +522,13 @@ void OnBuild(StringList cli_args) StringListAppend(&perm, &link_warnings, warnings); } + /* DXC */ + { + StringListAppend(&perm, &dxc_compile_args, Lit("-I src/ -H -WX -Ges")); + String dxc_args_define = StringFromStringList(&perm, Lit(" "), dxc_compile_args); + StringListAppend(&perm, &compile_args, StringF(&perm, Lit("-DDXC_ARGS=\"%F\""), FmtStr(dxc_args_define))); + } + /* RTC */ if (arg_rtc) { if (!arg_crtlib) { @@ -627,13 +635,6 @@ void OnBuild(StringList cli_args) String incbin_dir = StringReplace(&perm, out_inc_dir_path, Lit("\\"), Lit("/")); StringListAppend(&perm, &compile_args, StringF(&perm, Lit("-DINCBIN_DIR_RAW=\"%F\""), FmtStr(incbin_dir))); } - - /* DXC */ - { - StringListAppend(&perm, &dxc_compile_args, Lit("-H -WX -Ges")); - String dxc_args_define = StringFromStringList(&perm, Lit(" "), dxc_compile_args); - StringListAppend(&perm, &compile_args, StringF(&perm, Lit("-DDXC_ARGS=\"%F\""), FmtStr(dxc_args_define))); - } } /* ========================== * @@ -683,7 +684,8 @@ void OnBuild(StringList cli_args) D_TagList src_input_files = { 0 }; { D_Tag src_dir = D_TagFromPath(&perm, Lit("src/sh"), D_TagKind_Dir); - D_TagList src_files = D_GetDirContents(&perm, src_dir, 0); + D_TagList src_files = { 0 }; + D_GetDirContents(&perm, &src_files, src_dir, 0); for (D_TagListNode *n = src_files.first; n; n = n->next) { D_Tag file = n->tag; @@ -956,7 +958,20 @@ void OnBuild(StringList cli_args) D_TagList src_input_files = { 0 }; { D_Tag src_dir = D_TagFromPath(&perm, Lit("src"), D_TagKind_Dir); - D_TagList src_files = D_GetDirContents(&perm, src_dir, 0); + D_TagList src_files = { 0 }; + + D_GetDirContents(&perm, &src_files, src_dir, 0); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/base/base.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/sys/sys.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/gp/gp.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/tar/tar.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/ase/ase.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/mp3/mp3.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/json/json.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/ttf/ttf.cpp"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/dxc/dxc.cpp"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/sim/sim.c"), D_TagKind_File)); + D_TagListAppend(&perm, &src_files, D_TagFromPath(&perm, Lit("src/user/user.c"), D_TagKind_File)); for (D_TagListNode *n = src_files.first; n; n = n->next) { Bool ignore = 1; diff --git a/src/app.c b/src/app.c index 1c59b4ee..a7253745 100644 --- a/src/app.c +++ b/src/app.c @@ -1,26 +1,20 @@ #include "app.h" -#include "arena.h" -#include "string.h" -#include "sys.h" -#include "user.h" -#include "sim.h" +#include "base/base.h" +#include "sys/sys.h" +#include "ttf/ttf.h" +#include "gp/gp.h" +#include "sim/sim.h" +#include "user/user.h" #include "playback.h" -#include "log.h" #include "resource.h" #include "asset_cache.h" #include "font.h" #include "sprite.h" -#include "ttf.h" #include "mixer.h" #include "sound.h" -#include "util.h" #include "settings.h" #include "draw.h" -#include "math.h" -#include "gp.h" -#include "phys.h" #include "host.h" -#include "bitbuff.h" #include "watch.h" GLOBAL struct { diff --git a/src/ase/ase.c b/src/ase/ase.c new file mode 100644 index 00000000..0cf764bc --- /dev/null +++ b/src/ase/ase.c @@ -0,0 +1,3 @@ +#include "ase.h" + +#include "ase_core.c" diff --git a/src/ase/ase.h b/src/ase/ase.h new file mode 100644 index 00000000..7261ae37 --- /dev/null +++ b/src/ase/ase.h @@ -0,0 +1,8 @@ +#ifndef ASE_H +#define ASE_H + +#include "../base/base.h" + +#include "ase_core.h" + +#endif diff --git a/src/ase.c b/src/ase/ase_core.c similarity index 99% rename from src/ase.c rename to src/ase/ase_core.c index 844660db..467111ac 100644 --- a/src/ase.c +++ b/src/ase/ase_core.c @@ -4,16 +4,6 @@ * DEFLATE decoder based on Handmade Hero's png parser * ========================== */ -#include "ase.h" -#include "arena.h" -#include "bitbuff.h" -#include "string.h" -#include "log.h" - -/* ========================== * - * Bitbuf - * ========================== */ - struct huff_bb { u8 *data; u64 cur_bit; @@ -452,7 +442,6 @@ PACK(struct frame_header { INTERNAL void push_error_copy_msg(struct arena *arena, struct ase_error_list *list, struct string msg_src) { - logf_error("Error while decoding .ase: \"%F\"", FMT_STR(msg_src)); struct ase_error *e = arena_push(arena, struct ase_error); e->msg = string_copy(arena, msg_src); if (!list->first) { diff --git a/src/ase.h b/src/ase/ase_core.h similarity index 97% rename from src/ase.h rename to src/ase/ase_core.h index 5eea3b40..c8a5deee 100644 --- a/src/ase.h +++ b/src/ase/ase_core.h @@ -1,6 +1,3 @@ -#ifndef ASE_H -#define ASE_H - struct ase_error { struct string msg; struct ase_error *next; @@ -66,5 +63,3 @@ struct ase_decode_sheet_result { struct ase_decode_image_result ase_decode_image(struct arena *arena, struct string encoded); struct ase_decode_sheet_result ase_decode_sheet(struct arena *arena, struct string encoded); - -#endif diff --git a/src/asset_cache.c b/src/asset_cache.c index 0494bbd5..a4398585 100644 --- a/src/asset_cache.c +++ b/src/asset_cache.c @@ -1,11 +1,6 @@ #include "asset_cache.h" -#include "sys.h" -#include "string.h" -#include "memory.h" -#include "arena.h" -#include "util.h" -#include "log.h" -#include "snc.h" +#include "base/base.h" +#include "sys/sys.h" /* ========================== * * Global state diff --git a/src/asset_cache.h b/src/asset_cache.h index e2820448..d8ae7e9c 100644 --- a/src/asset_cache.h +++ b/src/asset_cache.h @@ -1,8 +1,8 @@ #ifndef ASSET_CACHE_H #define ASSET_CACHE_H -#include "sys.h" -#include "util.h" +#include "base/base.h" +#include "sys/sys.h" enum asset_status { ASSET_STATUS_NONE, diff --git a/src/base/base.c b/src/base/base.c new file mode 100644 index 00000000..e03020d9 --- /dev/null +++ b/src/base/base.c @@ -0,0 +1,12 @@ +#include "base.h" + +#include "base_fiber.c" +#include "base_arena.c" +#include "base_buddy.c" +#include "base_memory.c" +#include "base_rand.c" +#include "base_string.c" +#include "base_bitbuff.c" +#include "base_uid.c" +#include "base_uni.c" +#include "base_incbin.c" diff --git a/src/base/base.h b/src/base/base.h new file mode 100644 index 00000000..75e1b9f4 --- /dev/null +++ b/src/base/base.h @@ -0,0 +1,21 @@ +#ifndef BASE_H +#define BASE_H + +#include "../gstat.h" + +#include "base_intrinsics.h" +#include "base_atomic.h" +#include "base_fiber.h" +#include "base_memory.h" +#include "base_arena.h" +#include "base_buddy.h" +#include "base_math.h" +#include "base_rand.h" +#include "base_string.h" +#include "base_bitbuff.h" +#include "base_uid.h" +#include "base_uni.h" +#include "base_util.h" +#include "base_incbin.h" + +#endif diff --git a/src/arena.c b/src/base/base_arena.c similarity index 79% rename from src/arena.c rename to src/base/base_arena.c index 89e47742..e3bf6320 100644 --- a/src/arena.c +++ b/src/base/base_arena.c @@ -1,9 +1,4 @@ -#include "arena.h" -#include "sys.h" -#include "memory.h" -#include "string.h" -#include "atomic.h" -#include "gstat.h" +struct scratch_shared g_scratch_shared = ZI; /* NOTE: Application will exit if arena fails to reserve or commit initial memory. */ struct arena *arena_alloc(u64 reserve) @@ -17,19 +12,23 @@ struct arena *arena_alloc(u64 reserve) reserve += ARENA_BLOCK_SIZE - block_remainder; } - u8 *base = sys_memory_reserve(reserve); + u8 *base = memory_reserve(reserve); if (!base) { /* Hard fail on memory reserve failure for now */ - sys_panic(LIT("Failed to reserve memory")); + /* FIXME: Enable this */ + //sys_panic(LIT("Failed to reserve memory")); + (*(volatile int *)0) = 0; } u64 reserved = reserve; gstat_add(GSTAT_MEMORY_RESERVED, reserve); /* Commit initial block */ - base = sys_memory_commit(base, ARENA_BLOCK_SIZE); + base = memory_commit(base, ARENA_BLOCK_SIZE); if (!base) { /* Hard fail on commit failure */ - sys_panic(LIT("Failed to commit initial memory block: System may be out of memory")); + /* FIXME: Enable this */ + //sys_panic(LIT("Failed to commit initial memory block: System may be out of memory")); + (*(volatile int *)0) = 0; } ASSERT(((u64)base & 0xFFF) == 0); /* Base should be 4k aligned */ @@ -57,7 +56,7 @@ void arena_release(struct arena *arena) gstat_add(GSTAT_MEMORY_COMMITTED, -(i64)(arena->committed - ARENA_HEADER_SIZE)); gstat_add(GSTAT_MEMORY_RESERVED, -(i64)(arena->reserved)); gstat_add(GSTAT_NUM_ARENAS, -1); - sys_memory_release(arena); + memory_release(arena); } /* NOTE: Application will exit if arena fails to commit memory */ @@ -83,12 +82,16 @@ void *arena_push_bytes_no_zero(struct arena *arena, u64 size, u64 align) u64 new_capacity = arena->committed + commit_bytes; if (new_capacity > arena->reserved) { /* Hard fail if we overflow reserved memory for now */ - sys_panic(LIT("Failed to commit new memory block: Overflow of reserved memory")); + /* FIXME: Enable this */ + //sys_panic(LIT("Failed to commit new memory block: Overflow of reserved memory")); + (*(volatile int *)0) = 0; } void *commit_address = base + arena->committed; - if (!sys_memory_commit(commit_address, commit_bytes)) { + if (!memory_commit(commit_address, commit_bytes)) { /* Hard fail on memory allocation failure for now */ - sys_panic(LIT("Failed to commit new memory block: System may be out of memory")); + /* FIXME: Enable this */ + //sys_panic(LIT("Failed to commit new memory block: System may be out of memory")); + (*(volatile int *)0) = 0; } arena->committed += commit_bytes; gstat_add(GSTAT_MEMORY_COMMITTED, commit_bytes); @@ -130,12 +133,12 @@ void arena_set_readonly(struct arena *arena) #if RTC arena->readonly = 1; #endif - sys_memory_set_committed_readonly(arena, arena->committed + ARENA_HEADER_SIZE); + memory_set_committed_readonly(arena, arena->committed + ARENA_HEADER_SIZE); } void arena_set_readwrite(struct arena *arena) { - sys_memory_set_committed_readwrite(arena, arena->committed + ARENA_HEADER_SIZE); + memory_set_committed_readwrite(arena, arena->committed + ARENA_HEADER_SIZE); #if RTC arena->readonly = 0; #endif diff --git a/src/arena.h b/src/base/base_arena.h similarity index 79% rename from src/arena.h rename to src/base/base_arena.h index 118cafb4..24fdc803 100644 --- a/src/arena.h +++ b/src/base/base_arena.h @@ -1,9 +1,3 @@ -#ifndef ARENA_H -#define ARENA_H - -#include "sys.h" -#include "memory.h" - #define ARENA_HEADER_SIZE 256 #define ARENA_BLOCK_SIZE 16384 @@ -128,25 +122,49 @@ INLINE void *_arena_push_dry(struct arena *arena, u64 align) * Scratch * ========================== */ - /* Any parameterized arenas in the caller's scope should be passed into this - * function as a potential "conflict". This is to prevent friction in case the - * passed arena is itself a scratch arena from another scope (since - * parameterized arenas are often used to allocate persistent results for the - * caller). - * - * Use `scratch_begin_no_conflict` instead if there is no arena in the current - * scope that could potentially be a scratch arena from another scope. */ +#define SCRATCH_ARENAS_PER_CTX 2 + +struct scratch_ctx { + struct arena *arenas[SCRATCH_ARENAS_PER_CTX]; +}; + +struct scratch_shared { + struct scratch_ctx scratch_contexts[MAX_FIBERS]; +}; + +extern struct scratch_shared g_scratch_shared; + +INLINE struct scratch_ctx *scratch_ctx_from_fiber_id(i16 fiber_id) +{ + struct scratch_shared *shared = &g_scratch_shared; + struct scratch_ctx *ctx = &shared->scratch_contexts[fiber_id]; + if (!ctx->arenas[0]) { + for (i32 i = 0; i < (i32)countof(ctx->arenas); ++i) { + ctx->arenas[i] = arena_alloc(GIBI(64)); + } + } + return ctx; +} + +/* Any parameterized arenas in the caller's scope should be passed into this + * function as a potential "conflict". This is to prevent friction in case the + * passed arena is itself a scratch arena from another scope (since + * parameterized arenas are often used to allocate persistent results for the + * caller). + * + * Use `scratch_begin_no_conflict` instead if there is no arena in the current + * scope that could potentially be a scratch arena from another scope. */ #define scratch_begin(potential_conflict) _scratch_begin(potential_conflict) INLINE struct arena_temp _scratch_begin(struct arena *potential_conflict) { /* This function is currently hard-coded to support 2 scratch arenas */ - STATIC_ASSERT(SYS_SCRATCH_ARENAS_PER_CTX == 2); + STATIC_ASSERT(SCRATCH_ARENAS_PER_CTX == 2); /* Use `scratch_begin_no_conflict` if no conflicts are present */ ASSERT(potential_conflict != 0); - struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); + struct scratch_ctx *ctx = scratch_ctx_from_fiber_id(FiberId()); struct arena *scratch_arena = ctx->arenas[0]; if (potential_conflict && scratch_arena == potential_conflict) { scratch_arena = ctx->arenas[1]; @@ -169,7 +187,7 @@ INLINE struct arena_temp _scratch_begin(struct arena *potential_conflict) INLINE struct arena_temp _scratch_begin_no_conflict(void) { - struct sys_scratch_ctx *ctx = sys_scratch_ctx_from_fiber_id(sys_current_fiber_id()); + struct scratch_ctx *ctx = scratch_ctx_from_fiber_id(FiberId()); struct arena *scratch_arena = ctx->arenas[0]; struct arena_temp temp = arena_temp_begin(scratch_arena); return temp; @@ -179,5 +197,3 @@ INLINE void scratch_end(struct arena_temp scratch_temp) { arena_temp_end(scratch_temp); } - -#endif diff --git a/src/atomic.h b/src/base/base_atomic.h similarity index 96% rename from src/atomic.h rename to src/base/base_atomic.h index 50c32f90..96b816e2 100644 --- a/src/atomic.h +++ b/src/base/base_atomic.h @@ -1,6 +1,3 @@ -#ifndef ATOMIC_H -#define ATOMIC_H - #if PLATFORM_WINDOWS FORCE_INLINE i8 atomic8_fetch(struct atomic8 *x) { return (i8)_InterlockedCompareExchange8((char *)&x->_v, 0, 0); } @@ -28,7 +25,5 @@ FORCE_INLINE i64 atomic64_fetch_xor(struct atomic64 *x, i64 c) { return (i64)_In FORCE_INLINE i64 atomic64_fetch_add(struct atomic64 *x, i64 a) { return (i64)_InterlockedExchangeAdd64(&x->_v, a); } #else -# error "Atomics not implemented" -#endif - +# error Atomics not implemented #endif diff --git a/src/bitbuff.c b/src/base/base_bitbuff.c similarity index 99% rename from src/bitbuff.c rename to src/base/base_bitbuff.c index 687b0bbb..bfa56658 100644 --- a/src/bitbuff.c +++ b/src/base/base_bitbuff.c @@ -1,7 +1,3 @@ -#include "bitbuff.h" -#include "memory.h" -#include "arena.h" - /* TODO: Safety check that functions taking byte length can't overflow bit conversion (log2(num_bytes) > 61) */ #define WRITE_OVERFLOW_ARENA_PUSH_SIZE 4096 @@ -682,8 +678,6 @@ void br_read_dbg_marker(struct bitbuff_reader *br, struct string name) #if BITBUFF_TEST -#include "string.h" - void bitbuff_test(void) { struct arena_temp scratch = scratch_begin_no_conflict(); diff --git a/src/bitbuff.h b/src/base/base_bitbuff.h similarity index 98% rename from src/bitbuff.h rename to src/base/base_bitbuff.h index a90065a4..d7beb81e 100644 --- a/src/bitbuff.h +++ b/src/base/base_bitbuff.h @@ -1,6 +1,3 @@ -#ifndef BITBUFF_H -#define BITBUFF_H - /* ========================== * * Bitbuff * ========================== */ @@ -123,5 +120,3 @@ void br_read_dbg_marker(struct bitbuff_reader *br, struct string name); #if BITBUFF_TEST void bitbuff_test(void); #endif - -#endif diff --git a/src/buddy.c b/src/base/base_buddy.c similarity index 99% rename from src/buddy.c rename to src/base/base_buddy.c index f0e45c3a..5d73b7ef 100644 --- a/src/buddy.c +++ b/src/base/base_buddy.c @@ -1,6 +1,3 @@ -#include "buddy.h" -#include "arena.h" - /* TODO: Elminiate meta arena. Just store levels in first 4096 bytes of buddy arena, and then zone header data at the beginning of each allocation. */ /* ========================== * diff --git a/src/buddy.h b/src/base/base_buddy.h similarity index 95% rename from src/buddy.h rename to src/base/base_buddy.h index 3b8c5e58..29d7b897 100644 --- a/src/buddy.h +++ b/src/base/base_buddy.h @@ -1,6 +1,3 @@ -#ifndef BUDDY_H -#define BUDDY_H - struct buddy_block { b32 is_used; struct buddy_level *level; @@ -34,5 +31,3 @@ void buddy_ctx_release(struct buddy_ctx *ctx); struct buddy_block *buddy_alloc(struct buddy_ctx *ctx, u64 size); void buddy_release(struct buddy_block *block); - -#endif diff --git a/src/base/base_fiber.c b/src/base/base_fiber.c new file mode 100644 index 00000000..270eda08 --- /dev/null +++ b/src/base/base_fiber.c @@ -0,0 +1,14 @@ +#if PLATFORM_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#define UNICODE +#include + +i16 FiberId(void) +{ + return (i16)(i64)GetFiberData(); +} + +#else +# error FiberId not implemented for this platform +#endif diff --git a/src/base/base_fiber.h b/src/base/base_fiber.h new file mode 100644 index 00000000..8646278e --- /dev/null +++ b/src/base/base_fiber.h @@ -0,0 +1,4 @@ +#define MAX_FIBERS 4096 + +STATIC_ASSERT(MAX_FIBERS < I16_MAX); /* Fiber id should fit max fibers */ +i16 FiberId(void); diff --git a/src/incbin.c b/src/base/base_incbin.c similarity index 88% rename from src/incbin.c rename to src/base/base_incbin.c index f8b79dd0..36f972a8 100644 --- a/src/incbin.c +++ b/src/base/base_incbin.c @@ -4,12 +4,6 @@ * Msvc RC file lookup * ========================== */ -#include "incbin.h" -#include "arena.h" -#include "string.h" -#include "atomic.h" -#include "intrinsics.h" - #define WIN32_LEAN_AND_MEAN #define UNICODE #include @@ -59,9 +53,11 @@ struct string _incbin_get(struct _incbin_rc_resource *inc) struct rc_search_params params = { .name_lower = name_lower }; EnumResourceNamesW(0, RT_RCDATA, &enum_func, (LONG_PTR)¶ms); if (!params.found) { - sys_panic(string_format(scratch.arena, - LIT("INCBIN include not found in RC file: \"%F\""), - FMT_STR(inc->rc_name))); + /* FIXME: enable this */ + //sys_panic(string_format(scratch.arena, + // LIT("INCBIN include not found in RC file: \"%F\""), + // FMT_STR(inc->rc_name))); + (*(volatile int *)0) = 0; } inc->data = params.data; state = INCBIN_STATE_SEARCHED; diff --git a/src/incbin.h b/src/base/base_incbin.h similarity index 97% rename from src/incbin.h rename to src/base/base_incbin.h index fa03e6f8..8af2bcd4 100644 --- a/src/incbin.h +++ b/src/base/base_incbin.h @@ -1,6 +1,3 @@ -#ifndef INCBIN_H -#define INCBIN_H - #if COMPILER_MSVC /* ========================== * @@ -66,5 +63,3 @@ struct string _incbin_get(struct _incbin_rc_resource *inc); #define INCBIN_GET(var) STRING_FROM_POINTERS(_incbin_ ## var ## _start, _incbin_ ## var ## _end) #endif - -#endif diff --git a/src/intrinsics.h b/src/base/base_intrinsics.h similarity index 98% rename from src/intrinsics.h rename to src/base/base_intrinsics.h index 3dc45654..b8b57849 100644 --- a/src/intrinsics.h +++ b/src/base/base_intrinsics.h @@ -1,6 +1,3 @@ -#ifndef INTRINSICS_H -#define INTRINSICS_H - /* ========================== * * Math * ========================== */ @@ -119,5 +116,3 @@ INLINE u64 ix_clock(void) { return __rdtsc(); } - -#endif diff --git a/src/math.h b/src/base/base_math.h similarity index 99% rename from src/math.h rename to src/base/base_math.h index ea3397b8..777f437f 100644 --- a/src/math.h +++ b/src/base/base_math.h @@ -1,10 +1,5 @@ -#ifndef MATH_H -#define MATH_H - /* Math functions are default 32 bit (f32, i32, etc) unless specified */ -#include "intrinsics.h" - INLINE f32 math_sqrt(f32 x); /* ========================== * @@ -1299,5 +1294,3 @@ INLINE struct math_spring math_spring_init(f32 hertz, f32 damping_ratio, f32 dt) } return res; } - -#endif diff --git a/src/base/base_memory.c b/src/base/base_memory.c new file mode 100644 index 00000000..d64e849a --- /dev/null +++ b/src/base/base_memory.c @@ -0,0 +1,86 @@ +/* ========================== * + * Memory operations + * ========================== */ + +#if !CRTLIB + +__attribute((section(".text.memcpy"))) +void *memcpy(void *__restrict dst, const void *__restrict src, u64 n) +{ + for (u64 i = 0; i < n; ++i) { + ((u8 *)dst)[i] = ((u8 *)src)[i]; + } + return dst; +} + +__attribute((section(".text.memset"))) +void *memset(void *dst, i32 c, u64 n) +{ + for (u64 i = 0; i < n; ++i) { + ((u8 *)dst)[i] = c; + } + return dst; +} + +__attribute((section(".text.memcmp"))) +i32 memcmp(const void *p1, const void *p2, u64 n) +{ + i32 res = 0; + for (u64 i = 0; i < n; ++i) { + res = ((u8 *)p1)[i] - ((u8 *)p2)[i]; + if (res != 0) { + break; + } + } + return res; +} + +#endif /* !CRTLIB */ + +/* ========================== * + * Memory allocation + * ========================== */ + +#if PLATFORM_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#define UNICODE +#include + +void *memory_reserve(u64 size) +{ + void *ptr = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS); + return ptr; +} + +void memory_release(void *address) +{ + VirtualFree(address, 0, MEM_RELEASE); +} + +void *memory_commit(void *address, u64 size) +{ + void *ptr = VirtualAlloc(address, size, MEM_COMMIT, PAGE_READWRITE); + return ptr; +} + +void memory_decommit(void *address, u64 size) +{ + VirtualFree(address, size, MEM_DECOMMIT); +} + +void memory_set_committed_readonly(void *address, u64 size) +{ + DWORD old; + VirtualProtect(address, size, PAGE_READONLY, &old); +} + +void memory_set_committed_readwrite(void *address, u64 size) +{ + DWORD old; + VirtualProtect(address, size, PAGE_READWRITE, &old); +} + +#else +# error Memory allocation not implemented for this platform +#endif diff --git a/src/memory.h b/src/base/base_memory.h similarity index 62% rename from src/memory.h rename to src/base/base_memory.h index 28a3999a..17b9b62a 100644 --- a/src/memory.h +++ b/src/base/base_memory.h @@ -1,5 +1,6 @@ -#ifndef MEMORY_H -#define MEMORY_H +/* ========================== * + * Memory operations + * ========================== */ #define MEMZERO_STRUCT(ptr) MEMZERO((ptr), sizeof(*(ptr))) #define MEMZERO_ARRAY(a) MEMZERO((a), sizeof((a))) @@ -23,4 +24,13 @@ void *memset(void *dst, i32 c, u64 n); i32 memcmp(const void *p1, const void *p2, u64 n); #endif -#endif +/* ========================== * + * Memory allocation + * ========================== */ + +void *memory_reserve(u64 size); +void memory_release(void *address); +void *memory_commit(void *address, u64 size); +void memory_decommit(void *address, u64 size); +void memory_set_committed_readonly(void *address, u64 size); +void memory_set_committed_readwrite(void *address, u64 size); diff --git a/src/rand.c b/src/base/base_rand.c similarity index 77% rename from src/rand.c rename to src/base/base_rand.c index b5fb677a..3a18fd12 100644 --- a/src/rand.c +++ b/src/base/base_rand.c @@ -1,10 +1,17 @@ -#include "rand.h" -#include "sys.h" -#include "memory.h" - /* TODO: Use a value that gives good precision when dividing into range 0 -> 1 */ #define F64_RAND_MAX U64_MAX +#if PLATFORM_WINDOWS +# define BCRYPT_RNG_ALG_HANDLE ((void *)0x00000081) +u32 BCryptGenRandom(void *algorithm, u8 *buffer, u32 buffer_size, u32 flags); +void rand_true(struct string buffer) +{ + BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, (u8 *)buffer.text, buffer.len, 0); +} +#else +# error rand_true not implemented for this platform +#endif + /* ========================== * * Stateful prng * ========================== */ @@ -13,7 +20,7 @@ u64 rand_u64_from_state(struct rand_state *state) { u64 seed = state->seed; if (seed == 0) { - sys_true_rand(STRING_FROM_STRUCT(&seed)); + rand_true(STRING_FROM_STRUCT(&seed)); state->seed = seed; } return seed ^ rand_u64_from_seed(++state->counter); diff --git a/src/rand.h b/src/base/base_rand.h similarity index 92% rename from src/rand.h rename to src/base/base_rand.h index 5d09d624..a37331dd 100644 --- a/src/rand.h +++ b/src/base/base_rand.h @@ -1,17 +1,14 @@ -#ifndef RAND_H -#define RAND_H - struct rand_state { /* If a state's seed == 0 upon a call to a related function, it will be initialized using platform's true rng source */ u64 seed; u64 counter; }; +void rand_true(struct string buffer); + u64 rand_u64_from_state(struct rand_state *state); f64 rand_f64_from_state(struct rand_state *state, f64 range_start, f64 range_end); u64 rand_u64_from_seed(u64 seed); u64 rand_u64_from_seeds(u64 seed_a, u64 seed_b); f64 rand_f64_from_seed(u64 seed, f64 range_start, f64 range_end); - -#endif diff --git a/src/string.c b/src/base/base_string.c similarity index 99% rename from src/string.c rename to src/base/base_string.c index 683c9884..7e3e8954 100644 --- a/src/string.c +++ b/src/base/base_string.c @@ -1,9 +1,3 @@ -#include "string.h" -#include "arena.h" -#include "memory.h" -#include "math.h" -#include "uni.h" - /* * NOTE: Strings should be considered ~IMMUTABLE~ slices * diff --git a/src/string.h b/src/base/base_string.h similarity index 99% rename from src/string.h rename to src/base/base_string.h index 612f76e9..53353aa7 100644 --- a/src/string.h +++ b/src/base/base_string.h @@ -1,6 +1,3 @@ -#ifndef STRING_H -#define STRING_H - struct string_array { u64 count; struct string *strings; @@ -135,5 +132,3 @@ struct string string_from_wstr_no_limit(struct arena *arena, wchar_t *wstr); struct string string_from_wstr(struct arena *arena, wchar_t *wstr, u64 limit); struct string16 string16_from_wstr_no_limit(wchar_t *wstr); struct string16 string16_from_wstr(wchar_t *wstr, u64 limit); - -#endif diff --git a/src/uid.c b/src/base/base_uid.c similarity index 83% rename from src/uid.c rename to src/base/base_uid.c index 05a45b76..75d0fb8b 100644 --- a/src/uid.c +++ b/src/base/base_uid.c @@ -1,12 +1,8 @@ -#include "uid.h" -#include "rand.h" -#include "sys.h" - /* Returns a uid generated from the system's random number generator */ struct uid uid_true_rand(void) { struct uid res = ZI; - sys_true_rand(STRING_FROM_STRUCT(&res)); + rand_true(STRING_FROM_STRUCT(&res)); return res; } diff --git a/src/uid.h b/src/base/base_uid.h similarity index 70% rename from src/uid.h rename to src/base/base_uid.h index b2c8e4bc..8e462526 100644 --- a/src/uid.h +++ b/src/base/base_uid.h @@ -1,7 +1,3 @@ -#ifndef UID_H -#define UID_H - struct uid uid_true_rand(void); -struct uid uid_combine(struct uid a, struct uid b); -#endif +struct uid uid_combine(struct uid a, struct uid b); diff --git a/src/uni.c b/src/base/base_uni.c similarity index 99% rename from src/uni.c rename to src/base/base_uni.c index 84ceb50d..6a15e378 100644 --- a/src/uni.c +++ b/src/base/base_uni.c @@ -1,5 +1,3 @@ -#include "uni.h" - /* ========================== * * utf8 * ========================== */ diff --git a/src/uni.h b/src/base/base_uni.h similarity index 96% rename from src/uni.h rename to src/base/base_uni.h index 3d5deeb5..d8c0a915 100644 --- a/src/uni.h +++ b/src/base/base_uni.h @@ -1,6 +1,3 @@ -#ifndef UNI_H -#define UNI_H - /* ========================== * * utf8 * ========================== */ @@ -52,5 +49,3 @@ struct uni_encode_utf32_result { struct uni_decode_utf32_result uni_decode_utf32(struct string32 str); struct uni_encode_utf32_result uni_encode_utf32(u32 codepoint); - -#endif diff --git a/src/util.h b/src/base/base_util.h similarity index 85% rename from src/util.h rename to src/base/base_util.h index 10a46bb4..15ddb581 100644 --- a/src/util.h +++ b/src/base/base_util.h @@ -1,14 +1,3 @@ -#ifndef UTIL_H -#define UTIL_H - -#include "sys.h" -#include "string.h" -#include "memory.h" -#include "arena.h" -#include "atomic.h" -#include "math.h" -#include "snc.h" - /* Utility functions and stuff that don't have a home :( */ /* ========================== * @@ -256,49 +245,3 @@ INLINE void dict_remove_entry(struct dict *dict, struct dict_entry *entry) dict->first_free = entry; } } - -/* ========================== * - * Sleep frame - * ========================== */ - -INLINE void sleep_precise(i64 sleep_time_ns) -{ - __prof; - - i64 big_sleep = sys_current_scheduler_period_ns(); - i64 tolerance = (f64)big_sleep * 0.5; - //i64 tolerance = 1000000000; - - i64 now_ns = sys_time_ns(); - i64 target_ns = now_ns + sleep_time_ns; - - /* Sleep */ - while (now_ns < target_ns - big_sleep - tolerance) { - __profn("Sleep part"); - sys_wait(0, 0, 0, big_sleep); - now_ns = sys_time_ns(); - } - - /* Spin */ - { - __profn("Sleep spin"); - while (now_ns < target_ns) { - ix_pause(); - now_ns = sys_time_ns(); - } - } -} - -INLINE void sleep_frame(i64 last_frame_time_ns, i64 target_dt_ns) -{ - if (last_frame_time_ns != 0 && target_dt_ns > 0) { - i64 now_ns = sys_time_ns(); - i64 last_frame_dt_ns = now_ns - last_frame_time_ns; - i64 sleep_time_ns = target_dt_ns - last_frame_dt_ns; - if (sleep_time_ns > 0) { - sleep_precise(sleep_time_ns); - } - } -} - -#endif diff --git a/src/collider.c b/src/collider.c index 74edd732..239ae0b6 100644 --- a/src/collider.c +++ b/src/collider.c @@ -1,6 +1,5 @@ #include "collider.h" -#include "math.h" -#include "arena.h" +#include "base/base.h" #include "gstat.h" /* How close can non-overlapping shapes be before collision is considered */ diff --git a/src/common.h b/src/common.h index 802d7336..c4c57063 100644 --- a/src/common.h +++ b/src/common.h @@ -13,23 +13,23 @@ extern "C" { * ========================== */ /* Intrinsic header info: - * MMX - * SSE - * SSE2 - * SSE3 - * SSSE3 - * SSE4.1 - * SSE4.2 - * SSE4A - * AES - * AVX, AVX2, FMA + * -#include -#include -#include -#include /* SSE4.2 */ +#include "stddef.h" +#include "stdint.h" +#include "stdarg.h" +#include "intrin.h" +#include "nmmintrin.h" /* SSE4.2 */ /* ========================== * * Flag defaults @@ -86,7 +86,7 @@ extern "C" { # define COMPILER_CLANG 0 # define COMPILER_MSVC 1 #else -# error "Unknown compiler" +# error Unknown compiler #endif /* Operating system */ @@ -103,7 +103,7 @@ extern "C" { # define PLATFORM_MAC 0 # define PLATFORM_LINUX 1 #else -# error "Unknown OS" +# error Unknown platform #endif #if defined(__cplusplus) @@ -260,12 +260,12 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t); #endif /* alignof */ -#if COMPILER_MSVC || (LANGUAGE_C && (__STDC_VERSION__ < 202311L)) +#if (COMPILER_MSVC && LANGUAGE_C) || (LANGUAGE_C && (__STDC_VERSION__ < 202311L)) # define alignof(type) __alignof(type) #endif /* alignas */ -#if COMPILER_MSVC || (LANGUAGE_C && __STDC_VERSION__ < 202311L) +#if (COMPILER_MSVC && LANGUAGE_C) || (LANGUAGE_C && __STDC_VERSION__ < 202311L) # if COMPILER_MSVC # define alignas(n) __declspec(align(n)) # else @@ -326,7 +326,7 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t); # define WRITE_BARRIER() __asm__ volatile("" ::: "memory") # define READ_BARRIER() __asm__ volatile("" ::: "memory") #else -# error "Memory barriers not implemented" +# error Memory barriers not implemented #endif /* Cat */ diff --git a/src/draw.c b/src/draw.c index d4064737..fbea3c01 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,6 +1,5 @@ #include "draw.h" -#include "arena.h" -#include "math.h" +#include "base/base.h" #include "font.h" #include "sprite.h" #include "collider.h" diff --git a/src/draw.h b/src/draw.h index 43716f1b..cf36b7db 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,7 +1,7 @@ #ifndef DRAW_H #define DRAW_H -#include "gp.h" +#include "gp/gp.h" struct font; struct font_startup_receipt; diff --git a/src/dxc/dxc.cpp b/src/dxc/dxc.cpp new file mode 100644 index 00000000..bcd89ae9 --- /dev/null +++ b/src/dxc/dxc.cpp @@ -0,0 +1,10 @@ +extern "C" +{ +#include "dxc.h" +} + +#if PLATFORM_WINDOWS +# include "dxc_core_win32.cpp" +#else +# error Dxc core not implemented for this platform +#endif diff --git a/src/dxc/dxc.h b/src/dxc/dxc.h new file mode 100644 index 00000000..11eca208 --- /dev/null +++ b/src/dxc/dxc.h @@ -0,0 +1,8 @@ +#ifndef DXC_H +#define DXC_H + +#include "../base/base.h" + +#include "dxc_core.h" + +#endif diff --git a/src/dxc.h b/src/dxc/dxc_core.h similarity index 85% rename from src/dxc.h rename to src/dxc/dxc_core.h index 253edbeb..c142c3d3 100644 --- a/src/dxc.h +++ b/src/dxc/dxc_core.h @@ -1,6 +1,3 @@ -#ifndef DXC_H -#define DXC_H - struct dxc_compile_result { struct string dxc; struct string errors; @@ -8,5 +5,3 @@ struct dxc_compile_result { }; struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_source, i32 num_args, struct string *args); - -#endif diff --git a/src/dxc.cpp b/src/dxc/dxc_core_win32.cpp similarity index 71% rename from src/dxc.cpp rename to src/dxc/dxc_core_win32.cpp index 0e036b7c..81c3d1de 100644 --- a/src/dxc.cpp +++ b/src/dxc/dxc_core_win32.cpp @@ -1,4 +1,3 @@ - #if !RESOURCE_RELOADING extern "C" @@ -18,18 +17,22 @@ struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_ #else -extern "C" -{ -#include "dxc.h" -#include "arena.h" -#include "string.h" -} +#if COMPILER_CLANG +# pragma clang diagnostic ignored "-Wlanguage-extension-token" +#endif -#pragma clang diagnostic ignored "-Wlanguage-extension-token" -#include -#include -#include -#include +#define _ALLOW_RTCc_IN_STL + +#pragma warning(push, 0) +# define WIN32_LEAN_AND_MEAN +# define UNICODE +# define NTDDI_WIN11_DT 0x0C0A0000 +# define NTDDI_VERSION 0x0A000000 +# include +# include +# include +# include +#pragma warning(pop) #pragma comment(lib, "d3dcompiler") #pragma comment(lib, "dxcompiler") @@ -61,13 +64,16 @@ struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_ /* Compile */ CComPtr compile_results = 0; - dxc_compiler->Compile(&dxc_src_buffer, (LPCWSTR *)wstr_args, num_args, dxc_include_handler, IID_PPV_ARGS(&compile_results)); + dxc_compiler->Compile(&dxc_src_buffer, (LPCWSTR *)wstr_args, (u32)num_args, dxc_include_handler, IID_PPV_ARGS(&compile_results)); /* Copy errors */ CComPtr dxc_errors = 0; compile_results->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(&dxc_errors), 0); if (dxc_errors != 0) { - res.errors = string_copy(arena, STRING(dxc_errors->GetStringLength(), (u8 *)dxc_errors->GetBufferPointer())); + struct string blob_str = ZI; + blob_str.len = dxc_errors->GetBufferSize(); + blob_str.text = (u8 *)dxc_errors->GetBufferPointer(); + res.errors = string_copy(arena, blob_str); } /* Get status */ @@ -80,7 +86,10 @@ struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_ CComPtr dxc_shader = 0; compile_results->GetOutput(DXC_OUT_OBJECT, IID_PPV_ARGS(&dxc_shader), 0); if (dxc_shader != 0) { - res.dxc = string_copy(arena, STRING(dxc_shader->GetBufferSize(), (u8 *)dxc_shader->GetBufferPointer())); + struct string blob_str = ZI; + blob_str.len = dxc_shader->GetBufferSize(); + blob_str.text = (u8 *)dxc_shader->GetBufferPointer(); + res.dxc = string_copy(arena, blob_str); } } diff --git a/src/font.c b/src/font.c index 5e8c6c26..93eec2bf 100644 --- a/src/font.c +++ b/src/font.c @@ -1,11 +1,9 @@ #include "font.h" -#include "arena.h" -#include "ttf.h" +#include "base/base.h" +#include "ttf/ttf.h" +#include "gp/gp.h" #include "asset_cache.h" #include "resource.h" -#include "log.h" -#include "string.h" -#include "gp.h" #define LOOKUP_TABLE_SIZE (256) GLOBAL u32 g_font_codes[] = { diff --git a/src/font.h b/src/font.h index 73e3b96b..4249e4c4 100644 --- a/src/font.h +++ b/src/font.h @@ -1,7 +1,7 @@ #ifndef FONT_H #define FONT_H -#include "util.h" +#include "base/base.h" struct asset; struct asset_cache_startup_receipt; diff --git a/src/gp/gp.c b/src/gp/gp.c new file mode 100644 index 00000000..97f6290e --- /dev/null +++ b/src/gp/gp.c @@ -0,0 +1,7 @@ +#include "gp.h" + +#if PLATFORM_WINDOWS +# include "gp_core_dx12.c" +#else +# error Gp core not implemented for this platform +#endif diff --git a/src/gp/gp.h b/src/gp/gp.h new file mode 100644 index 00000000..5a264da0 --- /dev/null +++ b/src/gp/gp.h @@ -0,0 +1,12 @@ +#ifndef GP_H +#define GP_H + +#include "../base/base.h" +#include "../sys/sys.h" +#include "../tar/tar.h" +#include "../ase/ase.h" +#include "../dxc/dxc.h" + +#include "gp_core.h" + +#endif diff --git a/src/gp.h b/src/gp/gp_core.h similarity index 99% rename from src/gp.h rename to src/gp/gp_core.h index 422c9290..c6909b21 100644 --- a/src/gp.h +++ b/src/gp/gp_core.h @@ -1,6 +1,3 @@ -#ifndef GP_H -#define GP_H - struct sys_window; struct snc_counter; @@ -151,5 +148,3 @@ void gp_swapchain_wait(struct gp_swapchain *gp_swapchain); * 2. Blits `texture` to the backbuffer using `texture_xf` * 3. Presents the backbuffer */ void gp_present(struct gp_swapchain *gp_swapchain, struct v2i32 backbuffer_resolution, struct gp_resource *texture, struct xform texture_xf, i32 vsync); - -#endif diff --git a/src/gp_dx12.c b/src/gp/gp_core_dx12.c similarity index 99% rename from src/gp_dx12.c rename to src/gp/gp_core_dx12.c index 353495f9..280cacd6 100644 --- a/src/gp_dx12.c +++ b/src/gp/gp_core_dx12.c @@ -1,26 +1,12 @@ -#include "gp.h" -#include "sys.h" -#include "arena.h" -#include "memory.h" -#include "string.h" -#include "app.h" -#include "log.h" -#include "atomic.h" -#include "util.h" -#include "rand.h" -#include "sprite.h" -#include "gstat.h" -#include "snc.h" -#include "ase.h" -#include "resource.h" -#include "tar.h" -#include "inc.h" -#include "dxc.h" -#include "watch.h" +#include "../sprite.h" +#include "../gstat.h" +#include "../resource.h" +#include "../inc.h" +#include "../watch.h" /* Include common shader types */ #define SH_CPU 1 -#include "sh/sh_common.h" +#include "../sh/sh_common.h" #pragma warning(push, 0) # define UNICODE diff --git a/src/gstat.h b/src/gstat.h index be62c443..1cceffdc 100644 --- a/src/gstat.h +++ b/src/gstat.h @@ -5,7 +5,7 @@ #if GSTAT_ENABLED -#include "atomic.h" +#include "base/base.h" struct _gstats { struct atomic64_padded GSTAT_SOCK_BYTES_SENT; diff --git a/src/host.c b/src/host.c index 5088ef1f..fccaef09 100644 --- a/src/host.c +++ b/src/host.c @@ -1,11 +1,6 @@ #include "host.h" -#include "arena.h" -#include "bitbuff.h" -#include "sys.h" -#include "util.h" -#include "log.h" -#include "buddy.h" -#include "atomic.h" +#include "base/base.h" +#include "sys/sys.h" /* TODO: * diff --git a/src/host.h b/src/host.h index 65a6ed8b..09c7b4ba 100644 --- a/src/host.h +++ b/src/host.h @@ -1,8 +1,8 @@ #ifndef HOST_H #define HOST_H -#include "sys.h" -#include "snc.h" +#include "base/base.h" +#include "sys/sys.h" #define HOST_CHANNEL_ID_NIL (struct host_channel_id) { .gen = 0, .idx = 0 } #define HOST_CHANNEL_ID_ALL (struct host_channel_id) { .gen = U32_MAX, .idx = U32_MAX } diff --git a/src/inc.c b/src/inc.c index 6ab43a69..e183c53f 100644 --- a/src/inc.c +++ b/src/inc.c @@ -1,5 +1,5 @@ #include "inc.h" -#include "incbin.h" +#include "base/base.h" /* This is the file that actually includes binary data meant to be embedded in * the executable. Embedded files should be added as dependencies to this source diff --git a/src/json/json.c b/src/json/json.c new file mode 100644 index 00000000..43d0bc92 --- /dev/null +++ b/src/json/json.c @@ -0,0 +1,3 @@ +#include "json.h" + +#include "json_core.c" diff --git a/src/json/json.h b/src/json/json.h new file mode 100644 index 00000000..27a9660e --- /dev/null +++ b/src/json/json.h @@ -0,0 +1,8 @@ +#ifndef JSON_H +#define JSON_H + +#include "../base/base.h" + +#include "json_core.h" + +#endif diff --git a/src/json.c b/src/json/json_core.c similarity index 99% rename from src/json.c rename to src/json/json_core.c index da53f47d..6762b1fe 100644 --- a/src/json.c +++ b/src/json/json_core.c @@ -1,9 +1,3 @@ -#include "json.h" -#include "arena.h" -#include "string.h" -#include "arena.h" -#include "math.h" - /* TODO (if we want to be JSON standard compliant): * - Support unicode escape sequences in strings (\u) * - Don't allow leading 0s in numbers diff --git a/src/json.h b/src/json/json_core.h similarity index 95% rename from src/json.h rename to src/json/json_core.h index 74b45460..091db0dc 100644 --- a/src/json.h +++ b/src/json/json_core.h @@ -1,6 +1,3 @@ -#ifndef JSON_H -#define JSON_H - enum json_type { JSON_TYPE_NULL, JSON_TYPE_BOOL, @@ -45,5 +42,3 @@ struct json_parse_result { }; struct json_parse_result json_from_string(struct arena *arena, struct string src); - -#endif diff --git a/src/memory.c b/src/memory.c deleted file mode 100644 index 1960d336..00000000 --- a/src/memory.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "memory.h" - -#if !CRTLIB - -__attribute((section(".text.memcpy"))) -void *memcpy(void *__restrict dst, const void *__restrict src, u64 n) -{ - for (u64 i = 0; i < n; ++i) { - ((u8 *)dst)[i] = ((u8 *)src)[i]; - } - return dst; -} - -__attribute((section(".text.memset"))) -void *memset(void *dst, i32 c, u64 n) -{ - for (u64 i = 0; i < n; ++i) { - ((u8 *)dst)[i] = c; - } - return dst; -} - -__attribute((section(".text.memcmp"))) -i32 memcmp(const void *p1, const void *p2, u64 n) -{ - i32 res = 0; - for (u64 i = 0; i < n; ++i) { - res = ((u8 *)p1)[i] - ((u8 *)p2)[i]; - if (res != 0) { - break; - } - } - return res; -} - -#endif /* !CRTLIB */ diff --git a/src/mixer.c b/src/mixer.c index 526629b3..bcdd6116 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -1,9 +1,7 @@ #include "mixer.h" -#include "arena.h" +#include "base/base.h" +#include "sys/sys.h" #include "sound.h" -#include "sys.h" -#include "math.h" -#include "snc.h" /* TODO: Cap max sounds playing. */ diff --git a/src/mp3/mp3.c b/src/mp3/mp3.c new file mode 100644 index 00000000..27a43798 --- /dev/null +++ b/src/mp3/mp3.c @@ -0,0 +1,7 @@ +#include "mp3.h" + +#if PLATFORM_WINDOWS +# include "mp3_core_mmf.c" +#else +# error Mp3 core not implemented for this platform +#endif diff --git a/src/mp3/mp3.h b/src/mp3/mp3.h new file mode 100644 index 00000000..6ae91e50 --- /dev/null +++ b/src/mp3/mp3.h @@ -0,0 +1,8 @@ +#ifndef MP3_H +#define MP3_H + +#include "../base/base.h" + +#include "mp3_core.h" + +#endif diff --git a/src/mp3.h b/src/mp3/mp3_core.h similarity index 77% rename from src/mp3.h rename to src/mp3/mp3_core.h index c6ccdbbe..a891f5d7 100644 --- a/src/mp3.h +++ b/src/mp3/mp3_core.h @@ -1,6 +1,3 @@ -#ifndef MP3 -#define MP3 - #define MP3_DECODE_FLAG_NONE 0x00 #define MP3_DECODE_FLAG_STEREO 0x01 @@ -9,6 +6,4 @@ struct mp3_decode_result { b32 success; }; -struct mp3_decode_result mp3_decode(struct arena *arena, struct string encoded, u32 flags); - -#endif +struct mp3_decode_result mp3_decode(struct arena *arena, struct string encoded, u32 sample_rate, u32 flags); diff --git a/src/mp3_mmf.c b/src/mp3/mp3_core_mmf.c similarity index 96% rename from src/mp3_mmf.c rename to src/mp3/mp3_core_mmf.c index b22aecbe..f73311e7 100644 --- a/src/mp3_mmf.c +++ b/src/mp3/mp3_core_mmf.c @@ -2,12 +2,6 @@ * Microsoft Media Foundation (MMF) mp3 decoder * ========================== */ -#include "mp3.h" -#include "arena.h" -#include "playback.h" - - - #pragma warning(push, 0) # define COBJMACROS # define WIN32_LEAN_AND_MEAN @@ -24,11 +18,9 @@ #pragma comment(lib, "mfreadwrite") #pragma comment(lib, "shlwapi") -struct mp3_decode_result mp3_decode(struct arena *arena, struct string encoded, u32 flags) +struct mp3_decode_result mp3_decode(struct arena *arena, struct string encoded, u32 sample_rate, u32 flags) { struct mp3_decode_result res = ZI; - - u64 sample_rate = PLAYBACK_SAMPLE_RATE; u64 bytes_per_sample = 2; u64 channel_count; diff --git a/src/playback.h b/src/playback.h index 53e19ef9..12bae469 100644 --- a/src/playback.h +++ b/src/playback.h @@ -1,8 +1,6 @@ #ifndef PLAYBACK_H #define PLAYBACK_H -#define PLAYBACK_SAMPLE_RATE 48000 - struct mixer_startup_receipt; struct playback_startup_receipt { i32 _; }; diff --git a/src/playback_wasapi.c b/src/playback_wasapi.c index 98a05cdc..8e349e28 100644 --- a/src/playback_wasapi.c +++ b/src/playback_wasapi.c @@ -6,12 +6,10 @@ * ========================== */ #include "playback.h" -#include "arena.h" -#include "sys.h" +#include "base/base.h" +#include "sys/sys.h" #include "mixer.h" -#include "atomic.h" #include "app.h" -#include "snc.h" #define COBJMACROS #define WIN32_LEAN_AND_MEAN @@ -33,6 +31,8 @@ DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1, 0x78, 0x DEFINE_GUID(IID_IAudioClient3, 0x7ed4ee07, 0x8e67, 0x4cd4, 0x8c, 0x1a, 0x2b, 0x7a, 0x59, 0x87, 0xad, 0x42); DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2); +#define PLAYBACK_SAMPLE_RATE 48000 + struct wasapi_buffer { u32 frames_count; u8 *frames; diff --git a/src/prof_tracy.h b/src/prof_tracy.h index 9a7e0b97..6582732e 100644 --- a/src/prof_tracy.h +++ b/src/prof_tracy.h @@ -4,7 +4,7 @@ #if PROFILING #if COMPILER_MSVC -# error "MSVC not supported for profiling (cleanup attributes are required for profiling markup)" +# error MSVC not supported for profiling (cleanup attributes are required for profiling markup) #endif #define PROFILING_SYSTEM_TRACE 0 diff --git a/src/resource.c b/src/resource.c index 397e58db..4a0a06aa 100644 --- a/src/resource.c +++ b/src/resource.c @@ -1,9 +1,8 @@ #include "resource.h" #include "app.h" -#include "arena.h" -#include "tar.h" -#include "incbin.h" -#include "util.h" +#include "base/base.h" +#include "sys/sys.h" +#include "tar/tar.h" /* ========================== * * Global data diff --git a/src/resource.h b/src/resource.h index 86d6c36b..ec0cb974 100644 --- a/src/resource.h +++ b/src/resource.h @@ -1,7 +1,7 @@ #ifndef RESOURCE_H #define RESOURCE_H -#include "sys.h" +#include "sys/sys.h" #define RESOURCE_NAME_LEN_MAX 256 diff --git a/src/settings.c b/src/settings.c index d9a6f6c2..4dcfebbf 100644 --- a/src/settings.c +++ b/src/settings.c @@ -1,8 +1,7 @@ #include "settings.h" -#include "arena.h" -#include "json.h" -#include "string.h" -#include "math.h" +#include "base/base.h" +#include "sys/sys.h" +#include "json/json.h" struct string settings_serialize(struct arena *arena, const struct sys_window_settings *settings) { diff --git a/src/sh/blit.hlsl_rs b/src/sh/blit.hlsl_rs index a131ca0e..c55f3658 100644 --- a/src/sh/blit.hlsl_rs +++ b/src/sh/blit.hlsl_rs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sh/common.hlsl b/src/sh/common.hlsl index 5be9c381..90237fa4 100644 --- a/src/sh/common.hlsl +++ b/src/sh/common.hlsl @@ -1,4 +1,4 @@ -#include "sh_common.h" +#include "sh/sh_common.h" #define TAU 6.28318530718 #define PI 3.14159265359 diff --git a/src/sh/flood.hlsl_cs b/src/sh/flood.hlsl_cs index b361d674..dc9ed8ee 100644 --- a/src/sh/flood.hlsl_cs +++ b/src/sh/flood.hlsl_cs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sh/material.hlsl_rs b/src/sh/material.hlsl_rs index 02aeb3c2..9df35920 100644 --- a/src/sh/material.hlsl_rs +++ b/src/sh/material.hlsl_rs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sh/shade.hlsl_cs b/src/sh/shade.hlsl_cs index b1f6c6a6..44a9b0e5 100644 --- a/src/sh/shade.hlsl_cs +++ b/src/sh/shade.hlsl_cs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sh/shape.hlsl_rs b/src/sh/shape.hlsl_rs index 57139204..0302126b 100644 --- a/src/sh/shape.hlsl_rs +++ b/src/sh/shape.hlsl_rs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sh/ui.hlsl_rs b/src/sh/ui.hlsl_rs index eaf6d0bf..83e5605e 100644 --- a/src/sh/ui.hlsl_rs +++ b/src/sh/ui.hlsl_rs @@ -1,4 +1,4 @@ -#include "common.hlsl" +#include "sh/common.hlsl" ConstantBuffer sig : register(b0); diff --git a/src/sim/sim.c b/src/sim/sim.c new file mode 100644 index 00000000..0c7357f7 --- /dev/null +++ b/src/sim/sim.c @@ -0,0 +1,6 @@ +#include "sim.h" + +#include "sim_core.c" +#include "sim_ent.c" +#include "sim_phys.c" +#include "sim_step.c" diff --git a/src/sim/sim.h b/src/sim/sim.h new file mode 100644 index 00000000..43cfc7ca --- /dev/null +++ b/src/sim/sim.h @@ -0,0 +1,16 @@ +#ifndef SIM_H +#define SIM_H + +#include "../base/base.h" +#include "../collider.h" +#include "../sprite.h" +#include "../space.h" +#include "../host.h" +#include "../mixer.h" + +#include "sim_core.h" +#include "sim_phys.h" +#include "sim_ent.h" +#include "sim_step.h" + +#endif diff --git a/src/sim.c b/src/sim/sim_core.c similarity index 99% rename from src/sim.c rename to src/sim/sim_core.c index d5198a1f..a3531431 100644 --- a/src/sim.c +++ b/src/sim/sim_core.c @@ -1,11 +1,3 @@ -#include "sim.h" -#include "sim_ent.h" -#include "host.h" -#include "arena.h" -#include "util.h" -#include "arena.h" -#include "bitbuff.h" - /* Sim hierarchy is as follows: * * Client store -> clients -> snapshots -> ents diff --git a/src/sim.h b/src/sim/sim_core.h similarity index 99% rename from src/sim.h rename to src/sim/sim_core.h index 5ebc692f..2e532635 100644 --- a/src/sim.h +++ b/src/sim/sim_core.h @@ -1,6 +1,3 @@ -#ifndef SIM_H -#define SIM_H - #define SIM_CLIENT_NIL_HANDLE ((struct sim_client_handle) { .gen = 0, .idx = 0 }) /* Absolute layers */ @@ -249,12 +246,3 @@ void sim_snapshot_sync_ents(struct sim_snapshot *local_ss, struct sim_snapshot * /* Encode / decode */ void sim_snapshot_encode(struct bitbuff_writer *bw, struct sim_client *receiver, struct sim_snapshot *ss0, struct sim_snapshot *ss1); void sim_snapshot_decode(struct bitbuff_reader *br, struct sim_snapshot *ss); - - - - - - - - -#endif diff --git a/src/sim_ent.c b/src/sim/sim_ent.c similarity index 99% rename from src/sim_ent.c rename to src/sim/sim_ent.c index d6c06fde..70c181fe 100644 --- a/src/sim_ent.c +++ b/src/sim/sim_ent.c @@ -1,10 +1,3 @@ -#include "sim_ent.h" -#include "sim.h" -#include "math.h" -#include "bitbuff.h" -#include "uid.h" -#include "rand.h" - /* Id magic number constants (to be used in conjunction with ent ids in deterministic id combinations) */ #define SIM_ENT_CONTACT_BASIS_UID (UID(0x6a2a5d2dbecf534f, 0x0a8ca7c372a015af)) #define SIM_ENT_COLLISION_DEBUG_BASIS_UID (UID(0x302c01182013bb02, 0x570bd270399d11a5)) diff --git a/src/sim_ent.h b/src/sim/sim_ent.h similarity index 99% rename from src/sim_ent.h rename to src/sim/sim_ent.h index e2ab14f5..5193af98 100644 --- a/src/sim_ent.h +++ b/src/sim/sim_ent.h @@ -1,11 +1,3 @@ -#ifndef SIM_ENT_H -#define SIM_ENT_H - -#include "sim.h" -#include "sprite.h" -#include "mixer.h" -#include "phys.h" - #define SIM_ENT_NIL_ID ((struct sim_ent_id) { UID(0, 0) }) #define SIM_ENT_ROOT_ID ((struct sim_ent_id) { UID(0xaaaaaaaaaaaaaaaa, 0xaaaaaaaaaaaaaaaa) }) @@ -559,5 +551,3 @@ void sim_ent_sync(struct sim_ent *local, struct sim_ent *remote); /* Encode / decode */ void sim_ent_encode(struct bitbuff_writer *bw, struct sim_ent *e0, struct sim_ent *e1); void sim_ent_decode(struct bitbuff_reader *br, struct sim_ent *e); - -#endif diff --git a/src/phys.c b/src/sim/sim_phys.c similarity index 99% rename from src/phys.c rename to src/sim/sim_phys.c index d01b5c16..00fc994c 100644 --- a/src/phys.c +++ b/src/sim/sim_phys.c @@ -1,11 +1,3 @@ -#include "phys.h" -#include "arena.h" -#include "sim_ent.h" -#include "sim_step.h" -#include "math.h" -#include "space.h" -#include "uid.h" - #define CONTACT_SPRING_HZ 25 #define CONTACT_SPRING_DAMP 10 diff --git a/src/phys.h b/src/sim/sim_phys.h similarity index 98% rename from src/phys.h rename to src/sim/sim_phys.h index aa9704e1..6742a331 100644 --- a/src/phys.h +++ b/src/sim/sim_phys.h @@ -1,9 +1,3 @@ -#ifndef PHYS_H -#define PHYS_H - -#include "collider.h" -#include "math.h" - struct space; struct sim_ent_lookup; struct sim_step_ctx; @@ -248,5 +242,3 @@ void phys_update_aabbs(struct phys_step_ctx *ctx); * ========================== */ void phys_step(struct phys_step_ctx *ctx, f32 timestep); - -#endif diff --git a/src/sim_step.c b/src/sim/sim_step.c similarity index 99% rename from src/sim_step.c rename to src/sim/sim_step.c index b44e6ddb..3553332b 100644 --- a/src/sim_step.c +++ b/src/sim/sim_step.c @@ -1,21 +1,3 @@ -#include "sim_step.h" -#include "sim_ent.h" -#include "sim.h" -#include "sys.h" -#include "arena.h" -#include "util.h" -#include "sprite.h" -#include "math.h" -#include "atomic.h" -#include "app.h" -#include "log.h" -#include "phys.h" -#include "collider.h" -#include "rand.h" -#include "space.h" -#include "bitbuff.h" -#include "host.h" - /* ========================== * * Sim accel * ========================== */ diff --git a/src/sim_step.h b/src/sim/sim_step.h similarity index 95% rename from src/sim_step.h rename to src/sim/sim_step.h index adabef81..0dc9c4e2 100644 --- a/src/sim_step.h +++ b/src/sim/sim_step.h @@ -1,8 +1,3 @@ -#ifndef SIM_STEP_H -#define SIM_STEP_H - -#include "rand.h" - struct space; struct sim_snapshot; struct sim_snapshot_list; @@ -40,5 +35,3 @@ struct sim_step_ctx { }; void sim_step(struct sim_step_ctx *ctx); - -#endif diff --git a/src/sound.c b/src/sound.c index b50c2f48..384dce97 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,10 +1,11 @@ #include "sound.h" -#include "arena.h" -#include "log.h" -#include "sys.h" +#include "base/base.h" +#include "sys/sys.h" #include "resource.h" #include "asset_cache.h" -#include "mp3.h" +#include "mp3/mp3.h" + +#define SOUND_SAMPLE_RATE 48000 struct sound_task_params { struct sound_task_params *next_free; @@ -98,7 +99,7 @@ INTERNAL SYS_JOB_DEF(sound_load_asset_job, job) if (flags & SOUND_FLAG_STEREO) { decode_flags |= MP3_DECODE_FLAG_STEREO; } - decoded = mp3_decode(scratch.arena, resource_get_data(&sound_rs), decode_flags); + decoded = mp3_decode(scratch.arena, resource_get_data(&sound_rs), SOUND_SAMPLE_RATE, decode_flags); if (!decoded.success) { error_msg = LIT("Failed to decode sound file"); } diff --git a/src/space.c b/src/space.c index 113a6883..cbad7dea 100644 --- a/src/space.c +++ b/src/space.c @@ -1,6 +1,5 @@ #include "space.h" -#include "math.h" -#include "arena.h" +#include "base/base.h" #include "collider.h" /* FIXME: Default space entry & cell pointers to nil */ diff --git a/src/sprite.c b/src/sprite.c index e0e7f904..696d17a5 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -1,16 +1,10 @@ #include "sprite.h" -#include "arena.h" -#include "log.h" -#include "sys.h" +#include "base/base.h" +#include "sys/sys.h" +#include "ase/ase.h" #include "resource.h" -#include "ase.h" -#include "util.h" -#include "atomic.h" #include "app.h" -#include "gp.h" -#include "math.h" -#include "rand.h" -#include "snc.h" +#include "gp/gp.h" #include "watch.h" /* The evictor will begin evicting once cache usage is > threshold. diff --git a/src/sprite.h b/src/sprite.h index a3a71964..3b194b69 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -1,7 +1,7 @@ #ifndef SPRITE_H #define SPRITE_H -#include "util.h" +#include "base/base.h" struct sprite_sheet_span; struct sprite_sheet_slice_group; diff --git a/src/sys/sys.c b/src/sys/sys.c new file mode 100644 index 00000000..7a91b6cf --- /dev/null +++ b/src/sys/sys.c @@ -0,0 +1,6 @@ +#include "sys.h" + +#include "sys_snc.c" +#include "sys_sleep.c" +#include "sys_core.c" +#include "sys_log.c" diff --git a/src/sys/sys.h b/src/sys/sys.h new file mode 100644 index 00000000..048a6bd2 --- /dev/null +++ b/src/sys/sys.h @@ -0,0 +1,13 @@ +#ifndef SYS_H +#define SYS_H + +#include "../base/base.h" +#include "../app.h" +#include "../gstat.h" + +#include "sys_snc.h" +#include "sys_sleep.h" +#include "sys_core.h" +#include "sys_log.h" + +#endif diff --git a/src/sys/sys_core.c b/src/sys/sys_core.c new file mode 100644 index 00000000..c6f6fd5a --- /dev/null +++ b/src/sys/sys_core.c @@ -0,0 +1,5 @@ +#if PLATFORM_WINDOWS +# include "sys_core_win32.c" +#else +# error System core layer not implemented +#endif diff --git a/src/sys.h b/src/sys/sys_core.h similarity index 85% rename from src/sys.h rename to src/sys/sys_core.h index 8e784c19..9e8c1e59 100644 --- a/src/sys.h +++ b/src/sys/sys_core.h @@ -1,8 +1,3 @@ -#ifndef SYS_H -#define SYS_H - -struct snc_counter; - /* ========================== * * Wait * ========================== */ @@ -11,19 +6,11 @@ struct snc_counter; void sys_wait(volatile void *addr, void *cmp, u32 size, i64 timeout_ns); void sys_wake(void *addr, i32 count); -/* ========================== * - * Fiber - * ========================== */ - -#define SYS_MAX_FIBERS 4096 - -i16 sys_current_fiber_id(void); - /* ========================== * * Job * ========================== */ -/* Work pools contain their own worker threads with their own thread priority/affinity based on the intended context of the pool. */ + /* Work pools contain their own worker threads with their own thread priority/affinity based on the intended context of the pool. */ enum sys_pool { SYS_POOL_INHERIT = -1, @@ -59,44 +46,6 @@ typedef SYS_JOB_DEF(sys_job_func, job_data); void sys_run(i32 count, sys_job_func *func, void *sig, enum sys_pool pool_kind, enum sys_priority priority, struct snc_counter *counter); -/* ========================== * - * Scratch context - * ========================== */ - -#define SYS_SCRATCH_ARENAS_PER_CTX 2 - -struct sys_scratch_ctx { - struct arena *arenas[SYS_SCRATCH_ARENAS_PER_CTX]; -}; - -struct sys_scratch_ctx *sys_scratch_ctx_from_fiber_id(i16 fiber_id); - -/* ========================== * - * Memory - * ========================== */ - -/* Reserve a continuous block of virtual address space. Shouldn't be - * read / written to until memory is committed. - * NOTE: Guaranteed to be 16 byte aligned. */ -void *sys_memory_reserve(u64 size); - -/* Release a continuous block of virtual address space. Before releasing, - * decommit any committed memory within the reserved space to be safe. */ -void sys_memory_release(void *address); - -/* Commit a region of reserved address space to make it readable / writable */ -void *sys_memory_commit(void *address, u64 size); - -/* Decommit a region of committed memory (does not release the reserved - * address space) */ -void sys_memory_decommit(void *address, u64 size); - -/* Mark committed pages as readonly */ -void sys_memory_set_committed_readonly(void *address, u64 size); - -/* Mark committed pages as readable & writeable (default for committed memory) */ -void sys_memory_set_committed_readwrite(void *address, u64 size); - /* ========================== * * Time * ========================== */ @@ -381,19 +330,19 @@ struct sys_window_event_array { * create a Win32 window in one thread and process its messages on another. */ enum sys_window_settings_flags { - SYS_WINDOW_SETTINGS_FLAG_NONE = 0x00, - SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN = 0x01, + SYS_WINDOW_SETTINGS_FLAG_NONE = 0x00, + SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN = 0x01, /* NOTE: Both maximized and minimized can be true at the same time. This * means that the window was minimized from a maximized state, and will * restore to being maximized once it's un-minimized. */ - SYS_WINDOW_SETTINGS_FLAG_MAXIMIZED = 0x02, - SYS_WINDOW_SETTINGS_FLAG_MINIMIZED = 0x04 + SYS_WINDOW_SETTINGS_FLAG_MAXIMIZED = 0x02, + SYS_WINDOW_SETTINGS_FLAG_MINIMIZED = 0x04 }; enum sys_window_flags { - SYS_WINDOW_FLAG_NONE = 0x00, - SYS_WINDOW_FLAG_SHOWING = 0x02 + SYS_WINDOW_FLAG_NONE = 0x00, + SYS_WINDOW_FLAG_SHOWING = 0x02 }; /* sys_window_update_settings should be used when altering settings values */ @@ -502,8 +451,6 @@ void sys_set_clipboard_text(struct string str); struct string sys_get_clipboard_text(struct arena *arena); -void sys_true_rand(struct string b); - u32 sys_num_logical_processors(void); u32 sys_current_thread_id(void); @@ -533,5 +480,3 @@ void sys_panic(struct string msg); /* Must be defined by app */ void sys_app_startup(struct string args_str); - -#endif diff --git a/src/sys_win32.c b/src/sys/sys_core_win32.c similarity index 98% rename from src/sys_win32.c rename to src/sys/sys_core_win32.c index 6ba7caa9..3d8205df 100644 --- a/src/sys_win32.c +++ b/src/sys/sys_core_win32.c @@ -1,16 +1,3 @@ -#include "sys.h" -#include "memory.h" -#include "arena.h" -#include "app.h" -#include "string.h" -#include "atomic.h" -#include "log.h" -#include "math.h" -#include "util.h" -#include "uni.h" -#include "resource.h" -#include "gstat.h" - #pragma warning(push, 0) # define UNICODE # define WIN32_LEAN_AND_MEAN @@ -205,8 +192,6 @@ struct alignas(64) fiber { /* ---------------------------------------------------- */ /* -------------------- Cache line -------------------- */ /* ---------------------------------------------------- */ - struct sys_scratch_ctx scratch_ctx; /* 16 bytes */ - /* ---------------------------------------------------- */ sys_job_func *job_func; /* 08 bytes */ /* ---------------------------------------------------- */ void *job_sig; /* 08 bytes */ @@ -219,13 +204,12 @@ struct alignas(64) fiber { /* ---------------------------------------------------- */ struct yield_param *yield_param; /* 08 bytes */ /* ---------------------------------------------------- */ - u8 _pad3[8]; /* 08 bytes (padding) */ + u8 _pad3[24]; /* 24 bytes (padding) */ }; STATIC_ASSERT(sizeof(struct fiber) == 128); /* Padding validation (increase if necessary) */ STATIC_ASSERT(alignof(struct fiber) == 64); /* Avoid false sharing */ STATIC_ASSERT(offsetof(struct fiber, wake_lock) % 4 == 0); /* Atomic must be aligned */ -STATIC_ASSERT(SYS_MAX_FIBERS < I16_MAX); /* Max fibers should fit in fiber id */ struct alignas(64) worker_ctx { enum sys_pool pool_kind; @@ -338,7 +322,7 @@ GLOBAL struct { struct ticket_mutex fibers_lock; i16 num_fibers; struct arena *fiber_names_arena; - struct fiber fibers[SYS_MAX_FIBERS]; + struct fiber fibers[MAX_FIBERS]; /* Wait lists */ struct atomic64_padded waiter_wake_gen; @@ -528,7 +512,7 @@ INTERNAL void thread_wait_release(struct thread *thread) void sys_wait(volatile void *addr, void *cmp, u32 size, i64 timeout_ns) { - struct fiber *fiber = fiber_from_id(sys_current_fiber_id()); + struct fiber *fiber = fiber_from_id(FiberId()); i16 parent_id = fiber->parent_id; if (parent_id != 0) { *fiber->yield_param = (struct yield_param) { @@ -853,7 +837,7 @@ INTERNAL struct fiber *fiber_alloc(struct job_pool *pool) { { fiber_id = G.num_fibers++; - if (fiber_id >= SYS_MAX_FIBERS) { + if (fiber_id >= MAX_FIBERS) { sys_panic(LIT("Max fibers reached")); } fiber = &G.fibers[fiber_id]; @@ -909,14 +893,6 @@ INTERNAL struct fiber *fiber_alloc(struct job_pool *pool) __profn("ConvertThreadToFiber"); fiber->addr = ConvertThreadToFiber((void *)(i64)fiber_id); } - - /* Init scratch context */ - { - __profn("Initialize scratch context"); - for (u32 i = 0; i < countof(fiber->scratch_ctx.arenas); ++i) { - fiber->scratch_ctx.arenas[i] = arena_alloc(GIBI(64)); - } - } } fiber->wait_addr = 0; fiber->wait_time = 0; @@ -962,11 +938,6 @@ INTERNAL FORCE_NO_INLINE void fiber_resume(struct fiber *fiber) MemoryBarrier(); } -i16 sys_current_fiber_id(void) -{ - return (i16)(i64)GetFiberData(); -} - /* ========================== * * Job * ========================== */ @@ -974,7 +945,7 @@ i16 sys_current_fiber_id(void) INTERNAL void job_fiber_yield(struct fiber *fiber, struct fiber *parent_fiber) { (UNUSED)fiber; - ASSERT(fiber->id == sys_current_fiber_id()); + ASSERT(fiber->id == FiberId()); ASSERT(parent_fiber->id == fiber->parent_id); ASSERT(parent_fiber->id > 0); { @@ -1025,7 +996,7 @@ void sys_run(i32 count, sys_job_func *func, void *sig, enum sys_pool pool_kind, if (counter) { snc_counter_add(counter, count); } - struct fiber *fiber = fiber_from_id(sys_current_fiber_id()); + struct fiber *fiber = fiber_from_id(FiberId()); priority = clamp_i32(priority, fiber->job_priority, NUM_SYS_PRIORITIES - 1); /* A job cannot create a job with a higher priority than itself */ if (pool_kind == SYS_POOL_INHERIT) { pool_kind = fiber->job_pool; @@ -1127,7 +1098,7 @@ INTERNAL THREAD_DEF(job_worker_entry, worker_ctx_arg) } } - i32 worker_fiber_id = sys_current_fiber_id(); + i32 worker_fiber_id = FiberId(); struct fiber *job_fiber = 0; b32 shutdown = 0; @@ -1466,54 +1437,6 @@ INTERNAL THREAD_DEF(job_scheduler_entry, _) } } -/* ========================== * - * Scratch context - * ========================== */ - -struct sys_scratch_ctx *sys_scratch_ctx_from_fiber_id(i16 id) -{ - struct fiber *fiber = fiber_from_id(id); - return &fiber->scratch_ctx; -} - -/* ========================== * - * Memory - * ========================== */ - -void *sys_memory_reserve(u64 size) -{ - void *ptr = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS); - return ptr; -} - -void sys_memory_release(void *address) -{ - VirtualFree(address, 0, MEM_RELEASE); -} - -void *sys_memory_commit(void *address, u64 size) -{ - void *ptr = VirtualAlloc(address, size, MEM_COMMIT, PAGE_READWRITE); - return ptr; -} - -void sys_memory_decommit(void *address, u64 size) -{ - VirtualFree(address, size, MEM_DECOMMIT); -} - -void sys_memory_set_committed_readonly(void *address, u64 size) -{ - DWORD old; - VirtualProtect(address, size, PAGE_READONLY, &old); -} - -void sys_memory_set_committed_readwrite(void *address, u64 size) -{ - DWORD old; - VirtualProtect(address, size, PAGE_READWRITE, &old); -} - /* ========================== * * Time * ========================== */ @@ -3238,11 +3161,6 @@ struct string sys_get_clipboard_text(struct arena *arena) return res; } -void sys_true_rand(struct string b) -{ - BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, (PUCHAR)b.text, b.len, 0); -} - u32 sys_num_logical_processors(void) { return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); diff --git a/src/log.c b/src/sys/sys_log.c similarity index 72% rename from src/log.c rename to src/sys/sys_log.c index 143b4e67..3c8e18bb 100644 --- a/src/log.c +++ b/src/sys/sys_log.c @@ -1,9 +1,3 @@ -#include "arena.h" -#include "log.h" -#include "string.h" -#include "atomic.h" -#include "snc.h" - struct log_event_callback { log_event_callback_func *func; struct log_event_callback *next; @@ -14,7 +8,7 @@ struct log_event_callback { * Global state * ========================== */ -GLOBAL struct { +struct shared_log_ctx { struct atomic32 initialized; struct snc_mutex callbacks_mutex; @@ -24,9 +18,10 @@ GLOBAL struct { struct sys_file file; b32 file_valid; -} G = ZI, DEBUG_ALIAS(G, G_log); +}; -GLOBAL READONLY struct log_level_settings g_log_level_settings[LOG_LEVEL_COUNT] = { +GLOBAL struct shared_log_ctx g_shared_log_ctx = ZI; +GLOBAL READONLY struct log_level_settings g_log_settings[LOG_LEVEL_COUNT] = { [LOG_LEVEL_CRITICAL] = { LIT_NOCAST("CRITICAL"), COLOR_PURPLE @@ -65,17 +60,18 @@ GLOBAL READONLY struct log_level_settings g_log_level_settings[LOG_LEVEL_COUNT] void log_startup(struct string logfile_path) { __prof; - G.callbacks_arena = arena_alloc(MEBI(8)); + struct shared_log_ctx *ctx = &g_shared_log_ctx; + ctx->callbacks_arena = arena_alloc(MEBI(8)); if (logfile_path.len > 0) { /* Create / wipe log file */ sys_file_close(sys_file_open_write(logfile_path)); /* Keep log file open for appending */ if (sys_is_file(logfile_path)) { - G.file = sys_file_open_append(logfile_path); - G.file_valid = 1; + ctx->file = sys_file_open_append(logfile_path); + ctx->file_valid = 1; } } - atomic32_fetch_set(&G.initialized, 1); + atomic32_fetch_set(&ctx->initialized, 1); } /* ========================== * @@ -84,18 +80,19 @@ void log_startup(struct string logfile_path) void log_register_callback(log_event_callback_func *func, i32 level) { - if (!atomic32_fetch(&G.initialized)) { return; } - struct snc_lock lock = snc_lock_e(&G.callbacks_mutex); + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } + struct snc_lock lock = snc_lock_e(&ctx->callbacks_mutex); { - struct log_event_callback *callback = arena_push(G.callbacks_arena, struct log_event_callback); + struct log_event_callback *callback = arena_push(ctx->callbacks_arena, struct log_event_callback); callback->func = func; callback->level = level; - if (G.last_callback) { - G.last_callback->next = callback; + if (ctx->last_callback) { + ctx->last_callback->next = callback; } else { - G.first_callback = callback; + ctx->first_callback = callback; } - G.last_callback = callback; + ctx->last_callback = callback; } snc_unlock(&lock); } @@ -107,12 +104,13 @@ void log_register_callback(log_event_callback_func *func, i32 level) INTERNAL void append_to_logfile(struct string msg) { __prof; - if (!atomic32_fetch(&G.initialized)) { return; } + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } - if (G.file_valid) { + if (ctx->file_valid) { struct arena_temp scratch = scratch_begin_no_conflict(); struct string msg_line = string_cat(scratch.arena, msg, LIT("\n")); - sys_file_write(G.file, msg_line); + sys_file_write(ctx->file, msg_line); scratch_end(scratch); } } @@ -121,12 +119,13 @@ INTERNAL void append_to_logfile(struct string msg) * writing to log file. */ void _log_panic(struct string msg) { - if (!atomic32_fetch(&G.initialized)) { return; } + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } - if (G.file_valid) { - sys_file_write(G.file, LIT("******** PANICKING ********\n")); - sys_file_write(G.file, msg); - sys_file_write(G.file, LIT("\n***************************\n")); + if (ctx->file_valid) { + sys_file_write(ctx->file, LIT("******** PANICKING ********\n")); + sys_file_write(ctx->file, msg); + sys_file_write(ctx->file, LIT("\n***************************\n")); } } @@ -137,7 +136,8 @@ void _log(i32 level, struct string msg) #endif { __prof; - if (!atomic32_fetch(&G.initialized)) { return; } + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } if (level < 0 || level >= LOG_LEVEL_COUNT) { sys_panic(LIT("Invalid log level")); @@ -150,7 +150,7 @@ void _log(i32 level, struct string msg) u32 tid = sys_current_thread_id(); - struct log_level_settings settings = g_log_level_settings[level]; + struct log_level_settings settings = g_log_settings[level]; struct string shorthand = settings.shorthand; #if LOG_INCLUDE_SOURCE_LOCATION @@ -214,8 +214,8 @@ void _log(i32 level, struct string msg) event.line = line; #endif { - struct snc_lock lock = snc_lock_s(&G.callbacks_mutex); - for (struct log_event_callback *callback = G.first_callback; callback; callback = callback->next) { + struct snc_lock lock = snc_lock_s(&ctx->callbacks_mutex); + for (struct log_event_callback *callback = ctx->first_callback; callback; callback = callback->next) { if (level <= callback->level) { __profn("Run log callback"); callback->func(event); @@ -233,7 +233,8 @@ void _logfv(i32 level, struct string file, u32 line, struct string fmt, va_list void _logfv(i32 level, struct string fmt, va_list args) #endif { - if (!atomic32_fetch(&G.initialized)) { return; } + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } struct arena_temp scratch = scratch_begin_no_conflict(); struct string msg = string_formatv(scratch.arena, fmt, args); #if LOG_INCLUDE_SOURCE_LOCATION @@ -250,7 +251,8 @@ void _logf(i32 level, struct string file, u32 line, struct string fmt, ...) void _logf(i32 level, struct string fmt, ...) #endif { - if (!atomic32_fetch(&G.initialized)) { return; } + struct shared_log_ctx *ctx = &g_shared_log_ctx; + if (!atomic32_fetch(&ctx->initialized)) { return; } va_list args; va_start(args, fmt); #if LOG_INCLUDE_SOURCE_LOCATION diff --git a/src/log.h b/src/sys/sys_log.h similarity index 98% rename from src/log.h rename to src/sys/sys_log.h index 75c6fba2..53a632f6 100644 --- a/src/log.h +++ b/src/sys/sys_log.h @@ -1,9 +1,3 @@ -#ifndef LOG_H -#define LOG_H - -#include "string.h" -#include "sys.h" - #define LOG_LEVEL(l) (l <= LOG_LEVEL_COMPTIME) /* Log level configuration */ @@ -165,5 +159,3 @@ void _logf(i32 level, struct string file, u32 line, struct string fmt, ...); #else void _logf(i32 level, struct string fmt, ...); #endif - -#endif diff --git a/src/sys/sys_sleep.c b/src/sys/sys_sleep.c new file mode 100644 index 00000000..c4613ac7 --- /dev/null +++ b/src/sys/sys_sleep.c @@ -0,0 +1,39 @@ +void sys_sleep_precise(i64 sleep_time_ns) +{ + __prof; + + i64 big_sleep = sys_current_scheduler_period_ns(); + i64 tolerance = (f64)big_sleep * 0.5; + //i64 tolerance = 1000000000; + + i64 now_ns = sys_time_ns(); + i64 target_ns = now_ns + sleep_time_ns; + + /* Sleep */ + while (now_ns < target_ns - big_sleep - tolerance) { + __profn("Sleep part"); + sys_wait(0, 0, 0, big_sleep); + now_ns = sys_time_ns(); + } + + /* Spin */ + { + __profn("Sleep spin"); + while (now_ns < target_ns) { + ix_pause(); + now_ns = sys_time_ns(); + } + } +} + +void sys_sleep_frame(i64 last_frame_time_ns, i64 target_dt_ns) +{ + if (last_frame_time_ns != 0 && target_dt_ns > 0) { + i64 now_ns = sys_time_ns(); + i64 last_frame_dt_ns = now_ns - last_frame_time_ns; + i64 sleep_time_ns = target_dt_ns - last_frame_dt_ns; + if (sleep_time_ns > 0) { + sys_sleep_precise(sleep_time_ns); + } + } +} diff --git a/src/sys/sys_sleep.h b/src/sys/sys_sleep.h new file mode 100644 index 00000000..b807c12e --- /dev/null +++ b/src/sys/sys_sleep.h @@ -0,0 +1,3 @@ +void sys_sleep_precise(i64 sleep_time_ns); + +void sys_sleep_frame(i64 last_frame_time_ns, i64 target_dt_ns); diff --git a/src/snc.c b/src/sys/sys_snc.c similarity index 96% rename from src/snc.c rename to src/sys/sys_snc.c index f78fcd26..f1611ec4 100644 --- a/src/snc.c +++ b/src/sys/sys_snc.c @@ -1,9 +1,3 @@ -#include "snc.h" -#include "atomic.h" -#include "sys.h" -#include "memory.h" -#include "intrinsics.h" - #define DEFAULT_MUTEX_SPIN 4000 /* ========================== * @@ -52,7 +46,7 @@ struct snc_lock snc_lock_spin_e(struct snc_mutex *m, i32 spin) } #if RTC - atomic32_fetch_set(&m->exclusive_fiber_id, sys_current_fiber_id()); + atomic32_fetch_set(&m->exclusive_fiber_id, FiberId()); #endif struct snc_lock lock = ZI; diff --git a/src/snc.h b/src/sys/sys_snc.h similarity index 98% rename from src/snc.h rename to src/sys/sys_snc.h index 8ac39461..0c0154a4 100644 --- a/src/snc.h +++ b/src/sys/sys_snc.h @@ -1,6 +1,3 @@ -#ifndef SNC_H -#define SNC_H - /* ========================== * * Mutex * ========================== */ @@ -69,5 +66,3 @@ STATIC_ASSERT(alignof(struct snc_counter) == 64); /* Prevent false sharing */ void snc_counter_add(struct snc_counter *counter, i64 x); void snc_counter_wait(struct snc_counter *counter); - -#endif diff --git a/src/tar/tar.c b/src/tar/tar.c new file mode 100644 index 00000000..7badbbf0 --- /dev/null +++ b/src/tar/tar.c @@ -0,0 +1,3 @@ +#include "tar.h" + +#include "tar_core.c" diff --git a/src/tar/tar.h b/src/tar/tar.h new file mode 100644 index 00000000..8d5ff52c --- /dev/null +++ b/src/tar/tar.h @@ -0,0 +1,9 @@ +#ifndef TAR_H +#define TAR_H + +#include "../base/base.h" +#include "../sys/sys.h" + +#include "tar_core.h" + +#endif diff --git a/src/tar.c b/src/tar/tar_core.c similarity index 98% rename from src/tar.c rename to src/tar/tar_core.c index 3ca91d72..049304b4 100644 --- a/src/tar.c +++ b/src/tar/tar_core.c @@ -1,8 +1,3 @@ -#include "tar.h" -#include "bitbuff.h" -#include "string.h" -#include "util.h" - #define ARCHIVE_LOOKUP_TABLE_CAPACITY_FACTOR 2.0 /* File types: diff --git a/src/tar.h b/src/tar/tar_core.h similarity index 89% rename from src/tar.h rename to src/tar/tar_core.h index 1b9aef94..54909512 100644 --- a/src/tar.h +++ b/src/tar/tar_core.h @@ -1,8 +1,3 @@ -#ifndef TAR_H -#define TAR_H - -#include "util.h" - struct tar_entry { b32 valid; struct string file_name; @@ -20,5 +15,3 @@ struct tar_archive { struct tar_archive tar_parse(struct arena *arena, struct string data, struct string prefix); struct tar_entry *tar_get(struct tar_archive *archive, struct string name); - -#endif diff --git a/src/ttf/ttf.cpp b/src/ttf/ttf.cpp new file mode 100644 index 00000000..0d0b0acc --- /dev/null +++ b/src/ttf/ttf.cpp @@ -0,0 +1,10 @@ +extern "C" +{ +#include "ttf.h" +} + +#if PLATFORM_WINDOWS +# include "ttf_core_dwrite.cpp" +#else +# error TTF core not implemented for this platform +#endif diff --git a/src/ttf/ttf.h b/src/ttf/ttf.h new file mode 100644 index 00000000..eb453b45 --- /dev/null +++ b/src/ttf/ttf.h @@ -0,0 +1,8 @@ +#ifndef TTF_H +#define TTF_H + +#include "../base/base.h" + +#include "ttf_core.h" + +#endif diff --git a/src/ttf.h b/src/ttf/ttf_core.h similarity index 89% rename from src/ttf.h rename to src/ttf/ttf_core.h index d6134c16..2355085a 100644 --- a/src/ttf.h +++ b/src/ttf/ttf_core.h @@ -1,8 +1,3 @@ -#ifndef TTF_H -#define TTF_H - -#include "util.h" - struct font_glyph; struct ttf_decode_result { @@ -15,5 +10,3 @@ struct ttf_decode_result { struct ttf_startup_receipt { i32 _; }; struct ttf_startup_receipt ttf_startup(void); struct ttf_decode_result ttf_decode(struct arena *arena, struct string encoded, f32 point_size, u32 *cache_codes, u32 cache_codes_count); - -#endif diff --git a/src/ttf_dwrite.cpp b/src/ttf/ttf_core_dwrite.cpp similarity index 98% rename from src/ttf_dwrite.cpp rename to src/ttf/ttf_core_dwrite.cpp index c16d2808..f514fc1b 100644 --- a/src/ttf_dwrite.cpp +++ b/src/ttf/ttf_core_dwrite.cpp @@ -3,12 +3,7 @@ extern "C" { -#include "ttf.h" -#include "util.h" -#include "string.h" -#include "memory.h" -#include "arena.h" -#include "font.h" +#include "../font.h" } #pragma warning(push, 0) @@ -68,7 +63,9 @@ struct ttf_startup_receipt ttf_startup(void) # pragma clang diagnostic pop #endif if (error != S_OK) { - sys_panic(LIT("Error creating DWrite factory")); + /* FIXME: Enable this */ + //sys_panic(LIT("Error creating DWrite factory")); + (*(volatile int *)0) = 0; } return { 0 }; diff --git a/src/user/user.c b/src/user/user.c new file mode 100644 index 00000000..204c2f4a --- /dev/null +++ b/src/user/user.c @@ -0,0 +1,3 @@ +#include "user.h" + +#include "user_core.c" diff --git a/src/user/user.h b/src/user/user.h new file mode 100644 index 00000000..3ecb1b64 --- /dev/null +++ b/src/user/user.h @@ -0,0 +1,9 @@ +#ifndef USER_H +#define USER_H + +#include "../base/base.h" +#include "../sim/sim.h" + +#include "user_core.h" + +#endif diff --git a/src/user.c b/src/user/user_core.c similarity index 99% rename from src/user.c rename to src/user/user_core.c index 6fbe1b2b..2959ee43 100644 --- a/src/user.c +++ b/src/user/user_core.c @@ -1,23 +1,15 @@ #include "user.h" -#include "sim.h" -#include "sim_ent.h" -#include "sim_step.h" -#include "gp.h" +#include "base/base.h" +#include "sys/sys.h" +#include "gp/gp.h" +#include "sim/sim.h" #include "font.h" #include "sprite.h" #include "draw.h" -#include "intrinsics.h" #include "asset_cache.h" -#include "string.h" -#include "math.h" -#include "sys.h" #include "mixer.h" -#include "atomic.h" #include "collider.h" -#include "rand.h" -#include "log.h" #include "host.h" -#include "bitbuff.h" #include "gstat.h" #include "app.h" @@ -2115,7 +2107,7 @@ INTERNAL SYS_JOB_DEF(user_update_job, _) } { __profn("Frame limiter wait"); - sleep_frame(time_ns, 1000000000 / FPS_LIMIT); + sys_sleep_frame(time_ns, 1000000000 / FPS_LIMIT); time_ns = sys_time_ns(); } } @@ -2280,7 +2272,7 @@ INTERNAL SYS_JOB_DEF(local_sim_job, _) struct arena_temp scratch = scratch_begin_no_conflict(); { __profn("Sim sleep"); - sleep_frame(real_time_ns, step_dt_ns * compute_timescale); + sys_sleep_frame(real_time_ns, step_dt_ns * compute_timescale); } { __profn("Sim update"); diff --git a/src/user.h b/src/user/user_core.h similarity index 97% rename from src/user.h rename to src/user/user_core.h index 9867ade5..3bb6eecf 100644 --- a/src/user.h +++ b/src/user/user_core.h @@ -1,7 +1,5 @@ -#ifndef USER_H -#define USER_H - -#include "sys.h" +#include "base/base.h" +#include "sys/sys.h" struct font_startup_receipt; struct sprite_startup_receipt; @@ -71,5 +69,3 @@ struct user_startup_receipt user_startup(struct font_startup_receipt *font_sr, struct host_startup_receipt *host_sr, struct sim_startup_receipt *sim_sr, struct string connect_address_str); - -#endif diff --git a/src/util.c b/src/util.c deleted file mode 100644 index e5be98fd..00000000 --- a/src/util.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "util.h" - -/* ========================== * - * String utils - * ========================== */ - -#if 0 -struct string util_file_name_from_path(struct string path) -{ - u8 *start = path.text; - u8 *end = path.text + path.len; - - /* Find file type start pos */ - u8 *type_start = end + 1; - for (u8 *c = end; c >= start; --c) { - if (*c == '.') { - type_start = c; - break; - } - } - - /* Find filename start pos */ - u8 *filename_start = start; - for (u8 *c = type_start - 1; c >= start; --c) { - if (*c == '/') { - filename_start = c; - break; - } - } - - return (struct string) { - .text = filename_start, - .len = type_start - filename_start - 1 - }; -} -#endif diff --git a/src/watch.c b/src/watch.c index 8cf7a74a..adaa68b9 100644 --- a/src/watch.c +++ b/src/watch.c @@ -2,12 +2,8 @@ #if RESOURCE_RELOADING -#include "sys.h" -#include "snc.h" -#include "arena.h" -#include "atomic.h" -#include "util.h" -#include "string.h" +#include "base/base.h" +#include "sys/sys.h" struct watch_event { struct string name;