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 (9979B)


      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 /* TODO(rnp): need 1 UBO per filter slot */
    103 #define BEAMFORMER_COMPUTE_UBO_LIST \
    104 	X(DAS,        BeamformerDASPushConstants,    das)
    105 
    106 #define X(k, ...) BeamformerComputeUBOKind_##k,
    107 typedef enum {BEAMFORMER_COMPUTE_UBO_LIST BeamformerComputeUBOKind_Count} BeamformerComputeUBOKind;
    108 #undef X
    109 
    110 // X(kind, gl_kind, texture_format, pixel_type)
    111 #define BEAMFORMER_COMPUTE_TEXTURE_LIST \
    112 	X(FocalVectors,                GL_RG32F, GL_RG,          GL_FLOAT) \
    113 	X(SparseElements,              GL_R16I,  GL_RED_INTEGER, GL_SHORT) \
    114 	X(TransmitReceiveOrientations, GL_R8I,   GL_RED_INTEGER, GL_BYTE)
    115 
    116 #define BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL \
    117 	BEAMFORMER_COMPUTE_TEXTURE_LIST \
    118 	X(Hadamard,       GL_R16F)
    119 
    120 typedef enum {
    121 	#define X(k, ...) BeamformerComputeTextureKind_##k,
    122 	BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL
    123 	#undef X
    124 	BeamformerComputeTextureKind_Count
    125 } BeamformerComputeTextureKind;
    126 static_assert((BeamformerComputeTextureKind_Count - 1) == BeamformerComputeTextureKind_Hadamard,
    127               "BeamformerComputeTextureKind_Hadamard must be end of TextureKinds");
    128 
    129 typedef struct {
    130 	uv3 layout;
    131 	uv3 dispatch;
    132 	BeamformerShaderBakeParameters bake;
    133 } BeamformerShaderDescriptor;
    134 
    135 typedef struct BeamformerComputePlan BeamformerComputePlan;
    136 struct BeamformerComputePlan {
    137 	BeamformerComputePipeline pipeline;
    138 
    139 	u32 programs[BeamformerMaxComputeShaderStages];
    140 
    141 	u32 dirty_programs;
    142 
    143 	BeamformerAcquisitionKind acquisition_kind;
    144 	u32                       acquisition_count;
    145 
    146 	u32 rf_size;
    147 	i32 hadamard_order;
    148 	b32 iq_pipeline;
    149 
    150 	m4  voxel_transform;
    151 	m4  ui_voxel_transform;
    152 
    153 	iv3 output_points;
    154 	i32 average_frames;
    155 
    156 	u32 textures[BeamformerComputeTextureKind_Count];
    157 	u32 ubos[BeamformerComputeUBOKind_Count];
    158 
    159 	BeamformerFilter filters[BeamformerFilterSlots];
    160 
    161 	#define X(k, type, name) type name ##_ubo_data;
    162 	BEAMFORMER_COMPUTE_UBO_LIST
    163 	#undef X
    164 
    165 	u128 shader_hashes[BeamformerMaxComputeShaderStages];
    166 	BeamformerShaderDescriptor shader_descriptors[BeamformerMaxComputeShaderStages];
    167 
    168 	BeamformerComputePlan *next;
    169 };
    170 
    171 typedef struct {
    172 	GLsync upload_syncs[BeamformerMaxRawDataFramesInFlight];
    173 	GLsync compute_syncs[BeamformerMaxRawDataFramesInFlight];
    174 
    175 	u8 *buffer;
    176 
    177 	u32 ssbo;
    178 
    179 	u32 size;
    180 	u32 active_rf_size;
    181 
    182 	u32 data_timestamp_query;
    183 
    184 	u32 insertion_index;
    185 	u32 compute_index;
    186 } BeamformerRFBuffer;
    187 
    188 typedef struct {
    189 	BeamformerRFBuffer rf_buffer;
    190 
    191 	BeamformerComputePlan *compute_plans[BeamformerMaxParameterBlocks];
    192 	BeamformerComputePlan *compute_plan_freelist;
    193 
    194 	/* NOTE(rnp): two interstage ssbos are allocated so that they may be used to
    195 	 * ping pong data between compute stages */
    196 	u32 ping_pong_ssbos[2];
    197 	u32 last_output_ssbo_index;
    198 
    199 	u32 ping_pong_ssbo_size;
    200 
    201 	f32 processing_progress;
    202 	b32 processing_compute;
    203 
    204 	u32 shader_timer_ids[BeamformerMaxComputeShaderStages];
    205 
    206 	BeamformerRenderModel unit_cube_model;
    207 } BeamformerComputeContext;
    208 
    209 typedef struct {
    210 	BeamformerComputeStatsTable table;
    211 	f32 average_times[BeamformerShaderKind_Count];
    212 
    213 	u64 last_rf_timer_count;
    214 	f32 rf_time_delta_average;
    215 
    216 	u32 latest_frame_index;
    217 	u32 latest_rf_index;
    218 } ComputeShaderStats;
    219 
    220 /* TODO(rnp): maybe this also gets used for CPU timing info as well */
    221 typedef enum {
    222 	ComputeTimingInfoKind_ComputeFrameBegin,
    223 	ComputeTimingInfoKind_ComputeFrameEnd,
    224 	ComputeTimingInfoKind_Shader,
    225 	ComputeTimingInfoKind_RF_Data,
    226 } ComputeTimingInfoKind;
    227 
    228 typedef struct {
    229 	u64 timer_count;
    230 	ComputeTimingInfoKind kind;
    231 	union {
    232 		BeamformerShaderKind shader;
    233 	};
    234 } ComputeTimingInfo;
    235 
    236 typedef struct {
    237 	u32 write_index;
    238 	u32 read_index;
    239 	b32 compute_frame_active;
    240 	ComputeTimingInfo buffer[4096];
    241 } ComputeTimingTable;
    242 
    243 typedef struct {
    244 	BeamformerRFBuffer      *rf_buffer;
    245 	BeamformerSharedMemory  *shared_memory;
    246 	i64                      shared_memory_size;
    247 	ComputeTimingTable      *compute_timing_table;
    248 	i32                     *compute_worker_sync;
    249 } BeamformerUploadThreadContext;
    250 
    251 struct BeamformerFrame {
    252 	u32 texture;
    253 	b32 ready_to_present;
    254 
    255 	iv3 dim;
    256 	i32 mips;
    257 
    258 	/* NOTE: for use when displaying either prebeamformed frames or on the current frame
    259 	 * when we intend to recompute on the next frame */
    260 	m4  voxel_transform;
    261 
    262 	// metadata
    263 	GLenum                    gl_kind;
    264 	u32                       id;
    265 	u32                       compound_count;
    266 	u32                       parameter_block;
    267 	BeamformerAcquisitionKind acquisition_kind;
    268 	BeamformerViewPlaneTag    view_plane_tag;
    269 
    270 	BeamformerFrame *next;
    271 };
    272 
    273 typedef struct {
    274 	OSThread handle;
    275 
    276 	Arena arena;
    277 	iptr  window_handle;
    278 	iptr  gl_context;
    279 	iptr  user_context;
    280 	i32   sync_variable;
    281 	b32   awake;
    282 } GLWorkerThreadContext;
    283 
    284 typedef enum {
    285 	BeamformerState_Uninitialized = 0,
    286 	BeamformerState_Running,
    287 	BeamformerState_ShouldClose,
    288 	BeamformerState_Terminated,
    289 } BeamformerState;
    290 
    291 typedef struct {
    292 	BeamformerState state;
    293 
    294 	iv2 window_size;
    295 
    296 	Arena  arena;
    297 	Arena  ui_backing_store;
    298 	void  *ui;
    299 	u32    ui_dirty_parameter_blocks;
    300 
    301 	u64    frame_timestamp;
    302 
    303 	BeamformerComputeContext compute_context;
    304 
    305 	/* TODO(rnp): ideally this would go in the UI but its hard to manage with the UI
    306 	 * destroying itself on hot-reload */
    307 	FrameViewRenderContext frame_view_render_context;
    308 
    309 	Stream error_stream;
    310 
    311 	BeamformWorkQueue *beamform_work_queue;
    312 
    313 	ComputeShaderStats *compute_shader_stats;
    314 	ComputeTimingTable *compute_timing_table;
    315 
    316 	BeamformerSharedMemory *shared_memory;
    317 	i64                     shared_memory_size;
    318 
    319 	BeamformerFrame beamform_frames[BeamformerMaxBacklogFrames];
    320 	BeamformerFrame *latest_frame;
    321 	u32 next_render_frame_index;
    322 	u32 display_frame_index;
    323 
    324 	/* NOTE: this will only be used when we are averaging */
    325 	u32             averaged_frame_index;
    326 	BeamformerFrame averaged_frames[2];
    327 
    328 	GLWorkerThreadContext  upload_worker;
    329 	GLWorkerThreadContext  compute_worker;
    330 } BeamformerCtx;
    331 #define BeamformerContextMemory(m) (BeamformerCtx *)align_pointer_up((m), alignof(BeamformerCtx));
    332 
    333 typedef enum {
    334 	BeamformerFileReloadKind_Shader,
    335 	BeamformerFileReloadKind_ComputeShader,
    336 } BeamformerFileReloadKind;
    337 
    338 typedef struct BeamformerShaderReloadContext BeamformerShaderReloadContext;
    339 struct BeamformerShaderReloadContext {
    340 	BeamformerShaderReloadContext * link;
    341 	s8     header;
    342 	GLenum gl_type;
    343 	i32    reloadable_info_index;
    344 };
    345 
    346 typedef struct {
    347 	BeamformerFileReloadKind kind;
    348 	union {
    349 		BeamformerShaderReloadContext * shader_reload_context;
    350 		BeamformerShaderKind            compute_shader_kind;
    351 	};
    352 } BeamformerFileReloadContext;
    353 
    354 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(iptr user_context, Arena *arena, iptr gl_context)
    355 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn);
    356 
    357 #define BEAMFORMER_RF_UPLOAD_FN(name) void name(BeamformerUploadThreadContext *ctx)
    358 typedef BEAMFORMER_RF_UPLOAD_FN(beamformer_rf_upload_fn);
    359 
    360 #define BEAMFORMER_DEBUG_UI_DEINIT_FN(name) void name(BeamformerCtx *ctx)
    361 typedef BEAMFORMER_DEBUG_UI_DEINIT_FN(beamformer_debug_ui_deinit_fn);
    362 
    363 #endif /* BEAMFORMER_INTERNAL_H */