beamformer.h (7015B)
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 #define FRAME_VIEW_RENDER_DYNAMIC_RANGE_LOC 1 61 #define FRAME_VIEW_RENDER_THRESHOLD_LOC 2 62 #define FRAME_VIEW_RENDER_GAMMA_LOC 3 63 #define FRAME_VIEW_RENDER_LOG_SCALE_LOC 4 64 65 typedef struct { 66 u32 shader; 67 u32 framebuffer; 68 u32 vao; 69 u32 vbo; 70 b32 updated; 71 } FrameViewRenderContext; 72 73 #include "beamformer_parameters.h" 74 #include "beamformer_work_queue.h" 75 76 #define CS_MIN_MAX_MIPS_LEVEL_UNIFORM_LOC 1 77 #define CS_SUM_PRESCALE_UNIFORM_LOC 1 78 79 typedef struct { 80 u32 programs[BeamformerShaderKind_ComputeCount]; 81 82 /* NOTE: Decoded data is only relevant in the context of a single frame. We use two 83 * buffers so that they can be swapped when chaining multiple compute stages */ 84 u32 rf_data_ssbos[2]; 85 u32 last_output_ssbo_index; 86 87 u32 raw_data_ssbo; 88 u32 shared_ubo; 89 90 u32 channel_mapping_texture; 91 u32 sparse_elements_texture; 92 u32 focal_vectors_texture; 93 u32 hadamard_texture; 94 95 f32 processing_progress; 96 b32 processing_compute; 97 98 u32 rf_data_timestamp_query; 99 100 u32 shader_timer_ids[MAX_COMPUTE_SHADER_STAGES]; 101 102 uv4 dec_data_dim; 103 u32 rf_raw_size; 104 } ComputeShaderCtx; 105 106 typedef enum { 107 #define X(type, id, pretty, fixed_tx) DASShaderKind_##type = id, 108 DAS_TYPES 109 #undef X 110 DASShaderKind_Count 111 } DASShaderKind; 112 113 typedef struct { 114 BeamformerComputeStatsTable table; 115 f32 average_times[BeamformerShaderKind_Count]; 116 117 u64 last_rf_timer_count; 118 f32 rf_time_delta_average; 119 120 u32 latest_frame_index; 121 u32 latest_rf_index; 122 } ComputeShaderStats; 123 124 /* TODO(rnp): maybe this also gets used for CPU timing info as well */ 125 typedef enum { 126 ComputeTimingInfoKind_ComputeFrameBegin, 127 ComputeTimingInfoKind_ComputeFrameEnd, 128 ComputeTimingInfoKind_Shader, 129 ComputeTimingInfoKind_RF_Data, 130 } ComputeTimingInfoKind; 131 132 typedef struct { 133 u64 timer_count; 134 ComputeTimingInfoKind kind; 135 union { 136 BeamformerShaderKind shader; 137 }; 138 } ComputeTimingInfo; 139 140 typedef struct { 141 u32 write_index; 142 u32 read_index; 143 b32 compute_frame_active; 144 ComputeTimingInfo buffer[4096]; 145 } ComputeTimingTable; 146 147 typedef struct BeamformFrame { 148 uv3 dim; 149 u32 texture; 150 151 /* NOTE: for use when displaying either prebeamformed frames or on the current frame 152 * when we intend to recompute on the next frame */ 153 v4 min_coordinate; 154 v4 max_coordinate; 155 156 u32 mips; 157 DASShaderKind das_shader_kind; 158 u32 compound_count; 159 u32 id; 160 161 struct BeamformFrame *next; 162 } BeamformFrame; 163 164 struct BeamformComputeFrame { 165 BeamformFrame frame; 166 ImagePlaneTag image_plane_tag; 167 b32 ready_to_present; 168 }; 169 170 #define GL_PARAMETERS \ 171 X(MAJOR_VERSION, version_major, "") \ 172 X(MINOR_VERSION, version_minor, "") \ 173 X(TEXTURE_BUFFER_OFFSET_ALIGNMENT, texture_buffer_offset_alignment, "") \ 174 X(MAX_TEXTURE_BUFFER_SIZE, max_texture_buffer_size, "") \ 175 X(MAX_TEXTURE_SIZE, max_2d_texture_dim, "") \ 176 X(MAX_3D_TEXTURE_SIZE, max_3d_texture_dim, "") \ 177 X(MAX_SHADER_STORAGE_BLOCK_SIZE, max_ssbo_size, "") \ 178 X(MAX_UNIFORM_BLOCK_SIZE, max_ubo_size, "") \ 179 X(MAX_SERVER_WAIT_TIMEOUT, max_server_wait_time, " [ns]") 180 181 typedef struct { 182 enum gl_vendor_ids vendor_id; 183 #define X(glname, name, suffix) i32 name; 184 GL_PARAMETERS 185 #undef X 186 } GLParams; 187 188 typedef struct { 189 GLParams gl; 190 191 uv2 window_size; 192 b32 should_exit; 193 194 Arena ui_backing_store; 195 void *ui; 196 /* TODO(rnp): this is nasty and should be removed */ 197 b32 ui_read_params; 198 199 BeamformComputeFrame beamform_frames[MAX_BEAMFORMED_SAVED_FRAMES]; 200 BeamformComputeFrame *latest_frame; 201 u32 next_render_frame_index; 202 u32 display_frame_index; 203 204 /* NOTE: this will only be used when we are averaging */ 205 u32 averaged_frame_index; 206 BeamformComputeFrame averaged_frames[2]; 207 208 ComputeShaderCtx csctx; 209 210 /* TODO(rnp): ideally this would go in the UI but its hard to manage with the UI 211 * destroying itself on hot-reload */ 212 FrameViewRenderContext frame_view_render_context; 213 214 CudaLib cuda_lib; 215 OS os; 216 Stream error_stream; 217 218 BeamformWorkQueue *beamform_work_queue; 219 220 ComputeShaderStats *compute_shader_stats; 221 ComputeTimingTable *compute_timing_table; 222 223 SharedMemoryRegion shared_memory; 224 } BeamformerCtx; 225 226 struct ShaderReloadContext { 227 BeamformerCtx *beamformer_context; 228 s8 path; 229 s8 name; 230 s8 header; 231 u32 *shader; 232 ShaderReloadContext *link; 233 GLenum gl_type; 234 BeamformerShaderKind kind; 235 }; 236 237 #define BEAMFORMER_FRAME_STEP_FN(name) void name(BeamformerCtx *ctx, Arena *arena, \ 238 BeamformerInput *input) 239 typedef BEAMFORMER_FRAME_STEP_FN(beamformer_frame_step_fn); 240 241 #define BEAMFORMER_COMPUTE_SETUP_FN(name) void name(iptr user_context, Arena arena, iptr gl_context) 242 typedef BEAMFORMER_COMPUTE_SETUP_FN(beamformer_compute_setup_fn); 243 244 #define BEAMFORMER_COMPLETE_COMPUTE_FN(name) void name(iptr user_context, Arena arena, iptr gl_context) 245 typedef BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute_fn); 246 247 #define BEAMFORMER_RELOAD_SHADER_FN(name) b32 name(BeamformerCtx *ctx, ShaderReloadContext *src, \ 248 Arena arena, s8 shader_name) 249 typedef BEAMFORMER_RELOAD_SHADER_FN(beamformer_reload_shader_fn); 250 251 #endif /*_BEAMFORMER_H_ */