ogl_beamforming

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

main_linux.c (3253B)


      1 /* See LICENSE for license details. */
      2 #include "compiler.h"
      3 
      4 #if !OS_LINUX
      5 #error This file is only meant to be compiled for Linux
      6 #endif
      7 
      8 #include "beamformer.h"
      9 
     10 #include "os_linux.c"
     11 
     12 #define OS_DEBUG_LIB_NAME      "./beamformer.so"
     13 #define OS_DEBUG_LIB_TEMP_NAME "./beamformer_temp.so"
     14 
     15 #define OS_CUDA_LIB_NAME       "./external/cuda_toolkit.so"
     16 #define OS_CUDA_LIB_TEMP_NAME  "./external/cuda_toolkit_temp.so"
     17 
     18 #define OS_RENDERDOC_SONAME    "librenderdoc.so"
     19 
     20 /* TODO(rnp): what do if not X11? */
     21 iptr glfwGetGLXContext(iptr);
     22 function iptr
     23 os_get_native_gl_context(iptr window)
     24 {
     25 	return glfwGetGLXContext(window);
     26 }
     27 
     28 iptr glfwGetProcAddress(char *);
     29 function iptr
     30 os_gl_proc_address(char *name)
     31 {
     32 	return glfwGetProcAddress(name);
     33 }
     34 
     35 #include "static.c"
     36 
     37 function void
     38 dispatch_file_watch_events(OS *os, Arena arena)
     39 {
     40 	FileWatchContext *fwctx = &os->file_watch_context;
     41 	u8 *mem     = arena_alloc(&arena, 4096, 16, 1);
     42 	Stream path = stream_alloc(&arena, 256);
     43 	struct inotify_event *event;
     44 
     45 	iz rlen;
     46 	while ((rlen = read(fwctx->handle, mem, 4096)) > 0) {
     47 		for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) {
     48 			event = (struct inotify_event *)data;
     49 			for (u32 i = 0; i < fwctx->count; i++) {
     50 				FileWatchDirectory *dir = fwctx->data + i;
     51 				if (event->wd != dir->handle)
     52 					continue;
     53 
     54 				s8  file = c_str_to_s8(event->name);
     55 				u64 hash = s8_hash(file);
     56 				for (u32 i = 0; i < dir->count; i++) {
     57 					FileWatch *fw = dir->data + i;
     58 					if (fw->hash == hash) {
     59 						stream_append_s8s(&path, dir->name, s8("/"), file);
     60 						stream_append_byte(&path, 0);
     61 						stream_commit(&path, -1);
     62 						fw->callback(os, stream_to_s8(&path),
     63 						             fw->user_data, arena);
     64 						stream_reset(&path, 0);
     65 						break;
     66 					}
     67 				}
     68 			}
     69 		}
     70 	}
     71 }
     72 
     73 extern i32
     74 main(void)
     75 {
     76 	BeamformerCtx   ctx   = {0};
     77 	BeamformerInput input = {.executable_reloaded = 1};
     78 	Arena temp_memory = os_alloc_arena(MB(16));
     79 	ctx.error_stream  = stream_alloc(&temp_memory, MB(1));
     80 
     81 	ctx.ui_backing_store        = sub_arena(&temp_memory, MB(2), KB(4));
     82 	ctx.os.compute_worker.arena = sub_arena(&temp_memory, MB(2), KB(4));
     83 
     84 	#define X(name) ctx.os.name = os_ ## name;
     85 	OS_FNS
     86 	#undef X
     87 
     88 	ctx.os.file_watch_context.handle = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
     89 	ctx.os.compute_worker.asleep     = 1;
     90 	ctx.os.error_handle              = STDERR_FILENO;
     91 
     92 	setup_beamformer(&ctx, &input, &temp_memory);
     93 	os_wake_waiters(&ctx.os.compute_worker.sync_variable);
     94 
     95 	struct pollfd fds[1] = {{0}};
     96 	fds[0].fd     = ctx.os.file_watch_context.handle;
     97 	fds[0].events = POLLIN;
     98 
     99 	u64 last_time = os_get_timer_counter();
    100 	while (!ctx.should_exit) {
    101 		poll(fds, countof(fds), 0);
    102 		if (fds[0].revents & POLLIN)
    103 			dispatch_file_watch_events(&ctx.os, temp_memory);
    104 
    105 		u64 now = os_get_timer_counter();
    106 		input.last_mouse = input.mouse;
    107 		input.mouse.rl   = GetMousePosition();
    108 		input.dt         = (f64)(now - last_time) / os_get_timer_frequency();
    109 		last_time        = now;
    110 
    111 		beamformer_frame_step(&ctx, &temp_memory, &input);
    112 
    113 		input.executable_reloaded = 0;
    114 	}
    115 
    116 	beamformer_invalidate_shared_memory(&ctx);
    117 
    118 	/* NOTE: make sure this will get cleaned up after external
    119 	 * programs release their references */
    120 	shm_unlink(OS_SHARED_MEMORY_NAME);
    121 }