ogl_beamforming

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

Commit: c51178dd60f8705325e767a0c88954672a07e318
Parent: 27acc14d5d7b249501eb686a9aca46bac4af6bab
Author: Randy Palamar
Date:   Tue, 12 Nov 2024 21:38:35 -0700

move pipe polling out of beamformer

ideally we keep as many platform specific paths out of the
beamfomer as possible.

Diffstat:
Mbeamformer.c | 8+++-----
Mbeamformer.h | 7++++---
Mmain_generic.c | 22+++++++++++++++++++---
Mos_unix.c | 2+-
Mos_win32.c | 2+-
Mstatic.c | 25+++----------------------
Mutil.h | 9++++++---
7 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/beamformer.c b/beamformer.c @@ -586,8 +586,7 @@ check_compute_timers(ComputeShaderCtx *cs, PartialComputeCtx *pc, BeamformerPara #include "ui.c" -DEBUG_EXPORT void -do_beamformer(BeamformerCtx *ctx, Arena *arena) +DEBUG_EXPORT BEAMFORMER_FRAME_STEP_FN(beamformer_frame_step) { dt_for_frame = GetFrameTime(); @@ -601,8 +600,7 @@ do_beamformer(BeamformerCtx *ctx, Arena *arena) BeamformerParameters *bp = &ctx->params->raw; /* NOTE: Check for and Load RF Data into GPU */ - /* TODO: move pipe polling out of the beamformer */ - if (ctx->platform.poll_pipe(ctx->data_pipe)) { + if (input->pipe_data_available) { BeamformWork *work = beamform_work_queue_push(ctx, arena, BW_FULL_COMPUTE); /* NOTE: we can only read in the new data if we get back a work item. * otherwise we have too many frames in flight and should wait until the @@ -642,7 +640,7 @@ do_beamformer(BeamformerCtx *ctx, Arena *arena) alloc_output_image(ctx, bp->output_points); - size rlen = ctx->platform.read_pipe(ctx->data_pipe, rf_data_buf, rf_raw_size); + size rlen = ctx->platform.read_pipe(input->pipe_handle, rf_data_buf, rf_raw_size); if (rlen != rf_raw_size) { stream_append_s8(&ctx->error_stream, s8("Partial Read Occurred: ")); stream_append_i64(&ctx->error_stream, rlen); diff --git a/beamformer.h b/beamformer.h @@ -263,9 +263,6 @@ typedef struct BeamformerCtx { Arena export_buffer; - Pipe data_pipe; - u32 partial_transfer_count; - CudaLib cuda_lib; Platform platform; Stream error_stream; @@ -277,4 +274,8 @@ typedef struct BeamformerCtx { #define LABEL_GL_OBJECT(type, id, s) {s8 _s = (s); glObjectLabel(type, id, _s.len, (c8 *)_s.data);} +#define BEAMFORMER_FRAME_STEP_FN(name) void name(BeamformerCtx *ctx, Arena *arena, \ + BeamformerInput *input) +typedef BEAMFORMER_FRAME_STEP_FN(beamformer_frame_step_fn); + #endif /*_BEAMFORMER_H_ */ diff --git a/main_generic.c b/main_generic.c @@ -33,10 +33,15 @@ int main(void) { - BeamformerCtx ctx = {0}; + BeamformerCtx ctx = {0}; + BeamformerInput input = {0}; Arena temp_memory = os_alloc_arena((Arena){0}, 16 * MEGABYTE); ctx.error_stream = stream_alloc(&temp_memory, 1 * MEGABYTE); + Pipe data_pipe = os_open_named_pipe(OS_PIPE_NAME); + input.pipe_handle = data_pipe.file; + ASSERT(data_pipe.file == INVALID_FILE); + #define X(name) ctx.platform.name = os_ ## name; PLATFORM_FNS #undef X @@ -44,7 +49,18 @@ main(void) setup_beamformer(&ctx, temp_memory); while(!(ctx.flags & SHOULD_EXIT)) { - do_program_step(&ctx, &temp_memory); + do_debug(&ctx.error_stream); + if (ctx.gl.vendor_id == GL_VENDOR_NVIDIA) + check_and_load_cuda_lib(&ctx.cuda_lib, &ctx.error_stream); + + if (ctx.flags & RELOAD_SHADERS) { + ctx.flags &= ~RELOAD_SHADERS; + reload_shaders(&ctx, temp_memory); + } + + input.pipe_data_available = os_poll_pipe(data_pipe); + + beamformer_frame_step(&ctx, &temp_memory, &input); } /* NOTE: make sure this will get cleaned up after external @@ -52,5 +68,5 @@ main(void) os_remove_shared_memory(OS_SMEM_NAME); /* NOTE: garbage code needed for Linux */ - os_close_named_pipe(ctx.data_pipe); + os_close_named_pipe(data_pipe); } diff --git a/os_unix.c b/os_unix.c @@ -138,7 +138,7 @@ static PLATFORM_READ_PIPE_FN(os_read_pipe) do { if (r != -1) total_read += r; - r = read(p.file, buf + total_read, len - total_read); + r = read(pipe, buf + total_read, len - total_read); } while (r); return total_read; } diff --git a/os_win32.c b/os_win32.c @@ -218,7 +218,7 @@ static PLATFORM_POLL_PIPE_FN(os_poll_pipe) static PLATFORM_READ_PIPE_FN(os_read_pipe) { i32 total_read = 0; - ReadFile(p.file, buf, len, &total_read, 0); + ReadFile(pipe, buf, len, &total_read, 0); return total_read; } diff --git a/static.c b/static.c @@ -20,9 +20,7 @@ static struct { #else static void *debug_lib; -/* TODO: move this to a header */ -typedef void do_beamformer_fn(BeamformerCtx *, Arena *); -static do_beamformer_fn *do_beamformer; +static beamformer_frame_step_fn *beamformer_frame_step; static void do_debug(Stream *error_stream) @@ -32,7 +30,7 @@ do_debug(Stream *error_stream) if (test_stats.filesize > 32 && test_stats.timestamp > updated_time) { os_unload_library(debug_lib); debug_lib = os_load_library(OS_DEBUG_LIB_NAME, OS_DEBUG_LIB_TEMP_NAME, error_stream); - do_beamformer = os_lookup_dynamic_symbol(debug_lib, "do_beamformer", error_stream); + beamformer_frame_step = os_lookup_dynamic_symbol(debug_lib, "beamformer_frame_step", error_stream); updated_time = test_stats.timestamp; } } @@ -275,10 +273,8 @@ setup_beamformer(BeamformerCtx *ctx, Arena temp_memory) ctx->fsctx.db = -50.0f; ctx->fsctx.threshold = 40.0f; - ctx->data_pipe = os_open_named_pipe(OS_PIPE_NAME); - ctx->params = os_open_shared_memory_area(OS_SMEM_NAME, sizeof(*ctx->params)); + ctx->params = os_open_shared_memory_area(OS_SMEM_NAME, sizeof(*ctx->params)); /* TODO: properly handle this? */ - ASSERT(ctx->data_pipe.file != INVALID_FILE); ASSERT(ctx->params); /* NOTE: default compute shader pipeline */ @@ -307,18 +303,3 @@ setup_beamformer(BeamformerCtx *ctx, Arena temp_memory) reload_shaders(ctx, temp_memory); } - -static void -do_program_step(BeamformerCtx *ctx, Arena *memory) -{ - do_debug(&ctx->error_stream); - if (ctx->gl.vendor_id == GL_VENDOR_NVIDIA) - check_and_load_cuda_lib(&ctx->cuda_lib, &ctx->error_stream); - - if (ctx->flags & RELOAD_SHADERS) { - ctx->flags &= ~RELOAD_SHADERS; - reload_shaders(ctx, *memory); - } - - do_beamformer(ctx, memory); -} diff --git a/util.h b/util.h @@ -171,9 +171,8 @@ typedef PLATFORM_CLOSE_FN(platform_close_fn); typedef PLATFORM_OPEN_FOR_WRITE_FN(platform_open_for_write_fn); #define PLATFORM_POLL_PIPE_FN(name) b32 name(Pipe p) -typedef PLATFORM_POLL_PIPE_FN(platform_poll_pipe_fn); -#define PLATFORM_READ_PIPE_FN(name) size name(Pipe p, void *buf, size len) +#define PLATFORM_READ_PIPE_FN(name) size name(iptr pipe, void *buf, size len) typedef PLATFORM_READ_PIPE_FN(platform_read_pipe_fn); #define PLATFORM_WRITE_NEW_FILE_FN(name) b32 name(char *fname, s8 raw) @@ -186,7 +185,6 @@ typedef PLATFORM_WRITE_FILE_FN(platform_write_file_fn); X(alloc_arena) \ X(close) \ X(open_for_write) \ - X(poll_pipe) \ X(read_pipe) \ X(write_new_file) \ X(write_file) @@ -195,6 +193,11 @@ typedef PLATFORM_WRITE_FILE_FN(platform_write_file_fn); typedef struct { PLATFORM_FNS } Platform; #undef X +typedef struct { + b32 pipe_data_available; + iptr pipe_handle; +} BeamformerInput; + #include "util.c" #endif /* _UTIL_H_ */