ogl_beamforming

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

Commit: 4da4b88e8cea6ef3655f1c14c996083c249b8473
Parent: 2ba978b1f252038611b619eb81d15fa3a83ffbf7
Author: Randy Palamar
Date:   Fri, 20 Nov 2026 06:22:01 -0700

shaders: group push constants in pc global

for larger shaders like DAS it was becoming hard to track where
certain variables were coming from and it is annoying to use worse
names locally to avoid clashing with a global push consant name.

Diffstat:
Mbuild.c | 2+-
Mgenerated/beamformer.c | 14+++++++-------
Mshaders/coherency_weighting.glsl | 4++--
Mshaders/das.glsl | 44++++++++++++++++++++++----------------------
Mshaders/decode.glsl | 14+++++++-------
Mshaders/filter.glsl | 8++++----
Mshaders/render_3d.frag.glsl | 37+++++++++++++++++++------------------
Mshaders/render_3d.vert.glsl | 6+++---
Mshaders/reshape.glsl | 8++++----
9 files changed, 69 insertions(+), 68 deletions(-)

diff --git a/build.c b/build.c @@ -3719,7 +3719,7 @@ meta_push_shader_reload_info(MetaprogramContext *m, MetaContext *ctx) .prefix = str8("\" "), .suffix = str8(";\\n\""), }); - meta_push_line(m, str8("\"};\\n\"")); + meta_push_line(m, str8("\"} pc;\\n\"")); meta_push_line(m, str8("\"\\n\"),")); }break; diff --git a/generated/beamformer.c b/generated/beamformer.c @@ -686,7 +686,7 @@ read_only str8 beamformer_shader_global_header_strings[] = { "layout(push_constant, std430) uniform PushConstants {\n" " uint64_t rf_buffer;\n" " uint64_t output_buffer;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "#define ComplexFilter ((CompileFlags & (1 << 0)) != 0)\n" @@ -696,7 +696,7 @@ read_only str8 beamformer_shader_global_header_strings[] = { "layout(push_constant, std430) uniform PushConstants {\n" " uint64_t input_buffer;\n" " uint64_t output_buffer;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "#define AcquisitionKind_FORCES 0\n" @@ -738,12 +738,12 @@ read_only str8 beamformer_shader_global_header_strings[] = { " float32_t steering_angle;\n" " uint16_t tx_rx_orientation;\n" " uint16_t readi_group;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "layout(push_constant, std430) uniform PushConstants {\n" " uint64_t coherent_sum;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "#define Deinterleave ((CompileFlags & (1 << 0)) != 0)\n" @@ -754,7 +754,7 @@ read_only str8 beamformer_shader_global_header_strings[] = { " uint64_t output_buffer;\n" " uint64_t left_input_buffer;\n" " uint64_t right_input_buffer;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "layout(push_constant, std430) uniform PushConstants {\n" @@ -762,7 +762,7 @@ read_only str8 beamformer_shader_global_header_strings[] = { " uint64_t input_data;\n" " uint32_t image_elements;\n" " float32_t scale;\n" - "};\n" + "} pc;\n" "\n"), str8_comp("" "layout(push_constant, std430) uniform PushConstants {\n" @@ -779,7 +779,7 @@ read_only str8 beamformer_shader_global_header_strings[] = { " uint32_t input_size_y;\n" " uint32_t input_size_z;\n" " uint32_t data_kind;\n" - "};\n" + "} pc;\n" "\n"), }; diff --git a/shaders/coherency_weighting.glsl b/shaders/coherency_weighting.glsl @@ -16,10 +16,10 @@ layout(std430, buffer_reference, buffer_reference_align = 8) restrict buffer Flo }; #if InputDataKind == DataKind_Float32 - #define COHERENT_SAMPLE(index) Float32(coherent_sum).values[index] + #define COHERENT_SAMPLE(index) Float32(pc.coherent_sum).values[index] #define INCOHERENT_SAMPLE(index) Float32(HeapBase + IncoherentSum).values[index] #elif InputDataKind == DataKind_Float32Complex - #define COHERENT_SAMPLE(index) Float32Complex(coherent_sum).values[index] + #define COHERENT_SAMPLE(index) Float32Complex(pc.coherent_sum).values[index] #define INCOHERENT_SAMPLE(index) Float32(HeapBase + IncoherentSum).values[index] #else #error DataKind unsupported for CoherencyWeighting diff --git a/shaders/das.glsl b/shaders/das.glsl @@ -72,13 +72,13 @@ vec2 rotate_iq(const vec2 iq, const float time) u32 batch_channel_count() { const bool safe = (ReceiveChannelCount % ChunkChannelCount) == 0; - const u32 result = safe ? ChunkChannelCount : min(ReceiveChannelCount - channel_offset, ChunkChannelCount); + const u32 result = safe ? ChunkChannelCount : min(ReceiveChannelCount - pc.channel_offset, ChunkChannelCount); return result; } u64 rf_data_pointer(const u32 channel, const u32 acquisition) { - u64 result = rf_data + ChannelByteStride * channel + AcquisitionByteStride * acquisition; + u64 result = pc.rf_data + ChannelByteStride * channel + AcquisitionByteStride * acquisition; result -= InputDataKindByteSize * u32(InterpolationMode == InterpolationMode_Cubic); return result; } @@ -248,7 +248,7 @@ f32 rca_transmit_distance(const vec3 world_point, const vec2 focal_vector, const mat4 transducer_transform(const u32 acquisition) { const u32 receive_tile_channels = ReceiveChannelCount / ReceiveTileCount; - const u32 receive_index = channel_offset / receive_tile_channels; + const u32 receive_index = pc.channel_offset / receive_tile_channels; const u32 transmit_tile_channels = TransmitChannelCount / TransmitTileCount; const u32 transmit_index = acquisition / transmit_tile_channels; @@ -273,8 +273,8 @@ RESULT_TYPE RCA(const vec3 world_point) u64 rf_pointer = rf_data_pointer(0u, u32(acquisition)); for (f32 chunk_channel = 0.f; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { - f32 rx_channel = f32(channel_offset) + chunk_channel; - vec3 rx_center = vec3(rx_channel * xdc_element_pitch, 0); + f32 rx_channel = f32(pc.channel_offset) + chunk_channel; + vec3 rx_center = vec3(rx_channel * pc.xdc_element_pitch, 0); vec2 receive_vector = xdc_world_point - rca_plane_projection(rx_center, rx_rows); f32 a_arg = abs(FNumber * receive_vector.x / abs(xdc_world_point.y)); @@ -293,23 +293,23 @@ RESULT_TYPE HERCULES(const vec3 world_point) { // TODO(rnp): this whole thing needs have an outer loop over transmit tiles const mat4 xdc_transform = transducer_transform(0); - const bool rx_cols = RX_ORIENTATION(tx_rx_orientation) == RCAOrientation_Columns; - const vec2 focal_vector = f32vec2(steering_angle, focus_depth); + const bool rx_cols = RX_ORIENTATION(pc.tx_rx_orientation) == RCAOrientation_Columns; + const vec2 focal_vector = f32vec2(pc.steering_angle, pc.focus_depth); const vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; - const f32 transmit_index = sample_index(rca_transmit_distance(world_point, focal_vector, u8(tx_rx_orientation))); + const f32 transmit_index = sample_index(rca_transmit_distance(world_point, focal_vector, u8(pc.tx_rx_orientation))); const f32 z_delta_squared = xdc_world_point.z * xdc_world_point.z; const f32 f_number_over_z = abs(FNumber / xdc_world_point.z); const f32 apodization_test = 0.25f / (f_number_over_z * f_number_over_z); const f32 rx_world_point = xdc_world_point[s32(!rx_cols)]; const f32 tx_world_point = xdc_world_point[s32(rx_cols)]; - const f32 rx_pitch = xdc_element_pitch[s32(!rx_cols)]; - const f32 tx_pitch = xdc_element_pitch[s32(rx_cols)]; + const f32 rx_pitch = pc.xdc_element_pitch[s32(!rx_cols)]; + const f32 tx_pitch = pc.xdc_element_pitch[s32(rx_cols)]; RESULT_TYPE result = RESULT_TYPE(0); for (f32 chunk_channel = 0.f; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { - f32 rx_channel = f32(channel_offset) + chunk_channel; + f32 rx_channel = f32(pc.channel_offset) + chunk_channel; f32 element_receive_delta_squared = rx_world_point - rx_channel * rx_pitch; element_receive_delta_squared *= element_receive_delta_squared; @@ -344,10 +344,10 @@ RESULT_TYPE FORCES(const vec3 world_point) RESULT_TYPE result = RESULT_TYPE(0); const mat4 xdc_transform = transducer_transform(0); - const u32 rx_axis = u32(RX_ORIENTATION(tx_rx_orientation) == RCAOrientation_Rows); - const f32 element_pitch = xdc_element_pitch[rx_axis]; + const u32 rx_axis = u32(RX_ORIENTATION(pc.tx_rx_orientation) == RCAOrientation_Rows); + const f32 element_pitch = pc.xdc_element_pitch[rx_axis]; // TODO(rnp): the sign of the origin offset might be flipped - const f32 origin_offset = focus_depth * tan(radians(steering_angle)); + const f32 origin_offset = pc.focus_depth * tan(radians(pc.steering_angle)); vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; f32 lateral_coord = xdc_world_point[rx_axis]; @@ -371,7 +371,7 @@ RESULT_TYPE FORCES(const vec3 world_point) } for (u32 channel = 0; channel < batch_channel_count(); channel += 1u) { - f32 rx_channel = f32(channel_offset + channel); + f32 rx_channel = f32(pc.channel_offset + channel); f32 receive_delta = lateral_coord - rx_channel * element_pitch; f32 a_arg = abs(FNumber * receive_delta / axial_coord); @@ -397,10 +397,10 @@ RESULT_TYPE READI_FORCES(const vec3 world_point) RESULT_TYPE result = RESULT_TYPE(0); const mat4 xdc_transform = transducer_transform(0); - const u32 rx_axis = u32(RX_ORIENTATION(tx_rx_orientation) == RCAOrientation_Rows); - const f32 element_pitch = xdc_element_pitch[rx_axis]; + const u32 rx_axis = u32(RX_ORIENTATION(pc.tx_rx_orientation) == RCAOrientation_Rows); + const f32 element_pitch = pc.xdc_element_pitch[rx_axis]; // TODO(rnp): the sign of the origin offset might be flipped - const f32 origin_offset = focus_depth * tan(radians(steering_angle)); + const f32 origin_offset = pc.focus_depth * tan(radians(pc.steering_angle)); vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; f32 lateral_coord = xdc_world_point[rx_axis]; @@ -410,10 +410,10 @@ RESULT_TYPE READI_FORCES(const vec3 world_point) f32 transmit_oopz_squared = tx_out_of_plane_delta * tx_out_of_plane_delta + z_delta_squared; // NOTE(tkh): The row we use matches the acquisition group, the column is the element group we are beamforming. - s32 hadamard_offset = s32(readi_group) * s32(ReadiGroupCount); + s32 hadamard_offset = s32(pc.readi_group) * s32(ReadiGroupCount); for (f32 chunk_channel = 0; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { - f32 rx_channel = f32(channel_offset) + chunk_channel; + f32 rx_channel = f32(pc.channel_offset) + chunk_channel; f32 receive_delta = lateral_coord - rx_channel * element_pitch; f32 a_arg = abs(FNumber * receive_delta / axial_coord); @@ -451,7 +451,7 @@ void main() vec3 image_points = vec3(OutputSizeX, OutputSizeY, OutputSizeZ) - 1.0f; vec3 point = vec3(out_voxel) / max(vec3(1.0f), image_points); - vec3 world_point = (voxel_transform * vec4(point, 1)).xyz; + vec3 world_point = (pc.voxel_transform * vec4(point, 1)).xyz; uint32_t out_index = output_index(out_voxel.x, out_voxel.y, out_voxel.z); @@ -481,5 +481,5 @@ void main() IncoherentOutput(HeapBase + IncoherentFrame).x[out_index] += RESULT_INCOHERENT_CAST(sum); #endif - Output(output_frame).x[out_index] += RESULT_COHERENT_CAST(sum); + Output(pc.output_frame).x[out_index] += RESULT_COHERENT_CAST(sum); } diff --git a/shaders/decode.glsl b/shaders/decode.glsl @@ -17,7 +17,7 @@ layout(std430, buffer_reference, buffer_reference_align = 16) readonly buffer F1 OutputDataType sample_rf_data(u32 index) { - OutputDataType result = OutputDataType(RF(rf_buffer).x[index]); + OutputDataType result = OutputDataType(RF(pc.rf_buffer).x[index]); return result; } @@ -30,7 +30,7 @@ void run_decode_large(void) u32 rf_offset = TransmitCount * ChunkChannelCount * gl_WorkGroupID.z + TransmitCount * channel; for (u32 index = gl_LocalInvocationID.x; index < TransmitCount; index += gl_WorkGroupSize.x) - rf[gl_LocalInvocationID.y][index] = RF(rf_buffer).x[rf_offset + index]; + rf[gl_LocalInvocationID.y][index] = RF(pc.rf_buffer).x[rf_offset + index]; barrier(); @@ -59,7 +59,7 @@ void run_decode_large(void) for (uint i = 0; i < ToProcess; i++, out_off += OutputTransmitStride) if (TransmitCount % (gl_WorkGroupSize.x * ToProcess) == 0 || transmit + i < TransmitCount) - Output(output_buffer).x[out_off] = result[i]; + Output(pc.output_buffer).x[out_off] = result[i]; } } @@ -87,7 +87,7 @@ void run_decode_coop(void) for (u32 k = 0; k < TransmitCount; k += CooperativeMatrixK) { u32 rf_tile_row = CooperativeMatrixM * tile_index.y; u32 rf_tile_col = k; - coopMatLoad(rf_matrix, RF(rf_buffer).x, offset + TransmitCount * rf_tile_row + rf_tile_col, + coopMatLoad(rf_matrix, RF(pc.rf_buffer).x, offset + TransmitCount * rf_tile_row + rf_tile_col, TransmitCount, gl_CooperativeMatrixLayoutRowMajor); u32 hadamard_tile_row = k; @@ -101,7 +101,7 @@ void run_decode_coop(void) for (s32 i = 0; i < result.length(); i++) result[i] = result[i] / f32(TransmitCount); - Output out_buffer = Output(output_buffer); + Output out_buffer = Output(pc.output_buffer); coopMatStore(result, out_buffer.x, offset + TransmitCount * result_row + result_col, TransmitCount, gl_CooperativeMatrixLayoutRowMajor); } @@ -116,7 +116,7 @@ void run_decode_small(void) if (time_sample < OutputTransmitStride) { InputDataType rf[TransmitCount]; for (s32 j = 0; j < TransmitCount; j++) - rf[j] = RF(rf_buffer).x[rf_offset + j]; + rf[j] = RF(pc.rf_buffer).x[rf_offset + j]; OutputDataType result[TransmitCount]; for (s32 j = 0; j < TransmitCount; j++) @@ -136,7 +136,7 @@ void run_decode_small(void) uint out_off = OutputChannelStride * channel + OutputSampleStride * time_sample; for (int i = 0; i < TransmitCount; i++, out_off += OutputTransmitStride) - Output(output_buffer).x[out_off] = result[i]; + Output(pc.output_buffer).x[out_off] = result[i]; } } diff --git a/shaders/filter.glsl b/shaders/filter.glsl @@ -86,7 +86,7 @@ void main() in_offset /= 2; // NOTE(rnp): broken out to avoid overflow from the subtraction - u64 input_address = input_buffer + in_offset; + u64 input_address = pc.input_buffer + in_offset; input_address += InputDataKindByteSize * (DecimationRate * gl_WorkGroupID.x * gl_WorkGroupSize.x); input_address -= InputDataKindByteSize * (FilterLength - 1); @@ -117,11 +117,11 @@ void main() if (BatchSampleCount != 0) { // NOTE(rnp): deinterleave - Output(output_buffer).x[out_offset] = OutputDataType(result.x); + Output(pc.output_buffer).x[out_offset] = OutputDataType(result.x); out_offset += BatchSampleCount; - Output(output_buffer).x[out_offset] = OutputDataType(result.y); + Output(pc.output_buffer).x[out_offset] = OutputDataType(result.y); } else { - Output(output_buffer).x[out_offset] = OutputDataType(result); + Output(pc.output_buffer).x[out_offset] = OutputDataType(result); } } } diff --git a/shaders/render_3d.frag.glsl b/shaders/render_3d.frag.glsl @@ -39,33 +39,33 @@ uint32_t texture_dimension(uvec3 points) uint32_t input_index(vec3 uv) { - uv *= vec3(input_size_x - 1, input_size_y - 1, input_size_z - 1); - uint32_t result = input_size_y * input_size_x * uint32_t(uv.z) + - input_size_x * uint32_t(uv.y) + - uint32_t(uv.x); - result = min(result, input_size_z * input_size_y * input_size_x - 1); + uv *= vec3(pc.input_size_x - 1, pc.input_size_y - 1, pc.input_size_z - 1); + u32 result = pc.input_size_y * pc.input_size_x * u32(uv.z) + + pc.input_size_x * u32(uv.y) + + u32(uv.x); + result = min(result, pc.input_size_z * pc.input_size_y * pc.input_size_x - 1); return result; } float sample_value(vec3 p) { float result; - if (input_data != 0) { - uint32_t index = input_index(texture_coordinate); - switch (data_kind) { - case DataKind_Float32:{ result = length(InputFloat(input_data).values[index]); }break; - case DataKind_Float32Complex:{ result = length(InputVec2(input_data).values[index]); }break; + if (pc.input_data != 0) { + u32 index = input_index(texture_coordinate); + switch (pc.data_kind) { + case DataKind_Float32:{ result = length(InputFloat(pc.input_data).values[index]); }break; + case DataKind_Float32Complex:{ result = length(InputVec2(pc.input_data).values[index]); }break; } } - float threshold_val = pow(10.0f, threshold / 20.0f); + float threshold_val = pow(10.0f, pc.threshold / 20.0f); result = clamp(result, 0.0f, threshold_val); result = result / threshold_val; - result = pow(result, gamma); + result = pow(result, pc.gamma); - if (db_cutoff > 0) { + if (pc.db_cutoff > 0) { result = 20 * log(result) / log(10); - result = clamp(result, -db_cutoff, 0) / -db_cutoff; + result = clamp(result, -pc.db_cutoff, 0) / -pc.db_cutoff; result = 1 - result; } @@ -82,7 +82,7 @@ float grad(float x) void main(void) { - uint32_t dimension = texture_dimension(uvec3(input_size_x, input_size_y, input_size_z)); + u32 dimension = texture_dimension(uvec3(pc.input_size_x, pc.input_size_y, pc.input_size_z)); if (dimension == 3) { // TODO(rnp): add slice offset passed in as a uniform @@ -94,6 +94,7 @@ void main(void) vec3 p = 2.0f * texture_coordinate - 1.0f; + const vec4 bb_colour = pc.bounding_box_colour; switch (dimension) { case 1:{ @@ -105,16 +106,16 @@ void main(void) float thickness = 4.f; float alpha = smoothstep((0.5f * thickness + 2.0f) * eps, (0.5f * thickness + 0.0f) * eps, de); - out_colour = vec4(bounding_box_colour.xyz, alpha); + out_colour = vec4(bb_colour.xyz, alpha); }break; case 0: // NOTE(rnp): 0 is a special case for X-Plane Rendering case 2: case 3: { - float t = clamp(sdf_wire_box_outside(p, vec3(1.0f), bounding_box_fraction) / bounding_box_fraction, 0, 1); + f32 t = clamp(sdf_wire_box_outside(p, vec3(1.0f), pc.bounding_box_fraction) / pc.bounding_box_fraction, 0, 1); - out_colour = vec4(t * vec3(data) + (1 - t) * bounding_box_colour.xyz, 1); + out_colour = vec4(t * vec3(data) + (1 - t) * bb_colour.xyz, 1); //if (u_solid_bb) out_colour = u_bb_colour; }break; } diff --git a/shaders/render_3d.vert.glsl b/shaders/render_3d.vert.glsl @@ -7,13 +7,13 @@ layout(std430, buffer_reference, buffer_reference_align = 16) readonly buffer Ve void main() { - vec3 position = Vector4(positions).values[gl_VertexIndex].xyz; - vec3 normal = Vector4(normals).values[gl_VertexIndex].xyz; + vec3 position = Vector4(pc.positions).values[gl_VertexIndex].xyz; + vec3 normal = Vector4(pc.normals).values[gl_VertexIndex].xyz; vec3 texture_coordinate = (2 * position + 1) / 2; f_texture_coordinate = texture_coordinate; f_normal = normal; //f_normal = normalize(mat3(mvp_matrix) * normal); - gl_Position = mvp_matrix * vec4(position, 1); + gl_Position = pc.mvp_matrix * vec4(position, 1); } diff --git a/shaders/reshape.glsl b/shaders/reshape.glsl @@ -16,12 +16,12 @@ void main(void) OutputDataType out_value = OutputDataType(0); #if Interleave - out_value[0] = Input(left_input_buffer).x[input_index]; - out_value[1] = Input(right_input_buffer).x[input_index]; + out_value[0] = Input(pc.left_input_buffer).x[input_index]; + out_value[1] = Input(pc.right_input_buffer).x[input_index]; #else - out_value = Input(left_input_buffer).x[input_index]; + out_value = Input(pc.left_input_buffer).x[input_index]; #endif - Output(output_buffer).x[output_index] = out_value; + Output(pc.output_buffer).x[output_index] = out_value; } }