Skip to content

Commit e0c9860

Browse files
committed
Build OpenCL program as soon as the OpenCL device is initialized.
Before this, the program was built when the first frame arrives and the following frames were dropped, because building the program takes a while. Now, the program is built before the device is started. When the first frame arrives, it only needs to be initialized, which is quite fast.
1 parent 2ea7c34 commit e0c9860

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,14 @@ class OpenCLDepthPacketProcessorImpl
138138
cl::Buffer buf_filtered;
139139

140140
bool deviceInitialized;
141+
bool programBuilt;
141142
bool programInitialized;
142143
std::string sourceCode;
143144

144-
OpenCLDepthPacketProcessorImpl(const int deviceId = -1) : deviceInitialized(false), programInitialized(false)
145+
OpenCLDepthPacketProcessorImpl(const int deviceId = -1)
146+
: deviceInitialized(false)
147+
, programBuilt(false)
148+
, programInitialized(false)
145149
{
146150
newIrFrame();
147151
newDepthFrame();
@@ -350,7 +354,8 @@ class OpenCLDepthPacketProcessorImpl
350354
std::cerr << OUT_NAME("init") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl;
351355
throw;
352356
}
353-
return true;
357+
358+
return buildProgram(sourceCode);
354359
}
355360

356361
bool initProgram()
@@ -360,16 +365,13 @@ class OpenCLDepthPacketProcessorImpl
360365
return false;
361366
}
362367

368+
if (!programBuilt)
369+
if (!buildProgram(sourceCode))
370+
return false;
371+
363372
cl_int err = CL_SUCCESS;
364373
try
365374
{
366-
std::string options;
367-
generateOptions(options);
368-
369-
cl::Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length()));
370-
program = cl::Program(context, source);
371-
program.build(options.c_str());
372-
373375
queue = cl::CommandQueue(context, device, 0, &err);
374376

375377
//Read only
@@ -454,14 +456,6 @@ class OpenCLDepthPacketProcessorImpl
454456
catch(const cl::Error &err)
455457
{
456458
std::cerr << OUT_NAME("init") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl;
457-
458-
if(err.err() == CL_BUILD_PROGRAM_FAILURE)
459-
{
460-
std::cout << OUT_NAME("init") "Build Status: " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(device) << std::endl;
461-
std::cout << OUT_NAME("init") "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(device) << std::endl;
462-
std::cout << OUT_NAME("init") "Build Log:\t " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
463-
}
464-
465459
throw;
466460
}
467461
programInitialized = true;
@@ -517,6 +511,38 @@ class OpenCLDepthPacketProcessorImpl
517511
return !source.empty();
518512
}
519513

514+
bool buildProgram(const std::string& sources)
515+
{
516+
try
517+
{
518+
std::cout<< OUT_NAME("buildProgram") "building OpenCL program..." <<std::endl;
519+
520+
std::string options;
521+
generateOptions(options);
522+
523+
cl::Program::Sources source(1, std::make_pair(sources.c_str(), sources.length()));
524+
program = cl::Program(context, source);
525+
program.build(options.c_str());
526+
std::cout<< OUT_NAME("buildProgram") "OpenCL program built successfully" <<std::endl;
527+
}
528+
catch(const cl::Error &err)
529+
{
530+
std::cerr << OUT_NAME("buildProgram") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl;
531+
532+
if(err.err() == CL_BUILD_PROGRAM_FAILURE)
533+
{
534+
std::cout << OUT_NAME("buildProgram") "Build Status: " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(device) << std::endl;
535+
std::cout << OUT_NAME("buildProgram") "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(device) << std::endl;
536+
std::cout << OUT_NAME("buildProgram") "Build Log:\t " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
537+
}
538+
539+
programBuilt = false;
540+
return false;
541+
}
542+
programBuilt = true;
543+
return true;
544+
}
545+
520546
void startTiming()
521547
{
522548
timing_current_start = cv::getTickCount();
@@ -578,8 +604,23 @@ OpenCLDepthPacketProcessor::~OpenCLDepthPacketProcessor()
578604
void OpenCLDepthPacketProcessor::setConfiguration(const libfreenect2::DepthPacketProcessor::Config &config)
579605
{
580606
DepthPacketProcessor::setConfiguration(config);
607+
608+
if ( impl_->config.MaxDepth != config.MaxDepth
609+
|| impl_->config.MinDepth != config.MinDepth)
610+
{
611+
// OpenCL program needs to be rebuilt, then reinitialized
612+
impl_->programBuilt = false;
613+
impl_->programInitialized = false;
614+
impl_->buildProgram(impl_->sourceCode);
615+
}
616+
else if (impl_->config.EnableBilateralFilter != config.EnableBilateralFilter
617+
|| impl_->config.EnableEdgeAwareFilter != config.EnableEdgeAwareFilter)
618+
{
619+
// OpenCL program only needs to be reinitialized
620+
impl_->programInitialized = false;
621+
}
622+
581623
impl_->config = config;
582-
impl_->programInitialized = false;
583624
}
584625

585626
void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char *buffer, size_t buffer_length)

0 commit comments

Comments
 (0)