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 (2861B)


      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((i32)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 j = 0; j < dir->count; j++) {
     57 					FileWatch *fw = dir->data + j;
     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 	Arena program_memory = os_alloc_arena(MB(16));
     77 
     78 	BeamformerCtx   *ctx   = 0;
     79 	BeamformerInput *input = 0;
     80 
     81 	setup_beamformer(&program_memory, &ctx, &input);
     82 	os_wake_waiters(&ctx->os.compute_worker.sync_variable);
     83 
     84 	struct pollfd fds[1] = {{0}};
     85 	fds[0].fd     = (i32)ctx->os.file_watch_context.handle;
     86 	fds[0].events = POLLIN;
     87 
     88 	u64 last_time = os_get_timer_counter();
     89 	while (!ctx->should_exit) {
     90 		poll(fds, countof(fds), 0);
     91 		if (fds[0].revents & POLLIN)
     92 			dispatch_file_watch_events(&ctx->os, program_memory);
     93 
     94 		u64 now = os_get_timer_counter();
     95 		input->last_mouse = input->mouse;
     96 		input->mouse.rl   = GetMousePosition();
     97 		input->dt         = (f32)((f64)(now - last_time) / (f64)os_get_timer_frequency());
     98 		last_time         = now;
     99 
    100 		beamformer_frame_step(ctx, input);
    101 
    102 		input->executable_reloaded = 0;
    103 	}
    104 
    105 	beamformer_invalidate_shared_memory(ctx);
    106 	beamformer_debug_ui_deinit(ctx);
    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 }