ogl_beamforming

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

Commit: 7aadc25e0c62338a7f934499d7869c4c1f425590
Parent: e8f8cf636dd1f81700dc2009c83dfd84989e95e1
Author: Randy Palamar
Date:   Mon, 17 Nov 2025 07:19:01 -0700

meta: make float bake parameter check into a bitfield

Diffstat:
Mbeamformer.c | 63+++++++++++++++++++++++++++++++--------------------------------
Mbuild.c | 54+++++++++++++++++++++++++-----------------------------
Mgenerated/beamformer.meta.c | 14+++++++-------
Mutil.c | 11+++++++++--
4 files changed, 72 insertions(+), 70 deletions(-)

diff --git a/beamformer.c b/beamformer.c @@ -748,41 +748,40 @@ load_compute_shader(BeamformerCtx *ctx, BeamformerComputePlan *cp, u32 shader_sl for (i32 index = 0; index < header_vector_length; index++) stream_append_s8(&shader_stream, beamformer_shader_global_header_strings[header_vector[index]]); - if (beamformer_shader_bake_parameter_counts[reloadable_index]) { - i32 count = beamformer_shader_bake_parameter_counts[reloadable_index]; - BeamformerShaderDescriptor *sd = cp->shader_descriptors + shader_slot; - - if (sd->layout.x != 0) { - stream_append_s8(&shader_stream, s8("layout(local_size_x = ")); - stream_append_u64(&shader_stream, sd->layout.x); - stream_append_s8(&shader_stream, s8(", local_size_y = ")); - stream_append_u64(&shader_stream, sd->layout.y); - stream_append_s8(&shader_stream, s8(", local_size_z = ")); - stream_append_u64(&shader_stream, sd->layout.z); - stream_append_s8(&shader_stream, s8(") in;\n\n")); - } + BeamformerShaderDescriptor *sd = cp->shader_descriptors + shader_slot; + + if (sd->layout.x != 0) { + stream_append_s8(&shader_stream, s8("layout(local_size_x = ")); + stream_append_u64(&shader_stream, sd->layout.x); + stream_append_s8(&shader_stream, s8(", local_size_y = ")); + stream_append_u64(&shader_stream, sd->layout.y); + stream_append_s8(&shader_stream, s8(", local_size_z = ")); + stream_append_u64(&shader_stream, sd->layout.z); + stream_append_s8(&shader_stream, s8(") in;\n\n")); + } - u32 *parameters = (u32 *)&sd->bake; - s8 *names = beamformer_shader_bake_parameter_names[reloadable_index]; - u8 *is_float = beamformer_shader_bake_parameter_is_float[reloadable_index]; - for (i32 index = 0; index < count; index++) { - stream_append_s8s(&shader_stream, s8("#define "), names[index], - is_float[index]? s8(" uintBitsToFloat") : s8(" "), s8("(0x")); - stream_append_hex_u64(&shader_stream, parameters[index]); - stream_append_s8(&shader_stream, s8(")\n")); - } + u32 *parameters = (u32 *)&sd->bake; + s8 *names = beamformer_shader_bake_parameter_names[reloadable_index]; + u32 float_bits = beamformer_shader_bake_parameter_float_bits[reloadable_index]; + i32 count = beamformer_shader_bake_parameter_counts[reloadable_index]; - stream_append_s8(&shader_stream, s8("#define DataKind (0x")); - stream_append_hex_u64(&shader_stream, sd->bake.data_kind); - stream_append_s8(&shader_stream, s8(")\n\n")); + for (i32 index = 0; index < count; index++) { + stream_append_s8s(&shader_stream, s8("#define "), names[index], + (float_bits & (1 << index))? s8(" uintBitsToFloat") : s8(" "), s8("(0x")); + stream_append_hex_u64(&shader_stream, parameters[index]); + stream_append_s8(&shader_stream, s8(")\n")); + } - s8 *flag_names = beamformer_shader_flag_strings[reloadable_index]; - u32 flag_count = beamformer_shader_flag_strings_count[reloadable_index]; - u32 flags = sd->bake.flags; - for (u32 bit = 0; bit < flag_count; bit++) { - stream_append_s8s(&shader_stream, s8("#define "), flag_names[bit], - (flags & (1 << bit))? s8(" 1") : s8(" 0"), s8("\n")); - } + stream_append_s8(&shader_stream, s8("#define DataKind (0x")); + stream_append_hex_u64(&shader_stream, sd->bake.data_kind); + stream_append_s8(&shader_stream, s8(")\n\n")); + + s8 *flag_names = beamformer_shader_flag_strings[reloadable_index]; + u32 flag_count = beamformer_shader_flag_strings_count[reloadable_index]; + u32 flags = sd->bake.flags; + for (u32 bit = 0; bit < flag_count; bit++) { + stream_append_s8s(&shader_stream, s8("#define "), flag_names[bit], + (flags & (1 << bit))? s8(" 1") : s8(" 0"), s8("\n")); } stream_append_s8(&shader_stream, s8("\n#line 1\n")); diff --git a/build.c b/build.c @@ -868,16 +868,17 @@ meta_push_(MetaprogramContext *m, s8 *items, iz count) stream_append_s8s_(&m->stream, items, count); } -#define meta_pad(m, b, n) stream_pad(&(m)->stream, (b), (n)) -#define meta_indent(m) meta_pad((m), '\t', (m)->indentation_level) -#define meta_begin_line(m, ...) do { meta_indent(m); meta_push(m, __VA_ARGS__); } while(0) -#define meta_end_line(m, ...) meta_push(m, ##__VA_ARGS__, s8("\n")) -#define meta_push_line(m, ...) do { meta_indent(m); meta_push(m, ##__VA_ARGS__, s8("\n")); } while(0) -#define meta_begin_scope(m, ...) do { meta_push_line(m, __VA_ARGS__); (m)->indentation_level++; } while(0) -#define meta_end_scope(m, ...) do { (m)->indentation_level--; meta_push_line(m, __VA_ARGS__); } while(0) -#define meta_push_u64(m, n) stream_append_u64(&(m)->stream, (n)) -#define meta_push_i64(m, n) stream_append_i64(&(m)->stream, (n)) -#define meta_push_u64_hex(m, n) stream_append_hex_u64(&(m)->stream, (n)) +#define meta_pad(m, b, n) stream_pad(&(m)->stream, (b), (n)) +#define meta_indent(m) meta_pad((m), '\t', (m)->indentation_level) +#define meta_begin_line(m, ...) do { meta_indent(m); meta_push(m, __VA_ARGS__); } while(0) +#define meta_end_line(m, ...) meta_push(m, ##__VA_ARGS__, s8("\n")) +#define meta_push_line(m, ...) do { meta_indent(m); meta_push(m, ##__VA_ARGS__, s8("\n")); } while(0) +#define meta_begin_scope(m, ...) do { meta_push_line(m, __VA_ARGS__); (m)->indentation_level++; } while(0) +#define meta_end_scope(m, ...) do { (m)->indentation_level--; meta_push_line(m, __VA_ARGS__); } while(0) +#define meta_push_u64(m, n) stream_append_u64(&(m)->stream, (n)) +#define meta_push_i64(m, n) stream_append_i64(&(m)->stream, (n)) +#define meta_push_u64_hex(m, n) stream_append_hex_u64(&(m)->stream, (n)) +#define meta_push_u64_hex_width(m, n, w) stream_append_hex_u64_width(&(m)->stream, (n), (w)) #define meta_begin_matlab_class_cracker(_1, _2, FN, ...) FN #define meta_begin_matlab_class_1(m, name) meta_begin_scope(m, s8("classdef " name)) @@ -1510,7 +1511,7 @@ typedef struct { typedef struct { s8 *names_upper; s8 *names_lower; - u8 *floating_point; + u32 floating_point; u32 entry_count; u32 shader_id; } MetaShaderBakeParameters; @@ -1734,15 +1735,17 @@ meta_pack_shader_bake_parameters(MetaContext *ctx, MetaEntry *e, iz entry_count, bp->entry_count++; } - bp->names_upper = push_array(ctx->arena, s8, bp->entry_count); - bp->names_lower = push_array(ctx->arena, s8, bp->entry_count); - bp->floating_point = push_array(ctx->arena, u8, bp->entry_count); + if (bp->entry_count > 32) + meta_entry_error(e, "maximum bake parameter count exceeded: limit: 32\n"); + + bp->names_upper = push_array(ctx->arena, s8, bp->entry_count); + bp->names_lower = push_array(ctx->arena, s8, bp->entry_count); u32 row_index = 0; for (MetaEntry *row = scope.start; row != scope.one_past_last; row++, row_index++) { - bp->names_upper[row_index] = row->arguments[0].string; - bp->names_lower[row_index] = row->arguments[1].string; - bp->floating_point[row_index] = row->kind == MetaEntryKind_BakeFloat; + bp->names_upper[row_index] = row->arguments[0].string; + bp->names_lower[row_index] = row->arguments[1].string; + bp->floating_point |= (row->kind == MetaEntryKind_BakeFloat) << row_index; } } @@ -2976,7 +2979,7 @@ metagen_emit_c_code(MetaContext *ctx, Arena arena) ctx->shader_names.data[s->name_id], s8("BakeParameters")); meta_begin_scope(m, s8("typedef struct {")); for (u32 entry = 0; entry < b->entry_count; entry++) { - s8 kind = b->floating_point[entry] ? s8("f32 ") : s8("u32 "); + s8 kind = (b->floating_point & (1 << entry))? s8("f32 ") : s8("u32 "); meta_push_line(m, kind, b->names_lower[entry], s8(";")); } meta_end_scope(m, s8("} "), name, s8(";\n")); @@ -3062,19 +3065,12 @@ metagen_emit_c_code(MetaContext *ctx, Arena arena) } meta_end_scope(m, s8("};\n")); - meta_begin_scope(m, s8("read_only global u8 *beamformer_shader_bake_parameter_is_float[] = {")); + meta_begin_scope(m, s8("read_only global u32 beamformer_shader_bake_parameter_float_bits[] = {")); for (iz shader = 0; shader < ctx->base_shaders.count; shader++) { MetaShader *s = ctx->base_shaders.data[shader].shader; - if (s->bake_parameters) { - meta_begin_line(m, s8("(u8 []){")); - for (u32 index = 0; index < s->bake_parameters->entry_count; index++) { - if (index != 0) meta_push(m, s8(", ")); - meta_push(m, s->bake_parameters->floating_point[index] ? s8("1") : s8("0")); - } - meta_end_line(m, s8("},")); - } else { - meta_push_line(m, s8("0,")); - } + meta_begin_line(m, s8("0x")); + meta_push_u64_hex_width(m, s->bake_parameters? s->bake_parameters->floating_point : 0, 8); + meta_end_line(m, s8("UL,")); } meta_end_scope(m, s8("};\n")); diff --git a/generated/beamformer.meta.c b/generated/beamformer.meta.c @@ -541,13 +541,13 @@ read_only global s8 *beamformer_shader_bake_parameter_names[] = { 0, }; -read_only global u8 *beamformer_shader_bake_parameter_is_float[] = { - (u8 []){0, 0, 0, 0, 0, 0, 0, 0, 0}, - (u8 []){0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, - (u8 []){0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, - 0, - 0, - 0, +read_only global u32 beamformer_shader_bake_parameter_float_bits[] = { + 0x00000000UL, + 0x00000600UL, + 0x00001fc0UL, + 0x00000000UL, + 0x00000000UL, + 0x00000000UL, }; read_only global i32 beamformer_shader_bake_parameter_counts[] = { diff --git a/util.c b/util.c @@ -298,8 +298,9 @@ stream_append_u64(Stream *s, u64 n) } function void -stream_append_hex_u64(Stream *s, u64 n) +stream_append_hex_u64_width(Stream *s, u64 n, iz width) { + assert(width <= 16); if (!s->errors) { u8 buf[16]; u8 *end = buf + sizeof(buf); @@ -308,13 +309,19 @@ stream_append_hex_u64(Stream *s, u64 n) *--beg = (u8)"0123456789abcdef"[n & 0x0F]; n >>= 4; } - while (end - beg < 2) + while (end - beg < width) *--beg = '0'; stream_append(s, beg, end - beg); } } function void +stream_append_hex_u64(Stream *s, u64 n) +{ + stream_append_hex_u64_width(s, n, 2); +} + +function void stream_append_i64(Stream *s, i64 n) { if (n < 0) {