ogl_beamforming

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

main_linux.c (2963B)


      1 /* See LICENSE for license details. */
      2 #ifndef __linux__
      3 #error This file is only meant to be compiled for Linux
      4 #endif
      5 
      6 #include "beamformer.h"
      7 
      8 #include "os_linux.c"
      9 
     10 #define OS_DEBUG_LIB_NAME      "./beamformer.so"
     11 #define OS_DEBUG_LIB_TEMP_NAME "./beamformer_temp.so"
     12 
     13 #define OS_CUDA_LIB_NAME       "./external/cuda_toolkit.so"
     14 #define OS_CUDA_LIB_TEMP_NAME  "./external/cuda_toolkit_temp.so"
     15 
     16 #define OS_RENDERDOC_SONAME    "librenderdoc.so"
     17 
     18 #define OS_SMEM_NAME           "/ogl_beamformer_shared_memory"
     19 
     20 #define OS_PATH_SEPERATOR      "/"
     21 
     22 #include "static.c"
     23 
     24 static void
     25 dispatch_file_watch_events(OS *os, Arena arena)
     26 {
     27 	FileWatchContext *fwctx = &os->file_watch_context;
     28 	u8 *mem     = alloc_(&arena, 4096, 64, 1);
     29 	Stream path = stream_alloc(&arena, 256);
     30 	struct inotify_event *event;
     31 
     32 	iz rlen;
     33 	while ((rlen = read(fwctx->handle, mem, 4096)) > 0) {
     34 		for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) {
     35 			event = (struct inotify_event *)data;
     36 			for (u32 i = 0; i < fwctx->directory_watch_count; i++) {
     37 				FileWatchDirectory *dir = fwctx->directory_watches + i;
     38 				if (event->wd != dir->handle)
     39 					continue;
     40 
     41 				s8  file = c_str_to_s8(event->name);
     42 				u64 hash = s8_hash(file);
     43 				for (u32 i = 0; i < dir->file_watch_count; i++) {
     44 					FileWatch *fw = dir->file_watches + i;
     45 					if (fw->hash == hash) {
     46 						stream_append_s8(&path, dir->name);
     47 						stream_append_byte(&path, '/');
     48 						stream_append_s8(&path, file);
     49 						stream_append_byte(&path, 0);
     50 						stream_commit(&path, -1);
     51 						fw->callback(os, stream_to_s8(&path),
     52 						             fw->user_data, arena);
     53 						stream_reset(&path, 0);
     54 						break;
     55 					}
     56 				}
     57 			}
     58 		}
     59 	}
     60 }
     61 
     62 int
     63 main(void)
     64 {
     65 	BeamformerCtx   ctx   = {0};
     66 	BeamformerInput input = {.executable_reloaded = 1};
     67 	Arena temp_memory = os_alloc_arena((Arena){0}, MB(16));
     68 	ctx.error_stream  = stream_alloc(&temp_memory, MB(1));
     69 
     70 	ctx.ui_backing_store        = sub_arena(&temp_memory, MB(2), KB(4));
     71 	ctx.os.compute_worker.arena = sub_arena(&temp_memory, MB(2), KB(4));
     72 
     73 	#define X(name) ctx.os.name = os_ ## name;
     74 	OS_FNS
     75 	#undef X
     76 
     77 	ctx.os.file_watch_context.handle = inotify_init1(O_NONBLOCK|O_CLOEXEC);
     78 	ctx.os.compute_worker.asleep     = 1;
     79 	ctx.os.stderr                    = STDERR_FILENO;
     80 
     81 	debug_init(&ctx.os, (iptr)&input, &temp_memory);
     82 	setup_beamformer(&ctx, &temp_memory);
     83 	os_wake_waiters(&ctx.os.compute_worker.sync_variable);
     84 
     85 	struct pollfd fds[1] = {{0}};
     86 	fds[0].fd     = ctx.os.file_watch_context.handle;
     87 	fds[0].events = POLLIN;
     88 
     89 	while (!ctx.should_exit) {
     90 		poll(fds, 2, 0);
     91 		if (fds[0].revents & POLLIN)
     92 			dispatch_file_watch_events(&ctx.os, temp_memory);
     93 
     94 		input.last_mouse = input.mouse;
     95 		input.mouse.rl   = GetMousePosition();
     96 
     97 		beamformer_frame_step(&ctx, &temp_memory, &input);
     98 
     99 		input.executable_reloaded = 0;
    100 	}
    101 
    102 	/* NOTE: make sure this will get cleaned up after external
    103 	 * programs release their references */
    104 	shm_unlink(OS_SMEM_NAME);
    105 }