ogl_beamforming

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

Commit: 8df38a8297cc287afa08cbf225030a40424f58ab
Parent: 40edd1d34976d60d77e0c21e87d58363f3f8330c
Author: Randy Palamar
Date:   Thu, 31 Oct 2024 10:24:35 -0600

use error stream in few missed places

Diffstat:
Mos_unix.c | 27+++++++++++++--------------
Mos_win32.c | 15++++++++-------
Mstatic.c | 38++++++++++++++++++--------------------
Mui.c | 22+++++++++++-----------
Mutil.c | 9++++-----
5 files changed, 54 insertions(+), 57 deletions(-)

diff --git a/os_unix.c b/os_unix.c @@ -8,13 +8,6 @@ #include <sys/stat.h> #include <unistd.h> -static void __attribute__((noreturn)) -os_fail(void) -{ - _exit(1); - unreachable(); -} - static b32 os_write_file(iptr file, s8 raw) { @@ -29,7 +22,15 @@ os_write_file(iptr file, s8 raw) static void os_write_err_msg(s8 msg) { - write(STDERR_FILENO, msg.data, msg.len); + os_write_file(STDERR_FILENO, msg); +} + +static void __attribute__((noreturn)) +os_fatal(s8 msg) +{ + os_write_err_msg(msg); + _exit(1); + unreachable(); } static PLATFORM_ALLOC_ARENA_FN(os_alloc_arena) @@ -47,10 +48,8 @@ static PLATFORM_ALLOC_ARENA_FN(os_alloc_arena) munmap(old.beg, oldsize); result.beg = mmap(0, capacity, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - if (result.beg == MAP_FAILED) { - os_write_err_msg(s8("os_alloc_arena: couldn't allocate memory\n")); - os_fail(); - } + if (result.beg == MAP_FAILED) + os_fatal(s8("os_alloc_arena: couldn't allocate memory\n")); result.end = result.beg + capacity; return result; } @@ -201,7 +200,7 @@ os_load_library(char *name, char *temp_name, Stream *e) s8 errs[] = {s8("WARNING: os_load_library("), cstr_to_s8(name), s8("): "), cstr_to_s8(dlerror()), s8("\n")}; stream_append_s8_array(e, errs, ARRAY_COUNT(errs)); - os_write_err_msg(stream_to_s8(*e)); + os_write_err_msg(stream_to_s8(e)); e->widx = 0; } @@ -221,7 +220,7 @@ os_lookup_dynamic_symbol(void *h, char *name, Stream *e) s8 errs[] = {s8("WARNING: os_lookup_dynamic_symbol("), cstr_to_s8(name), s8("): "), cstr_to_s8(dlerror()), s8("\n")}; stream_append_s8_array(e, errs, ARRAY_COUNT(errs)); - os_write_err_msg(stream_to_s8(*e)); + os_write_err_msg(stream_to_s8(e)); e->widx = 0; } diff --git a/os_win32.c b/os_win32.c @@ -74,13 +74,6 @@ W32(b32) VirtualFree(u8 *, size, u32); static iptr win32_stderr_handle; -static void __attribute__((noreturn)) -os_fail(void) -{ - ExitProcess(1); - unreachable(); -} - static b32 os_write_file(iptr file, s8 raw) { @@ -97,6 +90,14 @@ os_write_err_msg(s8 msg) os_write_file(win32_stderr_handle, msg); } +static void __attribute__((noreturn)) +os_fatal(s8 msg) +{ + os_write_err_msg(msg); + ExitProcess(1); + unreachable(); +} + static PLATFORM_ALLOC_ARENA_FN(os_alloc_arena) { Arena result; diff --git a/static.c b/static.c @@ -53,28 +53,28 @@ gl_debug_logger(u32 src, u32 type, u32 id, u32 lvl, i32 len, const char *msg, co } stream_append_s8(e, (s8){.len = len, .data = (u8 *)msg}); stream_append_byte(e, '\n'); - os_write_err_msg(stream_to_s8(*e)); + os_write_err_msg(stream_to_s8(e)); e->widx = 0; } static void -get_gl_params(GLParams *gl) +get_gl_params(GLParams *gl, Stream *err) { char *vendor = (char *)glGetString(GL_VENDOR); if (!vendor) { - os_write_err_msg(s8("Failed to determine GL Vendor\n")); - os_fail(); + stream_append_s8(err, s8("Failed to determine GL Vendor\n")); + os_fatal(stream_to_s8(err)); } switch (vendor[0]) { case 'A': gl->vendor_id = GL_VENDOR_AMD; break; case 'I': gl->vendor_id = GL_VENDOR_INTEL; break; case 'N': gl->vendor_id = GL_VENDOR_NVIDIA; break; - default: { - os_write_err_msg(s8("Unknown GL Vendor: ")); - os_write_err_msg(cstr_to_s8(vendor)); - os_write_err_msg(s8("\n")); - os_fail(); - } break; + default: + stream_append_s8(err, s8("Unknown GL Vendor: ")); + stream_append_s8(err, cstr_to_s8(vendor)); + stream_append_byte(err, '\n'); + os_fatal(stream_to_s8(err)); + break; } glGetIntegerv(GL_MAJOR_VERSION, &gl->version_major); @@ -106,10 +106,8 @@ validate_gl_requirements(GLParams *gl) break; } - if (invalid) { - os_write_err_msg(s8("Only OpenGL Versions 4.5 or newer are supported!\n")); - os_fail(); - } + if (invalid) + os_fatal(s8("Only OpenGL Versions 4.5 or newer are supported!\n")); } static void @@ -138,7 +136,7 @@ dump_gl_params(GLParams *gl, Arena a) stream_append_i64(&s, gl->max_ubo_size); stream_append_s8(&s, s8("\n-----------------------\n")); if (!s.errors) - os_write_err_msg(stream_to_s8(s)); + os_write_err_msg(stream_to_s8(&s)); #endif } @@ -194,11 +192,11 @@ reload_shaders(BeamformerCtx *ctx, Arena a) FileStats fs = os_get_file_stats((char *)compute_shaders[i].path.data); s8 shader_text = os_read_file(&tmp, (char *)compute_shaders[i].path.data, fs.filesize); if (shader_text.len == -1) { - os_write_err_msg(s8("failed to read shader: ")); - os_write_err_msg(compute_shaders[i].path); - os_write_err_msg(s8("\n")); + stream_append_s8(&ctx->error_stream, s8("failed to read shader: ")); + stream_append_s8(&ctx->error_stream, compute_shaders[i].path); + stream_append_byte(&ctx->error_stream, '\n'); /* TODO: maybe we don't need to fail here */ - os_fail(); + os_fatal(stream_to_s8(&ctx->error_stream)); } /* NOTE: arena works as stack (since everything here is 1 byte aligned) */ if (compute_shaders[i].needs_header) { @@ -272,7 +270,7 @@ setup_beamformer(BeamformerCtx *ctx, Arena temp_memory) SetWindowMinSize(INFO_COLUMN_WIDTH * 2, ctx->window_size.h); /* NOTE: Gather information about the GPU */ - get_gl_params(&ctx->gl); + get_gl_params(&ctx->gl, &ctx->error_stream); dump_gl_params(&ctx->gl, temp_memory); validate_gl_requirements(&ctx->gl); diff --git a/ui.c b/ui.c @@ -350,9 +350,9 @@ do_value_listing(s8 prefix, s8 suffix, f32 value, Font font, Arena a, Rect r) stream_append_f64(&buf, value, 100); v2 txt_p = {.x = r.pos.x + LISTING_LEFT_COLUMN_WIDTH, .y = r.pos.y}; - draw_text(font, prefix, r.pos, 0, colour_from_normalized(FG_COLOUR)); - draw_text(font, stream_to_s8(buf), txt_p, 0, colour_from_normalized(FG_COLOUR)); - draw_text(font, suffix, suffix_p, 0, colour_from_normalized(FG_COLOUR)); + draw_text(font, prefix, r.pos, 0, colour_from_normalized(FG_COLOUR)); + draw_text(font, stream_to_s8(&buf), txt_p, 0, colour_from_normalized(FG_COLOUR)); + draw_text(font, suffix, suffix_p, 0, colour_from_normalized(FG_COLOUR)); r.pos.y += suffix_s.h + LISTING_LINE_PAD; r.size.y -= suffix_s.h + LISTING_LINE_PAD; @@ -371,7 +371,7 @@ do_text_input_listing(s8 prefix, s8 suffix, BPModifiableValue bmv, BeamformerCtx txt_s = measure_text(ctx->font, (s8){.len = ctx->is.buf_len, .data = ctx->is.buf}); } else { bmv_sprint(&bmv, &buf); - txt_s = measure_text(ctx->font, stream_to_s8(buf)); + txt_s = measure_text(ctx->font, stream_to_s8(&buf)); } Rect edit_rect = { @@ -404,7 +404,7 @@ do_text_input_listing(s8 prefix, s8 suffix, BPModifiableValue bmv, BeamformerCtx Color colour = colour_from_normalized(lerp_v4(FG_COLOUR, HOVERED_COLOUR, *hover_t)); if (!bmv_equal(&bmv, &ctx->is.store)) { - draw_text(ctx->font, stream_to_s8(buf), edit_rect.pos, 0, colour); + draw_text(ctx->font, stream_to_s8(&buf), edit_rect.pos, 0, colour); } else { do_text_input(ctx, 7, edit_rect, colour); } @@ -623,9 +623,9 @@ draw_debug_overlay(BeamformerCtx *ctx, Arena arena, Rect r) buf.widx = 0; stream_append_f64_e(&buf, cs->last_frame_time[index]); stream_append_s8(&buf, s8(" [s]")); - v2 txt_fs = measure_text(ctx->font, stream_to_s8(buf)); + v2 txt_fs = measure_text(ctx->font, stream_to_s8(&buf)); v2 rpos = {.x = r.pos.x + r.size.w - txt_fs.w, .y = pos.y}; - draw_text(ctx->font, stream_to_s8(buf), rpos, 0, colour_from_normalized(FG_COLOUR)); + draw_text(ctx->font, stream_to_s8(&buf), rpos, 0, colour_from_normalized(FG_COLOUR)); compute_time_sum += cs->last_frame_time[index]; } @@ -639,9 +639,9 @@ draw_debug_overlay(BeamformerCtx *ctx, Arena arena, Rect r) buf.widx = 0; stream_append_f64_e(&buf, times[i]); stream_append_s8(&buf, s8(" [s]")); - v2 txt_fs = measure_text(ctx->font, stream_to_s8(buf)); + v2 txt_fs = measure_text(ctx->font, stream_to_s8(&buf)); v2 rpos = {.x = r.pos.x + r.size.w - txt_fs.w, .y = pos.y}; - draw_text(ctx->font, stream_to_s8(buf), rpos, 0, colour_from_normalized(FG_COLOUR)); + draw_text(ctx->font, stream_to_s8(&buf), rpos, 0, colour_from_normalized(FG_COLOUR)); } { @@ -701,7 +701,7 @@ draw_ui(BeamformerCtx *ctx, Arena arena) Stream buf = stream_alloc(&arena, 64); stream_append_f64(&buf, -188.8f, 10); stream_append_s8(&buf, s8(" mm")); - v2 txt_s = measure_text(ctx->small_font, stream_to_s8(buf)); + v2 txt_s = measure_text(ctx->small_font, stream_to_s8(&buf)); rr.pos.x += 0.02 * rr.size.w; rr.pos.y += 0.02 * rr.size.h; @@ -783,7 +783,7 @@ draw_ui(BeamformerCtx *ctx, Arena arena) if (i == 0 && mm > 0) stream_append_byte(&buf, '+'); stream_append_f64(&buf, mm, 10); stream_append_s8(&buf, s8(" mm")); - draw_text(ctx->small_font, stream_to_s8(buf), txt_pos, + draw_text(ctx->small_font, stream_to_s8(&buf), txt_pos, rot[i], txt_colour); start_pos.E[i] += inc; end_pos.E[i] += inc; diff --git a/util.c b/util.c @@ -66,10 +66,9 @@ stream_alloc(Arena *a, size cap) } static s8 -stream_to_s8(Stream s) +stream_to_s8(Stream *s) { - ASSERT(!s.errors); - s8 result = {.len = s.widx, .data = s.data}; + s8 result = {.len = s->widx, .data = s->data}; return result; } @@ -86,8 +85,8 @@ stream_append_s8(Stream *s, s8 str) { s->errors |= (s->cap - s->widx) < str.len; if (!s->errors) { - for (size i = 0; i < str.len; i++) - s->data[s->widx++] = str.data[i]; + mem_copy(str.data, s->data + s->widx, str.len); + s->widx += str.len; } }