ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

Commit: 34818d846b483c0f013258250bd902cd3e71f384
Parent: 2541769f8cfe626ab38228f16b6b7df70f8c2ba7
Author: Randy Palamar
Date:   Fri, 21 Mar 2025 20:30:07 -0600

os: remove os_write_err_msg

This gets rid of a pointless global variable in w32 build.

Diffstat:
Mmain_w32.c | 10+++++-----
Mos_unix.c | 8+-------
Mos_win32.c | 15+++------------
Mstatic.c | 27++++++++++++++++++---------
4 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/main_w32.c b/main_w32.c @@ -49,11 +49,11 @@ dispatch_file_watch(Platform *platform, FileWatchDirectory *fw_dir, u8 *buf, Are Stream path = {.data = arena_commit(&arena, KB(1)), .cap = KB(1)}; if (fni->action != FILE_ACTION_MODIFIED) { - stream_reset(&path, 0); stream_append_s8(&path, s8("unknown file watch event: ")); stream_append_u64(&path, fni->action); stream_append_byte(&path, '\n'); - os_write_err_msg(stream_to_s8(&path)); + platform->write_file(platform->error_file_handle, stream_to_s8(&path)); + stream_reset(&path, 0); } stream_append_s8(&path, fw_dir->name); @@ -104,7 +104,7 @@ clear_io_queue(Platform *platform, BeamformerInput *input, Arena arena) } static b32 -poll_pipe(Pipe *p, Stream *e) +poll_pipe(Pipe *p, Stream *e, Platform *platform) { u8 data; i32 total_read = 0; @@ -125,7 +125,7 @@ poll_pipe(Pipe *p, Stream *e) stream_append_s8(e, s8("poll_pipe: failed to reopen pipe: error: ")); stream_append_i64(e, GetLastError()); stream_append_byte(e, '\n'); - os_write_err_msg(stream_to_s8(e)); + platform->write_file(platform->error_file_handle, stream_to_s8(e)); stream_reset(e, 0); } } @@ -169,7 +169,7 @@ main(void) input.last_mouse = input.mouse; input.mouse.rl = GetMousePosition(); - input.pipe_data_available = poll_pipe(&data_pipe, &ctx.error_stream); + input.pipe_data_available = poll_pipe(&data_pipe, &ctx.error_stream, &ctx.platform); beamformer_frame_step(&ctx, &temp_memory, &input); diff --git a/os_unix.c b/os_unix.c @@ -39,16 +39,10 @@ static PLATFORM_WRITE_FILE_FN(os_write_file) return 1; } -static void -os_write_err_msg(s8 msg) -{ - os_write_file(STDERR_FILENO, msg); -} - static void __attribute__((noreturn)) os_fatal(s8 msg) { - os_write_err_msg(msg); + os_write_file(STDERR_FILENO, msg); _exit(1); unreachable(); } diff --git a/os_win32.c b/os_win32.c @@ -137,20 +137,10 @@ static PLATFORM_WRITE_FILE_FN(os_write_file) return raw.len == wlen; } -/* TODO(rnp): cleanup callers of this function they should route through error file handle instead */ -static iptr win32_stderr_handle; -static void -os_write_err_msg(s8 msg) -{ - if (!win32_stderr_handle) - win32_stderr_handle = GetStdHandle(STD_ERROR_HANDLE); - os_write_file(win32_stderr_handle, msg); -} - static void __attribute__((noreturn)) os_fatal(s8 msg) { - os_write_err_msg(msg); + os_write_file(GetStdHandle(STD_ERROR_HANDLE), msg); ExitProcess(1); unreachable(); } @@ -221,7 +211,8 @@ static PLATFORM_READ_FILE_FN(os_read_file) static PLATFORM_WRITE_NEW_FILE_FN(os_write_new_file) { if (raw.len > (size)U32_MAX) { - os_write_err_msg(s8("os_write_file: files >4GB are not yet handled on win32\n")); + os_write_file(GetStdHandle(STD_ERROR_HANDLE), + s8("os_write_file: files >4GB are not yet handled on win32\n")); return 0; } diff --git a/static.c b/static.c @@ -34,7 +34,7 @@ static FILE_WATCH_CALLBACK_FN(debug_reload) #undef X stream_append_s8(&err, s8("Reloaded Main Executable\n")); - os_write_err_msg(stream_to_s8(&err)); + platform->write_file(platform->error_file_handle, stream_to_s8(&err)); input->executable_reloaded = 1; @@ -62,19 +62,25 @@ debug_init(Platform *p, iptr input, Arena *arena) } } - os_write_err_msg(stream_to_s8(&err)); + p->write_file(p->error_file_handle, stream_to_s8(&err)); } #endif /* _DEBUG */ #define static_path_join(a, b) (a OS_PATH_SEPERATOR b) +struct gl_debug_ctx { + Stream stream; + Platform *platform; +}; + static void gl_debug_logger(u32 src, u32 type, u32 id, u32 lvl, i32 len, const char *msg, const void *userctx) { (void)src; (void)type; (void)id; - Stream *e = (Stream *)userctx; + struct gl_debug_ctx *ctx = (struct gl_debug_ctx *)userctx; + Stream *e = &ctx->stream; stream_append_s8(e, s8("[GL DEBUG ")); switch (lvl) { case GL_DEBUG_SEVERITY_HIGH: stream_append_s8(e, s8("HIGH]: ")); break; @@ -85,7 +91,7 @@ gl_debug_logger(u32 src, u32 type, u32 id, u32 lvl, i32 len, const char *msg, co } stream_append(e, (char *)msg, len); stream_append_byte(e, '\n'); - os_write_err_msg(stream_to_s8(e)); + ctx->platform->write_file(ctx->platform->error_file_handle, stream_to_s8(e)); stream_reset(e, 0); } @@ -147,7 +153,7 @@ validate_gl_requirements(GLParams *gl, Arena a) } static void -dump_gl_params(GLParams *gl, Arena a) +dump_gl_params(GLParams *gl, Arena a, Platform *p) { (void)gl; (void)a; #ifdef _DEBUG @@ -174,7 +180,7 @@ dump_gl_params(GLParams *gl, Arena a) stream_append_s8(&s, s8("\nMax Server Wait Time [ns]: ")); stream_append_i64(&s, gl->max_server_wait_time); stream_append_s8(&s, s8("\n-----------------------\n")); - os_write_err_msg(stream_to_s8(&s)); + p->write_file(p->error_file_handle, stream_to_s8(&s)); #endif } @@ -224,7 +230,7 @@ static FILE_WATCH_CALLBACK_FN(load_cuda_lib) CUDA_LIB_FNS #undef X - os_write_err_msg(stream_to_s8(&err)); + platform->write_file(platform->error_file_handle, stream_to_s8(&err)); } #define X(name) if (!cl->name) cl->name = name ## _stub; @@ -272,7 +278,7 @@ setup_beamformer(BeamformerCtx *ctx, Arena *memory) /* NOTE: Gather information about the GPU */ get_gl_params(&ctx->gl, &ctx->error_stream); - dump_gl_params(&ctx->gl, *memory); + dump_gl_params(&ctx->gl, *memory, &ctx->platform); validate_gl_requirements(&ctx->gl, *memory); glfwWindowHint(GLFW_VISIBLE, 0); @@ -313,7 +319,10 @@ setup_beamformer(BeamformerCtx *ctx, Arena *memory) } /* NOTE: set up OpenGL debug logging */ - glDebugMessageCallback(gl_debug_logger, &ctx->error_stream); + struct gl_debug_ctx *gl_debug_ctx = push_struct(memory, typeof(*gl_debug_ctx)); + gl_debug_ctx->stream = stream_alloc(memory, 1024); + gl_debug_ctx->platform = &ctx->platform; + glDebugMessageCallback(gl_debug_logger, gl_debug_ctx); #ifdef _DEBUG glEnable(GL_DEBUG_OUTPUT); #endif