starfield

starfield.git
git clone git://git.lenczewski.org/starfield.git
Log | Files | Refs | Submodules | README | LICENSE

commit 48e5f70fd4904cb9b6c9b7b4251e304aa870cdc2
parent 11b0825dd77182992237c4e189c5a7fdec73cc95
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Thu,  9 May 2024 00:36:48 +0000

Move all windows state into platform state object

Diffstat:
Acommon/starfield_api_inlines.c | 7+++++++
Mplatform/win_starfield.c | 125+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mplatform/win_starfield.h | 22+++++++++++++---------
3 files changed, 94 insertions(+), 60 deletions(-)

diff --git a/common/starfield_api_inlines.c b/common/starfield_api_inlines.c @@ -0,0 +1,7 @@ +#include "starfield_api.h" + +extern inline void +arena_reset(struct starfield_arena *arena); + +extern inline void +arena_alloc(struct starfield_arena *arena, u64 size, u64 alignment); diff --git a/platform/win_starfield.c b/platform/win_starfield.c @@ -5,84 +5,125 @@ internal struct win_starfield_state state; int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) { + /* TODO: parse out commandline arguments for debug behaviour? */ (void) instance; (void) prev_instance; (void) cmdline; (void) cmdshow; - struct platform_api platform_api = load_platform_api(); + /* NOTE: the windows dll loader checks the parent directory of our + * instance when loading libraries, so no need to do anything fancy + */ + char starfield_library_path[] = "starfield.dll"; + + if (!load_starfield_api(starfield_library_path, &state.starfield_api)) { + fprintf(stderr, "Failed to load starfield library: %s\n", starfield_library_path); + _exit(1); + } + + state.platform_api = load_platform_api(); struct starfield_thread thread = { .id = 0, }; - struct starfield_memory memory; - if (!init_starfield_memory(256 * MiB, 256 * MiB, &memory)) { - fprintf(stderr, "Failed to allocate starfield memory\n"); + if (!init_memory(&state.platform_memory, 64 * MiB, 64 * MiB)) { + fprintf(stderr, "Failed to allocate platform memory\n"); _exit(1); } - /* NOTE: the windows dll loader checks the parent directory of our - * instance when loading libraries, so no need to do anything fancy - */ - char starfield_library_path[] = "starfield.dll"; - - struct starfield_api starfield_api; - if (!load_starfield_api(starfield_library_path, &starfield_api)) { - fprintf(stderr, "Failed to load starfield library: %s\n", starfield_library_path); + if (!init_memory(&state.starfield_memory, 64 * MiB, 64 * MiB)) { + fprintf(stderr, "Failed to allocate starfield memory\n"); _exit(1); } - starfield_api.init(&thread, &memory, &platform_api); + state.starfield_api.init(&thread, &state.starfield_memory, &state.platform_api); state.running = 1; - while (state.running) { - f32 dt = 0; + f32 dt = 0; + while (state.running) { struct starfield_input input = { 0, }; - starfield_api.update(&thread, &memory, &platform_api, &input, dt); + state.starfield_api.update(&thread, &state.starfield_memory, + &state.platform_api, &input, dt); struct starfield_video_buffer video = { 0, }; - starfield_api.render(&thread, &memory, &platform_api, &video, dt); + state.starfield_api.render(&thread, &state.starfield_memory, + &state.platform_api, &video, dt); struct starfield_audio_buffer audio = { 0, }; - starfield_api.sample(&thread, &memory, &platform_api, &audio, dt); + state.starfield_api.sample(&thread, &state.starfield_memory, + &state.platform_api, &audio, dt); Sleep(1000); } - starfield_api.free(&thread, &memory, &platform_api); + state.starfield_api.free(&thread, &state.starfield_memory, &state.platform_api); _exit(0); } -b32 -init_starfield_memory(u64 permanent_memory_cap, u64 temporary_memory_cap, - struct starfield_memory *memory) +#define LOAD_SYMBOL(loc, T, sym) \ + ((loc) = (T *) GetProcAddress(library, sym)) + +internal b32 +load_starfield_api(char const *filename, struct starfield_api *api) { -#if STARFIELD_DEBUG + HMODULE library = LoadLibraryA(filename); + if (!library) return false; + + if (!LOAD_SYMBOL(api->init, starfield_api_init_t, "starfield_init")) + return false; + + if (!LOAD_SYMBOL(api->update, starfield_api_update_t, "starfield_update")) + return false; + + if (!LOAD_SYMBOL(api->render, starfield_api_render_t, "starfield_render")) + return false; + + if (!LOAD_SYMBOL(api->sample, starfield_api_sample_t, "starfield_sample")) + return false; + + if (!LOAD_SYMBOL(api->free, starfield_api_free_t, "starfield_free")) + return false; + + return true; +} + +internal b32 +init_memory(struct starfield_memory *memory, u64 permanent_memory_cap, u64 temporary_memory_cap) +{ +#ifdef STARFIELD_DEBUG void *base_addr = (void *) (2 * TiB); #else void *base_addr = NULL; #endif - u64 len = permanent_memory_cap + temporary_memory_cap; + u64 huge_page_alignment = GetLargePageMinimum(); + u64 len = ALIGN_NEXT(permanent_memory_cap + temporary_memory_cap, huge_page_alignment); /* TODO: add hugetlb support? * https://learn.microsoft.com/en-us/windows/win32/memory/large-page-support */ - void *ptr = VirtualAlloc(base_addr, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + DWORD alloc_type = MEM_RESERVE | MEM_COMMIT; + void *ptr = VirtualAlloc(base_addr, len, + alloc_type | MEM_LARGE_PAGES, + PAGE_READWRITE); + + if (!ptr) + ptr = VirtualAlloc(base_addr, len, alloc_type, PAGE_READWRITE); - if (!ptr) return false; + if (!ptr) + return false; /* NOTE: windows guarantees that the memory returned by VirtualAlloc * is zero-ed, so no need to memset it here @@ -99,30 +140,6 @@ init_starfield_memory(u64 permanent_memory_cap, u64 temporary_memory_cap, return true; } -b32 -load_starfield_api(char const *filename, struct starfield_api *api) -{ - HMODULE library = LoadLibraryA(filename); - if (!library) return false; - - if (!(api->init = (starfield_api_init_t *) GetProcAddress(library, "starfield_init"))) - return false; - - if (!(api->update = (starfield_api_update_t *) GetProcAddress(library, "starfield_update"))) - return false; - - if (!(api->render = (starfield_api_render_t *) GetProcAddress(library, "starfield_render"))) - return false; - - if (!(api->sample = (starfield_api_sample_t *) GetProcAddress(library, "starfield_sample"))) - return false; - - if (!(api->free = (starfield_api_free_t *) GetProcAddress(library, "starfield_free"))) - return false; - - return true; -} - /* platform implementation * =========================================================================== */ @@ -152,7 +169,7 @@ DEBUG_PLATFORM_FREE_FILE_MEMORY(DEBUG_platform_free_file_memory) #endif -struct platform_api +internal struct platform_api load_platform_api(void) { return (struct platform_api) { @@ -163,3 +180,9 @@ load_platform_api(void) #endif }; } + +/* utils + * =========================================================================== + */ + +#include "starfield_api_inlines.c" diff --git a/platform/win_starfield.h b/platform/win_starfield.h @@ -1,28 +1,32 @@ #ifndef WIN_STARFIELD_H #define WIN_STARFIELD_H -#include "starfield_api.h" - #include <stdio.h> #include <windows.h> +#include "starfield_api.h" + struct starfield_thread { u32 id; }; struct win_starfield_state { + struct starfield_memory platform_memory, starfield_memory; + + struct platform_api platform_api; + struct starfield_api starfield_api; + b32 running; }; -struct platform_api -load_platform_api(void); +internal b32 +load_starfield_api(char const *filename, struct starfield_api *api); -b32 -init_starfield_memory(u64 permanent_memory_cap, u64 temporary_memory_cap, - struct starfield_memory *memory); +internal struct platform_api +load_platform_api(void); -b32 -load_starfield_api(char const *filename, struct starfield_api *api); +internal b32 +init_memory(struct starfield_memory *memory, u64 permanent_memory_cap, u64 temporary_memory_cap); #endif /* WIN_STARFIELD_H */