ogl_beamforming

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

beamformer_internal.h (21424B)


      1 /* See LICENSE for license details. */
      2 #ifndef BEAMFORMER_INTERNAL_H
      3 #define BEAMFORMER_INTERNAL_H
      4 
      5 #include "beamformer.h"
      6 
      7 #include "util.h"
      8 #include "opengl.h"
      9 
     10 #include "generated/beamformer.c"
     11 #include "generated/beamformer_core.c"
     12 #include "generated/beamformer_shader_data.c"
     13 
     14 #include "external/raylib/src/raylib.h"
     15 #include "external/raylib/src/rlgl.h"
     16 
     17 #define beamformer_info(s) str8("[info] " s "\n")
     18 
     19 #define os_path_separator() (str8){.data = &os_system_info()->path_separator_byte, .length = 1}
     20 
     21 typedef struct { u64 value; } GPUHandle;
     22 typedef struct { u64 value; } GPUCommandList;
     23 typedef struct { u64 value; } GPUSemaphore;
     24 typedef struct { u64 value; } GPUSplitBarrier;
     25 
     26 typedef struct { u64 value[1]; } VulkanHandle;
     27 
     28 typedef enum {
     29 	GPUTimeline_Graphics,
     30 	GPUTimeline_Compute,
     31 	GPUTimeline_Transfer,
     32 	GPUTimeline_Count,
     33 } GPUTimeline;
     34 
     35 typedef enum {
     36 	VulkanShaderKind_Vertex,
     37 	VulkanShaderKind_Mesh,
     38 	VulkanShaderKind_Fragment,
     39 	VulkanShaderKind_Compute,
     40 	VulkanShaderKind_Count,
     41 } VulkanShaderKind;
     42 
     43 typedef enum {
     44 	VulkanImageUsage_None,
     45 	VulkanImageUsage_Colour,
     46 	VulkanImageUsage_DepthStencil,
     47 	VulkanImageUsage_Count,
     48 } VulkanImageUsage;
     49 
     50 typedef enum {
     51 	GPUUsageFlag_ImageSampling       = 1 << 0,
     52 	GPUUsageFlag_HostRead            = 1 << 1, // NOTE: not valid on images
     53 	GPUUsageFlag_HostWrite           = 1 << 2, // NOTE: not valid on images
     54 	/* NOTE: uses:
     55 	 * - image-image copy operations
     56 	 * - buffer-buffer copy operations
     57 	 */
     58 	GPUUsageFlag_TransferSource      = 1 << 3,
     59 	GPUUsageFlag_TransferDestination = 1 << 4,
     60 
     61 	GPUUsageFlag_HostReadWrite       = (GPUUsageFlag_HostRead|GPUUsageFlag_HostWrite),
     62 } GPUUsageFlags;
     63 
     64 typedef struct {
     65 	VulkanShaderKind kind;
     66 	i32              specialization_struct_id;
     67 	void            *specialization_data;
     68 	str8             text;
     69 	str8             name;
     70 } VulkanPipelineCreateInfo;
     71 
     72 typedef struct {
     73 	GPUHandle handle;
     74 	u64       gpu_pointer;
     75 	i64       size;
     76 
     77 	// NOTE: only used for render models
     78 	u64       index_count;
     79 } GPUBuffer;
     80 
     81 typedef struct {
     82 	VulkanHandle image;
     83 	u32          width;
     84 	u32          height;
     85 	u32          samples;
     86 	u32          mip_map_levels;
     87 	// TODO(rnp): this is only here for importing from OpenGL, move it back into handle later
     88 	u64          memory_size;
     89 } GPUImage;
     90 
     91 typedef enum {
     92 	GPUVendor_AMD      = 0x1002,
     93 	GPUVendor_NVIDIA   = 0x10DE,
     94 	GPUVendor_Qualcomm = 0x5143,
     95 	GPUVendor_Intel    = 0x8086,
     96 } GPUVendor;
     97 
     98 typedef struct {
     99 	str8      name;
    100 	GPUVendor vendor;
    101 
    102 	f32 timestamp_period_ns;
    103 
    104 	u32 max_compute_shared_memory_size;
    105 	u16 max_msaa_samples;
    106 	u16 subgroup_size;
    107 
    108 	b32 cooperative_matrix;
    109 
    110 	u32 max_image_dimension_2D;
    111 	// NOTE(rnp): vulkan compute will output to a buffer so this won't be relevant
    112 	u32 max_image_dimension_3D;
    113 
    114 	u64 gpu_heap_size;
    115 	u64 gpu_heap_used;
    116 } GPUInfo;
    117 
    118 typedef struct {
    119 	GPUUsageFlags     flags;
    120 	i64               size;
    121 
    122 	// NOTE(rnp): these are required if buffer will have any CPU<->GPU transfers
    123 	u32               timeline_count;
    124 	GPUTimeline      *timelines_used;
    125 
    126 	OSHandle         *export;
    127 
    128 	str8              label;
    129 } GPUBufferAllocateInfo;
    130 
    131 typedef struct {
    132 	GPUSemaphore semaphore;
    133 	// NOTE(rnp): ignored if semaphore was exported
    134 	u64          value;
    135 } GPUSemaphoreSignalInfo;
    136 
    137 typedef struct {
    138 	GPUBuffer model;
    139 	u32       vertex_count;
    140 	u32       normals_offset;
    141 } RenderModel;
    142 
    143 #include "threads.c"
    144 
    145 ///////////////////////////
    146 // NOTE: vulkan layer API
    147 DEBUG_IMPORT void vk_load(OSLibrary vulkan, Stream *error);
    148 
    149 DEBUG_IMPORT GPUInfo *gpu_info(void);
    150 
    151 DEBUG_IMPORT void gpu_buffer_allocate(GPUBuffer *, GPUBufferAllocateInfo info);
    152 DEBUG_IMPORT void gpu_buffer_release(GPUBuffer *);
    153 DEBUG_IMPORT u64  gpu_buffer_range_upload(GPUBuffer *, void *data, u64 offset, u64 size, b32 non_temporal);
    154 DEBUG_IMPORT void gpu_buffer_range_download(void *output, GPUBuffer *, u64 source_offset, u64 size, b32 non_temporal);
    155 DEBUG_IMPORT u64  gpu_round_up_to_sync_size(u64, u64 min);
    156 
    157 // NOTE: images are 2D only, any other use case should just use a buffer and index in the shader
    158 DEBUG_IMPORT void vk_image_allocate(GPUImage *, u32 width, u32 height, u32 mips, u32 samples, VulkanImageUsage usage, GPUUsageFlags flags, OSHandle *export, str8 label);
    159 DEBUG_IMPORT void vk_image_release(GPUImage *);
    160 
    161 DEBUG_IMPORT void vk_render_model_allocate(GPUBuffer *, void *indices, u64 index_count, u64 model_size, str8 label);
    162 DEBUG_IMPORT void vk_render_model_range_upload(GPUBuffer *, void *data, u64 offset, u64 size, b32 non_temporal);
    163 DEBUG_IMPORT void vk_render_model_release(GPUBuffer *);
    164 
    165 /* NOTE: Pipelines do not have bindings. Data should be passed using push constants.
    166  * In particular the push constants should contain pointers to gpu memory using the
    167  * BufferDeviceAddress extension. */
    168 // TODO(rnp): change this to accept SPIR-V directly and accept BakeParameters as specialization data
    169 DEBUG_IMPORT VulkanHandle vk_pipeline(VulkanPipelineCreateInfo *infos, u32 count, u32 push_constants_size);
    170 DEBUG_IMPORT b32          vk_pipeline_valid(VulkanHandle);
    171 DEBUG_IMPORT void         vk_pipeline_release(VulkanHandle);
    172 
    173 DEBUG_IMPORT GPUSemaphore    gpu_semaphore_create(OSHandle *export);
    174 DEBUG_IMPORT u64             gpu_semaphore_value(GPUSemaphore);
    175 DEBUG_IMPORT void            gpu_host_signal_semaphore(GPUSemaphore semaphore, u64 value);
    176 
    177 DEBUG_IMPORT b32             gpu_buffer_needs_sync(GPUBuffer *);
    178 
    179 DEBUG_IMPORT b32             gpu_host_wait_timeline(GPUTimeline timeline, u64 value, u64 timeout_ns);
    180 DEBUG_IMPORT u64             gpu_host_signal_timeline(GPUTimeline timeline);
    181 
    182 DEBUG_IMPORT GPUCommandList  gpu_command_list_begin(GPUTimeline timeline);
    183 DEBUG_IMPORT u64             gpu_command_list_end(GPUCommandList command,
    184                                                   GPUSemaphoreSignalInfo *wait_infos,   u64 wait_info_count,
    185                                                   GPUSemaphoreSignalInfo *signal_infos, u64 signal_info_count);
    186 
    187 DEBUG_IMPORT void            gpu_command_bind_pipeline(GPUCommandList command, VulkanHandle pipeline);
    188 DEBUG_IMPORT void            gpu_command_pipeline_barrier(GPUCommandList command, b32 memory);
    189 DEBUG_IMPORT void            gpu_command_clear_buffer(GPUCommandList command, GPUBuffer *buffer, u64 offset, u64 size, u32 clear_word);
    190 DEBUG_IMPORT void            gpu_command_dispatch_compute(GPUCommandList command, uv3 dispatch);
    191 DEBUG_IMPORT void            gpu_command_push_constants(GPUCommandList command, u32 offset, u32 size, void *values);
    192 DEBUG_IMPORT void            gpu_command_timestamp(GPUCommandList command);
    193 DEBUG_IMPORT void            gpu_command_wait_timeline(GPUCommandList command, GPUTimeline timeline, u64 value);
    194 
    195 DEBUG_IMPORT void            gpu_command_begin_rendering(GPUCommandList command, GPUImage *restrict colour, GPUImage *restrict depth, GPUImage *restrict resolve);
    196 DEBUG_IMPORT void            gpu_command_draw(GPUCommandList command, GPUBuffer *model);
    197 DEBUG_IMPORT void            gpu_command_scissor(GPUCommandList command, u32 width, u32 height, u32 x_offset, u32 y_offset);
    198 DEBUG_IMPORT void            gpu_command_viewport(GPUCommandList command, f32 width, f32 height, f32 x_offset, f32 y_offset, f32 min_depth, f32 max_depth);
    199 DEBUG_IMPORT void            gpu_command_end_rendering(GPUCommandList command);
    200 
    201 DEBUG_IMPORT void            gpu_command_copy_buffer(GPUCommandList command,
    202                                                      GPUBuffer *restrict destination, u64 destination_offset,
    203                                                      GPUBuffer *restrict source,      u64 source_offset,
    204                                                      u64 size);
    205 
    206 // NOTE: returns array of valid timestamps. Calling thread may stall until results available.
    207 DEBUG_IMPORT u64 *           gpu_read_timestamps(GPUTimeline timeline, u64 *count, Arena *arena);
    208 
    209 /////////////////////////
    210 // NOTE(rnp): advanced buffer manipulation
    211 
    212 // IMPORTANT: there is no guarantee that the GPU can see writes through these
    213 // these APIs unless you call gpu_buffer_make_visible with the memory range
    214 // that was written
    215 // NOTE(rnp): caller is responsible for passing a buffer that was created with
    216 // the required access (read, write, or both).
    217 DEBUG_IMPORT void *          gpu_buffer_host_pointer(GPUBuffer *);
    218 // NOTE(rnp): returns a value to wait on from the Transfer timeline if it
    219 // was used (a wait value is never 0). Any passed in semaphores are always
    220 // signaled (possibly via a host signal operation).
    221 DEBUG_IMPORT u64             gpu_buffer_make_visible(GPUBuffer *, u64 offset, u64 size,
    222                                                      GPUSemaphoreSignalInfo *signal_infos,
    223                                                      u64 signal_info_count);
    224 
    225 #if BEAMFORMER_RENDERDOC_HOOKS
    226 DEBUG_IMPORT void *       vk_renderdoc_instance_handle(void);
    227 
    228 #define RENDERDOC_GET_API_FN(name) b32 name(u32 version, void **out_api)
    229 typedef RENDERDOC_GET_API_FN(renderdoc_get_api_fn);
    230 
    231 #define RENDERDOC_START_FRAME_CAPTURE_FN(name) void name(void *instance_handle, i64 window_handle)
    232 typedef RENDERDOC_START_FRAME_CAPTURE_FN(renderdoc_start_frame_capture_fn);
    233 
    234 #define RENDERDOC_END_FRAME_CAPTURE_FN(name) b32 name(void *instance_handle, i64 window_handle)
    235 typedef RENDERDOC_END_FRAME_CAPTURE_FN(renderdoc_end_frame_capture_fn);
    236 
    237 typedef alignas(16) u8 RenderDocAPI[216];
    238 #define RENDERDOC_API_FN_ADDR(a, offset) (*(i64 *)((*a) + offset))
    239 #define RENDERDOC_START_FRAME_CAPTURE(a) (renderdoc_start_frame_capture_fn *)RENDERDOC_API_FN_ADDR(a, 152)
    240 #define RENDERDOC_END_FRAME_CAPTURE(a)   (renderdoc_end_frame_capture_fn *)  RENDERDOC_API_FN_ADDR(a, 168)
    241 
    242 DEBUG_IMPORT renderdoc_start_frame_capture_fn *start_frame_capture;
    243 DEBUG_IMPORT renderdoc_end_frame_capture_fn   *end_frame_capture;
    244 #define start_renderdoc_capture() if (start_frame_capture) start_frame_capture(vk_renderdoc_instance_handle(), 0)
    245 #define end_renderdoc_capture()   if (end_frame_capture)   end_frame_capture(vk_renderdoc_instance_handle(), 0)
    246 #define renderdoc_attached(...)   (start_frame_capture != 0)
    247 
    248 #else
    249 #define start_renderdoc_capture(...)
    250 #define end_renderdoc_capture(...)
    251 #define renderdoc_attached(...) (0)
    252 #endif
    253 
    254 #include "util_os_ui.c"
    255 
    256 ///////////////////////////////
    257 // NOTE: CUDA Library Bindings
    258 
    259 #define cuda_supported() (cuda_init != cuda_init_stub)
    260 #define CUDA_INIT_FN(name) void name(u32 *input_dims, u32 *decoded_dims)
    261 typedef CUDA_INIT_FN(cuda_init_fn);
    262 CUDA_INIT_FN(cuda_init_stub) {}
    263 
    264 #define CUDA_REGISTER_BUFFERS_FN(name) void name(u32 *rf_data_ssbos, u32 rf_buffer_count, u32 raw_data_ssbo)
    265 typedef CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_fn);
    266 CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_stub) {}
    267 
    268 #define CUDA_HILBERT_FN(name) void name(u32 input_buffer_idx, u32 output_buffer_idx)
    269 typedef CUDA_HILBERT_FN(cuda_hilbert_fn);
    270 CUDA_HILBERT_FN(cuda_hilbert_stub) {}
    271 
    272 #define CUDA_SET_CHANNEL_MAPPING_FN(name) void name(i16 *channel_mapping)
    273 typedef CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_fn);
    274 CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_stub) {}
    275 
    276 #define CUDALibraryProcedureList \
    277 	X(hilbert,             "cuda_hilbert")             \
    278 	X(init,                "init_cuda_configuration")  \
    279 	X(register_buffers,    "register_cuda_buffers")    \
    280 	X(set_channel_mapping, "cuda_set_channel_mapping")
    281 
    282 #define X(name, ...) DEBUG_IMPORT cuda_## name ##_fn *cuda_## name;
    283 CUDALibraryProcedureList
    284 #undef X
    285 
    286 /////////////////////////////////////
    287 // NOTE: Core Beamformer Definitions
    288 
    289 #include "beamformer_compute_stats.c"
    290 #include "beamformer_shared_memory.c"
    291 
    292 typedef struct {
    293 	BeamformerFilterParameters parameters;
    294 	f32                        time_delay;
    295 	i32                        length;
    296 	void                      *data;
    297 } BeamformerFilter;
    298 
    299 typedef struct {
    300 	uv3 layout;
    301 	uv3 dispatch;
    302 	u32 compile_flags;
    303 	b32 uses_heap;
    304 	BeamformerDataKind input_data_kind;
    305 	BeamformerDataKind output_data_kind;
    306 	BeamformerShaderBakeParameters bake;
    307 } BeamformerShaderDescriptor;
    308 
    309 typedef struct BeamformerComputePlan BeamformerComputePlan;
    310 struct BeamformerComputePlan {
    311 	BeamformerComputePipeline pipeline;
    312 
    313 	VulkanHandle vulkan_pipelines[BeamformerMaxComputeShaderStages];
    314 
    315 	u32 first_image_shader_index;
    316 	u32 channel_count;
    317 	u32 raw_channel_byte_stride;
    318 
    319 	u32 dirty_programs;
    320 
    321 	BeamformerAcquisitionKind acquisition_kind;
    322 	u32                       acquisition_count;
    323 	BeamformerContrastMode    contrast_mode;
    324 
    325 	u32 rf_size;
    326 	b32 iq_pipeline;
    327 
    328 	u64 ui_last_voxel_transform_expected_hash;
    329 	m4  ui_voxel_transform;
    330 	// NOTE(rnp): voxel transform passed in from work item
    331 	m4  last_voxel_transform;
    332 
    333 	iv3 output_points;
    334 	i32 average_frames;
    335 
    336 	// TODO(rnp): specialization constants
    337 	v2  xdc_element_pitch;
    338 	m4  xdc_transform;
    339 
    340 	u32  readi_group;
    341 
    342 	u64 last_gpu_heap_pointer;
    343 	GPUBuffer gpu_temp_arena;
    344 
    345 	BeamformerFilterParameters filter_parameters[BeamformerFilterSlots];
    346 
    347 	u128 shader_hashes[BeamformerMaxComputeShaderStages];
    348 	BeamformerShaderDescriptor shader_descriptors[BeamformerMaxComputeShaderStages];
    349 
    350 	BeamformerComputePlan *next;
    351 };
    352 
    353 typedef struct {
    354 	u64 upload_complete_values[BeamformerMaxRawDataFramesInFlight];
    355 	u64 compute_complete_values[BeamformerMaxRawDataFramesInFlight];
    356 
    357 	GPUBuffer    buffer;
    358 	GPUSemaphore upload_semaphore;
    359 
    360 	u64 active_rf_size;
    361 
    362 	u64 timestamp;
    363 
    364 	u64 insertion_index;
    365 	u64 compute_index;
    366 } BeamformerRFBuffer;
    367 
    368 typedef struct {
    369 	BeamformerComputeStatsTable table;
    370 	f32 average_times[BeamformerShaderKind_Count];
    371 
    372 	u64 last_rf_timer_count;
    373 	f32 rf_time_delta_average;
    374 
    375 	u32 latest_frame_index;
    376 	u32 latest_rf_index;
    377 } ComputeShaderStats;
    378 
    379 /* TODO(rnp): maybe this also gets used for CPU timing info as well */
    380 typedef enum {
    381 	ComputeTimingInfoKind_ComputeFrameBegin,
    382 	ComputeTimingInfoKind_ComputeFrameEnd,
    383 	ComputeTimingInfoKind_Shader,
    384 	ComputeTimingInfoKind_RF_Data,
    385 } ComputeTimingInfoKind;
    386 
    387 typedef struct {
    388 	u64 timer_count;
    389 	ComputeTimingInfoKind kind;
    390 	union {
    391 		struct {
    392 			static_assert(BeamformerShaderKind_Count <= U16_MAX, "");
    393 			u16 shader;
    394 			u16 shader_slot;
    395 		};
    396 	};
    397 } ComputeTimingInfo;
    398 
    399 typedef struct {
    400 	u32 write_index;
    401 	u32 read_index;
    402 	b32 compute_frame_active;
    403 
    404 	u32                  in_flight_shader_count;
    405 	BeamformerShaderKind in_flight_shader_ids[BeamformerMaxComputeShaderStages];
    406 
    407 	ComputeTimingInfo buffer[4096];
    408 } ComputeTimingTable;
    409 
    410 typedef struct {
    411 	BeamformerRFBuffer      *rf_buffer;
    412 	BeamformerSharedMemory  *shared_memory;
    413 	i64                      shared_memory_size;
    414 	ComputeTimingTable      *compute_timing_table;
    415 	i32                     *compute_worker_sync;
    416 } BeamformerUploadThreadContext;
    417 
    418 typedef struct {
    419 	u64 gpu_pointer;
    420 	u64 timeline_valid_value;
    421 
    422 	/* NOTE: for use when displaying either prebeamformed frames or on the current frame
    423 	 * when we intend to recompute on the next frame */
    424 	m4  voxel_transform;
    425 
    426 	iv3 points;
    427 
    428 	u32                       id;
    429 	u32                       compound_count;
    430 	u16                       parameter_block;
    431 	u16                       transmit_receive_orientation;
    432 	f32                       focus_depth;
    433 	f32                       steering_angle;
    434 
    435 	BeamformerDataKind        data_kind;
    436 	BeamformerAcquisitionKind acquisition_kind;
    437 	BeamformerContrastMode    contrast_mode;
    438 	BeamformerViewPlaneTag    view_plane_tag;
    439 } BeamformerFrame;
    440 
    441 /* NOTE(rnp): backing storage for beamformed frames. The amount of backlog frames
    442  * is dependant on the currently requested output size. */
    443 typedef struct {
    444 	GPUBuffer   buffer[1];
    445 
    446 	u64         next_offset;
    447 	u64         counter;
    448 
    449 	BeamformerFrame frames[BeamformerMaxBacklogFrames];
    450 } BeamformerFrameBacklog;
    451 
    452 typedef struct {
    453 	BeamformerRFBuffer rf_buffer;
    454 
    455 	BeamformerComputePlan *compute_plans[BeamformerMaxParameterBlocks];
    456 	BeamformerComputePlan *compute_plan_freelist;
    457 
    458 	// NOTE(rnp): used to ping pong data between compute stages.
    459 	#define PING_PONG_BUFFER_SLOTS 2
    460 	GPUBuffer ping_pong_buffer;
    461 	OSHandle  ping_pong_export_handle;
    462 	u32       ping_pong_input_index;
    463 
    464 	f32 processing_progress;
    465 	b32 processing_compute;
    466 
    467 	BeamformerFrameBacklog backlog;
    468 } BeamformerComputeContext;
    469 
    470 typedef struct {
    471 	Arena    *arena;
    472 	iptr      user_context;
    473 	i32       sync_variable;
    474 	b32       awake;
    475 	OSThread  handle;
    476 } GLWorkerThreadContext;
    477 
    478 typedef struct {
    479 	str8                 name;
    480 	BeamformerRegisters *registers;
    481 } BeamformerCommand;
    482 
    483 typedef struct BeamformerRegistersNode BeamformerRegistersNode;
    484 struct BeamformerRegistersNode {
    485 	BeamformerRegistersNode *next;
    486 	BeamformerRegisters      v;
    487 };
    488 
    489 typedef struct BeamformerCommandNode BeamformerCommandNode;
    490 struct BeamformerCommandNode {
    491 	BeamformerCommandNode *next;
    492 	BeamformerCommandNode *prev;
    493 	BeamformerCommand      command;
    494 };
    495 
    496 typedef struct {
    497 	BeamformerCommandNode *first;
    498 	BeamformerCommandNode *last;
    499 	u64                    count;
    500 } BeamformerCommandList;
    501 
    502 typedef enum {
    503 	BeamformerState_Uninitialized = 0,
    504 	BeamformerState_Running,
    505 	BeamformerState_ShouldClose,
    506 	BeamformerState_Terminated,
    507 } BeamformerState;
    508 
    509 typedef enum {
    510 	RulerState_None,
    511 	RulerState_Start,
    512 	RulerState_Hold,
    513 	RulerState_Count,
    514 } RulerState;
    515 
    516 typedef struct {
    517 	v3 start;
    518 	v3 end;
    519 	RulerState state;
    520 } Ruler;
    521 
    522 typedef struct {
    523 	f32 t;
    524 	f32 scale;
    525 } BeamformerUIBlinker;
    526 
    527 #define BEAMFORMER_FRAME_VIEW_KIND_LIST \
    528 	X(Latest,   "Latest")     \
    529 	X(3DXPlane, "3D X-Plane") \
    530 	X(Copy,     "Copy")       \
    531 
    532 typedef enum {
    533 	#define X(kind, ...) BeamformerFrameViewKind_##kind,
    534 	BEAMFORMER_FRAME_VIEW_KIND_LIST
    535 	#undef X
    536 	BeamformerFrameViewKind_Count,
    537 } BeamformerFrameViewKind;
    538 
    539 typedef struct BeamformerFrameView BeamformerFrameView;
    540 struct BeamformerFrameView {
    541 	BeamformerFrameViewKind kind;
    542 	b32 dirty;
    543 
    544 	// NOTE(rnp): for FrameViewKindCopy
    545 	GPUBuffer copy_buffer;
    546 
    547 	GPUImage colour_image;
    548 	// NOTE(rnp): temporary, on w32 we must hold onto this when importing vulkan data to OpenGL
    549 	OSHandle export_handle;
    550 	u32      memory_object;
    551 	u32      texture;
    552 
    553 	b32 log_scale;
    554 	f32 threshold;
    555 	f32 dynamic_range;
    556 	f32 gamma;
    557 
    558 	union {
    559 		/* BeamformerFrameViewKind_Latest/BeamformerFrameViewKind_Copy */
    560 		struct {
    561 			b32   scale_bar_active[2];
    562 			Ruler ruler;
    563 			BeamformerViewPlaneTag view_plane;
    564 			BeamformerFrame frame;
    565 		};
    566 
    567 		/* BeamformerFrameViewKind_3DXPlane */
    568 		struct {
    569 			f32 hot_t[BeamformerViewPlaneTag_Count];
    570 			//b32 plane_active[BeamformerViewPlaneTag_Count];
    571 			b32 plane_active[BeamformerViewPlaneTag_Count - 2];
    572 			i32 plane_drag_index;
    573 			f32 rotation;
    574 			b32 demo;
    575 			v3  hit_start_point;
    576 			v3  hit_test_point;
    577 		};
    578 	};
    579 
    580 	BeamformerFrameView *prev, *next;
    581 };
    582 
    583 typedef struct BeamformerUIPanel BeamformerUIPanel;
    584 struct BeamformerUIPanel {
    585 	BeamformerUIPanel *parent;
    586 	BeamformerUIPanel *last_child;
    587 	BeamformerUIPanel *first_child;
    588 	BeamformerUIPanel *previous_sibling;
    589 	BeamformerUIPanel *next_sibling;
    590 
    591 	u64 child_count;
    592 
    593 	BeamformerPanelKind kind;
    594 	union {
    595 		BeamformerFrameView *frame_view;
    596 
    597 		BeamformerUIPanel *tab_focus;
    598 
    599 		BeamformerUIBlinker compute_stats_broken_shader_blinker;
    600 		BeamformerUIBlinker live_imaging_save_button_blinker;
    601 
    602 		struct {
    603 			u32 parameter_block;
    604 			b8  expand_emission;
    605 			b8  expand_coordinate[2];
    606 		} parameter_listing;
    607 
    608 		struct {
    609 			Axis2 axis;
    610 			f32   fraction;
    611 		} split;
    612 	} u;
    613 };
    614 
    615 typedef struct {
    616 	BeamformerState state;
    617 
    618 	iv2 window_size;
    619 
    620 	Arena *arena;
    621 	Arena *ui_arena;
    622 	void  *ui;
    623 	u32    ui_dirty_parameter_blocks;
    624 
    625 	u64    frame_timestamp;
    626 	u64    frame_index;
    627 	Arena *frame_arenas[2];
    628 
    629 	BeamformerUIPanel *auto_live_control_panel;
    630 	u64                live_imaging_active_frame;
    631 	b32                live_imaging_active;
    632 
    633 	BeamformerRegistersNode  base_registers;
    634 	BeamformerRegistersNode *registers;
    635 
    636 	BeamformerCommandList command_queues[2];
    637 
    638 	Stream error_stream;
    639 
    640 	BeamformerSharedMemory *shared_memory;
    641 	i64                     shared_memory_size;
    642 
    643 	BeamformerFrame *latest_frame;
    644 
    645 	// TODO(rnp): track elsewhere
    646 	b32 render_shader_updated;
    647 
    648 	/* NOTE: this will only be used when we are averaging */
    649 	u32             averaged_frame_index;
    650 	BeamformerFrame averaged_frames[2];
    651 
    652 	GLWorkerThreadContext  upload_worker;
    653 	GLWorkerThreadContext  compute_worker;
    654 
    655 	BeamformerComputeContext compute_context;
    656 
    657 	ComputeShaderStats compute_shader_stats[1];
    658 	ComputeTimingTable compute_timing_table[1];
    659 
    660 	BeamformWorkQueue  beamform_work_queue[1];
    661 
    662 	// TODO(rnp): this should go to the UI eventually
    663 	OSWindow main_window;
    664 } BeamformerCtx;
    665 
    666 typedef enum {
    667 	BeamformerFileReloadKind_ComputeShader,
    668 	BeamformerFileReloadKind_RenderShader,
    669 } BeamformerFileReloadKind;
    670 
    671 typedef struct {
    672 	BeamformerShaderKind shader;
    673 	VulkanHandle *       pipeline;
    674 } BeamformerShaderReloadData;
    675 
    676 typedef struct {
    677 	BeamformerShaderKind  shader;
    678 	VulkanShaderKind      shader_kind;
    679 
    680 	// NOTE(rnp): based on BakeShaders compile time value
    681 	str8                  filename_or_data;
    682 
    683 	BeamformerShaderDescriptor *shader_descriptor;
    684 
    685 	u64 gpu_heap_pointer;
    686 	uv3 layout;
    687 } BeamformerShaderReloadInfo;
    688 
    689 typedef struct {
    690 	BeamformerFileReloadKind kind;
    691 	union {
    692 		BeamformerShaderReloadData shader_reload;
    693 	};
    694 } BeamformerFileReloadContext;
    695 
    696 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(BeamformerCtx *ctx, Arena *arena)
    697 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn);
    698 
    699 #define BEAMFORMER_RF_UPLOAD_FN(name) void name(BeamformerUploadThreadContext *ctx)
    700 typedef BEAMFORMER_RF_UPLOAD_FN(beamformer_rf_upload_fn);
    701 
    702 #define BEAMFORMER_DEBUG_UI_DEINIT_FN(name) void name(BeamformerCtx *ctx)
    703 typedef BEAMFORMER_DEBUG_UI_DEINIT_FN(beamformer_debug_ui_deinit_fn);
    704 
    705 #endif /* BEAMFORMER_INTERNAL_H */