beamformer_core.c (59713B)
1 /* See LICENSE for license details. */ 2 /* TODO(rnp): 3 * [ ]: refactor: DecodeMode_None should use a different mapping and optional conversion shader 4 * for rf only mode with no filter and demod/filter should gain the OutputFloats flag for iq 5 * case and rf mode with filter; this can also be used instead of first pass uniform 6 * [ ]: refactor: replace UploadRF with just the scratch_rf_size variable, 7 * use below to spin wait in library 8 * [ ]: utilize umonitor/umwait (intel), monitorx/mwaitx (amd), and wfe/sev (aarch64) 9 * for power efficient low latency waiting 10 * [ ]: refactor: split decode into reshape and decode 11 * - the check for first pass reshaping is the last non constant check 12 * in the shader 13 * - this will also remove the need for the channel mapping in the decode shader 14 * [X]: refactor: ui: reload only shader which is affected by the interaction 15 * [ ]: BeamformWorkQueue -> BeamformerWorkQueue 16 * [ ]: need to keep track of gpu memory in some way 17 * - want to be able to store more than 16 2D frames but limit 3D frames 18 * - maybe keep track of how much gpu memory is committed for beamformed images 19 * and use that to determine when to loop back over existing textures 20 * - to do this maybe use a circular linked list instead of a flat array 21 * - then have a way of querying how many frames are available for a specific point count 22 * [ ]: bug: reinit cuda on hot-reload 23 */ 24 25 #include "compiler.h" 26 27 #if defined(BEAMFORMER_DEBUG) && !defined(BEAMFORMER_EXPORT) && OS_WINDOWS 28 #define BEAMFORMER_EXPORT __declspec(dllexport) 29 #endif 30 31 #include "beamformer_internal.h" 32 33 global f32 dt_for_frame; 34 35 #define DECODE_FIRST_PASS_UNIFORM_LOC 1 36 37 #define DAS_CYCLE_T_UNIFORM_LOC 2 38 #define DAS_FAST_CHANNEL_UNIFORM_LOC 3 39 40 #define MIN_MAX_MIPS_LEVEL_UNIFORM_LOC 1 41 #define SUM_PRESCALE_UNIFORM_LOC 1 42 43 #if !BEAMFORMER_RENDERDOC_HOOKS 44 #define start_renderdoc_capture(...) 45 #define end_renderdoc_capture(...) 46 #define renderdoc_attached(...) (0) 47 #else 48 global renderdoc_start_frame_capture_fn *start_frame_capture; 49 global renderdoc_end_frame_capture_fn *end_frame_capture; 50 #define start_renderdoc_capture(gl) if (start_frame_capture) start_frame_capture(gl, 0) 51 #define end_renderdoc_capture(gl) if (end_frame_capture) end_frame_capture(gl, 0) 52 #define renderdoc_attached(...) (start_frame_capture != 0) 53 #endif 54 55 typedef struct { 56 BeamformerFrame *frames; 57 u32 capacity; 58 u32 offset; 59 u32 cursor; 60 u32 needed_frames; 61 } ComputeFrameIterator; 62 63 function void 64 beamformer_compute_plan_release(BeamformerComputeContext *cc, u32 block) 65 { 66 assert(block < countof(cc->compute_plans)); 67 BeamformerComputePlan *cp = cc->compute_plans[block]; 68 if (cp) { 69 glDeleteBuffers(countof(cp->ubos), cp->ubos); 70 glDeleteTextures(countof(cp->textures), cp->textures); 71 for (u32 i = 0; i < countof(cp->filters); i++) 72 glDeleteBuffers(1, &cp->filters[i].ssbo); 73 cc->compute_plans[block] = 0; 74 SLLPushFreelist(cp, cc->compute_plan_freelist); 75 } 76 } 77 78 function BeamformerComputePlan * 79 beamformer_compute_plan_for_block(BeamformerComputeContext *cc, u32 block, Arena *arena) 80 { 81 assert(block < countof(cc->compute_plans)); 82 BeamformerComputePlan *result = cc->compute_plans[block]; 83 if (!result) { 84 result = SLLPopFreelist(cc->compute_plan_freelist); 85 if (!result) result = push_struct_no_zero(arena, BeamformerComputePlan); 86 zero_struct(result); 87 cc->compute_plans[block] = result; 88 89 result->ui_voxel_transform = m4_identity(); 90 91 glCreateBuffers(countof(result->ubos), result->ubos); 92 93 Stream label = arena_stream(*arena); 94 #define X(k, t, ...) \ 95 glNamedBufferStorage(result->ubos[BeamformerComputeUBOKind_##k], sizeof(t), \ 96 0, GL_DYNAMIC_STORAGE_BIT); \ 97 stream_append_s8(&label, s8(#t "[")); \ 98 stream_append_u64(&label, block); \ 99 stream_append_byte(&label, ']'); \ 100 glObjectLabel(GL_BUFFER, result->ubos[BeamformerComputeUBOKind_##k], \ 101 label.widx, (c8 *)label.data); \ 102 label.widx = 0; 103 BEAMFORMER_COMPUTE_UBO_LIST 104 #undef X 105 106 #define X(_k, t, ...) t, 107 GLenum gl_kind[] = {BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL}; 108 #undef X 109 read_only local_persist s8 tex_prefix[] = { 110 #define X(k, ...) s8_comp(#k "["), 111 BEAMFORMER_COMPUTE_TEXTURE_LIST_FULL 112 #undef X 113 }; 114 glCreateTextures(GL_TEXTURE_1D, BeamformerComputeTextureKind_Count - 1, result->textures); 115 for (u32 i = 0; i < BeamformerComputeTextureKind_Count - 1; i++) { 116 /* TODO(rnp): this could be predicated on channel count for this compute plan */ 117 glTextureStorage1D(result->textures[i], 1, gl_kind[i], BeamformerMaxChannelCount); 118 stream_append_s8(&label, tex_prefix[i]); 119 stream_append_u64(&label, block); 120 stream_append_byte(&label, ']'); 121 glObjectLabel(GL_TEXTURE, result->textures[i], label.widx, (c8 *)label.data); 122 label.widx = 0; 123 } 124 } 125 return result; 126 } 127 128 function void 129 beamformer_filter_update(BeamformerFilter *f, BeamformerFilterParameters fp, u32 block, u32 slot, Arena arena) 130 { 131 Stream sb = arena_stream(arena); 132 stream_append_s8s(&sb, 133 beamformer_filter_kind_strings[fp.kind % countof(beamformer_filter_kind_strings)], 134 s8("Filter[")); 135 stream_append_u64(&sb, block); 136 stream_append_s8(&sb, s8("][")); 137 stream_append_u64(&sb, slot); 138 stream_append_byte(&sb, ']'); 139 s8 label = arena_stream_commit(&arena, &sb); 140 141 void *filter = 0; 142 switch (fp.kind) { 143 case BeamformerFilterKind_Kaiser:{ 144 /* TODO(rnp): this should also support complex */ 145 /* TODO(rnp): implement this as an IFIR filter instead to reduce computation */ 146 filter = kaiser_low_pass_filter(&arena, fp.kaiser.cutoff_frequency, fp.sampling_frequency, 147 fp.kaiser.beta, (i32)fp.kaiser.length); 148 f->length = (i32)fp.kaiser.length; 149 f->time_delay = (f32)f->length / 2.0f / fp.sampling_frequency; 150 }break; 151 case BeamformerFilterKind_MatchedChirp:{ 152 typeof(fp.matched_chirp) *mc = &fp.matched_chirp; 153 f32 fs = fp.sampling_frequency; 154 f->length = (i32)(mc->duration * fs); 155 if (fp.complex) { 156 filter = baseband_chirp(&arena, mc->min_frequency, mc->max_frequency, fs, f->length, 1, 0.5f); 157 f->time_delay = complex_filter_first_moment(filter, f->length, fs); 158 } else { 159 filter = rf_chirp(&arena, mc->min_frequency, mc->max_frequency, fs, f->length, 1); 160 f->time_delay = real_filter_first_moment(filter, f->length, fs); 161 } 162 }break; 163 InvalidDefaultCase; 164 } 165 166 f->parameters = fp; 167 168 glDeleteBuffers(1, &f->ssbo); 169 glCreateBuffers(1, &f->ssbo); 170 glNamedBufferStorage(f->ssbo, f->length * (i32)sizeof(f32) * (fp.complex? 2 : 1), filter, 0); 171 glObjectLabel(GL_BUFFER, f->ssbo, (i32)label.len, (c8 *)label.data); 172 } 173 174 function ComputeFrameIterator 175 compute_frame_iterator(BeamformerCtx *ctx, u32 start_index, u32 needed_frames) 176 { 177 start_index = start_index % ARRAY_COUNT(ctx->beamform_frames); 178 179 ComputeFrameIterator result; 180 result.frames = ctx->beamform_frames; 181 result.offset = start_index; 182 result.capacity = ARRAY_COUNT(ctx->beamform_frames); 183 result.cursor = 0; 184 result.needed_frames = needed_frames; 185 return result; 186 } 187 188 function BeamformerFrame * 189 frame_next(ComputeFrameIterator *bfi) 190 { 191 BeamformerFrame *result = 0; 192 if (bfi->cursor != bfi->needed_frames) { 193 u32 index = (bfi->offset + bfi->cursor++) % bfi->capacity; 194 result = bfi->frames + index; 195 } 196 return result; 197 } 198 199 function b32 200 beamformer_frame_compatible(BeamformerFrame *f, iv3 dim, GLenum gl_kind) 201 { 202 b32 result = gl_kind == f->gl_kind && iv3_equal(dim, f->dim); 203 return result; 204 } 205 206 function iv3 207 das_valid_points(iv3 points) 208 { 209 iv3 result; 210 result.x = Max(points.x, 1); 211 result.y = Max(points.y, 1); 212 result.z = Max(points.z, 1); 213 return result; 214 } 215 216 function void 217 alloc_beamform_frame(BeamformerFrame *out, iv3 out_dim, GLenum gl_kind, s8 name, Arena arena) 218 { 219 out->dim = das_valid_points(out_dim); 220 221 /* NOTE: allocate storage for beamformed output data; 222 * this is shared between compute and fragment shaders */ 223 u32 max_dim = (u32)Max(out->dim.x, Max(out->dim.y, out->dim.z)); 224 out->mips = (i32)ctz_u32(round_up_power_of_2(max_dim)) + 1; 225 226 out->gl_kind = gl_kind; 227 228 Stream label = arena_stream(arena); 229 stream_append_s8(&label, name); 230 stream_append_byte(&label, '['); 231 stream_append_hex_u64(&label, out->id); 232 stream_append_byte(&label, ']'); 233 234 glDeleteTextures(1, &out->texture); 235 glCreateTextures(GL_TEXTURE_3D, 1, &out->texture); 236 glTextureStorage3D(out->texture, out->mips, gl_kind, out->dim.x, out->dim.y, out->dim.z); 237 238 glTextureParameteri(out->texture, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 239 glTextureParameteri(out->texture, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 240 241 LABEL_GL_OBJECT(GL_TEXTURE, out->texture, stream_to_s8(&label)); 242 } 243 244 function void 245 update_hadamard_texture(BeamformerComputePlan *cp, i32 order, Arena arena) 246 { 247 f32 *hadamard = make_hadamard_transpose(&arena, order); 248 if (hadamard) { 249 cp->hadamard_order = order; 250 u32 *texture = cp->textures + BeamformerComputeTextureKind_Hadamard; 251 glDeleteTextures(1, texture); 252 glCreateTextures(GL_TEXTURE_2D, 1, texture); 253 glTextureStorage2D(*texture, 1, GL_R32F, order, order); 254 glTextureSubImage2D(*texture, 0, 0, 0, order, order, GL_RED, GL_FLOAT, hadamard); 255 256 Stream label = arena_stream(arena); 257 stream_append_s8(&label, s8("Hadamard")); 258 stream_append_i64(&label, order); 259 LABEL_GL_OBJECT(GL_TEXTURE, *texture, stream_to_s8(&label)); 260 } 261 } 262 263 function void 264 alloc_shader_storage(BeamformerCtx *ctx, u32 decoded_data_size, Arena arena) 265 { 266 BeamformerComputeContext *cc = &ctx->compute_context; 267 glDeleteBuffers(countof(cc->ping_pong_ssbos), cc->ping_pong_ssbos); 268 glCreateBuffers(countof(cc->ping_pong_ssbos), cc->ping_pong_ssbos); 269 270 cc->ping_pong_ssbo_size = decoded_data_size; 271 272 Stream label = arena_stream(arena); 273 stream_append_s8(&label, s8("PingPongSSBO[")); 274 i32 s_widx = label.widx; 275 for (i32 i = 0; i < countof(cc->ping_pong_ssbos); i++) { 276 glNamedBufferStorage(cc->ping_pong_ssbos[i], (iz)decoded_data_size, 0, 0); 277 stream_append_i64(&label, i); 278 stream_append_byte(&label, ']'); 279 LABEL_GL_OBJECT(GL_BUFFER, cc->ping_pong_ssbos[i], stream_to_s8(&label)); 280 stream_reset(&label, s_widx); 281 } 282 283 /* TODO(rnp): (25.08.04) cuda lib is heavily broken atm. First there are multiple RF 284 * buffers and cuda decode shouldn't assume that the data is coming from the rf_buffer 285 * ssbo. Second each parameter block may need a different hadamard matrix so ideally 286 * decode should just take the texture as a parameter. Third, none of these dimensions 287 * need to be pre-known by the library unless its allocating GPU memory which it shouldn't 288 * need to do. For now grab out of parameter block 0 but it is not correct */ 289 BeamformerParameterBlock *pb = beamformer_parameter_block(ctx->shared_memory, 0); 290 /* NOTE(rnp): these are stubs when CUDA isn't supported */ 291 cuda_register_buffers(cc->ping_pong_ssbos, countof(cc->ping_pong_ssbos), cc->rf_buffer.ssbo); 292 u32 decoded_data_dimension[3] = {pb->parameters.sample_count, pb->parameters.channel_count, pb->parameters.acquisition_count}; 293 cuda_init(pb->parameters.raw_data_dimensions.E, decoded_data_dimension); 294 } 295 296 function void 297 push_compute_timing_info(ComputeTimingTable *t, ComputeTimingInfo info) 298 { 299 u32 index = atomic_add_u32(&t->write_index, 1) % countof(t->buffer); 300 t->buffer[index] = info; 301 } 302 303 function b32 304 fill_frame_compute_work(BeamformerCtx *ctx, BeamformWork *work, BeamformerViewPlaneTag plane, 305 u32 parameter_block, b32 indirect) 306 { 307 b32 result = work != 0; 308 if (result) { 309 u32 frame_id = atomic_add_u32(&ctx->next_render_frame_index, 1); 310 u32 frame_index = frame_id % countof(ctx->beamform_frames); 311 work->kind = indirect? BeamformerWorkKind_ComputeIndirect : BeamformerWorkKind_Compute; 312 work->lock = BeamformerSharedMemoryLockKind_DispatchCompute; 313 work->compute_context.parameter_block = parameter_block; 314 work->compute_context.frame = ctx->beamform_frames + frame_index; 315 work->compute_context.frame->ready_to_present = 0; 316 work->compute_context.frame->view_plane_tag = plane; 317 work->compute_context.frame->id = frame_id; 318 } 319 return result; 320 } 321 322 function void 323 do_sum_shader(BeamformerComputeContext *cc, u32 *in_textures, u32 in_texture_count, 324 u32 out_texture, iv3 out_data_dim) 325 { 326 /* NOTE: zero output before summing */ 327 glClearTexImage(out_texture, 0, GL_RED, GL_FLOAT, 0); 328 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT); 329 330 glBindImageTexture(0, out_texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_RG32F); 331 for (u32 i = 0; i < in_texture_count; i++) { 332 glBindImageTexture(1, in_textures[i], 0, GL_TRUE, 0, GL_READ_ONLY, GL_RG32F); 333 glDispatchCompute(ORONE((u32)out_data_dim.x / 32u), 334 ORONE((u32)out_data_dim.y), 335 ORONE((u32)out_data_dim.z / 32u)); 336 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); 337 } 338 } 339 340 function void 341 plan_compute_pipeline(BeamformerComputePlan *cp, BeamformerParameterBlock *pb) 342 { 343 b32 run_cuda_hilbert = 0; 344 b32 demodulate = 0; 345 346 for (u32 i = 0; i < pb->pipeline.shader_count; i++) { 347 switch (pb->pipeline.shaders[i]) { 348 case BeamformerShaderKind_CudaHilbert:{ run_cuda_hilbert = 1; }break; 349 case BeamformerShaderKind_Demodulate:{ demodulate = 1; }break; 350 default:{}break; 351 } 352 } 353 354 if (demodulate) run_cuda_hilbert = 0; 355 356 BeamformerDataKind data_kind = pb->pipeline.data_kind; 357 cp->iq_pipeline = data_kind == BeamformerDataKind_Float32Complex || 358 data_kind == BeamformerDataKind_Int16Complex || 359 demodulate || run_cuda_hilbert; 360 361 f32 sampling_frequency = pb->parameters.sampling_frequency; 362 u32 decimation_rate = Max(pb->parameters.decimation_rate, 1); 363 u32 sample_count = pb->parameters.sample_count; 364 if (demodulate) { 365 sample_count /= (2 * decimation_rate); 366 sampling_frequency /= 2 * (f32)decimation_rate; 367 } 368 369 cp->rf_size = sample_count * pb->parameters.channel_count * pb->parameters.acquisition_count; 370 if (cp->iq_pipeline) cp->rf_size *= 8; 371 else cp->rf_size *= 4; 372 373 u32 das_sample_stride = 1; 374 u32 das_transmit_stride = sample_count; 375 u32 das_channel_stride = sample_count * pb->parameters.acquisition_count; 376 377 f32 time_offset = pb->parameters.time_offset; 378 379 // TODO(rnp): subgroup size 380 u32 subgroup_size = gl_parameters.vendor_id == GLVendor_NVIDIA ? 32 : 64; 381 382 cp->pipeline.shader_count = 0; 383 for (u32 i = 0; i < pb->pipeline.shader_count; i++) { 384 BeamformerShaderParameters *sp = pb->pipeline.parameters + i; 385 u32 slot = cp->pipeline.shader_count; 386 u32 shader = pb->pipeline.shaders[i]; 387 b32 commit = 0; 388 389 BeamformerShaderDescriptor *ld = cp->shader_descriptors + slot - 1; 390 BeamformerShaderDescriptor *sd = cp->shader_descriptors + slot; 391 zero_struct(sd); 392 393 switch (shader) { 394 case BeamformerShaderKind_CudaHilbert:{ commit = run_cuda_hilbert; }break; 395 case BeamformerShaderKind_Decode:{ 396 /* TODO(rnp): rework decode first and demodulate after */ 397 b32 first = slot == 0; 398 399 sd->bake.data_kind = data_kind; 400 if (!first) { 401 if (data_kind == BeamformerDataKind_Int16) { 402 sd->bake.data_kind = BeamformerDataKind_Int16Complex; 403 } else { 404 sd->bake.data_kind = BeamformerDataKind_Float32Complex; 405 } 406 } 407 408 BeamformerShaderKind *last_shader = cp->pipeline.shaders + slot - 1; 409 assert(first || ((*last_shader == BeamformerShaderKind_Demodulate || 410 *last_shader == BeamformerShaderKind_Filter))); 411 412 BeamformerShaderDecodeBakeParameters *db = &sd->bake.Decode; 413 db->decode_mode = pb->parameters.decode_mode; 414 db->transmit_count = pb->parameters.acquisition_count; 415 416 u32 channel_stride = pb->parameters.acquisition_count * pb->parameters.sample_count; 417 db->input_sample_stride = first? 1 : ld->bake.Filter.output_sample_stride; 418 db->input_channel_stride = first? channel_stride : ld->bake.Filter.output_channel_stride; 419 db->input_transmit_stride = first? pb->parameters.sample_count : 1; 420 421 db->output_sample_stride = das_sample_stride; 422 db->output_channel_stride = das_channel_stride; 423 db->output_transmit_stride = das_transmit_stride; 424 if (first) { 425 db->output_channel_stride *= decimation_rate; 426 db->output_transmit_stride *= decimation_rate; 427 } 428 429 if (run_cuda_hilbert) sd->bake.flags |= BeamformerShaderDecodeFlags_DilateOutput; 430 431 if (db->decode_mode == BeamformerDecodeMode_None) { 432 sd->layout = (uv3){{subgroup_size, 1, 1}}; 433 434 sd->dispatch.x = (u32)ceil_f32((f32)sample_count / (f32)sd->layout.x); 435 sd->dispatch.y = (u32)ceil_f32((f32)pb->parameters.channel_count / (f32)sd->layout.y); 436 sd->dispatch.z = (u32)ceil_f32((f32)pb->parameters.acquisition_count / (f32)sd->layout.z); 437 } else if (db->transmit_count > 40) { 438 sd->bake.flags |= BeamformerShaderDecodeFlags_UseSharedMemory; 439 db->to_process = 2; 440 441 if (db->transmit_count == 48) 442 db->to_process = db->transmit_count / 16; 443 444 b32 use_16z = db->transmit_count == 48 || db->transmit_count == 80 || 445 db->transmit_count == 96 || db->transmit_count == 160; 446 sd->layout = (uv3){{4, 1, use_16z? 16 : 32}}; 447 448 sd->dispatch.x = (u32)ceil_f32((f32)sample_count / (f32)sd->layout.x); 449 sd->dispatch.y = (u32)ceil_f32((f32)pb->parameters.channel_count / (f32)sd->layout.y); 450 sd->dispatch.z = (u32)ceil_f32((f32)pb->parameters.acquisition_count / (f32)sd->layout.z / (f32)db->to_process); 451 } else { 452 db->to_process = 1; 453 454 /* NOTE(rnp): register caching. using more threads will cause the compiler to do 455 * contortions to avoid spilling registers. using less gives higher performance */ 456 sd->layout = (uv3){{subgroup_size / 2, 1, 1}}; 457 458 sd->dispatch.x = (u32)ceil_f32((f32)sample_count / (f32)sd->layout.x); 459 sd->dispatch.y = (u32)ceil_f32((f32)pb->parameters.channel_count / (f32)sd->layout.y); 460 sd->dispatch.z = 1; 461 } 462 463 if (first) sd->dispatch.x *= decimation_rate; 464 465 /* NOTE(rnp): decode 2 samples per dispatch when data is i16 */ 466 if (first && data_kind == BeamformerDataKind_Int16) 467 sd->dispatch.x = (u32)ceil_f32((f32)sd->dispatch.x / 2); 468 469 commit = first || db->decode_mode != BeamformerDecodeMode_None; 470 }break; 471 case BeamformerShaderKind_Demodulate: 472 case BeamformerShaderKind_Filter: 473 { 474 b32 first = slot == 0; 475 b32 demod = shader == BeamformerShaderKind_Demodulate; 476 BeamformerFilter *f = cp->filters + sp->filter_slot; 477 478 time_offset += f->time_delay; 479 480 BeamformerShaderFilterBakeParameters *fb = &sd->bake.Filter; 481 fb->filter_length = (u32)f->length; 482 if (demod) sd->bake.flags |= BeamformerShaderFilterFlags_Demodulate; 483 if (f->parameters.complex) sd->bake.flags |= BeamformerShaderFilterFlags_ComplexFilter; 484 485 sd->bake.data_kind = data_kind; 486 if (!first) sd->bake.data_kind = BeamformerDataKind_Float32; 487 488 /* NOTE(rnp): when we are demodulating we pretend that the sampler was alternating 489 * between sampling the I portion and the Q portion of an IQ signal. Therefore there 490 * is an implicit decimation factor of 2 which must always be included. All code here 491 * assumes that the signal was sampled in such a way that supports this operation. 492 * To recover IQ[n] from the sampled data (RF[n]) we do the following: 493 * I[n] = RF[n] 494 * Q[n] = RF[n + 1] 495 * IQ[n] = I[n] - j*Q[n] 496 */ 497 if (demod) { 498 fb->demodulation_frequency = pb->parameters.demodulation_frequency; 499 fb->sampling_frequency = pb->parameters.sampling_frequency / 2; 500 fb->decimation_rate = decimation_rate; 501 fb->sample_count = pb->parameters.sample_count; 502 503 fb->output_channel_stride = das_channel_stride; 504 fb->output_sample_stride = das_sample_stride; 505 fb->output_transmit_stride = das_transmit_stride; 506 507 if (first) { 508 fb->input_channel_stride = pb->parameters.sample_count * pb->parameters.acquisition_count / 2; 509 fb->input_sample_stride = 1; 510 fb->input_transmit_stride = pb->parameters.sample_count / 2; 511 512 if (pb->parameters.decode_mode == BeamformerDecodeMode_None) { 513 sd->bake.flags |= BeamformerShaderFilterFlags_OutputFloats; 514 } else { 515 /* NOTE(rnp): output optimized layout for decoding */ 516 fb->output_channel_stride = das_channel_stride; 517 fb->output_sample_stride = pb->parameters.acquisition_count; 518 fb->output_transmit_stride = 1; 519 } 520 } else { 521 assert(cp->pipeline.shaders[slot - 1] == BeamformerShaderKind_Decode); 522 fb->input_channel_stride = ld->bake.Decode.output_channel_stride; 523 fb->input_sample_stride = ld->bake.Decode.output_sample_stride; 524 fb->input_transmit_stride = ld->bake.Decode.output_transmit_stride; 525 } 526 } else { 527 fb->decimation_rate = 1; 528 fb->output_channel_stride = sample_count * pb->parameters.acquisition_count; 529 fb->output_sample_stride = 1; 530 fb->output_transmit_stride = sample_count; 531 fb->input_channel_stride = sample_count * pb->parameters.acquisition_count; 532 fb->input_sample_stride = 1; 533 fb->input_transmit_stride = sample_count; 534 fb->sample_count = sample_count; 535 } 536 537 /* TODO(rnp): filter may need a different dispatch layout */ 538 sd->layout = (uv3){{128, 1, 1}}; 539 sd->dispatch.x = (u32)ceil_f32((f32)sample_count / (f32)sd->layout.x); 540 sd->dispatch.y = (u32)ceil_f32((f32)pb->parameters.channel_count / (f32)sd->layout.y); 541 sd->dispatch.z = (u32)ceil_f32((f32)pb->parameters.acquisition_count / (f32)sd->layout.z); 542 543 commit = 1; 544 }break; 545 case BeamformerShaderKind_DAS:{ 546 sd->bake.data_kind = BeamformerDataKind_Float32; 547 if (cp->iq_pipeline) 548 sd->bake.data_kind = BeamformerDataKind_Float32Complex; 549 550 BeamformerShaderDASBakeParameters *db = &sd->bake.DAS; 551 BeamformerDASUBO *du = &cp->das_ubo_data; 552 du->xdc_element_pitch = pb->parameters.xdc_element_pitch; 553 db->sampling_frequency = sampling_frequency; 554 db->demodulation_frequency = pb->parameters.demodulation_frequency; 555 db->speed_of_sound = pb->parameters.speed_of_sound; 556 db->time_offset = time_offset; 557 db->f_number = pb->parameters.f_number; 558 db->acquisition_kind = pb->parameters.acquisition_kind; 559 db->sample_count = sample_count; 560 db->channel_count = pb->parameters.channel_count; 561 db->acquisition_count = pb->parameters.acquisition_count; 562 db->interpolation_mode = pb->parameters.interpolation_mode; 563 db->transmit_angle = pb->parameters.focal_vector.E[0]; 564 db->focus_depth = pb->parameters.focal_vector.E[1]; 565 db->transmit_receive_orientation = pb->parameters.transmit_receive_orientation; 566 567 // NOTE(rnp): old gcc will miscompile an assignment 568 mem_copy(du->voxel_transform.E, pb->parameters.das_voxel_transform.E, sizeof(du->voxel_transform)); 569 mem_copy(du->xdc_transform.E, pb->parameters.xdc_transform.E, sizeof(du->xdc_transform)); 570 571 du->voxel_transform = m4_mul(cp->ui_voxel_transform, du->voxel_transform); 572 573 u32 id = pb->parameters.acquisition_kind; 574 575 if (id == BeamformerAcquisitionKind_UFORCES || id == BeamformerAcquisitionKind_FORCES) 576 du->voxel_transform = m4_mul(du->xdc_transform, du->voxel_transform); 577 578 if (id == BeamformerAcquisitionKind_UFORCES || id == BeamformerAcquisitionKind_UHERCULES) 579 sd->bake.flags |= BeamformerShaderDASFlags_Sparse; 580 581 if (pb->parameters.single_focus) sd->bake.flags |= BeamformerShaderDASFlags_SingleFocus; 582 if (pb->parameters.single_orientation) sd->bake.flags |= BeamformerShaderDASFlags_SingleOrientation; 583 if (pb->parameters.coherency_weighting) sd->bake.flags |= BeamformerShaderDASFlags_CoherencyWeighting; 584 else sd->bake.flags |= BeamformerShaderDASFlags_Fast; 585 586 sd->layout = (uv3){{1, 1, 1}}; 587 588 b32 has_x = cp->output_points.x > 1; 589 b32 has_y = cp->output_points.y > 1; 590 b32 has_z = cp->output_points.z > 1; 591 592 u32 grid_3d_z_size = Max(1, subgroup_size / (4 * 4)); 593 u32 grid_2d_y_size = Max(1, subgroup_size / 8); 594 595 switch (iv3_dimension(cp->output_points)) { 596 597 case 1:{ 598 if (has_x) sd->layout.x = subgroup_size; 599 if (has_y) sd->layout.y = subgroup_size; 600 if (has_z) sd->layout.z = subgroup_size; 601 }break; 602 603 case 2:{ 604 if (has_x && has_y) {sd->layout.x = 8; sd->layout.y = grid_2d_y_size;} 605 if (has_x && has_z) {sd->layout.x = 8; sd->layout.z = grid_2d_y_size;} 606 if (has_y && has_z) {sd->layout.y = 8; sd->layout.z = grid_2d_y_size;} 607 }break; 608 609 case 3:{sd->layout = (uv3){{4, 4, grid_3d_z_size}};}break; 610 611 InvalidDefaultCase; 612 } 613 614 sd->dispatch.x = (u32)ceil_f32((f32)cp->output_points.x / sd->layout.x); 615 sd->dispatch.y = (u32)ceil_f32((f32)cp->output_points.y / sd->layout.y); 616 sd->dispatch.z = (u32)ceil_f32((f32)cp->output_points.z / sd->layout.z); 617 618 commit = 1; 619 }break; 620 default:{ commit = 1; }break; 621 } 622 623 if (commit) { 624 u32 index = cp->pipeline.shader_count++; 625 cp->pipeline.shaders[index] = shader; 626 cp->pipeline.parameters[index] = *sp; 627 } 628 } 629 cp->pipeline.data_kind = data_kind; 630 } 631 632 function void 633 stream_push_shader_header(Stream *s, BeamformerShaderKind shader_kind, s8 header) 634 { 635 stream_append_s8s(s, s8("#version 460 core\n\n"), header); 636 637 switch (shader_kind) { 638 case BeamformerShaderKind_DAS:{ 639 stream_append_s8(s, s8("" 640 "layout(location = " str(DAS_CYCLE_T_UNIFORM_LOC) ") uniform uint u_cycle_t;\n" 641 "layout(location = " str(DAS_FAST_CHANNEL_UNIFORM_LOC) ") uniform int u_channel;\n\n" 642 )); 643 }break; 644 case BeamformerShaderKind_Decode:{ 645 stream_append_s8s(s, s8("" 646 "layout(location = " str(DECODE_FIRST_PASS_UNIFORM_LOC) ") uniform bool u_first_pass;\n\n" 647 )); 648 }break; 649 case BeamformerShaderKind_MinMax:{ 650 stream_append_s8(s, s8("layout(location = " str(MIN_MAX_MIPS_LEVEL_UNIFORM_LOC) 651 ") uniform int u_mip_map;\n\n")); 652 }break; 653 case BeamformerShaderKind_Sum:{ 654 stream_append_s8(s, s8("layout(location = " str(SUM_PRESCALE_UNIFORM_LOC) 655 ") uniform float u_sum_prescale = 1.0;\n\n")); 656 }break; 657 default:{}break; 658 } 659 } 660 661 function void 662 load_compute_shader(BeamformerCtx *ctx, BeamformerComputePlan *cp, u32 shader_slot, Arena arena) 663 { 664 read_only local_persist s8 compute_headers[BeamformerShaderKind_ComputeCount] = { 665 /* X(name, type, gltype) */ 666 #define X(name, t, gltype) "\t" #gltype " " #name ";\n" 667 [BeamformerShaderKind_DAS] = s8_comp("layout(std140, binding = 0) uniform parameters {\n" 668 BEAMFORMER_DAS_UBO_PARAM_LIST 669 "};\n\n" 670 ), 671 #undef X 672 }; 673 674 BeamformerShaderKind shader = cp->pipeline.shaders[shader_slot]; 675 676 u32 program = 0; 677 i32 reloadable_index = beamformer_shader_reloadable_index_by_shader[shader]; 678 if (reloadable_index != -1) { 679 BeamformerShaderKind base_shader = beamformer_reloadable_shader_kinds[reloadable_index]; 680 s8 path; 681 if (!BakeShaders) 682 path = push_s8_from_parts(&arena, os_path_separator(), s8("shaders"), 683 beamformer_reloadable_shader_files[reloadable_index]); 684 685 Stream shader_stream = arena_stream(arena); 686 stream_push_shader_header(&shader_stream, base_shader, compute_headers[base_shader]); 687 688 i32 header_vector_length = beamformer_shader_header_vector_lengths[reloadable_index]; 689 i32 *header_vector = beamformer_shader_header_vectors[reloadable_index]; 690 for (i32 index = 0; index < header_vector_length; index++) 691 stream_append_s8(&shader_stream, beamformer_shader_global_header_strings[header_vector[index]]); 692 693 BeamformerShaderDescriptor *sd = cp->shader_descriptors + shader_slot; 694 695 if (sd->layout.x != 0) { 696 stream_append_s8(&shader_stream, s8("layout(local_size_x = ")); 697 stream_append_u64(&shader_stream, sd->layout.x); 698 stream_append_s8(&shader_stream, s8(", local_size_y = ")); 699 stream_append_u64(&shader_stream, sd->layout.y); 700 stream_append_s8(&shader_stream, s8(", local_size_z = ")); 701 stream_append_u64(&shader_stream, sd->layout.z); 702 stream_append_s8(&shader_stream, s8(") in;\n\n")); 703 } 704 705 u32 *parameters = (u32 *)&sd->bake; 706 s8 *names = beamformer_shader_bake_parameter_names[reloadable_index]; 707 u32 float_bits = beamformer_shader_bake_parameter_float_bits[reloadable_index]; 708 i32 count = beamformer_shader_bake_parameter_counts[reloadable_index]; 709 710 for (i32 index = 0; index < count; index++) { 711 stream_append_s8s(&shader_stream, s8("#define "), names[index], 712 (float_bits & (1 << index))? s8(" uintBitsToFloat") : s8(" "), s8("(0x")); 713 stream_append_hex_u64(&shader_stream, parameters[index]); 714 stream_append_s8(&shader_stream, s8(")\n")); 715 } 716 717 stream_append_s8(&shader_stream, s8("#define DataKind (0x")); 718 stream_append_hex_u64(&shader_stream, sd->bake.data_kind); 719 stream_append_s8(&shader_stream, s8(")\n\n")); 720 721 s8 *flag_names = beamformer_shader_flag_strings[reloadable_index]; 722 u32 flag_count = beamformer_shader_flag_strings_count[reloadable_index]; 723 u32 flags = sd->bake.flags; 724 for (u32 bit = 0; bit < flag_count; bit++) { 725 stream_append_s8s(&shader_stream, s8("#define "), flag_names[bit], 726 (flags & (1 << bit))? s8(" 1") : s8(" 0"), s8("\n")); 727 } 728 729 if (!renderdoc_attached()) 730 stream_append_s8(&shader_stream, s8("\n#line 1\n")); 731 732 s8 shader_text; 733 if (BakeShaders) { 734 stream_append_s8(&shader_stream, beamformer_shader_data[reloadable_index]); 735 shader_text = arena_stream_commit(&arena, &shader_stream); 736 } else { 737 shader_text = arena_stream_commit(&arena, &shader_stream); 738 i64 length = os_read_entire_file((c8 *)path.data, arena.beg, arena_capacity(&arena, u8)); 739 shader_text.len += length; 740 arena_commit(&arena, length); 741 } 742 743 /* TODO(rnp): instance name */ 744 s8 shader_name = beamformer_shader_names[shader]; 745 program = load_shader(arena, &shader_text, (u32 []){GL_COMPUTE_SHADER}, 1, shader_name); 746 } 747 748 glDeleteProgram(cp->programs[shader_slot]); 749 cp->programs[shader_slot] = program; 750 } 751 752 function void 753 beamformer_commit_parameter_block(BeamformerCtx *ctx, BeamformerComputePlan *cp, u32 block, Arena arena) 754 { 755 BeamformerParameterBlock *pb = beamformer_parameter_block_lock(ctx->shared_memory, block, -1); 756 for EachBit(pb->dirty_regions, region) { 757 switch (region) { 758 case BeamformerParameterBlockRegion_ComputePipeline: 759 case BeamformerParameterBlockRegion_Parameters: 760 { 761 cp->output_points = das_valid_points(pb->parameters.output_points.xyz); 762 cp->average_frames = pb->parameters.output_points.E[3]; 763 764 plan_compute_pipeline(cp, pb); 765 766 /* NOTE(rnp): these are both handled by plan_compute_pipeline() */ 767 u32 mask = 1 << BeamformerParameterBlockRegion_ComputePipeline | 768 1 << BeamformerParameterBlockRegion_Parameters; 769 pb->dirty_regions &= ~mask; 770 771 for (u32 shader_slot = 0; shader_slot < cp->pipeline.shader_count; shader_slot++) { 772 u128 hash = u128_hash_from_data(cp->shader_descriptors + shader_slot, sizeof(BeamformerShaderDescriptor)); 773 if (!u128_equal(hash, cp->shader_hashes[shader_slot])) 774 cp->dirty_programs |= 1 << shader_slot; 775 cp->shader_hashes[shader_slot] = hash; 776 } 777 778 #define X(k, t, v) glNamedBufferSubData(cp->ubos[BeamformerComputeUBOKind_##k], \ 779 0, sizeof(t), &cp->v ## _ubo_data); 780 BEAMFORMER_COMPUTE_UBO_LIST 781 #undef X 782 783 cp->acquisition_count = pb->parameters.acquisition_count; 784 cp->acquisition_kind = pb->parameters.acquisition_kind; 785 786 u32 decoded_data_size = cp->rf_size; 787 if (ctx->compute_context.ping_pong_ssbo_size < decoded_data_size) 788 alloc_shader_storage(ctx, decoded_data_size, arena); 789 790 if (cp->hadamard_order != (i32)cp->acquisition_count) 791 update_hadamard_texture(cp, (i32)cp->acquisition_count, arena); 792 793 mem_copy(cp->voxel_transform.E, pb->parameters.das_voxel_transform.E, sizeof(cp->voxel_transform)); 794 795 GLenum gl_kind = cp->iq_pipeline ? GL_RG32F : GL_R32F; 796 if (cp->average_frames > 1 && !beamformer_frame_compatible(ctx->averaged_frames + 0, cp->output_points, gl_kind)) { 797 alloc_beamform_frame(ctx->averaged_frames + 0, cp->output_points, gl_kind, s8("Averaged Frame"), arena); 798 alloc_beamform_frame(ctx->averaged_frames + 1, cp->output_points, gl_kind, s8("Averaged Frame"), arena); 799 } 800 }break; 801 case BeamformerParameterBlockRegion_ChannelMapping:{ 802 cuda_set_channel_mapping(pb->channel_mapping); 803 }break; 804 case BeamformerParameterBlockRegion_FocalVectors: 805 case BeamformerParameterBlockRegion_SparseElements: 806 case BeamformerParameterBlockRegion_TransmitReceiveOrientations: 807 { 808 BeamformerComputeTextureKind texture_kind = 0; 809 u32 pixel_type = 0, texture_format = 0; 810 switch (region) { 811 #define X(kind, _gl, tf, pt, ...) \ 812 case BeamformerParameterBlockRegion_## kind:{ \ 813 texture_kind = BeamformerComputeTextureKind_## kind; \ 814 texture_format = tf; \ 815 pixel_type = pt; \ 816 }break; 817 BEAMFORMER_COMPUTE_TEXTURE_LIST 818 #undef X 819 InvalidDefaultCase; 820 } 821 glTextureSubImage1D(cp->textures[texture_kind], 0, 0, BeamformerMaxChannelCount, 822 texture_format, pixel_type, 823 (u8 *)pb + BeamformerParameterBlockRegionOffsets[region]); 824 }break; 825 } 826 } 827 beamformer_parameter_block_unlock(ctx->shared_memory, block); 828 } 829 830 function void 831 do_compute_shader(BeamformerCtx *ctx, BeamformerComputePlan *cp, BeamformerFrame *frame, 832 BeamformerShaderKind shader, u32 shader_slot, BeamformerShaderParameters *sp, Arena arena) 833 { 834 BeamformerComputeContext *cc = &ctx->compute_context; 835 836 u32 program = cp->programs[shader_slot]; 837 glUseProgram(program); 838 839 u32 output_ssbo_idx = !cc->last_output_ssbo_index; 840 u32 input_ssbo_idx = cc->last_output_ssbo_index; 841 842 uv3 dispatch = cp->shader_descriptors[shader_slot].dispatch; 843 switch (shader) { 844 case BeamformerShaderKind_Decode:{ 845 glBindImageTexture(0, cp->textures[BeamformerComputeTextureKind_Hadamard], 0, 0, 0, GL_READ_ONLY, GL_R32F); 846 847 BeamformerDecodeMode mode = cp->shader_descriptors[shader_slot].bake.Decode.decode_mode; 848 if (shader_slot == 0) { 849 if (mode != BeamformerDecodeMode_None) { 850 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, cc->ping_pong_ssbos[input_ssbo_idx]); 851 glProgramUniform1ui(program, DECODE_FIRST_PASS_UNIFORM_LOC, 1); 852 853 glDispatchCompute(dispatch.x, dispatch.y, dispatch.z); 854 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); 855 } 856 } 857 858 if (mode != BeamformerDecodeMode_None) 859 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, cc->ping_pong_ssbos[input_ssbo_idx]); 860 861 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, cc->ping_pong_ssbos[output_ssbo_idx]); 862 863 glProgramUniform1ui(program, DECODE_FIRST_PASS_UNIFORM_LOC, 0); 864 865 glDispatchCompute(dispatch.x, dispatch.y, dispatch.z); 866 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); 867 868 cc->last_output_ssbo_index = !cc->last_output_ssbo_index; 869 }break; 870 case BeamformerShaderKind_CudaDecode:{ 871 cuda_decode(0, output_ssbo_idx, 0); 872 cc->last_output_ssbo_index = !cc->last_output_ssbo_index; 873 }break; 874 case BeamformerShaderKind_CudaHilbert:{ 875 cuda_hilbert(input_ssbo_idx, output_ssbo_idx); 876 cc->last_output_ssbo_index = !cc->last_output_ssbo_index; 877 }break; 878 case BeamformerShaderKind_Filter: 879 case BeamformerShaderKind_Demodulate: 880 { 881 if (shader_slot != 0) 882 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, cc->ping_pong_ssbos[input_ssbo_idx]); 883 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, cc->ping_pong_ssbos[output_ssbo_idx]); 884 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, cp->filters[sp->filter_slot].ssbo); 885 886 glDispatchCompute(dispatch.x, dispatch.y, dispatch.z); 887 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); 888 889 cc->last_output_ssbo_index = !cc->last_output_ssbo_index; 890 }break; 891 case BeamformerShaderKind_MinMax:{ 892 for (i32 i = 1; i < frame->mips; i++) { 893 glBindImageTexture(0, frame->texture, i - 1, GL_TRUE, 0, GL_READ_ONLY, GL_RG32F); 894 glBindImageTexture(1, frame->texture, i - 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_RG32F); 895 glProgramUniform1i(program, MIN_MAX_MIPS_LEVEL_UNIFORM_LOC, i); 896 897 u32 width = (u32)frame->dim.x >> i; 898 u32 height = (u32)frame->dim.y >> i; 899 u32 depth = (u32)frame->dim.z >> i; 900 glDispatchCompute(ORONE(width / 32), ORONE(height), ORONE(depth / 32)); 901 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); 902 } 903 }break; 904 case BeamformerShaderKind_DAS:{ 905 local_persist u32 das_cycle_t = 0; 906 907 BeamformerShaderBakeParameters *bp = &cp->shader_descriptors[shader_slot].bake; 908 b32 fast = (bp->flags & BeamformerShaderDASFlags_Fast) != 0; 909 b32 sparse = (bp->flags & BeamformerShaderDASFlags_Sparse) != 0; 910 911 if (fast) { 912 glClearTexImage(frame->texture, 0, GL_RED, GL_FLOAT, 0); 913 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT); 914 glBindImageTexture(0, frame->texture, 0, GL_TRUE, 0, GL_READ_WRITE, cp->iq_pipeline ? GL_RG32F : GL_R32F); 915 } else { 916 glBindImageTexture(0, frame->texture, 0, GL_TRUE, 0, GL_WRITE_ONLY, cp->iq_pipeline ? GL_RG32F : GL_R32F); 917 } 918 919 u32 sparse_texture = cp->textures[BeamformerComputeTextureKind_SparseElements]; 920 if (!sparse) sparse_texture = 0; 921 922 glBindBufferBase(GL_UNIFORM_BUFFER, 0, cp->ubos[BeamformerComputeUBOKind_DAS]); 923 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 1, cc->ping_pong_ssbos[input_ssbo_idx], 0, cp->rf_size); 924 glBindImageTexture(1, sparse_texture, 0, 0, 0, GL_READ_ONLY, GL_R16I); 925 glBindImageTexture(2, cp->textures[BeamformerComputeTextureKind_FocalVectors], 0, 0, 0, GL_READ_ONLY, GL_RG32F); 926 glBindImageTexture(3, cp->textures[BeamformerComputeTextureKind_TransmitReceiveOrientations], 0, 0, 0, GL_READ_ONLY, GL_R8I); 927 928 glProgramUniform1ui(program, DAS_CYCLE_T_UNIFORM_LOC, das_cycle_t++); 929 930 if (fast) { 931 i32 loop_end; 932 if (bp->DAS.acquisition_kind == BeamformerAcquisitionKind_RCA_VLS || 933 bp->DAS.acquisition_kind == BeamformerAcquisitionKind_RCA_TPW) 934 { 935 /* NOTE(rnp): to avoid repeatedly sampling the whole focal vectors 936 * texture we loop over transmits for VLS/TPW */ 937 loop_end = (i32)bp->DAS.acquisition_count; 938 } else { 939 loop_end = (i32)bp->DAS.channel_count; 940 } 941 f32 percent_per_step = 1.0f / (f32)loop_end; 942 cc->processing_progress = -percent_per_step; 943 for (i32 index = 0; index < loop_end; index++) { 944 cc->processing_progress += percent_per_step; 945 /* IMPORTANT(rnp): prevents OS from coalescing and killing our shader */ 946 glFinish(); 947 glProgramUniform1i(program, DAS_FAST_CHANNEL_UNIFORM_LOC, index); 948 glDispatchCompute(dispatch.x, dispatch.y, dispatch.z); 949 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); 950 } 951 } else { 952 glDispatchCompute(dispatch.x, dispatch.y, dispatch.z); 953 } 954 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT|GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); 955 }break; 956 case BeamformerShaderKind_Sum:{ 957 u32 aframe_index = ctx->averaged_frame_index % ARRAY_COUNT(ctx->averaged_frames); 958 BeamformerFrame *aframe = ctx->averaged_frames + aframe_index; 959 aframe->id = ctx->averaged_frame_index; 960 atomic_store_u32(&aframe->ready_to_present, 0); 961 /* TODO(rnp): hack we need a better way of specifying which frames to sum; 962 * this is fine for rolling averaging but what if we want to do something else */ 963 assert(frame >= ctx->beamform_frames); 964 assert(frame < ctx->beamform_frames + countof(ctx->beamform_frames)); 965 u32 base_index = (u32)(frame - ctx->beamform_frames); 966 u32 to_average = (u32)cp->average_frames; 967 u32 frame_count = 0; 968 u32 *in_textures = push_array(&arena, u32, BeamformerMaxSavedFrames); 969 ComputeFrameIterator cfi = compute_frame_iterator(ctx, 1 + base_index - to_average, to_average); 970 for (BeamformerFrame *it = frame_next(&cfi); it; it = frame_next(&cfi)) 971 in_textures[frame_count++] = it->texture; 972 973 assert(to_average == frame_count); 974 975 glProgramUniform1f(program, SUM_PRESCALE_UNIFORM_LOC, 1 / (f32)frame_count); 976 do_sum_shader(cc, in_textures, frame_count, aframe->texture, aframe->dim); 977 mem_copy(aframe->voxel_transform.E, frame->voxel_transform.E, sizeof(frame->voxel_transform)); 978 aframe->compound_count = frame->compound_count; 979 aframe->acquisition_kind = frame->acquisition_kind; 980 }break; 981 InvalidDefaultCase; 982 } 983 } 984 985 function s8 986 shader_text_with_header(s8 header, s8 filepath, b32 has_file, BeamformerShaderKind shader_kind, Arena *arena) 987 { 988 Stream sb = arena_stream(*arena); 989 stream_push_shader_header(&sb, shader_kind, header); 990 stream_append_s8(&sb, s8("\n#line 1\n")); 991 992 s8 result; 993 if (BakeShaders) { 994 /* TODO(rnp): better handling of shaders with no backing file */ 995 if (has_file) { 996 i32 reloadable_index = beamformer_shader_reloadable_index_by_shader[shader_kind]; 997 stream_append_s8(&sb, beamformer_shader_data[reloadable_index]); 998 } 999 result = arena_stream_commit(arena, &sb); 1000 } else { 1001 result = arena_stream_commit(arena, &sb); 1002 if (has_file) { 1003 i64 length = os_read_entire_file((c8 *)filepath.data, arena->beg, arena_capacity(arena, u8)); 1004 result.len += length; 1005 arena_commit(arena, length); 1006 } 1007 } 1008 1009 return result; 1010 } 1011 1012 /* NOTE(rnp): currently this function is only handling rendering shaders. 1013 * look at load_compute_shader for compute shaders */ 1014 function void 1015 beamformer_reload_shader(BeamformerCtx *ctx, BeamformerShaderReloadContext *src, Arena arena, s8 shader_name) 1016 { 1017 BeamformerShaderKind kind = beamformer_reloadable_shader_kinds[src->reloadable_info_index]; 1018 assert(kind == BeamformerShaderKind_Render3D); 1019 1020 s8 path = push_s8_from_parts(&arena, os_path_separator(), s8("shaders"), 1021 beamformer_reloadable_shader_files[src->reloadable_info_index]); 1022 1023 i32 shader_count = 1; 1024 BeamformerShaderReloadContext *link = src->link; 1025 while (link != src) { shader_count++; link = link->link; } 1026 1027 s8 *shader_texts = push_array(&arena, s8, shader_count); 1028 u32 *shader_types = push_array(&arena, u32, shader_count); 1029 1030 i32 index = 0; 1031 do { 1032 b32 has_file = link->reloadable_info_index >= 0; 1033 shader_texts[index] = shader_text_with_header(link->header, path, has_file, kind, &arena); 1034 shader_types[index] = link->gl_type; 1035 index++; 1036 link = link->link; 1037 } while (link != src); 1038 1039 u32 *shader = &ctx->frame_view_render_context.shader; 1040 glDeleteProgram(*shader); 1041 *shader = load_shader(arena, shader_texts, shader_types, shader_count, shader_name); 1042 ctx->frame_view_render_context.updated = 1; 1043 } 1044 1045 function void 1046 complete_queue(BeamformerCtx *ctx, BeamformWorkQueue *q, Arena *arena, iptr gl_context) 1047 { 1048 BeamformerComputeContext * cs = &ctx->compute_context; 1049 BeamformerSharedMemory * sm = ctx->shared_memory; 1050 1051 BeamformWork *work = beamform_work_queue_pop(q); 1052 while (work) { 1053 b32 can_commit = 1; 1054 switch (work->kind) { 1055 case BeamformerWorkKind_ExportBuffer:{ 1056 /* TODO(rnp): better way of handling DispatchCompute barrier */ 1057 post_sync_barrier(ctx->shared_memory, BeamformerSharedMemoryLockKind_DispatchCompute); 1058 beamformer_shared_memory_take_lock(ctx->shared_memory, (i32)work->lock, (u32)-1); 1059 BeamformerExportContext *ec = &work->export_context; 1060 switch (ec->kind) { 1061 case BeamformerExportKind_BeamformedData:{ 1062 BeamformerFrame *frame = ctx->latest_frame; 1063 if (frame) { 1064 assert(frame->ready_to_present); 1065 u32 texture = frame->texture; 1066 iv3 dim = frame->dim; 1067 u32 out_size = (u32)dim.x * (u32)dim.y * (u32)dim.z * 2 * sizeof(f32); 1068 if (out_size <= ec->size) { 1069 glGetTextureImage(texture, 0, GL_RG, GL_FLOAT, (i32)out_size, 1070 beamformer_shared_memory_scratch_arena(sm, ctx->shared_memory_size).beg); 1071 } 1072 } 1073 }break; 1074 case BeamformerExportKind_Stats:{ 1075 ComputeTimingTable *table = ctx->compute_timing_table; 1076 /* NOTE(rnp): do a little spin to let this finish updating */ 1077 spin_wait(table->write_index != atomic_load_u32(&table->read_index)); 1078 ComputeShaderStats *stats = ctx->compute_shader_stats; 1079 if (sizeof(stats->table) <= ec->size) 1080 mem_copy(beamformer_shared_memory_scratch_arena(sm, ctx->shared_memory_size).beg, 1081 &stats->table, sizeof(stats->table)); 1082 }break; 1083 InvalidDefaultCase; 1084 } 1085 beamformer_shared_memory_release_lock(ctx->shared_memory, work->lock); 1086 post_sync_barrier(ctx->shared_memory, BeamformerSharedMemoryLockKind_ExportSync); 1087 }break; 1088 case BeamformerWorkKind_CreateFilter:{ 1089 /* TODO(rnp): this should probably get deleted and moved to lazy loading */ 1090 BeamformerCreateFilterContext *fctx = &work->create_filter_context; 1091 u32 block = fctx->parameter_block; 1092 u32 slot = fctx->filter_slot; 1093 BeamformerComputePlan *cp = beamformer_compute_plan_for_block(cs, block, arena); 1094 beamformer_filter_update(cp->filters + slot, fctx->parameters, block, slot, *arena); 1095 }break; 1096 case BeamformerWorkKind_ComputeIndirect:{ 1097 fill_frame_compute_work(ctx, work, work->compute_indirect_context.view_plane, 1098 work->compute_indirect_context.parameter_block, 1); 1099 } /* FALLTHROUGH */ 1100 case BeamformerWorkKind_Compute:{ 1101 DEBUG_DECL(glClearNamedBufferData(cs->ping_pong_ssbos[0], GL_RG32F, GL_RG, GL_FLOAT, 0);) 1102 DEBUG_DECL(glClearNamedBufferData(cs->ping_pong_ssbos[1], GL_RG32F, GL_RG, GL_FLOAT, 0);) 1103 DEBUG_DECL(glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);) 1104 1105 push_compute_timing_info(ctx->compute_timing_table, 1106 (ComputeTimingInfo){.kind = ComputeTimingInfoKind_ComputeFrameBegin}); 1107 1108 BeamformerComputePlan *cp = beamformer_compute_plan_for_block(cs, work->compute_context.parameter_block, arena); 1109 if (beamformer_parameter_block_dirty(sm, work->compute_context.parameter_block)) { 1110 u32 block = work->compute_context.parameter_block; 1111 beamformer_commit_parameter_block(ctx, cp, block, *arena); 1112 atomic_store_u32(&ctx->ui_dirty_parameter_blocks, (u32)(ctx->beamform_work_queue != q) << block); 1113 } 1114 1115 post_sync_barrier(ctx->shared_memory, work->lock); 1116 1117 u32 dirty_programs = atomic_swap_u32(&cp->dirty_programs, 0); 1118 static_assert(ISPOWEROF2(BeamformerMaxComputeShaderStages), 1119 "max compute shader stages must be power of 2"); 1120 assert((dirty_programs & ~((u32)BeamformerMaxComputeShaderStages - 1)) == 0); 1121 for EachBit(dirty_programs, slot) 1122 load_compute_shader(ctx, cp, (u32)slot, *arena); 1123 1124 atomic_store_u32(&cs->processing_compute, 1); 1125 start_renderdoc_capture(gl_context); 1126 1127 BeamformerFrame *frame = work->compute_context.frame; 1128 1129 GLenum gl_kind = cp->iq_pipeline ? GL_RG32F : GL_R32F; 1130 if (!beamformer_frame_compatible(frame, cp->output_points, gl_kind)) 1131 alloc_beamform_frame(frame, cp->output_points, gl_kind, s8("Beamformed_Data"), *arena); 1132 1133 m4 voxel_transform = m4_mul(cp->ui_voxel_transform, cp->voxel_transform); 1134 mem_copy(frame->voxel_transform.E, voxel_transform.E, sizeof(voxel_transform)); 1135 frame->acquisition_kind = cp->acquisition_kind; 1136 frame->compound_count = cp->acquisition_count; 1137 1138 BeamformerComputeContext *cc = &ctx->compute_context; 1139 BeamformerComputePipeline *pipeline = &cp->pipeline; 1140 /* NOTE(rnp): first stage requires access to raw data buffer directly so we break 1141 * it out into a separate step. This way data can get released as soon as possible */ 1142 if (pipeline->shader_count > 0) { 1143 BeamformerRFBuffer *rf = &cs->rf_buffer; 1144 u32 slot = rf->compute_index % countof(rf->compute_syncs); 1145 1146 if (work->kind == BeamformerWorkKind_ComputeIndirect) { 1147 /* NOTE(rnp): compute indirect is used when uploading data. if compute thread 1148 * preempts upload it must wait for the fence to exist. then it must tell the 1149 * GPU to wait for upload to complete before it can start compute */ 1150 spin_wait(!atomic_load_u64(rf->upload_syncs + slot)); 1151 1152 glWaitSync(rf->upload_syncs[slot], 0, GL_TIMEOUT_IGNORED); 1153 glDeleteSync(rf->upload_syncs[slot]); 1154 rf->compute_index++; 1155 } else { 1156 slot = (rf->compute_index - 1) % countof(rf->compute_syncs); 1157 } 1158 1159 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 1, rf->ssbo, slot * rf->active_rf_size, rf->active_rf_size); 1160 1161 glBeginQuery(GL_TIME_ELAPSED, cc->shader_timer_ids[0]); 1162 do_compute_shader(ctx, cp, frame, pipeline->shaders[0], 0, pipeline->parameters + 0, *arena); 1163 glEndQuery(GL_TIME_ELAPSED); 1164 1165 if (work->kind == BeamformerWorkKind_ComputeIndirect) { 1166 atomic_store_u64(rf->compute_syncs + slot, glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); 1167 atomic_store_u64(rf->upload_syncs + slot, 0); 1168 } 1169 } 1170 1171 b32 did_sum_shader = 0; 1172 for (u32 i = 1; i < pipeline->shader_count; i++) { 1173 did_sum_shader |= pipeline->shaders[i] == BeamformerShaderKind_Sum; 1174 glBeginQuery(GL_TIME_ELAPSED, cc->shader_timer_ids[i]); 1175 do_compute_shader(ctx, cp, frame, pipeline->shaders[i], i, pipeline->parameters + i, *arena); 1176 glEndQuery(GL_TIME_ELAPSED); 1177 } 1178 1179 /* NOTE(rnp): the first of these blocks until work completes */ 1180 for (u32 i = 0; i < pipeline->shader_count; i++) { 1181 ComputeTimingInfo info = {0}; 1182 info.kind = ComputeTimingInfoKind_Shader; 1183 info.shader = pipeline->shaders[i]; 1184 glGetQueryObjectui64v(cc->shader_timer_ids[i], GL_QUERY_RESULT, &info.timer_count); 1185 push_compute_timing_info(ctx->compute_timing_table, info); 1186 } 1187 cs->processing_progress = 1; 1188 1189 frame->ready_to_present = 1; 1190 if (did_sum_shader) { 1191 u32 aframe_index = ((ctx->averaged_frame_index++) % countof(ctx->averaged_frames)); 1192 ctx->averaged_frames[aframe_index].view_plane_tag = frame->view_plane_tag; 1193 ctx->averaged_frames[aframe_index].ready_to_present = 1; 1194 atomic_store_u64((u64 *)&ctx->latest_frame, (u64)(ctx->averaged_frames + aframe_index)); 1195 } else { 1196 atomic_store_u64((u64 *)&ctx->latest_frame, (u64)frame); 1197 } 1198 cs->processing_compute = 0; 1199 1200 push_compute_timing_info(ctx->compute_timing_table, 1201 (ComputeTimingInfo){.kind = ComputeTimingInfoKind_ComputeFrameEnd}); 1202 1203 end_renderdoc_capture(gl_context); 1204 }break; 1205 InvalidDefaultCase; 1206 } 1207 1208 if (can_commit) { 1209 beamform_work_queue_pop_commit(q); 1210 work = beamform_work_queue_pop(q); 1211 } 1212 } 1213 } 1214 1215 function void 1216 coalesce_timing_table(ComputeTimingTable *t, ComputeShaderStats *stats) 1217 { 1218 /* TODO(rnp): we do not currently do anything to handle the potential for a half written 1219 * info item. this could result in garbage entries but they shouldn't really matter */ 1220 1221 u32 target = atomic_load_u32(&t->write_index); 1222 u32 stats_index = (stats->latest_frame_index + 1) % countof(stats->table.times); 1223 1224 static_assert(BeamformerShaderKind_Count + 1 <= 32, "timing coalescence bitfield test"); 1225 u32 seen_info_test = 0; 1226 1227 while (t->read_index != target) { 1228 ComputeTimingInfo info = t->buffer[t->read_index % countof(t->buffer)]; 1229 switch (info.kind) { 1230 case ComputeTimingInfoKind_ComputeFrameBegin:{ 1231 assert(t->compute_frame_active == 0); 1232 t->compute_frame_active = 1; 1233 /* NOTE(rnp): allow multiple instances of same shader to accumulate */ 1234 mem_clear(stats->table.times[stats_index], 0, sizeof(stats->table.times[stats_index])); 1235 }break; 1236 case ComputeTimingInfoKind_ComputeFrameEnd:{ 1237 assert(t->compute_frame_active == 1); 1238 t->compute_frame_active = 0; 1239 stats->latest_frame_index = stats_index; 1240 stats_index = (stats_index + 1) % countof(stats->table.times); 1241 }break; 1242 case ComputeTimingInfoKind_Shader:{ 1243 stats->table.times[stats_index][info.shader] += (f32)info.timer_count / 1.0e9f; 1244 seen_info_test |= (1u << info.shader); 1245 }break; 1246 case ComputeTimingInfoKind_RF_Data:{ 1247 stats->latest_rf_index = (stats->latest_rf_index + 1) % countof(stats->table.rf_time_deltas); 1248 f32 delta = (f32)(info.timer_count - stats->last_rf_timer_count) / 1.0e9f; 1249 stats->table.rf_time_deltas[stats->latest_rf_index] = delta; 1250 stats->last_rf_timer_count = info.timer_count; 1251 seen_info_test |= (1 << BeamformerShaderKind_Count); 1252 }break; 1253 } 1254 /* NOTE(rnp): do this at the end so that stats table is always in a consistent state */ 1255 atomic_add_u32(&t->read_index, 1); 1256 } 1257 1258 if (seen_info_test) { 1259 for EachEnumValue(BeamformerShaderKind, shader) { 1260 if (seen_info_test & (1 << shader)) { 1261 f32 sum = 0; 1262 for EachElement(stats->table.times, i) 1263 sum += stats->table.times[i][shader]; 1264 stats->average_times[shader] = sum / countof(stats->table.times); 1265 } 1266 } 1267 1268 if (seen_info_test & (1 << BeamformerShaderKind_Count)) { 1269 f32 sum = 0; 1270 for EachElement(stats->table.rf_time_deltas, i) 1271 sum += stats->table.rf_time_deltas[i]; 1272 stats->rf_time_delta_average = sum / countof(stats->table.rf_time_deltas); 1273 } 1274 } 1275 } 1276 1277 DEBUG_EXPORT BEAMFORMER_COMPLETE_COMPUTE_FN(beamformer_complete_compute) 1278 { 1279 BeamformerCtx *ctx = (BeamformerCtx *)user_context; 1280 BeamformerSharedMemory *sm = ctx->shared_memory; 1281 complete_queue(ctx, &sm->external_work_queue, arena, gl_context); 1282 complete_queue(ctx, ctx->beamform_work_queue, arena, gl_context); 1283 } 1284 1285 function void 1286 beamformer_rf_buffer_allocate(BeamformerRFBuffer *rf, u32 rf_size, b32 nvidia) 1287 { 1288 assert((rf_size % 64) == 0); 1289 if (!nvidia) glUnmapNamedBuffer(rf->ssbo); 1290 glDeleteBuffers(1, &rf->ssbo); 1291 glCreateBuffers(1, &rf->ssbo); 1292 1293 u32 buffer_flags = GL_DYNAMIC_STORAGE_BIT; 1294 if (!nvidia) buffer_flags |= GL_MAP_PERSISTENT_BIT|GL_MAP_WRITE_BIT; 1295 1296 glNamedBufferStorage(rf->ssbo, countof(rf->compute_syncs) * rf_size, 0, buffer_flags); 1297 1298 if (!nvidia) { 1299 u32 access = GL_MAP_PERSISTENT_BIT|GL_MAP_WRITE_BIT|GL_MAP_FLUSH_EXPLICIT_BIT|GL_MAP_UNSYNCHRONIZED_BIT; 1300 rf->buffer = glMapNamedBufferRange(rf->ssbo, 0, (GLsizei)(countof(rf->compute_syncs) * rf_size), access); 1301 } 1302 1303 LABEL_GL_OBJECT(GL_BUFFER, rf->ssbo, s8("Raw_RF_SSBO")); 1304 rf->size = rf_size; 1305 } 1306 1307 DEBUG_EXPORT BEAMFORMER_RF_UPLOAD_FN(beamformer_rf_upload) 1308 { 1309 BeamformerSharedMemory *sm = ctx->shared_memory; 1310 BeamformerSharedMemoryLockKind scratch_lock = BeamformerSharedMemoryLockKind_ScratchSpace; 1311 BeamformerSharedMemoryLockKind upload_lock = BeamformerSharedMemoryLockKind_UploadRF; 1312 1313 u64 rf_block_rf_size; 1314 if (atomic_load_u32(sm->locks + upload_lock) && 1315 (rf_block_rf_size = atomic_swap_u64(&sm->rf_block_rf_size, 0))) 1316 { 1317 beamformer_shared_memory_take_lock(ctx->shared_memory, (i32)scratch_lock, (u32)-1); 1318 1319 BeamformerRFBuffer *rf = ctx->rf_buffer; 1320 BeamformerParameterBlock *b = beamformer_parameter_block(sm, (u32)(rf_block_rf_size >> 32ULL)); 1321 BeamformerParameters *bp = &b->parameters; 1322 BeamformerDataKind data_kind = b->pipeline.data_kind; 1323 1324 b32 nvidia = gl_parameters.vendor_id == GLVendor_NVIDIA; 1325 1326 rf->active_rf_size = (u32)round_up_to(rf_block_rf_size & 0xFFFFFFFFULL, 64); 1327 if unlikely(rf->size < rf->active_rf_size) 1328 beamformer_rf_buffer_allocate(rf, rf->active_rf_size, nvidia); 1329 1330 u32 slot = rf->insertion_index++ % countof(rf->compute_syncs); 1331 1332 /* NOTE(rnp): if the rest of the code is functioning then the first 1333 * time the compute thread processes an upload it must have gone 1334 * through this path. therefore it is safe to spin until it gets processed */ 1335 spin_wait(atomic_load_u64(rf->upload_syncs + slot)); 1336 1337 if (atomic_load_u64(rf->compute_syncs + slot)) { 1338 GLenum sync_result = glClientWaitSync(rf->compute_syncs[slot], 0, 1000000000); 1339 if (sync_result == GL_TIMEOUT_EXPIRED || sync_result == GL_WAIT_FAILED) { 1340 // TODO(rnp): what do? 1341 } 1342 glDeleteSync(rf->compute_syncs[slot]); 1343 } 1344 1345 u32 size = bp->channel_count * bp->acquisition_count * bp->sample_count * beamformer_data_kind_byte_size[data_kind]; 1346 u8 *data = beamformer_shared_memory_scratch_arena(sm, ctx->shared_memory_size).beg; 1347 1348 if (nvidia) glNamedBufferSubData(rf->ssbo, slot * rf->active_rf_size, (i32)size, data); 1349 else memory_copy_non_temporal(rf->buffer + slot * rf->active_rf_size, data, size); 1350 store_fence(); 1351 1352 beamformer_shared_memory_release_lock(ctx->shared_memory, (i32)scratch_lock); 1353 post_sync_barrier(ctx->shared_memory, upload_lock); 1354 1355 if (!nvidia) 1356 glFlushMappedNamedBufferRange(rf->ssbo, slot * rf->active_rf_size, (i32)rf->active_rf_size); 1357 1358 atomic_store_u64(rf->upload_syncs + slot, glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); 1359 atomic_store_u64(rf->compute_syncs + slot, 0); 1360 1361 os_wake_all_waiters(ctx->compute_worker_sync); 1362 1363 ComputeTimingInfo info = {.kind = ComputeTimingInfoKind_RF_Data}; 1364 glGetQueryObjectui64v(rf->data_timestamp_query, GL_QUERY_RESULT, &info.timer_count); 1365 glQueryCounter(rf->data_timestamp_query, GL_TIMESTAMP); 1366 push_compute_timing_info(ctx->compute_timing_table, info); 1367 } 1368 } 1369 1370 function void 1371 beamformer_queue_compute(BeamformerCtx *ctx, BeamformerFrame *frame, u32 parameter_block) 1372 { 1373 BeamformerSharedMemory *sm = ctx->shared_memory; 1374 BeamformerSharedMemoryLockKind dispatch_lock = BeamformerSharedMemoryLockKind_DispatchCompute; 1375 if (!sm->live_imaging_parameters.active && beamformer_shared_memory_take_lock(sm, (i32)dispatch_lock, 0)) 1376 { 1377 BeamformWork *work = beamform_work_queue_push(ctx->beamform_work_queue); 1378 BeamformerViewPlaneTag tag = frame ? frame->view_plane_tag : 0; 1379 if (fill_frame_compute_work(ctx, work, tag, parameter_block, 0)) 1380 beamform_work_queue_push_commit(ctx->beamform_work_queue); 1381 } 1382 os_wake_all_waiters(&ctx->compute_worker.sync_variable); 1383 } 1384 1385 #include "ui.c" 1386 1387 function void 1388 beamformer_process_input_events(BeamformerCtx *ctx, BeamformerInput *input, 1389 BeamformerInputEvent *events, u32 event_count) 1390 { 1391 for (u32 index = 0; index < event_count; index++) { 1392 BeamformerInputEvent *event = events + index; 1393 switch (event->kind) { 1394 1395 case BeamformerInputEventKind_ExecutableReload:{ 1396 ui_init(ctx, ctx->ui_backing_store); 1397 1398 #if BEAMFORMER_RENDERDOC_HOOKS 1399 start_frame_capture = input->renderdoc_start_frame_capture; 1400 end_frame_capture = input->renderdoc_end_frame_capture; 1401 #endif 1402 }break; 1403 1404 case BeamformerInputEventKind_FileEvent:{ 1405 BeamformerFileReloadContext *frc = event->file_watch_user_context; 1406 switch (frc->kind) { 1407 case BeamformerFileReloadKind_Shader:{ 1408 BeamformerShaderReloadContext *src = frc->shader_reload_context; 1409 BeamformerShaderKind kind = beamformer_reloadable_shader_kinds[src->reloadable_info_index]; 1410 beamformer_reload_shader(ctx, src, ctx->arena, beamformer_shader_names[kind]); 1411 }break; 1412 case BeamformerFileReloadKind_ComputeShader:{ 1413 for EachElement(ctx->compute_context.compute_plans, block) { 1414 BeamformerComputePlan *cp = ctx->compute_context.compute_plans[block]; 1415 for (u32 slot = 0; cp && slot < cp->pipeline.shader_count; slot++) { 1416 i32 shader_index = beamformer_shader_reloadable_index_by_shader[cp->pipeline.shaders[slot]]; 1417 if (beamformer_reloadable_shader_kinds[shader_index] == frc->compute_shader_kind) 1418 atomic_or_u32(&cp->dirty_programs, 1 << slot); 1419 } 1420 } 1421 1422 if (ctx->latest_frame) 1423 beamformer_queue_compute(ctx, ctx->latest_frame, ctx->latest_frame->parameter_block); 1424 }break; 1425 InvalidDefaultCase; 1426 } 1427 }break; 1428 1429 InvalidDefaultCase; 1430 } 1431 } 1432 } 1433 1434 BEAMFORMER_EXPORT void 1435 beamformer_frame_step(BeamformerInput *input) 1436 { 1437 BeamformerCtx *ctx = BeamformerContextMemory(input->memory); 1438 1439 u64 current_time = os_timer_count(); 1440 dt_for_frame = (f64)(current_time - ctx->frame_timestamp) / os_system_info()->timer_frequency; 1441 ctx->frame_timestamp = current_time; 1442 1443 if (IsWindowResized()) { 1444 ctx->window_size.h = GetScreenHeight(); 1445 ctx->window_size.w = GetScreenWidth(); 1446 } 1447 1448 coalesce_timing_table(ctx->compute_timing_table, ctx->compute_shader_stats); 1449 1450 beamformer_process_input_events(ctx, input, input->event_queue, input->event_count); 1451 1452 BeamformerSharedMemory *sm = ctx->shared_memory; 1453 if (atomic_load_u32(sm->locks + BeamformerSharedMemoryLockKind_UploadRF)) 1454 os_wake_all_waiters(&ctx->upload_worker.sync_variable); 1455 if (atomic_load_u32(sm->locks + BeamformerSharedMemoryLockKind_DispatchCompute)) 1456 os_wake_all_waiters(&ctx->compute_worker.sync_variable); 1457 1458 BeamformerFrame *frame = ctx->latest_frame; 1459 BeamformerViewPlaneTag tag = frame? frame->view_plane_tag : 0; 1460 draw_ui(ctx, input, frame, tag); 1461 1462 ctx->frame_view_render_context.updated = 0; 1463 }