13 #if defined(PHOTOACOUSTICS_USE_GPU) || DOXYGEN 19 m_PixelCalculation(NULL),
23 m_ApodizationBuffer(nullptr),
24 m_DelaysBuffer(nullptr),
25 m_UsedLinesBuffer(nullptr),
26 m_ElementHeightsBuffer(nullptr),
27 m_ElementPositionsBuffer(nullptr)
29 MITK_INFO <<
"Instantiating OCL beamforming Filter...";
30 this->AddSourceFile(
"DAS.cl");
31 this->AddSourceFile(
"DMAS.cl");
32 this->AddSourceFile(
"sDMAS.cl");
33 this->m_FilterID =
"OpenCLBeamformingFilter";
37 unsigned int dim[] = { 128, 2048, 2 };
39 m_InputImage->Initialize(mitk::MakeScalarPixelType<float>(), 3, dim);
45 m_UsedLinesCalculation = mitk::OCLUsedLinesCalculation::New(m_Conf);
46 m_DelayCalculation = mitk::OCLDelayCalculation::New(m_Conf);
47 MITK_INFO <<
"Instantiating OCL beamforming Filter...[Done]";
52 if (this->m_PixelCalculation)
54 clReleaseKernel(m_PixelCalculation);
57 if (m_ApodizationBuffer) clReleaseMemObject(m_ApodizationBuffer);
58 if (m_ElementHeightsBuffer) clReleaseMemObject(m_ElementHeightsBuffer);
59 if (m_ElementPositionsBuffer) clReleaseMemObject(m_ElementPositionsBuffer);
65 if (!this->Initialize())
72 mitkThrow() <<
"Filter is not initialized. Cannot update.";
80 void mitk::PhotoacousticOCLBeamformingFilter::UpdateDataBuffers()
84 cl_context gpuContext = resources->
GetContext();
89 size_t outputSize = (size_t)m_Conf->GetReconstructionLines() * (size_t)m_Conf->GetSamplesPerLine() *
90 (size_t)m_inputSlices;
91 m_OutputDim[0] = m_Conf->GetReconstructionLines();
92 m_OutputDim[1] = m_Conf->GetSamplesPerLine();
93 m_OutputDim[2] = m_inputSlices;
94 this->InitExec(this->m_PixelCalculation, m_OutputDim, outputSize,
sizeof(
float));
98 MITK_ERROR <<
"Caught exception while initializing filter: " << e.what();
103 MITK_DEBUG <<
"Updating GPU Buffers for new configuration";
106 if (m_Apodisation ==
nullptr)
108 MITK_INFO <<
"No apodisation function set; Beamforming will be done without any apodisation.";
109 m_Apodisation =
new float[1]{ 1 };
113 if (m_ApodizationBuffer) clReleaseMemObject(m_ApodizationBuffer);
115 this->m_ApodizationBuffer = clCreateBuffer(gpuContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(
float) * m_ApodArraySize, const_cast<float*>(m_Apodisation), &clErr);
118 if (m_ElementHeightsBuffer) clReleaseMemObject(m_ElementHeightsBuffer);
119 this->m_ElementHeightsBuffer = clCreateBuffer(gpuContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(
float) * m_Conf->GetTransducerElements(),
const_cast<float*
>(m_Conf->GetElementHeights()), &clErr);
122 if (m_ElementPositionsBuffer) clReleaseMemObject(m_ElementPositionsBuffer);
123 this->m_ElementPositionsBuffer = clCreateBuffer(gpuContext, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(
float) * m_Conf->GetTransducerElements(),
const_cast<float*
>(m_Conf->GetElementPositions()), &clErr);
127 m_UsedLinesCalculation->SetElementPositionsBuffer(m_ElementPositionsBuffer);
128 m_UsedLinesCalculation->SetElementHeightsBuffer(m_ElementHeightsBuffer);
129 m_UsedLinesCalculation->Update();
130 m_UsedLinesBuffer = m_UsedLinesCalculation->GetGPUOutput()->GetGPUBuffer();
133 m_DelayCalculation->SetInputs(m_UsedLinesBuffer);
134 m_DelayCalculation->Update();
136 m_DelaysBuffer = m_DelayCalculation->GetGPUOutput()->GetGPUBuffer();
139 void mitk::PhotoacousticOCLBeamformingFilter::Execute()
145 unsigned int reconstructionLines = this->m_Conf->GetReconstructionLines();
146 unsigned int samplesPerLine = this->m_Conf->GetSamplesPerLine();
148 clErr = clSetKernelArg(this->m_PixelCalculation, 2,
sizeof(cl_mem), &(this->m_UsedLinesBuffer));
149 clErr |= clSetKernelArg(this->m_PixelCalculation, 3,
sizeof(cl_mem), &(this->m_DelaysBuffer));
150 clErr |= clSetKernelArg(this->m_PixelCalculation, 4,
sizeof(cl_mem), &(this->m_ApodizationBuffer));
151 clErr |= clSetKernelArg(this->m_PixelCalculation, 5,
sizeof(cl_ushort), &(this->m_ApodArraySize));
152 clErr |= clSetKernelArg(this->m_PixelCalculation, 6,
sizeof(cl_uint), &(this->m_Conf->GetInputDim()[0]));
153 clErr |= clSetKernelArg(this->m_PixelCalculation, 7,
sizeof(cl_uint), &(this->m_Conf->GetInputDim()[1]));
154 clErr |= clSetKernelArg(this->m_PixelCalculation, 8,
sizeof(cl_uint), &(m_inputSlices));
155 clErr |= clSetKernelArg(this->m_PixelCalculation, 9,
sizeof(cl_uint), &(reconstructionLines));
156 clErr |= clSetKernelArg(this->m_PixelCalculation, 10,
sizeof(cl_uint), &(samplesPerLine));
160 int reconstructionLines = this->m_Conf->GetReconstructionLines();
161 int samplesPerLine = this->m_Conf->GetSamplesPerLine();
162 float totalSamples_i = (float)(m_Conf->GetReconstructionDepth()) / (
float)(m_Conf->GetSpeedOfSound() * m_Conf->GetTimeSpacing());
163 totalSamples_i = totalSamples_i <= m_Conf->GetInputDim()[1] ? totalSamples_i : m_Conf->GetInputDim()[1];
164 float horizontalExtent = m_Conf->GetHorizontalExtent();
165 float mult = 1 / (this->m_Conf->GetTimeSpacing() * this->m_Conf->GetSpeedOfSound());
166 char isPAImage = (char)m_Conf->GetIsPhotoacousticImage();
168 clErr = clSetKernelArg(this->m_PixelCalculation, 2,
sizeof(cl_mem), &(this->m_ElementHeightsBuffer));
169 clErr |= clSetKernelArg(this->m_PixelCalculation, 3,
sizeof(cl_mem), &(this->m_ElementPositionsBuffer));
170 clErr |= clSetKernelArg(this->m_PixelCalculation, 4,
sizeof(cl_mem), &(this->m_ApodizationBuffer));
171 clErr |= clSetKernelArg(this->m_PixelCalculation, 5,
sizeof(cl_ushort), &(this->m_ApodArraySize));
172 clErr |= clSetKernelArg(this->m_PixelCalculation, 6,
sizeof(cl_uint), &(this->m_Conf->GetInputDim()[0]));
173 clErr |= clSetKernelArg(this->m_PixelCalculation, 7,
sizeof(cl_uint), &(this->m_Conf->GetInputDim()[1]));
174 clErr |= clSetKernelArg(this->m_PixelCalculation, 8,
sizeof(cl_int), &(m_inputSlices));
175 clErr |= clSetKernelArg(this->m_PixelCalculation, 9,
sizeof(cl_int), &(reconstructionLines));
176 clErr |= clSetKernelArg(this->m_PixelCalculation, 10,
sizeof(cl_int), &(samplesPerLine));
177 clErr |= clSetKernelArg(this->m_PixelCalculation, 11,
sizeof(cl_float), &(totalSamples_i));
178 clErr |= clSetKernelArg(this->m_PixelCalculation, 12,
sizeof(cl_float), &(horizontalExtent));
179 clErr |= clSetKernelArg(this->m_PixelCalculation, 13,
sizeof(cl_float), &(mult));
180 clErr |= clSetKernelArg(this->m_PixelCalculation, 14,
sizeof(cl_char), &(isPAImage));
181 clErr |= clSetKernelArg(this->m_PixelCalculation, 15,
sizeof(cl_mem), &(this->m_UsedLinesBuffer));
184 if (m_OutputDim[2] == 1 || m_ChunkSize[2] == 1)
186 if (!this->ExecuteKernelChunksInBatches(m_PixelCalculation, 2, m_ChunkSize, m_inputSlices, 50))
187 mitkThrow() <<
"openCL Error when executing Kernel";
191 if (!this->ExecuteKernelChunksInBatches(m_PixelCalculation, 3, m_ChunkSize, m_inputSlices, 50))
192 mitkThrow() <<
"openCL Error when executing Kernel";
199 us::Module *mitk::PhotoacousticOCLBeamformingFilter::GetModule()
204 bool mitk::PhotoacousticOCLBeamformingFilter::Initialize()
206 bool buildErr =
true;
213 switch (m_Conf->GetAlgorithm())
215 case BeamformingSettings::BeamformingAlgorithm::DAS:
218 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDAS", &clErr);
221 case BeamformingSettings::BeamformingAlgorithm::DMAS:
224 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDMAS", &clErr);
227 case BeamformingSettings::BeamformingAlgorithm::sDMAS:
230 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"cksDMAS", &clErr);
235 MITK_INFO <<
"No beamforming algorithm specified, setting to DAS";
236 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDAS", &clErr);
243 switch (m_Conf->GetAlgorithm())
245 case BeamformingSettings::BeamformingAlgorithm::DAS:
248 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDAS_g", &clErr);
251 case BeamformingSettings::BeamformingAlgorithm::DMAS:
254 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDMAS_g", &clErr);
257 case BeamformingSettings::BeamformingAlgorithm::sDMAS:
260 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"cksDMAS_g", &clErr);
265 MITK_INFO <<
"No beamforming algorithm specified, setting to DAS";
266 this->m_PixelCalculation = clCreateKernel(this->m_ClProgram,
"ckDAS_g", &clErr);
282 m_InputImage =
image;
283 m_inputSlices = image->GetDimension(2);
286 void mitk::PhotoacousticOCLBeamformingFilter::SetInput(
void* data,
unsigned int* dimensions,
unsigned int BpE)
297 void* pData = m_Output->TransferDataToCPU(m_CommandQue);
299 const unsigned int dimension = 3;
300 unsigned int dimensions[3] = { (
unsigned int)m_OutputDim[0], (
unsigned int)m_OutputDim[1], (
unsigned int)m_OutputDim[2] };
306 outputImage->Initialize(this->GetOutputType(), dimension, dimensions);
307 outputImage->SetSpacing(p_slg->GetSpacing());
308 outputImage->SetImportVolume(pData, 0, 0, mitk::Image::ImportMemoryManagementType::CopyMemory);
317 void* mitk::PhotoacousticOCLBeamformingFilter::GetOutput()
#define CHECK_OCL_ERR(_er)
virtual void InvalidateStorage()=0
Remove all invalid (=do not compile) programs from the internal storage.
ServiceReferenceU GetServiceReference(const std::string &clazz)
static void Update(vtkPolyData *)
bool Initialize()
Initialize all necessary parts of the filter.
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool IsInitialized()
Returns true if the initialization was successfull.
void * GetService(const ServiceReferenceBase &reference)
void SetInput(mitk::OclDataSet::Pointer DataSet)
SetInput SetInput Set the input DataSet (as mitk::OclDataSet).
void * GetOutput()
Returns an pointer to the filtered data.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Module * GetModule() const
mitk::Image::Pointer image
virtual cl_context GetContext() const =0
Returns a valid OpenCL Context (if applicable) or nullptr if none present.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
Declaration of the OpenCL Resources micro-service.