4040
4141#define __CL_ENABLE_EXCEPTIONS
4242#ifdef __APPLE__
43- #include < OpenCL/cl.hpp>
43+ #include < OpenCL/cl.hpp>
4444#else
45- #include < CL/cl.hpp>
45+ #include < CL/cl.hpp>
4646#endif
4747
4848#ifndef REG_OPENCL_FILE
@@ -104,8 +104,6 @@ class OpenCLDepthPacketProcessorImpl
104104
105105 double timing_current_start;
106106
107- bool enable_bilateral_filter, enable_edge_filter;
108-
109107 Frame *ir_frame, *depth_frame;
110108
111109 cl::Context context;
@@ -157,10 +155,11 @@ class OpenCLDepthPacketProcessorImpl
157155 cl::Buffer buf_ir_sum;
158156 cl::Buffer buf_filtered;
159157
160- bool isInitialized;
161- const int deviceId;
158+ bool deviceInitialized;
159+ bool programInitialized;
160+ std::string sourceCode;
162161
163- OpenCLDepthPacketProcessorImpl (const int deviceId = -1 ) : isInitialized (false ), deviceId(deviceId )
162+ OpenCLDepthPacketProcessorImpl (const int deviceId = -1 ) : deviceInitialized (false ), programInitialized( false )
164163 {
165164 newIrFrame ();
166165 newDepthFrame ();
@@ -170,8 +169,7 @@ class OpenCLDepthPacketProcessorImpl
170169 timing_current_start = 0.0 ;
171170 image_size = 512 * 424 ;
172171
173- enable_bilateral_filter = true ;
174- enable_edge_filter = true ;
172+ deviceInitialized = initDevice (deviceId);
175173 }
176174
177175 void generateOptions (std::string &options) const
@@ -222,8 +220,8 @@ class OpenCLDepthPacketProcessorImpl
222220 oss << " -D EDGE_AVG_DELTA_THRESHOLD=" << params.edge_avg_delta_threshold << " f" ;
223221 oss << " -D MAX_EDGE_COUNT=" << params.max_edge_count << " f" ;
224222
225- oss << " -D MIN_DEPTH=" << params. min_depth << " f" ;
226- oss << " -D MAX_DEPTH=" << params. max_depth << " f" ;
223+ oss << " -D MIN_DEPTH=" << config. MinDepth * 1000 . 0f << " f" ;
224+ oss << " -D MAX_DEPTH=" << config. MaxDepth * 1000 . 0f << " f" ;
227225 options = oss.str ();
228226 }
229227
@@ -278,7 +276,7 @@ class OpenCLDepthPacketProcessorImpl
278276 }
279277 }
280278
281- bool selectDevice (std::vector<cl::Device> &devices)
279+ bool selectDevice (std::vector<cl::Device> &devices, const int deviceId )
282280 {
283281 if (deviceId != -1 && devices.size () > (size_t )deviceId)
284282 {
@@ -305,14 +303,8 @@ class OpenCLDepthPacketProcessorImpl
305303 return selected;
306304 }
307305
308- bool init ( )
306+ bool initDevice ( const int deviceId )
309307 {
310- if (isInitialized)
311- {
312- return true ;
313- }
314-
315- std::string sourceCode;
316308 if (!readProgram (sourceCode))
317309 {
318310 return false ;
@@ -336,7 +328,7 @@ class OpenCLDepthPacketProcessorImpl
336328 std::vector<cl::Device> devices;
337329 getDevices (platforms, devices);
338330 listDevice (devices);
339- if (selectDevice (devices))
331+ if (selectDevice (devices, deviceId ))
340332 {
341333 std::string devName, devVendor, devType;
342334 size_t devTypeID;
@@ -370,7 +362,25 @@ class OpenCLDepthPacketProcessorImpl
370362 }
371363
372364 context = cl::Context (device);
365+ }
366+ catch (const cl::Error &err)
367+ {
368+ std::cerr << OUT_NAME (" init" ) " ERROR: " << err.what () << " (" << err.err () << " )" << std::endl;
369+ throw err;
370+ }
371+ return true ;
372+ }
373+
374+ bool initProgram ()
375+ {
376+ if (!deviceInitialized)
377+ {
378+ return false ;
379+ }
373380
381+ cl_int err = CL_SUCCESS;
382+ try
383+ {
374384 std::string options;
375385 generateOptions (options);
376386
@@ -471,9 +481,8 @@ class OpenCLDepthPacketProcessorImpl
471481 }
472482
473483 throw err;
474- return false ;
475484 }
476- isInitialized = true ;
485+ programInitialized = true ;
477486 return true ;
478487 }
479488
@@ -509,7 +518,7 @@ class OpenCLDepthPacketProcessorImpl
509518 eventFPS2[0 ] = eventPPS2[0 ];
510519 }
511520
512- queue.enqueueReadBuffer (enable_edge_filter ? buf_filtered : buf_depth, CL_FALSE, 0 , buf_depth_size, depth_frame->data , &eventFPS2, &event1);
521+ queue.enqueueReadBuffer (config. EnableEdgeAwareFilter ? buf_filtered : buf_depth, CL_FALSE, 0 , buf_depth_size, depth_frame->data , &eventFPS2, &event1);
513522 event0.wait ();
514523 event1.wait ();
515524 }
@@ -589,9 +598,7 @@ void OpenCLDepthPacketProcessor::setConfiguration(const libfreenect2::DepthPacke
589598{
590599 DepthPacketProcessor::setConfiguration (config);
591600 impl_->config = config;
592-
593- impl_->enable_bilateral_filter = config.EnableBilateralFilter ;
594- impl_->enable_edge_filter = config.EnableEdgeAwareFilter ;
601+ impl_->programInitialized = false ;
595602}
596603
597604void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse (unsigned char *buffer, size_t buffer_length)
@@ -605,7 +612,6 @@ void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char *
605612 }
606613
607614 impl_->fill_trig_table (p0table);
608- impl_->init ();
609615}
610616
611617void OpenCLDepthPacketProcessor::loadXTableFromFile (const char *filename)
@@ -636,7 +642,7 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet)
636642{
637643 bool has_listener = this ->listener_ != 0 ;
638644
639- if (!impl_->init ())
645+ if (!impl_->programInitialized && !impl_-> initProgram ())
640646 {
641647 std::cerr << OUT_NAME (" process" ) " could not initialize OpenCLDepthPacketProcessor" << std::endl;
642648 return ;
0 commit comments