Skip to content

Commit ee91298

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 ce025f6 commit ee91298

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 59 additions & 17 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 err;
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
@@ -455,13 +457,6 @@ class OpenCLDepthPacketProcessorImpl
455457
{
456458
std::cerr << OUT_NAME("init") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl;
457459

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-
465460
throw err;
466461
}
467462
programInitialized = true;
@@ -518,6 +513,38 @@ class OpenCLDepthPacketProcessorImpl
518513
return !source.empty();
519514
}
520515

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

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

0 commit comments

Comments
 (0)