ogl_beamforming

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

Commit: a85112dca0dd2c5509e71884b574ccfeccd9c743
Parent: 5ecd2554b77675bad84f81a6c9fb7e60907764bb
Author: Randy Palamar
Date:   Sat,  5 Jul 2025 20:19:35 -0600

lib: allow fully async dispatch

Diffstat:
Mhelpers/ogl_beamformer_lib.c | 39++++++++++++++++++++-------------------
Mhelpers/ogl_beamformer_lib_base.h | 3+++
Mtests/throughput.c | 8+++-----
3 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/helpers/ogl_beamformer_lib.c b/helpers/ogl_beamformer_lib.c @@ -120,12 +120,9 @@ lib_release_lock(BeamformerSharedMemoryLockKind lock) function b32 try_wait_sync(BeamformerSharedMemoryLockKind lock, i32 timeout_ms) { - b32 result = 0; - if (lib_try_lock(lock, 0) && lib_try_lock(lock, timeout_ms)) { - /* TODO(rnp): non-critical race condition */ - lib_release_lock(lock); - result = 1; - } + b32 result = lib_try_lock(lock, 0) && lib_try_lock(lock, timeout_ms); + /* TODO(rnp): non-critical race condition */ + if (result) lib_release_lock(lock); return result; } @@ -183,8 +180,19 @@ set_beamformer_pipeline(i32 *stages, i32 stages_count) b32 beamformer_start_compute(i32 timeout_ms) { - b32 result = check_shared_memory() && - try_wait_sync(BeamformerSharedMemoryLockKind_DispatchCompute, timeout_ms); + i32 lock = BeamformerSharedMemoryLockKind_DispatchCompute; + b32 result = check_shared_memory() && lib_try_lock(lock, timeout_ms); + return result; +} + +b32 +beamformer_wait_for_compute_dispatch(i32 timeout_ms) +{ + i32 lock = BeamformerSharedMemoryLockKind_DispatchCompute; + b32 result = check_shared_memory() && lib_try_lock(lock, timeout_ms); + /* NOTE(rnp): if you are calling this function you are probably about + * to start some other work and it might be better to not do this... */ + if (result) lib_release_lock(BeamformerSharedMemoryLockKind_DispatchCompute); return result; } @@ -266,11 +274,11 @@ beamformer_push_data_with_compute(void *data, u32 data_size, u32 image_plane_tag result = image_plane_tag < BeamformerViewPlaneTag_Count; if (result) { BeamformWork *work = try_push_work_queue(); - result = work != 0; - if (result) { + if (work) { work->kind = BeamformerWorkKind_ComputeIndirect; work->compute_indirect_plane = image_plane_tag; beamform_work_queue_push_commit(&g_bp->external_work_queue); + result = beamformer_start_compute(0); } } else { g_lib_last_error = BF_LIB_ERR_KIND_INVALID_IMAGE_PLANE; @@ -391,11 +399,8 @@ beamform_data_synchronized(void *data, u32 data_size, u32 output_points[3], f32 BeamformerExportContext export; export.kind = BeamformerExportKind_BeamformedData; export.size = output_size; - if (beamformer_export_buffer(export) && - lib_try_lock(BeamformerSharedMemoryLockKind_DispatchCompute, 0)) - { + if (beamformer_export_buffer(export) && beamformer_start_compute(0)) result = beamformer_read_output(out_data, output_size, timeout_ms); - } } else { g_lib_last_error = BF_LIB_ERR_KIND_EXPORT_SPACE_OVERFLOW; } @@ -412,12 +417,8 @@ beamformer_compute_timings(BeamformerComputeStatsTable *output, i32 timeout_ms) BeamformerExportContext export; export.kind = BeamformerExportKind_Stats; export.size = sizeof(*output); - - if (beamformer_export_buffer(export) && - lib_try_lock(BeamformerSharedMemoryLockKind_DispatchCompute, 0)) - { + if (beamformer_export_buffer(export) && beamformer_start_compute(0)) result = beamformer_read_output(output, sizeof(*output), timeout_ms); - } } return result; } diff --git a/helpers/ogl_beamformer_lib_base.h b/helpers/ogl_beamformer_lib_base.h @@ -46,6 +46,9 @@ LIB_FN uint32_t beamformer_compute_timings(BeamformerComputeStatsTable *output, /* NOTE: tells the beamformer to start beamforming and waits until it starts or for timeout_ms */ LIB_FN uint32_t beamformer_start_compute(int32_t timeout_ms); +/* NOTE: waits for previously queued beamform to start or for timeout_ms */ +LIB_FN uint32_t beamformer_wait_for_compute_dispatch(int32_t timeout_ms); + /* NOTE: these functions only queue an upload; you must flush (old data functions or start_compute) */ LIB_FN uint32_t beamformer_push_data(void *data, uint32_t size, int32_t timeout_ms); LIB_FN uint32_t beamformer_push_data_with_compute(void *data, uint32_t size, uint32_t image_plane_tag, int32_t timeout_ms); diff --git a/tests/throughput.c b/tests/throughput.c @@ -307,11 +307,9 @@ send_frame(i16 *restrict i16_data, BeamformerParameters *restrict bp) { b32 result = 0; u32 data_size = bp->rf_raw_dim[0] * bp->rf_raw_dim[1] * sizeof(i16); - - if (beamformer_push_data_with_compute(i16_data, data_size, BeamformerViewPlaneTag_XZ, 100)) - //if (beamformer_push_data(i16_data, data_size, 100)) - result = beamformer_start_compute(-1); - if (!result) printf("lib error: %s\n", beamformer_get_last_error_string()); + if (beamformer_wait_for_compute_dispatch(10000)) + result = beamformer_push_data_with_compute(i16_data, data_size, BeamformerViewPlaneTag_XZ, 100); + if (!result && !g_should_exit) printf("lib error: %s\n", beamformer_get_last_error_string()); return result; }