beamformer_internal.h (10317B)
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.meta.c" 11 #include "generated/beamformer_shaders.c" 12 13 #include <raylib_extended.h> 14 #include <rlgl.h> 15 16 #include "threads.c" 17 #include "util_gl.c" 18 #include "util_os.c" 19 20 #define beamformer_info(s) s8("[info] " s "\n") 21 22 #define os_path_separator() (s8){.data = &os_system_info()->path_separator_byte, .len = 1} 23 24 /////////////////////////////// 25 // NOTE: CUDA Library Bindings 26 27 #define CUDA_INIT_FN(name) void name(u32 *input_dims, u32 *decoded_dims) 28 typedef CUDA_INIT_FN(cuda_init_fn); 29 CUDA_INIT_FN(cuda_init_stub) {} 30 31 #define CUDA_REGISTER_BUFFERS_FN(name) void name(u32 *rf_data_ssbos, u32 rf_buffer_count, u32 raw_data_ssbo) 32 typedef CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_fn); 33 CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_stub) {} 34 35 #define CUDA_DECODE_FN(name) void name(size_t input_offset, u32 output_buffer_idx, u32 rf_channel_offset) 36 typedef CUDA_DECODE_FN(cuda_decode_fn); 37 CUDA_DECODE_FN(cuda_decode_stub) {} 38 39 #define CUDA_HILBERT_FN(name) void name(u32 input_buffer_idx, u32 output_buffer_idx) 40 typedef CUDA_HILBERT_FN(cuda_hilbert_fn); 41 CUDA_HILBERT_FN(cuda_hilbert_stub) {} 42 43 #define CUDA_SET_CHANNEL_MAPPING_FN(name) void name(i16 *channel_mapping) 44 typedef CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_fn); 45 CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_stub) {} 46 47 #define CUDALibraryProcedureList \ 48 X(decode, "cuda_decode") \ 49 X(hilbert, "cuda_hilbert") \ 50 X(init, "init_cuda_configuration") \ 51 X(register_buffers, "register_cuda_buffers") \ 52 X(set_channel_mapping, "cuda_set_channel_mapping") 53 54 #define X(name, ...) DEBUG_IMPORT cuda_## name ##_fn *cuda_## name; 55 CUDALibraryProcedureList 56 #undef X 57 58 ///////////////////////////////////// 59 // NOTE: Core Beamformer Definitions 60 61 /* TODO(rnp): this should be a UBO */ 62 #define FRAME_VIEW_MODEL_MATRIX_LOC 0 63 #define FRAME_VIEW_VIEW_MATRIX_LOC 1 64 #define FRAME_VIEW_PROJ_MATRIX_LOC 2 65 #define FRAME_VIEW_DYNAMIC_RANGE_LOC 3 66 #define FRAME_VIEW_THRESHOLD_LOC 4 67 #define FRAME_VIEW_GAMMA_LOC 5 68 #define FRAME_VIEW_LOG_SCALE_LOC 6 69 #define FRAME_VIEW_BB_COLOUR_LOC 7 70 #define FRAME_VIEW_BB_FRACTION_LOC 8 71 #define FRAME_VIEW_SOLID_BB_LOC 10 72 73 #define FRAME_VIEW_BB_COLOUR 0.92, 0.88, 0.78, 1.0 74 #define FRAME_VIEW_BB_FRACTION 0.007f 75 76 #define FRAME_VIEW_RENDER_TARGET_SIZE 1024, 1024 77 78 typedef struct { 79 u32 shader; 80 u32 framebuffers[2]; /* [0] -> multisample target, [1] -> normal target for resolving */ 81 u32 renderbuffers[2]; /* only used for 3D views, size is fixed */ 82 b32 updated; 83 } FrameViewRenderContext; 84 85 #include "beamformer_parameters.h" 86 #include "beamformer_shared_memory.c" 87 88 typedef struct { 89 iptr elements_offset; 90 i32 elements; 91 u32 buffer; 92 u32 vao; 93 } BeamformerRenderModel; 94 95 typedef struct { 96 BeamformerFilterParameters parameters; 97 f32 time_delay; 98 i32 length; 99 u32 ssbo; 100 } BeamformerFilter; 101 102 /* X(name, type, gltype) */ 103 #define BEAMFORMER_DAS_UBO_PARAM_LIST \ 104 X(voxel_transform, m4, mat4) \ 105 X(xdc_transform, m4, mat4) \ 106 X(xdc_element_pitch, v2, vec2) 107 108 typedef alignas(16) struct { 109 #define X(name, type, ...) type name; 110 BEAMFORMER_DAS_UBO_PARAM_LIST 111 #undef X 112 float _pad[2]; 113 } BeamformerDASUBO; 114 static_assert((sizeof(BeamformerDASUBO) & 15) == 0, "UBO size must be a multiple of 16"); 115 116 /* TODO(rnp): need 1 UBO per filter slot */ 117 #define BEAMFORMER_COMPUTE_UBO_LIST \ 118 X(DAS, BeamformerDASUBO, das) 119 120 #define X(k, ...) BeamformerComputeUBOKind_##k, 121 typedef enum {BEAMFORMER_COMPUTE_UBO_LIST BeamformerComputeUBOKind_Count} BeamformerComputeUBOKind; 122 #undef X 123 124 // X(kind, gl_kind, texture_format, pixel_type) 125 #define BEAMFORMER_COMPUTE_TEXTURE_LIST \ 126 X(FocalVectors, GL_RG32F, GL_RG, GL_FLOAT) \ 127 X(SparseElements, GL_R16I, GL_RED_INTEGER, GL_SHORT) \ 128 X(TransmitReceiveOrientations, GL_R8I, GL_RED_INTEGER, GL_BYTE) 129 130 #define BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL \ 131 BEAMFORMER_COMPUTE_TEXTURE_LIST \ 132 X(Hadamard, GL_R32F) 133 134 typedef enum { 135 #define X(k, ...) BeamformerComputeTextureKind_##k, 136 BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL 137 #undef X 138 BeamformerComputeTextureKind_Count 139 } BeamformerComputeTextureKind; 140 static_assert((BeamformerComputeTextureKind_Count - 1) == BeamformerComputeTextureKind_Hadamard, 141 "BeamformerComputeTextureKind_Hadamard must be end of TextureKinds"); 142 143 typedef struct { 144 uv3 layout; 145 uv3 dispatch; 146 BeamformerShaderBakeParameters bake; 147 } BeamformerShaderDescriptor; 148 149 typedef struct BeamformerComputePlan BeamformerComputePlan; 150 struct BeamformerComputePlan { 151 BeamformerComputePipeline pipeline; 152 153 u32 programs[BeamformerMaxComputeShaderStages]; 154 155 u32 dirty_programs; 156 157 BeamformerAcquisitionKind acquisition_kind; 158 u32 acquisition_count; 159 160 u32 rf_size; 161 i32 hadamard_order; 162 b32 iq_pipeline; 163 164 v3 min_coordinate; 165 v3 max_coordinate; 166 iv3 output_points; 167 i32 average_frames; 168 169 u32 textures[BeamformerComputeTextureKind_Count]; 170 u32 ubos[BeamformerComputeUBOKind_Count]; 171 172 BeamformerFilter filters[BeamformerFilterSlots]; 173 174 #define X(k, type, name) type name ##_ubo_data; 175 BEAMFORMER_COMPUTE_UBO_LIST 176 #undef X 177 178 u128 shader_hashes[BeamformerMaxComputeShaderStages]; 179 BeamformerShaderDescriptor shader_descriptors[BeamformerMaxComputeShaderStages]; 180 181 BeamformerComputePlan *next; 182 }; 183 184 typedef struct { 185 GLsync upload_syncs[BeamformerMaxRawDataFramesInFlight]; 186 GLsync compute_syncs[BeamformerMaxRawDataFramesInFlight]; 187 188 u8 *buffer; 189 190 u32 ssbo; 191 192 u32 size; 193 u32 active_rf_size; 194 195 u32 data_timestamp_query; 196 197 u32 insertion_index; 198 u32 compute_index; 199 } BeamformerRFBuffer; 200 201 typedef struct { 202 BeamformerRFBuffer rf_buffer; 203 204 BeamformerComputePlan *compute_plans[BeamformerMaxParameterBlockSlots]; 205 BeamformerComputePlan *compute_plan_freelist; 206 207 /* NOTE(rnp): two interstage ssbos are allocated so that they may be used to 208 * ping pong data between compute stages */ 209 u32 ping_pong_ssbos[2]; 210 u32 last_output_ssbo_index; 211 212 u32 ping_pong_ssbo_size; 213 214 f32 processing_progress; 215 b32 processing_compute; 216 217 u32 shader_timer_ids[BeamformerMaxComputeShaderStages]; 218 219 BeamformerRenderModel unit_cube_model; 220 } BeamformerComputeContext; 221 222 typedef struct { 223 BeamformerComputeStatsTable table; 224 f32 average_times[BeamformerShaderKind_Count]; 225 226 u64 last_rf_timer_count; 227 f32 rf_time_delta_average; 228 229 u32 latest_frame_index; 230 u32 latest_rf_index; 231 } ComputeShaderStats; 232 233 /* TODO(rnp): maybe this also gets used for CPU timing info as well */ 234 typedef enum { 235 ComputeTimingInfoKind_ComputeFrameBegin, 236 ComputeTimingInfoKind_ComputeFrameEnd, 237 ComputeTimingInfoKind_Shader, 238 ComputeTimingInfoKind_RF_Data, 239 } ComputeTimingInfoKind; 240 241 typedef struct { 242 u64 timer_count; 243 ComputeTimingInfoKind kind; 244 union { 245 BeamformerShaderKind shader; 246 }; 247 } ComputeTimingInfo; 248 249 typedef struct { 250 u32 write_index; 251 u32 read_index; 252 b32 compute_frame_active; 253 ComputeTimingInfo buffer[4096]; 254 } ComputeTimingTable; 255 256 typedef struct { 257 BeamformerRFBuffer * rf_buffer; 258 BeamformerSharedMemory * shared_memory; 259 ComputeTimingTable * compute_timing_table; 260 i32 * compute_worker_sync; 261 } BeamformerUploadThreadContext; 262 263 struct BeamformerFrame { 264 u32 texture; 265 b32 ready_to_present; 266 267 iv3 dim; 268 i32 mips; 269 270 /* NOTE: for use when displaying either prebeamformed frames or on the current frame 271 * when we intend to recompute on the next frame */ 272 v3 min_coordinate; 273 v3 max_coordinate; 274 275 // metadata 276 GLenum gl_kind; 277 u32 id; 278 u32 compound_count; 279 u32 parameter_block; 280 BeamformerAcquisitionKind acquisition_kind; 281 BeamformerViewPlaneTag view_plane_tag; 282 283 BeamformerFrame *next; 284 }; 285 286 typedef struct { 287 OSThread handle; 288 289 Arena arena; 290 iptr window_handle; 291 iptr gl_context; 292 iptr user_context; 293 i32 sync_variable; 294 b32 awake; 295 } GLWorkerThreadContext; 296 297 typedef enum { 298 BeamformerState_Uninitialized = 0, 299 BeamformerState_Running, 300 BeamformerState_ShouldClose, 301 BeamformerState_Terminated, 302 } BeamformerState; 303 304 typedef struct { 305 BeamformerState state; 306 307 iv2 window_size; 308 309 Arena arena; 310 Arena ui_backing_store; 311 void *ui; 312 u32 ui_dirty_parameter_blocks; 313 314 u64 frame_timestamp; 315 316 BeamformerComputeContext compute_context; 317 318 /* TODO(rnp): ideally this would go in the UI but its hard to manage with the UI 319 * destroying itself on hot-reload */ 320 FrameViewRenderContext frame_view_render_context; 321 322 Stream error_stream; 323 324 BeamformWorkQueue *beamform_work_queue; 325 326 ComputeShaderStats *compute_shader_stats; 327 ComputeTimingTable *compute_timing_table; 328 329 BeamformerSharedMemory *shared_memory; 330 331 BeamformerFrame beamform_frames[BeamformerMaxSavedFrames]; 332 BeamformerFrame *latest_frame; 333 u32 next_render_frame_index; 334 u32 display_frame_index; 335 336 /* NOTE: this will only be used when we are averaging */ 337 u32 averaged_frame_index; 338 BeamformerFrame averaged_frames[2]; 339 340 GLWorkerThreadContext upload_worker; 341 GLWorkerThreadContext compute_worker; 342 } BeamformerCtx; 343 #define BeamformerContextMemory(m) (BeamformerCtx *)align_pointer_up((m), alignof(BeamformerCtx)); 344 345 typedef enum { 346 BeamformerFileReloadKind_Shader, 347 BeamformerFileReloadKind_ComputeShader, 348 } BeamformerFileReloadKind; 349 350 typedef struct BeamformerShaderReloadContext BeamformerShaderReloadContext; 351 struct BeamformerShaderReloadContext { 352 BeamformerShaderReloadContext * link; 353 s8 header; 354 GLenum gl_type; 355 i32 reloadable_info_index; 356 }; 357 358 typedef struct { 359 BeamformerFileReloadKind kind; 360 union { 361 BeamformerShaderReloadContext * shader_reload_context; 362 BeamformerShaderKind compute_shader_kind; 363 }; 364 } BeamformerFileReloadContext; 365 366 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(iptr user_context, Arena *arena, iptr gl_context) 367 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn); 368 369 #define BEAMFORMER_RF_UPLOAD_FN(name) void name(BeamformerUploadThreadContext *ctx) 370 typedef BEAMFORMER_RF_UPLOAD_FN(beamformer_rf_upload_fn); 371 372 #define BEAMFORMER_DEBUG_UI_DEINIT_FN(name) void name(BeamformerCtx *ctx) 373 typedef BEAMFORMER_DEBUG_UI_DEINIT_FN(beamformer_debug_ui_deinit_fn); 374 375 #endif /* BEAMFORMER_INTERNAL_H */