ogl_beamforming

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

filter.glsl (4230B)


      1 /* See LICENSE for license details. */
      2 #if  (InputDataKind == DataKind_Int16Complex         || \
      3      (InputDataKind == DataKind_Int16 && Demodulate) || \
      4      (InputDataKind == DataKind_Float16 && Demodulate))
      5   #define SAMPLE_TYPE f16vec2
      6 #elif InputDataKind == DataKind_Int16
      7   #define SAMPLE_TYPE f16
      8 #elif InputDataKind == DataKind_Float32 && Demodulate
      9   #define SAMPLE_TYPE f32vec2
     10 #endif
     11 
     12 #ifndef SAMPLE_TYPE
     13   #define SAMPLE_TYPE InputDataType
     14 #endif
     15 
     16 #define ComplexSampleType (InputDataKind == DataKind_Float32Complex || \
     17                            InputDataKind == DataKind_Float16Complex || \
     18                            InputDataKind == DataKind_Int16Complex   || \
     19                            Demodulate)
     20 #if ComplexSampleType
     21   #define RESULT_TYPE f32vec2
     22 #else
     23   #define RESULT_TYPE f32
     24 #endif
     25 
     26 #if ComplexFilter
     27   #define FILTER_TYPE f32vec2
     28 #else
     29   #define FILTER_TYPE f32
     30 #endif
     31 
     32 #if ComplexFilter && ComplexSampleType
     33   #define apply_filter(iq, h) complex_mul(f32vec2(iq), f32vec2(h))
     34 #else
     35   #define apply_filter(iq, h) ((iq) * (h))
     36 #endif
     37 
     38 layout(std430, buffer_reference) restrict readonly  buffer Input  { InputDataType x[];  };
     39 layout(std430, buffer_reference) restrict writeonly buffer Output { OutputDataType x[]; };
     40 
     41 layout(std430, buffer_reference, buffer_reference_align = 64) restrict readonly buffer Filter {
     42 	FILTER_TYPE values[FilterLength];
     43 };
     44 
     45 f32vec2 complex_mul(f32vec2 a, f32vec2 b)
     46 {
     47 	mat2 m = mat2(b.x, b.y, -b.y, b.x);
     48 	f32vec2 result = m * a;
     49 	return result;
     50 }
     51 
     52 #if Demodulate
     53 SAMPLE_TYPE rotate_iq(SAMPLE_TYPE iq, u32 index)
     54 {
     55 	f32  arg = radians(360) * index / SamplesPerWave;
     56 	vec2 cs  = vec2(cos(arg), -sin(arg));
     57 	if (SamplesPerWave == 2.f) {
     58 		cs = f32vec2(((index & 1u) == 0) ? 1.f : -1.f, 0.f);
     59 	} else if (SamplesPerWave == 1.f) {
     60 		cs = f32vec2(1.f, 0.f);
     61 	}
     62 	SAMPLE_TYPE result = SAMPLE_TYPE(complex_mul(iq, cs));
     63 	return result;
     64 }
     65 #endif
     66 
     67 shared SAMPLE_TYPE rf[DecimationRate * gl_WorkGroupSize.x + FilterLength - 1];
     68 
     69 void main()
     70 {
     71 	uint out_sample = gl_GlobalInvocationID.x;
     72 	uint channel    = gl_GlobalInvocationID.y;
     73 	uint transmit   = gl_GlobalInvocationID.z;
     74 
     75 	/////////////////////////
     76 	// NOTE: sample caching
     77 	{
     78 		bool offset_wraps = (DecimationRate * gl_WorkGroupID.x * gl_WorkGroupSize.x) < (FilterLength - 1);
     79 
     80 		u32 in_offset = InputDataKindByteSize * (InputChannelStride * channel + InputTransmitStride * transmit);
     81 		// NOTE(rnp): when demodulating we want to load 2 elements at a time but the
     82 		// input strides were specified in terms of a single element. therefore we
     83 		// must divide this by two. by doing this here we can gracefully handle
     84 		// the case where there are an odd number of samples (this drops the last one).
     85 		if (Demodulate)
     86 			in_offset /= 2;
     87 
     88 		// NOTE(rnp): broken out to avoid overflow from the subtraction
     89 		u64 input_address = input_buffer + in_offset;
     90 		input_address += InputDataKindByteSize * (DecimationRate * gl_WorkGroupID.x * gl_WorkGroupSize.x);
     91 		input_address -= InputDataKindByteSize * (FilterLength - 1);
     92 
     93 		const SAMPLE_TYPE scale = SAMPLE_TYPE(bool(ComplexFilter) ? 1 : sqrt(2.0f));
     94 		for (u32 index = gl_LocalInvocationIndex; index < rf.length(); index += gl_WorkGroupSize.x) {
     95 			SAMPLE_TYPE s = SAMPLE_TYPE(0);
     96 			if (!offset_wraps || index >= FilterLength - 1) {
     97 				s = SAMPLE_TYPE(Input(input_address).x[index]);
     98 				#if Demodulate
     99 				s = scale * rotate_iq(s * SAMPLE_TYPE(1, -1), index);
    100 				#endif
    101 			}
    102 			rf[index] = s;
    103 		}
    104 	}
    105 	barrier();
    106 
    107 	Filter f = Filter(HeapBase + FilterCoefficients);
    108 	if (out_sample < SampleCount / DecimationRate) {
    109 		RESULT_TYPE result = RESULT_TYPE(0);
    110 		u32 offset = DecimationRate * gl_LocalInvocationIndex;
    111 		for (u32 j = 0; j < FilterLength; j++)
    112 			result += apply_filter(rf[offset + j], f.values[j]);
    113 
    114 		u32 out_offset = OutputChannelStride  * channel +
    115 		                 OutputTransmitStride * transmit +
    116 		                 OutputSampleStride   * out_sample;
    117 
    118 		if (BatchSampleCount != 0) {
    119 			// NOTE(rnp): deinterleave
    120 			Output(output_buffer).x[out_offset] = OutputDataType(result.x);
    121 			out_offset += BatchSampleCount;
    122 			Output(output_buffer).x[out_offset] = OutputDataType(result.y);
    123 		} else {
    124 			Output(output_buffer).x[out_offset] = OutputDataType(result);
    125 		}
    126 	}
    127 }