Skip to content

Commit c6ba4fb

Browse files
author
Thiemo Wiedemeyer
committed
OpenCL depth packet processor now uses min and max depth from config.
splitted device and program initialization to enable reconfiguration while processor is running.
1 parent 66b546c commit c6ba4fb

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
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,7 @@ class OpenCLDepthPacketProcessorImpl
471481
}
472482

473483
throw err;
474-
return false;
475484
}
476-
isInitialized = true;
477485
return true;
478486
}
479487

@@ -509,7 +517,7 @@ class OpenCLDepthPacketProcessorImpl
509517
eventFPS2[0] = eventPPS2[0];
510518
}
511519

512-
queue.enqueueReadBuffer(enable_edge_filter ? buf_filtered : buf_depth, CL_FALSE, 0, buf_depth_size, depth_frame->data, &eventFPS2, &event1);
520+
queue.enqueueReadBuffer(config.EnableEdgeAwareFilter ? buf_filtered : buf_depth, CL_FALSE, 0, buf_depth_size, depth_frame->data, &eventFPS2, &event1);
513521
event0.wait();
514522
event1.wait();
515523
}
@@ -589,9 +597,7 @@ void OpenCLDepthPacketProcessor::setConfiguration(const libfreenect2::DepthPacke
589597
{
590598
DepthPacketProcessor::setConfiguration(config);
591599
impl_->config = config;
592-
593-
impl_->enable_bilateral_filter = config.EnableBilateralFilter;
594-
impl_->enable_edge_filter = config.EnableEdgeAwareFilter;
600+
impl_->programInitialized = false;
595601
}
596602

597603
void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char *buffer, size_t buffer_length)
@@ -605,7 +611,6 @@ void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char *
605611
}
606612

607613
impl_->fill_trig_table(p0table);
608-
impl_->init();
609614
}
610615

611616
void OpenCLDepthPacketProcessor::loadXTableFromFile(const char *filename)
@@ -636,7 +641,7 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet)
636641
{
637642
bool has_listener = this->listener_ != 0;
638643

639-
if(!impl_->init())
644+
if(!impl_->programInitialized && !impl_->initProgram())
640645
{
641646
std::cerr << OUT_NAME("process") "could not initialize OpenCLDepthPacketProcessor" << std::endl;
642647
return;

0 commit comments

Comments
 (0)