ogl_beamforming

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

beamformer.h (9208B)


      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 
    110 // NOTE(rnp): for vulkan cross API export on win32 (will be removed eventually)
    111 BEAMFORMER_IMPORT void           os_release_handle(OSHandle handle);
    112 
    113 /* NOTE(rnp): this functionality is only needed on win32 to provide cross process
    114  * synchronization. While posix has equivalent functionality there is no reason to
    115  * use it over a value located in shared memory. */
    116 #if defined(_WIN32)
    117 BEAMFORMER_IMPORT OSW32Semaphore os_w32_create_semaphore(const char *name, int32_t initial_count, int32_t maximum_count);
    118 BEAMFORMER_IMPORT uint32_t       os_w32_semaphore_wait(OSW32Semaphore, uint32_t timeout_ms);
    119 BEAMFORMER_IMPORT void           os_w32_semaphore_release(OSW32Semaphore, int32_t count);
    120 #endif
    121 
    122 //////////////////////////////
    123 // BEAMFORMER APPLICATION API
    124 
    125 typedef enum {
    126 	BeamformerInputEventKind_ButtonPress,
    127 	BeamformerInputEventKind_ButtonRelease,
    128 	BeamformerInputEventKind_ExecutableReload,
    129 	BeamformerInputEventKind_FileEvent,
    130 } BeamformerInputEventKind;
    131 
    132 // TODO: not yet used
    133 typedef enum {
    134 	BeamformerButtonID_MouseLeft,
    135 	BeamformerButtonID_MouseRight,
    136 	BeamformerButtonID_MouseMiddle,
    137 	BeamformerButtonID_Count,
    138 } BeamformerButtonID;
    139 
    140 typedef enum {
    141 	BeamformerInputModifier_LeftAlt      = (1 << 0),
    142 	BeamformerInputModifier_RightAlt     = (1 << 1),
    143 
    144 	BeamformerInputModifier_LeftControl  = (1 << 2),
    145 	BeamformerInputModifier_RightControl = (1 << 3),
    146 
    147 	BeamformerInputModifier_LeftShift    = (1 << 4),
    148 	BeamformerInputModifier_RightShift   = (1 << 5),
    149 
    150 	BeamformerInputModifier_LeftMeta     = (1 << 6),
    151 	BeamformerInputModifier_RightMeta    = (1 << 7),
    152 
    153 	BeamformerInputModifier_Alt     = BeamformerInputModifier_LeftAlt|BeamformerInputModifier_RightAlt,
    154 	BeamformerInputModifier_Control = BeamformerInputModifier_LeftControl|BeamformerInputModifier_RightControl,
    155 	BeamformerInputModifier_Shift   = BeamformerInputModifier_LeftShift|BeamformerInputModifier_RightShift,
    156 	BeamformerInputModifier_Meta    = BeamformerInputModifier_LeftMeta|BeamformerInputModifier_RightMeta,
    157 } BeamformerInputModifiers;
    158 
    159 typedef struct {
    160 	BeamformerInputEventKind kind;
    161 	union {
    162 		BeamformerButtonID  button_id;
    163 		void *              file_watch_user_context;
    164 	};
    165 } BeamformerInputEvent;
    166 
    167 typedef struct {
    168 	/* NOTE(rnp): besides vulkan library code the beamformer will not allocate memory on its
    169 	 * own. Recommended minimum size is 16MB. If shared memory is not provided it is recommended
    170 	 * to increase this to at least 1GB to help facilitate loading of external data files (not yet
    171 	 * implemented). */
    172 	void *      memory;
    173 	uint64_t    memory_size;
    174 
    175 	/* NOTE(rnp): beamformer will use this to communicate with external processes. While it
    176 	 * it won't be required in the future it is currently the only way to load data.
    177 	 * Recommended size is 2-4GB. Currently this size will also limit the size of any data
    178 	 * another process wishes to export. The name is required for listing in the UI so that
    179 	 * users of external processes can open the region on their end. */
    180 	void *      shared_memory;
    181 	uint64_t    shared_memory_size;
    182 	uint8_t *   shared_memory_name;
    183 	uint32_t    shared_memory_name_length;
    184 
    185 	float       mouse_x;
    186 	float       mouse_y;
    187 
    188 	uint32_t    event_count;
    189 
    190 	BeamformerInputModifiers input_modifiers;
    191 	BeamformerInputEvent     event_queue[256];
    192 
    193 	/* NOTE(rnp): the beamformer is not allowed to dynamically load libraries
    194 	 * itself. Besides Vulkan, which is required, libraries are optional and
    195 	 * the beamformer will not use features from libraries which have not
    196 	 * been provided. */
    197 	OSLibrary cuda_library_handle;
    198 	OSLibrary vulkan_library_handle;
    199 
    200 	#if BEAMFORMER_RENDERDOC_HOOKS
    201 	void *renderdoc_start_frame_capture;
    202 	void *renderdoc_end_frame_capture;
    203 	void *renderdoc_set_capture_file_path_template;
    204 	#endif
    205 } BeamformerInput;
    206 
    207 BEAMFORMER_EXPORT void beamformer_init(BeamformerInput *);
    208 
    209 /* NOTE(rnp): while the platform can also decide to terminate the beamformer,
    210  * the beamformer itself may indicate that it wants to terminate. If the
    211  * beamformer itself decides to terminate it is unnecessary to call
    212  * `beamformer_terminate()` but it will act as a NOP if you do. */
    213 BEAMFORMER_EXPORT uint32_t beamformer_should_close(BeamformerInput *);
    214 
    215 /* IMPORTANT(rnp): since the beamformer may be interacting with external hardware
    216  * it is critical that the platform calls this when it wishes to terminate the
    217  * beamformer. Otherwise the external hardware may be left in a bad state and require
    218  * a reboot. The beamformer will not waste time releasing resources unless it was
    219  * compiled with BEAMFORMER_DEBUG enabled (useful for address sanitizer). */
    220 BEAMFORMER_EXPORT void beamformer_terminate(BeamformerInput *);
    221 
    222 #if !BEAMFORMER_DEBUG
    223 BEAMFORMER_EXPORT void beamformer_frame_step(BeamformerInput *);
    224 #endif
    225 
    226 #if BEAMFORMER_DEBUG
    227 BEAMFORMER_EXPORT void beamformer_debug_hot_release(BeamformerInput *);
    228 BEAMFORMER_EXPORT void beamformer_debug_hot_reload(OSLibrary new_library, BeamformerInput *);
    229 #endif
    230 
    231 #endif /*BEAMFORMER_H */