beamformer.h (8076B)
1 /* See LICENSE for license details. */ 2 #ifndef BEAMFORMER_H 3 #define BEAMFORMER_H 4 5 #include <stdint.h> 6 7 #define BEAMFORMER_NAME_STRING "OGL Beamformer" 8 9 /////////////////////////////// 10 // COMPILE TIME CONFIGURATION 11 12 /* NOTE(rnp): By design the beamformer has very little compile time configuration. 13 * The few options it does have are documented here. 14 * 15 * BEAMFORMER_IMPORT 16 * BEAMFORMER_EXPORT 17 * The symbol markup for imported and exported symbols. In a typical 18 * release unity build these are both defined to `static`. 19 * 20 * BEAMFORMER_DEBUG 21 * Compile the beamformer with handling for hot reloading at runtime. 22 * This requires compiling `beamformer_core.c` as a dynamic library which the 23 * platform is required to load at runtime. 24 * IMPORTANT: When the platform wants to reload the library at runtime it 25 * MUST NOT unload the old library immediately; the beamformer may still 26 * be executing code in old library. Instead the platform must first call 27 * `beamformer_debug_hot_release` with the program's memory, then it may close the 28 * old handle. Then `beamformer_debug_hot_reload` should be called with the new handle 29 * so that the beamformer may resume operation. 30 * 31 * BEAMFORMER_RENDERDOC_HOOKS 32 * Add RenderDoc API calls to capture complete compute frames. As compute is performed 33 * asynchronously from normal rendering it is not possible to capture normally. In this 34 * configuration the beamformer will use the function pointers provided in the 35 * BeamformerInput to make these calls. 36 * IMPORTANT: The renderdoc library will only be visible when the application is started 37 * through RenderDoc. Furthermore the library has startup code which will halt the program 38 * if loaded normally. It must be loaded using platform module loading APIs. For example 39 * GetModuleHandle or dlopen with the RTLD_NOLOAD flag set. 40 * 41 */ 42 43 #ifndef BEAMFORMER_IMPORT 44 #define BEAMFORMER_IMPORT 45 #endif 46 47 #ifndef BEAMFORMER_EXPORT 48 #define BEAMFORMER_EXPORT 49 #endif 50 51 #ifdef BEAMFORMER_DEBUG 52 #undef BEAMFORMER_DEBUG 53 #define BEAMFORMER_DEBUG (1) 54 #else 55 #define BEAMFORMER_DEBUG (0) 56 #endif 57 58 #ifdef BEAMFORMER_RENDERDOC_HOOKS 59 #undef BEAMFORMER_RENDERDOC_HOOKS 60 #define BEAMFORMER_RENDERDOC_HOOKS (1) 61 #else 62 #define BEAMFORMER_RENDERDOC_HOOKS (0) 63 #endif 64 65 /////////////////// 66 // REQUIRED OS API 67 #define OSInvalidHandleValue ((u64)-1) 68 typedef struct { uint64_t value[1]; } OSBarrier; 69 typedef struct { uint64_t value[1]; } OSHandle; 70 typedef struct { uint64_t value[1]; } OSLibrary; 71 typedef struct { uint64_t value[1]; } OSThread; 72 typedef struct { uint64_t value[1]; } OSW32Semaphore; 73 74 typedef uint64_t os_thread_entry_point_fn(void *user_context); 75 76 typedef struct { 77 uint64_t timer_frequency; 78 79 uint32_t logical_processor_count; 80 uint32_t page_size; 81 82 uint8_t path_separator_byte; 83 } OSSystemInfo; 84 85 BEAMFORMER_IMPORT OSSystemInfo * os_system_info(void); 86 87 BEAMFORMER_IMPORT OSThread os_create_thread(const char *name, void *user_context, os_thread_entry_point_fn *fn); 88 BEAMFORMER_IMPORT OSBarrier os_barrier_alloc(uint32_t thread_count); 89 BEAMFORMER_IMPORT void os_barrier_enter(OSBarrier); 90 91 /* NOTE(rnp): since the beamformer may spawn threads, which may need to keep time, 92 * passing in a single timer value with the rest of the input is insufficient. */ 93 BEAMFORMER_IMPORT uint64_t os_timer_count(void); 94 95 BEAMFORMER_IMPORT void os_add_file_watch(const char *path, int64_t path_length, void *user_context); 96 BEAMFORMER_IMPORT int64_t os_read_entire_file(const char *file, void *buffer, int64_t buffer_capacity); 97 98 BEAMFORMER_IMPORT void * os_lookup_symbol(OSLibrary library, const char *symbol); 99 100 /* NOTE(rnp): memory watch timed waiting functions. (-1) is an infinite timeout. the beamformer 101 * will use these with the intention of yielding the thread back to the OS. */ 102 BEAMFORMER_IMPORT uint32_t os_wait_on_address(int32_t *lock, int32_t current, uint32_t timeout_ms); 103 BEAMFORMER_IMPORT void os_wake_all_waiters(int32_t *lock); 104 105 // NOTE(rnp): eventually logging will just be done internally 106 BEAMFORMER_IMPORT void os_console_log(uint8_t *data, int64_t length); 107 BEAMFORMER_IMPORT void os_fatal(uint8_t *data, int64_t length); 108 109 /* NOTE(rnp): this functionality is only needed on win32 to provide cross process 110 * synchronization. While posix has equivalent functionality there is no reason to 111 * use it over a value located in shared memory. */ 112 #if defined(_WIN32) 113 BEAMFORMER_IMPORT OSW32Semaphore os_w32_create_semaphore(const char *name, int32_t initial_count, int32_t maximum_count); 114 BEAMFORMER_IMPORT uint32_t os_w32_semaphore_wait(OSW32Semaphore, uint32_t timeout_ms); 115 BEAMFORMER_IMPORT void os_w32_semaphore_release(OSW32Semaphore, int32_t count); 116 #endif 117 118 ////////////////////////////// 119 // BEAMFORMER APPLICATION API 120 121 typedef enum { 122 BeamformerInputEventKind_ButtonPress, 123 BeamformerInputEventKind_ButtonRelease, 124 BeamformerInputEventKind_ExecutableReload, 125 BeamformerInputEventKind_FileEvent, 126 } BeamformerInputEventKind; 127 128 // TODO: not yet used 129 typedef enum { 130 BeamformerButtonID_MouseLeft, 131 BeamformerButtonID_MouseRight, 132 BeamformerButtonID_MouseMiddle, 133 BeamformerButtonID_Count, 134 } BeamformerButtonID; 135 136 typedef struct { 137 BeamformerInputEventKind kind; 138 union { 139 BeamformerButtonID button_id; 140 void * file_watch_user_context; 141 }; 142 } BeamformerInputEvent; 143 144 typedef struct { 145 /* NOTE(rnp): besides vulkan library code the beamformer will not allocate memory on its 146 * own. Recommended minimum size is 16MB. If shared memory is not provided it is recommended 147 * to increase this to at least 1GB to help facilitate loading of external data files (not yet 148 * implemented). */ 149 void * memory; 150 uint64_t memory_size; 151 152 /* NOTE(rnp): beamformer will use this to communicate with external processes. While it 153 * it won't be required in the future it is currently the only way to load data. 154 * Recommended size is 2-4GB. Currently this size will also limit the size of any data 155 * another process wishes to export. The name is required for listing in the UI so that 156 * users of external processes can open the region on their end. */ 157 void * shared_memory; 158 uint64_t shared_memory_size; 159 uint8_t * shared_memory_name; 160 uint32_t shared_memory_name_length; 161 162 float mouse_x; 163 float mouse_y; 164 float last_mouse_x; 165 float last_mouse_y; 166 167 uint32_t event_count; 168 169 BeamformerInputEvent event_queue[256]; 170 171 /* NOTE(rnp): the beamformer is not allowed to dynamically load libraries 172 * itself. Libraries are optional and the beamformer will not use features 173 * from libraries which have not been provided. */ 174 OSLibrary cuda_library_handle; 175 176 #if BEAMFORMER_RENDERDOC_HOOKS 177 void *renderdoc_start_frame_capture; 178 void *renderdoc_end_frame_capture; 179 #endif 180 } BeamformerInput; 181 182 BEAMFORMER_EXPORT void beamformer_init(BeamformerInput *); 183 184 /* NOTE(rnp): while the platform can also decide to terminate the beamformer, 185 * the beamformer itself may indicate that it wants to terminate. If the 186 * beamformer itself decides to terminate it is unnecessary to call 187 * `beamformer_terminate()` but it will act as a NOP if you do. */ 188 BEAMFORMER_EXPORT uint32_t beamformer_should_close(BeamformerInput *); 189 190 /* IMPORTANT(rnp): since the beamformer may be interacting with external hardware 191 * it is critical that the platform calls this when it wishes to terminate the 192 * beamformer. Otherwise the external hardware may be left in a bad state and require 193 * a reboot. The beamformer will not waste time releasing resources unless it was 194 * compiled with BEAMFORMER_DEBUG enabled (useful for address sanitizer). */ 195 BEAMFORMER_EXPORT void beamformer_terminate(BeamformerInput *); 196 197 #if !BEAMFORMER_DEBUG 198 BEAMFORMER_EXPORT void beamformer_frame_step(BeamformerInput *); 199 #endif 200 201 #if BEAMFORMER_DEBUG 202 BEAMFORMER_EXPORT void beamformer_debug_hot_release(BeamformerInput *); 203 BEAMFORMER_EXPORT void beamformer_debug_hot_reload(OSLibrary new_library, BeamformerInput *); 204 #endif 205 206 #endif /*BEAMFORMER_H */