ogl_beamforming

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

main_linux.c (3038B)


      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_PATH_SEPERATOR      "/"
     19 
     20 /* TODO(rnp): what do if not X11? */
     21 iptr glfwGetGLXContext(iptr);
     22 
     23 function iptr
     24 os_get_native_gl_context(iptr window)
     25 {
     26 	return glfwGetGLXContext(window);
     27 }
     28 
     29 #include "static.c"
     30 
     31 function void
     32 dispatch_file_watch_events(OS *os, Arena arena)
     33 {
     34 	FileWatchContext *fwctx = &os->file_watch_context;
     35 	u8 *mem     = arena_alloc(&arena, 4096, 16, 1);
     36 	Stream path = stream_alloc(&arena, 256);
     37 	struct inotify_event *event;
     38 
     39 	iz rlen;
     40 	while ((rlen = read(fwctx->handle, mem, 4096)) > 0) {
     41 		for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) {
     42 			event = (struct inotify_event *)data;
     43 			for (u32 i = 0; i < fwctx->count; i++) {
     44 				FileWatchDirectory *dir = fwctx->data + i;
     45 				if (event->wd != dir->handle)
     46 					continue;
     47 
     48 				s8  file = c_str_to_s8(event->name);
     49 				u64 hash = s8_hash(file);
     50 				for (u32 i = 0; i < dir->count; i++) {
     51 					FileWatch *fw = dir->data + i;
     52 					if (fw->hash == hash) {
     53 						stream_append_s8s(&path, dir->name, s8("/"), file);
     54 						stream_append_byte(&path, 0);
     55 						stream_commit(&path, -1);
     56 						fw->callback(os, stream_to_s8(&path),
     57 						             fw->user_data, arena);
     58 						stream_reset(&path, 0);
     59 						break;
     60 					}
     61 				}
     62 			}
     63 		}
     64 	}
     65 }
     66 
     67 int
     68 main(void)
     69 {
     70 	BeamformerCtx   ctx   = {0};
     71 	BeamformerInput input = {.executable_reloaded = 1};
     72 	Arena temp_memory = os_alloc_arena((Arena){0}, MB(16));
     73 	ctx.error_stream  = stream_alloc(&temp_memory, MB(1));
     74 
     75 	ctx.ui_backing_store        = sub_arena(&temp_memory, MB(2), KB(4));
     76 	ctx.os.compute_worker.arena = sub_arena(&temp_memory, MB(2), KB(4));
     77 
     78 	#define X(name) ctx.os.name = os_ ## name;
     79 	OS_FNS
     80 	#undef X
     81 
     82 	ctx.os.file_watch_context.handle = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
     83 	ctx.os.compute_worker.asleep     = 1;
     84 	ctx.os.stderr                    = STDERR_FILENO;
     85 	ctx.os.export_pipe_name          = OS_EXPORT_PIPE_NAME;
     86 
     87 	debug_init(&ctx.os, (iptr)&input, &temp_memory);
     88 	setup_beamformer(&ctx, &temp_memory);
     89 	os_wake_waiters(&ctx.os.compute_worker.sync_variable);
     90 
     91 	struct pollfd fds[1] = {{0}};
     92 	fds[0].fd     = ctx.os.file_watch_context.handle;
     93 	fds[0].events = POLLIN;
     94 
     95 	while (!ctx.should_exit) {
     96 		poll(fds, countof(fds), 0);
     97 		if (fds[0].revents & POLLIN)
     98 			dispatch_file_watch_events(&ctx.os, temp_memory);
     99 
    100 		input.last_mouse = input.mouse;
    101 		input.mouse.rl   = GetMousePosition();
    102 
    103 		beamformer_frame_step(&ctx, &temp_memory, &input);
    104 
    105 		input.executable_reloaded = 0;
    106 	}
    107 
    108 	/* NOTE: make sure this will get cleaned up after external
    109 	 * programs release their references */
    110 	shm_unlink(OS_SHARED_MEMORY_NAME);
    111 }