Commit: 56b8c6e685692d301c3340fa8eccc0d303d6e100
Parent: 0d7e5e3f8abcd482e616e918c38277e251503b01
Author: Randy Palamar
Date: Tue, 18 Jun 2024 16:04:05 -0600
add extra rf data buffers for decoding and uploading
Diffstat:
3 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/beamformer.c b/beamformer.c
@@ -3,6 +3,7 @@
#include <raylib.h>
#include <rlgl.h>
+#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
@@ -15,12 +16,12 @@
#include "util.c"
static void
-do_compute_shader(BeamformerCtx *ctx, enum compute_shaders shader)
+do_compute_shader(BeamformerCtx *ctx, u32 rf_ssbo_idx, enum compute_shaders shader)
{
ComputeShaderCtx *csctx = &ctx->csctx;
glUseProgram(csctx->programs[shader]);
- glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, csctx->rf_data_ssbo);
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, csctx->rf_data_ssbos[rf_ssbo_idx]);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ctx->out_data_ssbo);
glUniform3uiv(csctx->rf_data_dim_id, 1, csctx->rf_data_dim.E);
@@ -60,11 +61,16 @@ do_beamformer(BeamformerCtx *ctx, Arena arena, s8 rf_data)
scale.y *= -1.0;
}
- /* Load RF Data into GPU */
- glBindBuffer(GL_SHADER_STORAGE_BUFFER, ctx->csctx.rf_data_ssbo);
+ /* NOTE: grab operating idx and swap it; other buffer can now be used for storage */
+ u32 rf_ssbo_idx = atomic_fetch_xor_explicit(&ctx->csctx.rf_data_idx, 1, memory_order_relaxed);
+ ASSERT(rf_ssbo_idx == 0 || rf_ssbo_idx == 1);
+
+ /* NOTE: Load RF Data into GPU */
+ /* TODO: This should be done in a seperate thread */
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, ctx->csctx.rf_data_ssbos[!rf_ssbo_idx]);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, rf_data.len, rf_data.data);
- do_compute_shader(ctx, CS_UFORCES);
+ do_compute_shader(ctx, rf_ssbo_idx, CS_UFORCES);
BeginDrawing();
diff --git a/main.c b/main.c
@@ -110,7 +110,9 @@ init_compute_shader_ctx(ComputeShaderCtx *ctx, uv3 rf_data_dim)
ctx->rf_data_dim = rf_data_dim;
size rf_data_size = rf_data_dim.w * rf_data_dim.h * rf_data_dim.d * sizeof(f32);
- ctx->rf_data_ssbo = rlLoadShaderBuffer(rf_data_size, NULL, GL_DYNAMIC_COPY);
+ for (u32 i = 0; i < ARRAY_COUNT(ctx->rf_data_ssbos); i++)
+ ctx->rf_data_ssbos[i] = rlLoadShaderBuffer(rf_data_size, NULL, GL_DYNAMIC_COPY);
+ ctx->rf_data_idx = 0;
}
static void
diff --git a/util.c b/util.c
@@ -67,7 +67,14 @@ enum program_flags {
typedef struct {
u32 programs[CS_LAST];
- u32 rf_data_ssbo;
+ /* need 3 storage buffers: incoming rf data, currently decoding rf data, decoded data.
+ * last buffer will always be for decoded data. other two will swap everytime there
+ * is new data. current operating idx is stored in rf_data_idx (0 or 1) which needs
+ * to be accessed atomically
+ */
+ u32 rf_data_ssbos[3];
+ _Atomic u32 rf_data_idx;
+
uv3 rf_data_dim;
i32 rf_data_dim_id;
i32 out_data_dim_id;