ogl_beamforming

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

beamformer.h (12372B)


      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 "VK 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 //
     68 // NOTE(rnp): in addition to the platform layer defined in base_platform.h
     69 // the beamformer additionally requires the following functions.
     70 
     71 BEAMFORMER_IMPORT void           os_add_file_watch(const char *path, int64_t path_length, void *user_context);
     72 
     73 BEAMFORMER_IMPORT void *         os_lookup_symbol(OSLibrary library, const char *symbol);
     74 
     75 BEAMFORMER_IMPORT OSThread       os_create_thread(const char *name, void *user_context, os_thread_entry_point_fn *fn);
     76 BEAMFORMER_IMPORT OSBarrier      os_barrier_alloc(u32 thread_count);
     77 BEAMFORMER_IMPORT void           os_barrier_enter(OSBarrier);
     78 
     79 // NOTE(rnp): currently beamformer will only create one window.
     80 // once raylib is removed it may request multiple
     81 BEAMFORMER_IMPORT OSWindow       os_window_create(uint8_t *title, int64_t title_length, int32_t width, int32_t height);
     82 //BEAMFORMER_IMPORT void           os_window_title(OSWindow window, uint8_t *title, int64_t title_length);
     83 //BEAMFORMER_IMPORT void           os_window_destroy(OSWindow window);
     84 
     85 BEAMFORMER_IMPORT uint8_t *      os_get_clipboard_text(int64_t *length);
     86 BEAMFORMER_IMPORT void           os_set_clipboard_text(uint8_t *data, int64_t length);
     87 
     88 // NOTE(rnp): eventually logging will just be done internally
     89 BEAMFORMER_IMPORT void           os_console_log(uint8_t *data, int64_t length);
     90 BEAMFORMER_IMPORT void           os_fatal(uint8_t *data, int64_t length);
     91 
     92 // NOTE(rnp): for vulkan cross API export on win32 (will be removed eventually)
     93 BEAMFORMER_IMPORT void           os_release_handle(OSHandle handle);
     94 
     95 //////////////////////////////
     96 // BEAMFORMER APPLICATION API
     97 
     98 typedef enum {
     99 	BeamformerInputEventKind_ButtonPress,
    100 	BeamformerInputEventKind_ButtonRelease,
    101 	BeamformerInputEventKind_MouseScroll,
    102 	BeamformerInputEventKind_WindowResize,
    103 	BeamformerInputEventKind_ExecutableReload,
    104 	BeamformerInputEventKind_FileEvent,
    105 } BeamformerInputEventKind;
    106 
    107 typedef enum {
    108 	BeamformerButtonID_Space        = ' ',
    109 	BeamformerButtonID_Apostrophe   = '\'',
    110 	BeamformerButtonID_Comma        = ',',
    111 	BeamformerButtonID_Minus        = '-',
    112 	BeamformerButtonID_Period       = '.',
    113 	BeamformerButtonID_Slash        = '/',
    114 	BeamformerButtonID_0            = '0',
    115 	BeamformerButtonID_1            = '1',
    116 	BeamformerButtonID_2            = '2',
    117 	BeamformerButtonID_3            = '3',
    118 	BeamformerButtonID_4            = '4',
    119 	BeamformerButtonID_5            = '5',
    120 	BeamformerButtonID_6            = '6',
    121 	BeamformerButtonID_7            = '7',
    122 	BeamformerButtonID_8            = '8',
    123 	BeamformerButtonID_9            = '9',
    124 	BeamformerButtonID_Semicolon    = ';',
    125 	BeamformerButtonID_Equal        = '=',
    126 	BeamformerButtonID_A            = 'A',
    127 	BeamformerButtonID_B            = 'B',
    128 	BeamformerButtonID_C            = 'C',
    129 	BeamformerButtonID_D            = 'D',
    130 	BeamformerButtonID_E            = 'E',
    131 	BeamformerButtonID_F            = 'F',
    132 	BeamformerButtonID_G            = 'G',
    133 	BeamformerButtonID_H            = 'H',
    134 	BeamformerButtonID_I            = 'I',
    135 	BeamformerButtonID_J            = 'J',
    136 	BeamformerButtonID_K            = 'K',
    137 	BeamformerButtonID_L            = 'L',
    138 	BeamformerButtonID_M            = 'M',
    139 	BeamformerButtonID_N            = 'N',
    140 	BeamformerButtonID_O            = 'O',
    141 	BeamformerButtonID_P            = 'P',
    142 	BeamformerButtonID_Q            = 'Q',
    143 	BeamformerButtonID_R            = 'R',
    144 	BeamformerButtonID_S            = 'S',
    145 	BeamformerButtonID_T            = 'T',
    146 	BeamformerButtonID_U            = 'U',
    147 	BeamformerButtonID_V            = 'V',
    148 	BeamformerButtonID_W            = 'W',
    149 	BeamformerButtonID_X            = 'X',
    150 	BeamformerButtonID_Y            = 'Y',
    151 	BeamformerButtonID_Z            = 'Z',
    152 	BeamformerButtonID_LeftBracket  = '[',
    153 	BeamformerButtonID_Backslash    = '\\',
    154 	BeamformerButtonID_RightBracket = ']',
    155 	BeamformerButtonID_Grave        = '`',
    156 
    157 	BeamformerButtonID_Escape,
    158 	BeamformerButtonID_Enter,
    159 	BeamformerButtonID_Tab,
    160 	BeamformerButtonID_Backspace,
    161 	BeamformerButtonID_Insert,
    162 	BeamformerButtonID_Delete,
    163 	BeamformerButtonID_Right,
    164 	BeamformerButtonID_Left,
    165 	BeamformerButtonID_Down,
    166 	BeamformerButtonID_Up,
    167 	BeamformerButtonID_PageUp,
    168 	BeamformerButtonID_PageDown,
    169 	BeamformerButtonID_Home,
    170 	BeamformerButtonID_End,
    171 	BeamformerButtonID_CapsLock,
    172 	BeamformerButtonID_ScrollLock,
    173 	BeamformerButtonID_NumLock,
    174 	BeamformerButtonID_PrintScreen,
    175 	BeamformerButtonID_Pause,
    176 	BeamformerButtonID_F1,
    177 	BeamformerButtonID_F2,
    178 	BeamformerButtonID_F3,
    179 	BeamformerButtonID_F4,
    180 	BeamformerButtonID_F5,
    181 	BeamformerButtonID_F6,
    182 	BeamformerButtonID_F7,
    183 	BeamformerButtonID_F8,
    184 	BeamformerButtonID_F9,
    185 	BeamformerButtonID_F10,
    186 	BeamformerButtonID_F11,
    187 	BeamformerButtonID_F12,
    188 	BeamformerButtonID_F13,
    189 	BeamformerButtonID_F14,
    190 	BeamformerButtonID_F15,
    191 	BeamformerButtonID_F16,
    192 	BeamformerButtonID_F17,
    193 	BeamformerButtonID_F18,
    194 	BeamformerButtonID_F19,
    195 	BeamformerButtonID_F20,
    196 	BeamformerButtonID_F21,
    197 	BeamformerButtonID_F22,
    198 	BeamformerButtonID_F23,
    199 	BeamformerButtonID_F24,
    200 	BeamformerButtonID_F25,
    201 	BeamformerButtonID_KP0,
    202 	BeamformerButtonID_KP1,
    203 	BeamformerButtonID_KP2,
    204 	BeamformerButtonID_KP3,
    205 	BeamformerButtonID_KP4,
    206 	BeamformerButtonID_KP5,
    207 	BeamformerButtonID_KP6,
    208 	BeamformerButtonID_KP7,
    209 	BeamformerButtonID_KP8,
    210 	BeamformerButtonID_KP9,
    211 	BeamformerButtonID_KPDecimal,
    212 	BeamformerButtonID_KPDivide,
    213 	BeamformerButtonID_KPMultiply,
    214 	BeamformerButtonID_KPSubtract,
    215 	BeamformerButtonID_KPAdd,
    216 	BeamformerButtonID_KPEnter,
    217 	BeamformerButtonID_KPEqual,
    218 
    219 	BeamformerButtonID_LeftShift,
    220 	BeamformerButtonID_LeftControl,
    221 	BeamformerButtonID_LeftAlt,
    222 	BeamformerButtonID_LeftSuper,
    223 	BeamformerButtonID_RightShift,
    224 	BeamformerButtonID_RightControl,
    225 	BeamformerButtonID_RightAlt,
    226 	BeamformerButtonID_RightSuper,
    227 	BeamformerButtonID_ModifierFirst = BeamformerButtonID_LeftShift,
    228 	BeamformerButtonID_ModifierLast  = BeamformerButtonID_RightSuper,
    229 
    230 	BeamformerButtonID_Menu,
    231 
    232 	BeamformerButtonID_MouseLeft,
    233 	BeamformerButtonID_MouseRight,
    234 	BeamformerButtonID_MouseMiddle,
    235 
    236 	BeamformerButtonID_Count,
    237 } BeamformerButtonID;
    238 
    239 typedef enum {
    240 	BeamformerInputModifier_LeftAlt      = (1 << 0),
    241 	BeamformerInputModifier_RightAlt     = (1 << 1),
    242 
    243 	BeamformerInputModifier_LeftControl  = (1 << 2),
    244 	BeamformerInputModifier_RightControl = (1 << 3),
    245 
    246 	BeamformerInputModifier_LeftShift    = (1 << 4),
    247 	BeamformerInputModifier_RightShift   = (1 << 5),
    248 
    249 	BeamformerInputModifier_LeftMeta     = (1 << 6),
    250 	BeamformerInputModifier_RightMeta    = (1 << 7),
    251 
    252 	BeamformerInputModifier_Alt     = BeamformerInputModifier_LeftAlt|BeamformerInputModifier_RightAlt,
    253 	BeamformerInputModifier_Control = BeamformerInputModifier_LeftControl|BeamformerInputModifier_RightControl,
    254 	BeamformerInputModifier_Shift   = BeamformerInputModifier_LeftShift|BeamformerInputModifier_RightShift,
    255 	BeamformerInputModifier_Meta    = BeamformerInputModifier_LeftMeta|BeamformerInputModifier_RightMeta,
    256 	BeamformerInputModifier_Any     = BeamformerInputModifier_Alt|
    257 	                                  BeamformerInputModifier_Control|
    258 	                                  BeamformerInputModifier_Shift|
    259 	                                  BeamformerInputModifier_Meta,
    260 } BeamformerInputModifiers;
    261 
    262 typedef struct {
    263 	BeamformerInputEventKind kind;
    264 	BeamformerInputModifiers modifiers;
    265 	union {
    266 		struct {
    267 			BeamformerButtonID button_id;
    268 			// NOTE(rnp): if the button is not also an input key codepoint should be 0
    269 			uint32_t           codepoint;
    270 		};
    271 		struct {float x, y;} scroll;
    272 
    273 		struct {
    274 			uint32_t width, height;
    275 			OSWindow window;
    276 		} window_resize;
    277 
    278 		void *file_watch_user_context;
    279 	};
    280 } BeamformerInputEvent;
    281 
    282 typedef struct {
    283 	/* NOTE(rnp): beamformer will use this to communicate with external processes. While it
    284 	 * it won't be required in the future it is currently the only way to load data.
    285 	 * Recommended size is 2-4GB. Currently this size will also limit the size of any data
    286 	 * another process wishes to export. The name is required for listing in the UI so that
    287 	 * users of external processes can open the region on their end. */
    288 	void *      shared_memory;
    289 	uint64_t    shared_memory_size;
    290 	uint8_t *   shared_memory_name;
    291 	uint32_t    shared_memory_name_length;
    292 
    293 	float       mouse_x;
    294 	float       mouse_y;
    295 
    296 	uint32_t    event_count;
    297 
    298 	BeamformerInputEvent event_queue[256];
    299 
    300 	/* NOTE(rnp): the beamformer is not allowed to dynamically load libraries
    301 	 * itself. Besides Vulkan, which is required, libraries are optional and
    302 	 * the beamformer will not use features from libraries which have not
    303 	 * been provided. */
    304 	OSLibrary cuda_library_handle;
    305 	OSLibrary vulkan_library_handle;
    306 
    307 	#if BEAMFORMER_RENDERDOC_HOOKS
    308 	void *renderdoc_start_frame_capture;
    309 	void *renderdoc_end_frame_capture;
    310 	void *renderdoc_set_capture_file_path_template;
    311 	#endif
    312 } BeamformerInput;
    313 
    314 // NOTE(rnp): returns the beamformer's memory, to be passed as part of input.
    315 BEAMFORMER_EXPORT void *beamformer_init(BeamformerInput *);
    316 
    317 /* NOTE(rnp): while the platform can also decide to terminate the beamformer,
    318  * the beamformer itself may indicate that it wants to terminate. If the
    319  * beamformer itself decides to terminate it is unnecessary to call
    320  * `beamformer_terminate()` but it will act as a NOP if you do. */
    321 BEAMFORMER_EXPORT uint32_t beamformer_should_close(void *memory, BeamformerInput *);
    322 
    323 /* IMPORTANT(rnp): since the beamformer may be interacting with external hardware
    324  * it is critical that the platform calls this when it wishes to terminate the
    325  * beamformer. Otherwise the external hardware may be left in a bad state and require
    326  * a reboot. The beamformer will not waste time releasing resources unless it was
    327  * compiled with BEAMFORMER_DEBUG enabled (useful for address sanitizer). */
    328 BEAMFORMER_EXPORT void beamformer_terminate(void *memory, BeamformerInput *);
    329 
    330 #if !BEAMFORMER_DEBUG
    331 BEAMFORMER_EXPORT void beamformer_frame_step(void *memory, BeamformerInput *);
    332 #endif
    333 
    334 #if BEAMFORMER_DEBUG
    335 BEAMFORMER_EXPORT void beamformer_debug_hot_release(void *memory, BeamformerInput *);
    336 BEAMFORMER_EXPORT void beamformer_debug_hot_reload(OSLibrary new_library);
    337 #endif
    338 
    339 #endif /*BEAMFORMER_H */