ogl_beamforming

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

beamformer.h (7635B)


      1 /* See LICENSE for license details. */
      2 #ifndef _BEAMFORMER_H_
      3 #define _BEAMFORMER_H_
      4 
      5 #include <raylib_extended.h>
      6 #include <rlgl.h>
      7 
      8 #include "util.h"
      9 #include "opengl.h"
     10 #include "util_gl.c"
     11 
     12 enum gl_vendor_ids {
     13 	GL_VENDOR_AMD,
     14 	GL_VENDOR_ARM,
     15 	GL_VENDOR_INTEL,
     16 	GL_VENDOR_NVIDIA,
     17 };
     18 
     19 typedef struct {
     20 	v2   mouse;
     21 	v2   last_mouse;
     22 	b32  executable_reloaded;
     23 	f32  dt;
     24 } BeamformerInput;
     25 
     26 #define CUDA_INIT_FN(name) void name(u32 *input_dims, u32 *decoded_dims)
     27 typedef CUDA_INIT_FN(cuda_init_fn);
     28 CUDA_INIT_FN(cuda_init_stub) {}
     29 
     30 #define CUDA_REGISTER_BUFFERS_FN(name) void name(u32 *rf_data_ssbos, u32 rf_buffer_count, u32 raw_data_ssbo)
     31 typedef CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_fn);
     32 CUDA_REGISTER_BUFFERS_FN(cuda_register_buffers_stub) {}
     33 
     34 #define CUDA_DECODE_FN(name) void name(size_t input_offset, u32 output_buffer_idx, u32 rf_channel_offset)
     35 typedef CUDA_DECODE_FN(cuda_decode_fn);
     36 CUDA_DECODE_FN(cuda_decode_stub) {}
     37 
     38 #define CUDA_HILBERT_FN(name) void name(u32 input_buffer_idx, u32 output_buffer_idx)
     39 typedef CUDA_HILBERT_FN(cuda_hilbert_fn);
     40 CUDA_HILBERT_FN(cuda_hilbert_stub) {}
     41 
     42 #define CUDA_SET_CHANNEL_MAPPING_FN(name) void name(i16 *channel_mapping)
     43 typedef CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_fn);
     44 CUDA_SET_CHANNEL_MAPPING_FN(cuda_set_channel_mapping_stub) {}
     45 
     46 #define CUDA_LIB_FNS \
     47 	X(decode,              "cuda_decode")              \
     48 	X(hilbert,             "cuda_hilbert")             \
     49 	X(init,                "init_cuda_configuration")  \
     50 	X(register_buffers,    "register_cuda_buffers")    \
     51 	X(set_channel_mapping, "cuda_set_channel_mapping")
     52 
     53 typedef struct {
     54 	void *lib;
     55 	#define X(name, symname) cuda_ ## name ## _fn *name;
     56 	CUDA_LIB_FNS
     57 	#undef X
     58 } CudaLib;
     59 
     60 /* TODO(rnp): this should be a UBO */
     61 #define FRAME_VIEW_MODEL_MATRIX_LOC   0
     62 #define FRAME_VIEW_VIEW_MATRIX_LOC    1
     63 #define FRAME_VIEW_PROJ_MATRIX_LOC    2
     64 #define FRAME_VIEW_DYNAMIC_RANGE_LOC  3
     65 #define FRAME_VIEW_THRESHOLD_LOC      4
     66 #define FRAME_VIEW_GAMMA_LOC          5
     67 #define FRAME_VIEW_LOG_SCALE_LOC      6
     68 #define FRAME_VIEW_BB_COLOUR_LOC      7
     69 #define FRAME_VIEW_BB_FRACTION_LOC    8
     70 #define FRAME_VIEW_SOLID_BB_LOC      10
     71 
     72 #define FRAME_VIEW_BB_COLOUR   0.92, 0.88, 0.78, 1.0
     73 #define FRAME_VIEW_BB_FRACTION 0.007f
     74 
     75 #define FRAME_VIEW_RENDER_TARGET_SIZE 1024, 1024
     76 
     77 typedef struct {
     78 	u32 shader;
     79 	u32 framebuffers[2];  /* [0] -> multisample target, [1] -> normal target for resolving */
     80 	u32 renderbuffers[2]; /* only used for 3D views, size is fixed */
     81 	b32 updated;
     82 } FrameViewRenderContext;
     83 
     84 #include "beamformer_parameters.h"
     85 #include "beamformer_work_queue.h"
     86 
     87 typedef struct {
     88 	iptr elements_offset;
     89 	i32  elements;
     90 	u32  buffer;
     91 	u32  vao;
     92 } BeamformerRenderModel;
     93 
     94 typedef struct {
     95 	u32 programs[BeamformerShaderKind_ComputeCount];
     96 
     97 	/* NOTE: Decoded data is only relevant in the context of a single frame. We use two
     98 	 * buffers so that they can be swapped when chaining multiple compute stages */
     99 	u32 rf_data_ssbos[2];
    100 	u32 last_output_ssbo_index;
    101 
    102 	u32 raw_data_ssbo;
    103 	u32 shared_ubo;
    104 
    105 	u32 channel_mapping_texture;
    106 	u32 sparse_elements_texture;
    107 	u32 focal_vectors_texture;
    108 	u32 hadamard_texture;
    109 
    110 	f32 processing_progress;
    111 	b32 processing_compute;
    112 
    113 	u32 rf_data_timestamp_query;
    114 
    115 	u32 shader_timer_ids[MAX_COMPUTE_SHADER_STAGES];
    116 
    117 	uv4 dec_data_dim;
    118 	u32 rf_raw_size;
    119 
    120 	BeamformerRenderModel unit_cube_model;
    121 } ComputeShaderCtx;
    122 
    123 typedef enum {
    124 	#define X(type, id, pretty, fixed_tx) DASShaderKind_##type = id,
    125 	DAS_TYPES
    126 	#undef X
    127 	DASShaderKind_Count
    128 } DASShaderKind;
    129 
    130 typedef struct {
    131 	BeamformerComputeStatsTable table;
    132 	f32 average_times[BeamformerShaderKind_Count];
    133 
    134 	u64 last_rf_timer_count;
    135 	f32 rf_time_delta_average;
    136 
    137 	u32 latest_frame_index;
    138 	u32 latest_rf_index;
    139 } ComputeShaderStats;
    140 
    141 /* TODO(rnp): maybe this also gets used for CPU timing info as well */
    142 typedef enum {
    143 	ComputeTimingInfoKind_ComputeFrameBegin,
    144 	ComputeTimingInfoKind_ComputeFrameEnd,
    145 	ComputeTimingInfoKind_Shader,
    146 	ComputeTimingInfoKind_RF_Data,
    147 } ComputeTimingInfoKind;
    148 
    149 typedef struct {
    150 	u64 timer_count;
    151 	ComputeTimingInfoKind kind;
    152 	union {
    153 		BeamformerShaderKind shader;
    154 	};
    155 } ComputeTimingInfo;
    156 
    157 typedef struct {
    158 	u32 write_index;
    159 	u32 read_index;
    160 	b32 compute_frame_active;
    161 	ComputeTimingInfo buffer[4096];
    162 } ComputeTimingTable;
    163 
    164 typedef struct BeamformerFrame BeamformerFrame;
    165 struct BeamformerFrame {
    166 	uv3 dim;
    167 	u32 texture;
    168 
    169 	/* NOTE: for use when displaying either prebeamformed frames or on the current frame
    170 	 * when we intend to recompute on the next frame */
    171 	v4  min_coordinate;
    172 	v4  max_coordinate;
    173 
    174 	u32 mips;
    175 	DASShaderKind das_shader_kind;
    176 	u32 compound_count;
    177 	u32 id;
    178 
    179 	BeamformerFrame *next;
    180 };
    181 
    182 struct BeamformerComputeFrame {
    183 	BeamformerFrame frame;
    184 	b32             ready_to_present;
    185 	BeamformerViewPlaneTag view_plane_tag;
    186 };
    187 
    188 #define GL_PARAMETERS \
    189 	X(MAJOR_VERSION,                   version_major,                   "")      \
    190 	X(MINOR_VERSION,                   version_minor,                   "")      \
    191 	X(TEXTURE_BUFFER_OFFSET_ALIGNMENT, texture_buffer_offset_alignment, "")      \
    192 	X(MAX_TEXTURE_BUFFER_SIZE,         max_texture_buffer_size,         "")      \
    193 	X(MAX_TEXTURE_SIZE,                max_2d_texture_dim,              "")      \
    194 	X(MAX_3D_TEXTURE_SIZE,             max_3d_texture_dim,              "")      \
    195 	X(MAX_SHADER_STORAGE_BLOCK_SIZE,   max_ssbo_size,                   "")      \
    196 	X(MAX_UNIFORM_BLOCK_SIZE,          max_ubo_size,                    "")      \
    197 	X(MAX_SERVER_WAIT_TIMEOUT,         max_server_wait_time,            " [ns]")
    198 
    199 typedef struct {
    200 	enum gl_vendor_ids vendor_id;
    201 	#define X(glname, name, suffix) i32 name;
    202 	GL_PARAMETERS
    203 	#undef X
    204 } GLParams;
    205 
    206 typedef struct {
    207 	GLParams gl;
    208 
    209 	uv2 window_size;
    210 	b32 should_exit;
    211 
    212 	Arena  ui_backing_store;
    213 	void  *ui;
    214 	/* TODO(rnp): this is nasty and should be removed */
    215 	b32    ui_read_params;
    216 
    217 	BeamformerComputeFrame beamform_frames[MAX_BEAMFORMED_SAVED_FRAMES];
    218 	BeamformerComputeFrame *latest_frame;
    219 	u32 next_render_frame_index;
    220 	u32 display_frame_index;
    221 
    222 	/* NOTE: this will only be used when we are averaging */
    223 	u32                    averaged_frame_index;
    224 	BeamformerComputeFrame averaged_frames[2];
    225 
    226 	ComputeShaderCtx  csctx;
    227 
    228 	/* TODO(rnp): ideally this would go in the UI but its hard to manage with the UI
    229 	 * destroying itself on hot-reload */
    230 	FrameViewRenderContext frame_view_render_context;
    231 
    232 	CudaLib cuda_lib;
    233 	OS      os;
    234 	Stream  error_stream;
    235 
    236 	BeamformWorkQueue *beamform_work_queue;
    237 
    238 	ComputeShaderStats *compute_shader_stats;
    239 	ComputeTimingTable *compute_timing_table;
    240 
    241 	SharedMemoryRegion shared_memory;
    242 } BeamformerCtx;
    243 
    244 struct ShaderReloadContext {
    245 	BeamformerCtx *beamformer_context;
    246 	s8   path;
    247 	s8   name;
    248 	s8   header;
    249 	u32 *shader;
    250 	ShaderReloadContext *link;
    251 	GLenum     gl_type;
    252 	BeamformerShaderKind kind;
    253 };
    254 
    255 #define BEAMFORMER_FRAME_STEP_FN(name) void name(BeamformerCtx *ctx, Arena *arena, \
    256                                                  BeamformerInput *input)
    257 typedef BEAMFORMER_FRAME_STEP_FN(beamformer_frame_step_fn);
    258 
    259 #define BEAMFORMER_COMPUTE_SETUP_FN(name) void name(iptr user_context, Arena arena, iptr gl_context)
    260 typedef BEAMFORMER_COMPUTE_SETUP_FN(beamformer_compute_setup_fn);
    261 
    262 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(iptr user_context, Arena arena, iptr gl_context)
    263 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn);
    264 
    265 #define BEAMFORMER_RELOAD_SHADER_FN(name) b32 name(BeamformerCtx *ctx, ShaderReloadContext *src, \
    266                                                    Arena arena, s8 shader_name)
    267 typedef BEAMFORMER_RELOAD_SHADER_FN(beamformer_reload_shader_fn);
    268 
    269 #endif /*_BEAMFORMER_H_ */