das.glsl (17627B)
1 /* See LICENSE for license details. */ 2 #if InputDataKind == DataKind_Float32 || InputDataKind == DataKind_Float16 3 #if CoherencyWeighting 4 #define RESULT_TYPE vec2 5 #define RESULT_COHERENT_CAST(a) (a).x 6 #define RESULT_INCOHERENT_CAST(a) (a).y 7 #endif 8 #define SAMPLE_TYPE f32 9 #elif InputDataKind == DataKind_Float32Complex || InputDataKind == DataKind_Float16Complex 10 #if CoherencyWeighting 11 #define RESULT_TYPE vec3 12 #define RESULT_COHERENT_CAST(a) (a).xy 13 #define RESULT_INCOHERENT_CAST(a) (a).z 14 #endif 15 #define SAMPLE_TYPE f32vec2 16 #else 17 #error InputDataKind unsupported for DAS 18 #endif 19 20 #ifndef RESULT_TYPE 21 #define RESULT_TYPE SAMPLE_TYPE 22 #endif 23 24 #ifndef RESULT_COHERENT_CAST 25 #define RESULT_COHERENT_CAST(a) (a) 26 #endif 27 28 #if CoherencyWeighting 29 #define RESULT_STORE(a) RESULT_TYPE(RESULT_COHERENT_CAST(a), length(a)) 30 #else 31 #define RESULT_STORE(a) (a) 32 #endif 33 34 // NOTE(rnp): we don't want das to get recompiled when it isn't actually using the Heap 35 // but we also don't want to check everywhere in here for the existence of the Heap 36 #ifndef HeapBase 37 #define HeapBase u64(0) 38 #endif 39 40 layout(std430, buffer_reference) readonly buffer Input { InputDataType x[]; }; 41 layout(std430, buffer_reference) buffer Output { OutputDataType x[]; }; 42 43 layout(std430, buffer_reference) buffer IncoherentOutput { f32 x[]; }; 44 45 layout(std430, buffer_reference) readonly buffer F16 { f16 x[]; }; 46 layout(std430, buffer_reference) readonly buffer F32 { f32 x[]; }; 47 layout(std430, buffer_reference) readonly buffer S16 { s16 x[]; }; 48 layout(std430, buffer_reference) readonly buffer U8 { u8 x[]; }; 49 layout(std430, buffer_reference) readonly buffer U32V4 { u32vec4 x[]; }; 50 layout(std430, buffer_reference) readonly buffer F32V2 { f32vec2 x[]; }; 51 layout(std430, buffer_reference) readonly buffer F32V4 { f32vec4 x[]; }; 52 layout(std430, buffer_reference) readonly buffer F16V2 { f16vec2 x[]; }; 53 layout(std430, buffer_reference) readonly buffer F16V4 { f16vec4 x[]; }; 54 55 #define RX_ORIENTATION(tx_rx) bitfieldExtract((tx_rx), 0, 4) 56 #define TX_ORIENTATION(tx_rx) bitfieldExtract((tx_rx), 4, 4) 57 58 #define C_SPLINE 0.5 59 60 #if InputDataKind == DataKind_Float32Complex || InputDataKind == DataKind_Float16Complex 61 vec2 rotate_iq(const vec2 iq, const float time) 62 { 63 float arg = radians(360) * DemodulationFrequency * time; 64 mat2 phasor = mat2( cos(arg), sin(arg), 65 -sin(arg), cos(arg)); 66 vec2 result = phasor * iq; 67 return result; 68 } 69 #else 70 #define rotate_iq(a, b) (a) 71 #endif 72 73 // NOTE(rnp): while the input RF buffer is padded such that we could continue reading 74 // DAS is very expensive so we want to avoid any extra work possible. 75 u32 batch_channel_count() 76 { 77 const bool safe = (ReceiveChannelCount % ChunkChannelCount) == 0; 78 const u32 result = safe ? ChunkChannelCount : min(ReceiveChannelCount - channel_offset, ChunkChannelCount); 79 return result; 80 } 81 82 /* NOTE: See: https://cubic.org/docs/hermite.htm */ 83 SAMPLE_TYPE cubic(const u64 rf_pointer, const f32 t) 84 { 85 const mat4 h = mat4( 86 2, -3, 0, 1, 87 -2, 3, 0, 0, 88 1, -2, 1, 0, 89 1, -1, 0, 0 90 ); 91 92 #if InputDataKind == DataKind_Float32 93 f32vec4 samples = F32V4(rf_pointer).x[0]; 94 #elif InputDataKind == DataKind_Float16 95 f16vec4 samples = F16V4(rf_pointer).x[0]; 96 #elif InputDataKind == DataKind_Float16Complex 97 f32vec2 samples[4]; 98 uvec4 load = U32V4(rf_pointer).x[0]; 99 samples[0] = unpackHalf2x16(load[0]); 100 samples[1] = unpackHalf2x16(load[1]); 101 samples[2] = unpackHalf2x16(load[2]); 102 samples[3] = unpackHalf2x16(load[3]); 103 #else 104 f32vec2 samples[4]; 105 vec4 load1 = F32V4(rf_pointer).x[0]; 106 vec4 load2 = F32V4(rf_pointer).x[1]; 107 samples[0] = load1.xy; 108 samples[1] = load1.zw; 109 samples[2] = load2.xy; 110 samples[3] = load2.zw; 111 #endif 112 113 vec4 Sh = vec4(t * t * t, t * t, t, 1) * h; 114 SAMPLE_TYPE P1 = samples[1]; 115 SAMPLE_TYPE P2 = samples[2]; 116 SAMPLE_TYPE T1 = C_SPLINE * (P2 - samples[0]); 117 SAMPLE_TYPE T2 = C_SPLINE * (samples[3] - P1); 118 119 #if InputDataKind == DataKind_Float32 || InputDataKind == DataKind_Float16 120 SAMPLE_TYPE result = dot(Sh, vec4(P1, P2, T1, T2)); 121 #else 122 mat2x4 C = mat2x4(vec4(P1.x, P2.x, T1.x, T2.x), vec4(P1.y, P2.y, T1.y, T2.y)); 123 SAMPLE_TYPE result = Sh * C; 124 #endif 125 return result; 126 } 127 128 SAMPLE_TYPE sample_rf(const u64 rf_pointer, const f32 index) 129 { 130 SAMPLE_TYPE result = SAMPLE_TYPE(0); 131 132 switch (InterpolationMode) { 133 case InterpolationMode_Nearest:{ 134 if (index >= 0.f && index < (f32(SampleCount) - 0.5f)) 135 result = rotate_iq(Input(rf_pointer + InputDataKindByteSize * u32(round(index))).x[0], index / SamplingFrequency); 136 }break; 137 case InterpolationMode_Linear:{ 138 if (index >= 0.f && index < f32(SampleCount - 1)) { 139 #if InputDataKind == DataKind_Float32 140 f32vec2 rf = F32V2(rf_pointer + InputDataKindByteSize * u32(index)).x[0]; 141 #elif InputDataKind == DataKind_Float16 142 f16vec2 rf = F16V2(rf_pointer + InputDataKindByteSize * u32(index)).x[0]; 143 #elif InputDataKind == DataKind_Float16Complex 144 f16vec4 load = F16V4(rf_pointer + InputDataKindByteSize * u32(index)).x[0]; 145 f16vec2 rf[2] = {load.xy, load.zw}; 146 #else 147 f32vec4 load = F32V4(rf_pointer + InputDataKindByteSize * u32(index)).x[0]; 148 f32vec2 rf[2] = {load.xy, load.zw}; 149 #endif 150 151 f32 t = fract(index); 152 result = (1 - t) * rf[0] + t * rf[1]; 153 result = rotate_iq(result, index / SamplingFrequency); 154 } 155 }break; 156 case InterpolationMode_Cubic:{ 157 if (index >= 1.f && index < f32(SampleCount - 2)) 158 result = rotate_iq(cubic(rf_pointer + InputDataKindByteSize * u32(index), fract(index)), index / SamplingFrequency); 159 }break; 160 } 161 return result; 162 } 163 164 float sample_index(const float distance) 165 { 166 float time = distance / SpeedOfSound + TimeOffset; 167 return time * SamplingFrequency; 168 } 169 170 u32 output_index(const u32 x, const u32 y, const u32 z) 171 { 172 u32 result = OutputSizeX * OutputSizeY * z + OutputSizeX * y + x; 173 return result; 174 } 175 176 float apodize(const float arg) 177 { 178 /* IMPORTANT: do not move calculation of arg into this function. It will generate a 179 * conditional move resulting in cos always being evaluated causing a slowdown */ 180 181 /* NOTE: constant F# dynamic receive apodization. This is implemented as: 182 * 183 * / |x_e - x_i|\ 184 * a(x, z) = cos(F# * π * ----------- ) ^ 2 185 * \ |z_e - z_i|/ 186 * 187 * where x,z_e are transducer element positions and x,z_i are image positions. */ 188 float a = cos(radians(180) * arg); 189 return a * a; 190 } 191 192 vec2 rca_plane_projection(const vec3 point, const bool rows) 193 { 194 vec2 result = vec2(point[int(rows)], point[2]); 195 return result; 196 } 197 198 float plane_wave_transmit_distance(const vec3 point, const float transmit_angle, const bool tx_rows) 199 { 200 return dot(rca_plane_projection(point, tx_rows), vec2(sin(transmit_angle), cos(transmit_angle))); 201 } 202 203 float cylindrical_wave_transmit_distance(const vec3 point, const float focal_depth, 204 const float transmit_angle, const bool tx_rows) 205 { 206 vec2 f = focal_depth * vec2(sin(transmit_angle), cos(transmit_angle)); 207 return distance(rca_plane_projection(point, tx_rows), f); 208 } 209 210 u8 tx_rx_orientation_for_acquisition(const s32 acquisition) 211 { 212 u8 result = u8(TransmitReceiveOrientation); 213 if (!SingleOrientation) result = U8(HeapBase + TransmitReceiveOrientations).x[acquisition]; 214 return result; 215 } 216 217 f32vec2 focal_vector_for_acquisition(const s32 acquisition) 218 { 219 f32vec2 result = SingleFocus ? f32vec2(TransmitAngle, FocusDepth) : F32V2(HeapBase + FocalVectors).x[acquisition]; 220 return result; 221 } 222 223 f32 rca_transmit_distance(const vec3 world_point, const vec2 focal_vector, const u8 transmit_receive_orientation) 224 { 225 float result = 0; 226 if (TX_ORIENTATION(transmit_receive_orientation) != RCAOrientation_None) { 227 bool tx_rows = TX_ORIENTATION(transmit_receive_orientation) == RCAOrientation_Rows; 228 float transmit_angle = radians(focal_vector.x); 229 float focal_depth = focal_vector.y; 230 231 if (isinf(focal_depth)) { 232 result = plane_wave_transmit_distance(world_point, transmit_angle, tx_rows); 233 } else { 234 result = cylindrical_wave_transmit_distance(world_point, focal_depth, transmit_angle, tx_rows); 235 } 236 } 237 return result; 238 } 239 240 RESULT_TYPE RCA(const vec3 world_point) 241 { 242 RESULT_TYPE result = RESULT_TYPE(0); 243 for (s32 acquisition = 0; acquisition < s32(AcquisitionCount); acquisition++) { 244 const u8 tx_rx_orientation = tx_rx_orientation_for_acquisition(acquisition); 245 const bool rx_rows = RX_ORIENTATION(tx_rx_orientation) == RCAOrientation_Rows; 246 const vec2 focal_vector = focal_vector_for_acquisition(acquisition); 247 vec2 xdc_world_point = rca_plane_projection((xdc_transform * vec4(world_point, 1)).xyz, rx_rows); 248 f32 transmit_index = sample_index(rca_transmit_distance(world_point, focal_vector, tx_rx_orientation)); 249 250 u64 rf_pointer = rf_data + InputDataKindByteSize * acquisition * SampleCount; 251 rf_pointer -= InputDataKindByteSize * u32(InterpolationMode == InterpolationMode_Cubic); 252 253 for (f32 chunk_channel = 0.f; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { 254 f32 rx_channel = f32(channel_offset) + chunk_channel; 255 vec3 rx_center = vec3(rx_channel * xdc_element_pitch, 0); 256 vec2 receive_vector = xdc_world_point - rca_plane_projection(rx_center, rx_rows); 257 f32 a_arg = abs(FNumber * receive_vector.x / abs(xdc_world_point.y)); 258 259 if (a_arg < 0.5f) { 260 f32 index = transmit_index + length(receive_vector) * SamplingFrequency / SpeedOfSound; 261 SAMPLE_TYPE value = apodize(a_arg) * sample_rf(rf_pointer, index); 262 result += RESULT_STORE(value); 263 } 264 rf_pointer += InputDataKindByteSize * SampleCount * AcquisitionCount; 265 } 266 } 267 return result; 268 } 269 270 RESULT_TYPE HERCULES(const vec3 world_point) 271 { 272 const u8 tx_rx_orientation = tx_rx_orientation_for_acquisition(0); 273 const bool rx_cols = RX_ORIENTATION(tx_rx_orientation) == RCAOrientation_Columns; 274 const vec2 focal_vector = focal_vector_for_acquisition(0); 275 const vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; 276 277 const f32 transmit_index = sample_index(rca_transmit_distance(world_point, focal_vector, tx_rx_orientation)); 278 const f32 z_delta_squared = xdc_world_point.z * xdc_world_point.z; 279 const f32 f_number_over_z = abs(FNumber / xdc_world_point.z); 280 const f32 apodization_test = 0.25f / (f_number_over_z * f_number_over_z); 281 282 const f32 rx_world_point = xdc_world_point[s32(!rx_cols)]; 283 const f32 tx_world_point = xdc_world_point[s32(rx_cols)]; 284 const f32 rx_pitch = xdc_element_pitch[s32(!rx_cols)]; 285 const f32 tx_pitch = xdc_element_pitch[s32(rx_cols)]; 286 287 RESULT_TYPE result = RESULT_TYPE(0); 288 for (f32 chunk_channel = 0.f; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { 289 f32 rx_channel = f32(channel_offset) + chunk_channel; 290 291 f32 element_receive_delta_squared = rx_world_point - rx_channel * rx_pitch; 292 element_receive_delta_squared *= element_receive_delta_squared; 293 294 u64 rf_pointer = rf_data + InputDataKindByteSize * (u32(chunk_channel) * SampleCount * AcquisitionCount + u32(Sparse) * SampleCount); 295 rf_pointer -= InputDataKindByteSize * u32(InterpolationMode == InterpolationMode_Cubic); 296 297 for (f32 transmit = f32(Sparse); transmit < f32(AcquisitionCount); transmit += 1.f) { 298 f32 tx_channel = Sparse ? f32(S16(HeapBase + SparseElements).x[s32(transmit) - s32(Sparse)]) : transmit; 299 300 f32 element_transmit_delta_squared = tx_world_point - tx_channel * tx_pitch; 301 element_transmit_delta_squared *= element_transmit_delta_squared; 302 303 f32 element_delta_squared = element_transmit_delta_squared + element_receive_delta_squared; 304 if (element_delta_squared < apodization_test) { 305 /* NOTE: tribal knowledge */ 306 float apodization = transmit == 0 ? inversesqrt(float(AcquisitionCount)) : 1.0f; 307 apodization *= apodize(f_number_over_z * sqrt(element_delta_squared)); 308 309 float index = transmit_index + sqrt(z_delta_squared + element_delta_squared) * SamplingFrequency / SpeedOfSound; 310 SAMPLE_TYPE value = apodization * sample_rf(rf_pointer, index); 311 result += RESULT_STORE(value); 312 } 313 314 rf_pointer += InputDataKindByteSize * SampleCount; 315 } 316 } 317 return result; 318 } 319 320 RESULT_TYPE FORCES(const vec3 world_point) 321 { 322 RESULT_TYPE result = RESULT_TYPE(0); 323 324 const vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; 325 326 // TODO(rnp): the sign of the origin offset might be flipped 327 f32 origin_offset = FocusDepth * tan(radians(TransmitAngle)); 328 f32 transmit_y_delta = world_point.y + origin_offset; 329 f32 z_delta_squared = xdc_world_point.z * xdc_world_point.z; 330 f32 transmit_yz_squared = transmit_y_delta * transmit_y_delta + z_delta_squared; 331 332 for (f32 chunk_channel = 0; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { 333 f32 rx_channel = f32(channel_offset) + chunk_channel; 334 f32 receive_x_delta = xdc_world_point.x - rx_channel * xdc_element_pitch.x; 335 f32 a_arg = abs(FNumber * receive_x_delta / xdc_world_point.z); 336 337 if (a_arg < 0.5f) { 338 u64 rf_pointer = rf_data + InputDataKindByteSize * (u32(chunk_channel) * SampleCount * AcquisitionCount + u32(Sparse) * SampleCount); 339 rf_pointer -= InputDataKindByteSize * u32(InterpolationMode == InterpolationMode_Cubic); 340 341 f32 receive_index = sample_index(sqrt(receive_x_delta * receive_x_delta + z_delta_squared)); 342 f32 apodization = apodize(a_arg); 343 for (f32 transmit = f32(Sparse); transmit < f32(AcquisitionCount); transmit += 1.f) { 344 f32 tx_channel = Sparse ? f32(S16(HeapBase + SparseElements).x[s32(transmit) - s32(Sparse)]) : transmit; 345 f32 transmit_x_delta = xdc_world_point.x - xdc_element_pitch.x * tx_channel; 346 f32 transmit_index = sqrt(transmit_yz_squared + transmit_x_delta * transmit_x_delta) * SamplingFrequency / SpeedOfSound; 347 348 SAMPLE_TYPE value = apodization * sample_rf(rf_pointer, receive_index + transmit_index); 349 result += RESULT_STORE(value); 350 rf_pointer += InputDataKindByteSize * SampleCount; 351 } 352 } 353 } 354 return result; 355 } 356 357 RESULT_TYPE READI_FORCES(const vec3 world_point) 358 { 359 RESULT_TYPE result = RESULT_TYPE(0); 360 361 const vec3 xdc_world_point = (xdc_transform * vec4(world_point, 1)).xyz; 362 363 // TODO(rnp): the sign of the origin offset might be flipped 364 f32 origin_offset = FocusDepth * tan(radians(TransmitAngle)); 365 f32 transmit_y_delta = world_point.y + origin_offset; 366 f32 z_delta_squared = xdc_world_point.z * xdc_world_point.z; 367 f32 transmit_yz_squared = transmit_y_delta * transmit_y_delta + z_delta_squared; 368 369 // NOTE(tkh): The row we use matches the acquisition group, the column is the element group we are beamforming. 370 s32 hadamard_offset = s32(readi_group) * s32(ReadiGroupCount); 371 372 for (f32 chunk_channel = 0; chunk_channel < f32(batch_channel_count()); chunk_channel += 1.f) { 373 f32 rx_channel = f32(channel_offset) + chunk_channel; 374 f32 receive_x_delta = xdc_world_point.x - rx_channel * xdc_element_pitch.x; 375 f32 a_arg = abs(FNumber * receive_x_delta / xdc_world_point.z); 376 377 if (a_arg < 0.5f) { 378 u64 channel_rf_pointer = rf_data + InputDataKindByteSize * u32(chunk_channel) * SampleCount * AcquisitionCount; 379 channel_rf_pointer -= InputDataKindByteSize * u32(InterpolationMode == InterpolationMode_Cubic); 380 381 f32 receive_index = sample_index(sqrt(receive_x_delta * receive_x_delta + z_delta_squared)); 382 f32 apodization = apodize(a_arg); 383 384 // NOTE(tkh): Iterating over groups of tx elements, each group is AcquisitionCount 385 // sequential elements. The first element in each group is beamformed using the first 386 // acquisition, the second element in each group is beamformed using the second acquisition, etc. 387 for (s32 tx_group = 0; tx_group < s32(ReadiGroupCount); tx_group++) { 388 f32 group_apodization = apodization * F16(HeapBase + Hadamard).x[hadamard_offset + tx_group]; 389 u64 rf_pointer = channel_rf_pointer; 390 391 for (f32 tx_event = 0; tx_event < f32(AcquisitionCount); tx_event += 1.f) { 392 f32 tx_element = f32(tx_group) * f32(AcquisitionCount) + tx_event; 393 f32 transmit_x_delta = xdc_world_point.x - xdc_element_pitch.x * tx_element; 394 f32 transmit_index = sqrt(transmit_yz_squared + transmit_x_delta * transmit_x_delta) * SamplingFrequency / SpeedOfSound; 395 396 SAMPLE_TYPE value = group_apodization * sample_rf(rf_pointer, receive_index + transmit_index); 397 result += RESULT_STORE(value); 398 rf_pointer += InputDataKindByteSize * SampleCount; 399 } 400 } 401 } 402 } 403 return result; 404 } 405 406 void main() 407 { 408 uvec3 out_voxel = gl_GlobalInvocationID; 409 if (!all(lessThan(out_voxel, uvec3(OutputSizeX, OutputSizeY, OutputSizeZ)))) 410 return; 411 412 vec3 image_points = vec3(OutputSizeX, OutputSizeY, OutputSizeZ) - 1.0f; 413 vec3 point = vec3(out_voxel) / max(vec3(1.0f), image_points); 414 vec3 world_point = (voxel_transform * vec4(point, 1)).xyz; 415 416 uint32_t out_index = output_index(out_voxel.x, out_voxel.y, out_voxel.z); 417 418 RESULT_TYPE sum = RESULT_TYPE(0); 419 switch (AcquisitionKind) { 420 case AcquisitionKind_FORCES: 421 case AcquisitionKind_UFORCES: 422 { 423 sum = ReadiGroupCount > 1 ? READI_FORCES(world_point) 424 : FORCES(world_point); 425 }break; 426 case AcquisitionKind_HERCULES: 427 case AcquisitionKind_UHERCULES: 428 case AcquisitionKind_HERO_PA: 429 { 430 sum = HERCULES(world_point); 431 }break; 432 case AcquisitionKind_Flash: 433 case AcquisitionKind_RCA_TPW: 434 case AcquisitionKind_RCA_VLS: 435 { 436 sum = RCA(world_point); 437 }break; 438 } 439 440 #if CoherencyWeighting 441 IncoherentOutput(HeapBase + IncoherentFrame).x[out_index] += RESULT_INCOHERENT_CAST(sum); 442 #endif 443 444 Output(output_frame).x[out_index] += RESULT_COHERENT_CAST(sum); 445 }