ogl_beamforming

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

beamformer.h (5912B)


      1 /* See LICENSE for license details. */
      2 #ifndef _BEAMFORMER_H_
      3 #define _BEAMFORMER_H_
      4 
      5 #include <glad.h>
      6 
      7 #define GRAPHICS_API_OPENGL_43
      8 #include <raylib_extended.h>
      9 #include <rlgl.h>
     10 
     11 #include "util.h"
     12 #include "util_gl.c"
     13 
     14 enum gl_vendor_ids {
     15 	GL_VENDOR_AMD,
     16 	GL_VENDOR_ARM,
     17 	GL_VENDOR_INTEL,
     18 	GL_VENDOR_NVIDIA,
     19 };
     20 
     21 typedef struct {
     22 	v2   mouse;
     23 	v2   last_mouse;
     24 	b32  executable_reloaded;
     25 } BeamformerInput;
     26 
     27 #define INIT_CUDA_CONFIGURATION_FN(name) void name(u32 *input_dims, u32 *decoded_dims, i16 *channel_mapping)
     28 typedef INIT_CUDA_CONFIGURATION_FN(init_cuda_configuration_fn);
     29 INIT_CUDA_CONFIGURATION_FN(init_cuda_configuration_stub) {}
     30 
     31 #define REGISTER_CUDA_BUFFERS_FN(name) void name(u32 *rf_data_ssbos, u32 rf_buffer_count, u32 raw_data_ssbo)
     32 typedef REGISTER_CUDA_BUFFERS_FN(register_cuda_buffers_fn);
     33 REGISTER_CUDA_BUFFERS_FN(register_cuda_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_LIB_FNS               \
     44 	X(cuda_decode)             \
     45 	X(cuda_hilbert)            \
     46 	X(init_cuda_configuration) \
     47 	X(register_cuda_buffers)
     48 
     49 typedef struct {
     50 	void                       *lib;
     51 	u64                         timestamp;
     52 	#define X(name) name ## _fn *name;
     53 	CUDA_LIB_FNS
     54 	#undef X
     55 } CudaLib;
     56 
     57 #define FRAME_VIEW_RENDER_DYNAMIC_RANGE_LOC 1
     58 #define FRAME_VIEW_RENDER_THRESHOLD_LOC     2
     59 #define FRAME_VIEW_RENDER_GAMMA_LOC         3
     60 #define FRAME_VIEW_RENDER_LOG_SCALE_LOC     4
     61 
     62 typedef struct {
     63 	u32 shader;
     64 	u32 framebuffer;
     65 	u32 vao;
     66 	u32 vbo;
     67 	b32 updated;
     68 } FrameViewRenderContext;
     69 
     70 #include "beamformer_parameters.h"
     71 #include "beamformer_work_queue.h"
     72 
     73 #define CS_MIN_MAX_MIPS_LEVEL_UNIFORM_LOC 1
     74 #define CS_SUM_PRESCALE_UNIFORM_LOC       1
     75 
     76 typedef struct {
     77 	u32 programs[CS_LAST];
     78 
     79 	/* NOTE: Decoded data is only relevant in the context of a single frame. We use two
     80 	 * buffers so that they can be swapped when chaining multiple compute stages */
     81 	u32 rf_data_ssbos[2];
     82 	u32 last_output_ssbo_index;
     83 
     84 	u32 raw_data_ssbo;
     85 	u32 shared_ubo;
     86 
     87 	u32 channel_mapping_texture;
     88 	u32 sparse_elements_texture;
     89 	u32 focal_vectors_texture;
     90 	u32 hadamard_texture;
     91 
     92 	f32 processing_progress;
     93 	b32 processing_compute;
     94 
     95 	uv4 dec_data_dim;
     96 	u32 rf_raw_size;
     97 } ComputeShaderCtx;
     98 
     99 typedef enum {
    100 #define X(type, id, pretty, fixed_tx) DAS_ ##type = id,
    101 DAS_TYPES
    102 #undef X
    103 DAS_LAST
    104 } DASShaderID;
    105 
    106 typedef struct {
    107 	/* TODO(rnp): there is assumption here that each shader will occur only once
    108 	 * per compute. add an insertion index and change these to hold the max number
    109 	 * of executed compute stages */
    110 	u32 timer_ids[CS_LAST];
    111 	f32 times[CS_LAST];
    112 	b32 timer_active[CS_LAST];
    113 } ComputeShaderStats;
    114 
    115 typedef struct BeamformFrame {
    116 	uv3 dim;
    117 	u32 texture;
    118 
    119 	/* NOTE: for use when displaying either prebeamformed frames or on the current frame
    120 	 * when we intend to recompute on the next frame */
    121 	v4  min_coordinate;
    122 	v4  max_coordinate;
    123 
    124 	u32 mips;
    125 	DASShaderID das_shader_id;
    126 	u32 compound_count;
    127 	u32 id;
    128 
    129 	struct BeamformFrame *next;
    130 } BeamformFrame;
    131 
    132 struct BeamformComputeFrame {
    133 	BeamformFrame      frame;
    134 	ComputeShaderStats stats;
    135 	ImagePlaneTag image_plane_tag;
    136 	b32 in_flight;
    137 	b32 ready_to_present;
    138 };
    139 
    140 #define GL_PARAMETERS \
    141 	X(MAJOR_VERSION,                   version_major,                   "")      \
    142 	X(MINOR_VERSION,                   version_minor,                   "")      \
    143 	X(TEXTURE_BUFFER_OFFSET_ALIGNMENT, texture_buffer_offset_alignment, "")      \
    144 	X(MAX_TEXTURE_BUFFER_SIZE,         max_texture_buffer_size,         "")      \
    145 	X(MAX_TEXTURE_SIZE,                max_2d_texture_dim,              "")      \
    146 	X(MAX_3D_TEXTURE_SIZE,             max_3d_texture_dim,              "")      \
    147 	X(MAX_SHADER_STORAGE_BLOCK_SIZE,   max_ssbo_size,                   "")      \
    148 	X(MAX_UNIFORM_BLOCK_SIZE,          max_ubo_size,                    "")      \
    149 	X(MAX_SERVER_WAIT_TIMEOUT,         max_server_wait_time,            " [ns]")
    150 
    151 typedef struct {
    152 	enum gl_vendor_ids vendor_id;
    153 	#define X(glname, name, suffix) i32 name;
    154 	GL_PARAMETERS
    155 	#undef X
    156 } GLParams;
    157 
    158 typedef struct {
    159 	GLParams gl;
    160 
    161 	uv2 window_size;
    162 	b32 start_compute;
    163 	b32 should_exit;
    164 
    165 	Arena  ui_backing_store;
    166 	void  *ui;
    167 	/* TODO(rnp): this is nasty and should be removed */
    168 	b32    ui_read_params;
    169 
    170 	BeamformComputeFrame beamform_frames[MAX_BEAMFORMED_SAVED_FRAMES];
    171 	u32 next_render_frame_index;
    172 	u32 display_frame_index;
    173 
    174 	/* NOTE: this will only be used when we are averaging */
    175 	u32                  averaged_frame_index;
    176 	BeamformComputeFrame averaged_frames[2];
    177 
    178 	ComputeShaderCtx  csctx;
    179 
    180 	/* TODO(rnp): ideally this would go in the UI but its hard to manage with the UI
    181 	 * destroying itself on hot-reload */
    182 	FrameViewRenderContext frame_view_render_context;
    183 
    184 	Arena export_buffer;
    185 
    186 	CudaLib cuda_lib;
    187 	OS      os;
    188 	Stream  error_stream;
    189 
    190 	BeamformWorkQueue *beamform_work_queue;
    191 
    192 	BeamformerSharedMemory *shared_memory;
    193 } BeamformerCtx;
    194 
    195 struct ComputeShaderReloadContext {
    196 	BeamformerCtx *beamformer_ctx;
    197 	s8    label;
    198 	s8    path;
    199 	ComputeShaderID shader;
    200 	b32   needs_header;
    201 };
    202 
    203 #define BEAMFORMER_FRAME_STEP_FN(name) void name(BeamformerCtx *ctx, Arena *arena, \
    204                                                  BeamformerInput *input)
    205 typedef BEAMFORMER_FRAME_STEP_FN(beamformer_frame_step_fn);
    206 
    207 #define BEAMFORMER_COMPUTE_SETUP_FN(name) void name(iptr user_context, Arena arena, iptr gl_context)
    208 typedef BEAMFORMER_COMPUTE_SETUP_FN(beamformer_compute_setup_fn);
    209 
    210 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(iptr user_context, Arena arena, iptr gl_context)
    211 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn);
    212 
    213 #endif /*_BEAMFORMER_H_ */