36 : m_ClCompilerFlags(
""),
39 m_FilterID(
"mitkOclFilter"),
46 : m_ClCompilerFlags(
""),
75 clErr = clEnqueueNDRangeKernel( this->m_CommandQue, kernel, workSizeDim,
76 NULL, this->m_GlobalWorkSize, m_LocalWorkSize, 0, NULL, NULL);
80 return ( clErr == CL_SUCCESS );
94 if ( m_ClFiles.empty())
100 if (m_ClProgram == NULL)
104 this->m_ClProgram = resources->GetProgram( this->m_FilterID );
108 MITK_INFO <<
"Program not stored in resource manager, compiling.";
109 this->CompileSource();
113 return m_Initialized;
118 for( CStringList::iterator it = m_ClFiles.begin(); it != m_ClFiles.end(); ++it )
124 MITK_WARN <<
"Could not load resource: " << mdr.
GetName() <<
" is invalid!";
126 us:ModuleResourceStream rss(mdr);
129 std::istreambuf_iterator<char> eos;
130 std::string source(std::istreambuf_iterator<char>(rss), eos);
133 std::string src(m_Preambel);
138 char* tmp =
new char[src.size() + 1];
139 strcpy(tmp,src.c_str());
142 sourceCode.push_back((
const char*)tmp);
143 sourceCodeSize.push_back(src.size());
154 if (m_ClFiles.empty())
156 MITK_ERROR(
"ocl.filter") <<
"No shader source file was set";
164 cl_context gpuContext = resources->
GetContext();
166 LoadSourceFiles(sourceCode, sourceCodeSize);
168 if ( !sourceCode.empty() )
171 m_ClProgram = clCreateProgramWithSource(gpuContext, sourceCode.size(), &sourceCode[0], &sourceCodeSize[0], &clErr);
176 std::string compilerOptions =
"";
177 compilerOptions.append(m_ClCompilerFlags);
179 MITK_DEBUG(
"ocl.filter") <<
"cl compiler flags: " << compilerOptions.c_str();
181 clErr = clBuildProgram(m_ClProgram, 0, NULL, compilerOptions.c_str(), NULL, NULL);
185 if (clErr != CL_SUCCESS)
187 MITK_ERROR(
"ocl.filter") <<
"Failed to build source";
189 oclLogBinary(m_ClProgram, resources->GetCurrentDevice() );
190 m_Initialized =
false;
194 resources->InsertProgram(m_ClProgram, m_FilterID,
true);
197 for( CStringList::iterator it = sourceCode.begin(); it != sourceCode.end(); ++it )
204 MITK_ERROR(
"ocl.filter") <<
"Could not load from source";
205 m_Initialized =
false;
212 this->m_LocalWorkSize[0] = locx;
213 this->m_LocalWorkSize[1] = locy;
214 this->m_LocalWorkSize[2] = locz;
216 this->m_GlobalWorkSize[0] = dimx;
217 this->m_GlobalWorkSize[1] = dimy;
218 this->m_GlobalWorkSize[2] = dimz;
221 this->m_GlobalWorkSize[0] =
iDivUp( dimx, this->m_LocalWorkSize[0]) * this->m_LocalWorkSize[0];
224 this->m_GlobalWorkSize[1] =
iDivUp( dimy, this->m_LocalWorkSize[1]) * this->m_LocalWorkSize[1];
226 this->m_GlobalWorkSize[2] =
iDivUp( dimz, this->m_LocalWorkSize[2]) * this->m_LocalWorkSize[2];
231 this->m_Preambel = preambel;
236 m_ClFiles.push_back(filename);
241 m_ClCompilerFlags = flags;
247 return m_Initialized;
#define CHECK_OCL_ERR(_er)
unsigned int iDivUp(unsigned int dividend, unsigned int divisor)
Method to estimate an integer quotient C from given dividend and divisor higher or equal to the corre...
ServiceReferenceU GetServiceReference(const std::string &clazz)
virtual cl_command_queue GetCommandQueue() const =0
Returns a valid cl_command_queue related to the (one) OpenCL context.
void SetSourcePreambel(const char *preambel)
Add some source code on the beginning of the loaded source.
std::string GetName() const
bool Initialize()
Initialize all necessary parts of the filter.
void oclLogBinary(cl_program clProg, cl_device_id clDev)
Logs the GPU Program binary code.
void CompileSource()
Compile the program source.
virtual bool IsInitialized()
Returns true if the initialization was successfull.
std::vector< const char * > CStringList
void * GetService(const ServiceReferenceBase &reference)
virtual ~OclFilter()
Destructor.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
static const std::string filename
std::vector< vcl_size_t > ClSizeList
CStringList m_ClFiles
List of sourcefiles that will be compiled for this filter.
void oclLogBuildInfo(cl_program clProg, cl_device_id clDev)
Shows the OpenCL-Program build info, called if clBuildProgram != CL_SUCCES.
virtual void RemoveProgram(const std::string &name)=0
Remove given program from storage.
void SetCompilerFlags(const char *flags)
Set specific compilerflags to compile the CL source. Default is set to NULL; example: "-cl-fast-relax...
virtual cl_context GetContext() const =0
Returns a valid OpenCL Context (if applicable) or NULL if none present.
void SetWorkingSize(unsigned int locx, unsigned int dimx, unsigned int locy=1, unsigned int dimy=1, unsigned int locz=1, unsigned int dimz=1)
Set the working size for the following OpenCL kernel call.
bool ExecuteKernel(cl_kernel kernel, unsigned int workSizeDim)
Execute the given kernel on the OpenCL Index-Space defined by the local and global work sizes...
void LoadSourceFiles(CStringList &SourceCodeList, ClSizeList &SourceCodeSizeList)
Helper functions that load sourcefiles from the module context in the Initialize function.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void AddSourceFile(const char *filename)
Add a source file from the resource files to the OpenCL shader file list. Multiple files can be added...
Declaration of the OpenCL Resources micro-service.