@@ -138,10 +138,14 @@ class OpenCLDepthPacketProcessorImpl
138
138
cl::Buffer buf_filtered;
139
139
140
140
bool deviceInitialized;
141
+ bool programBuilt;
141
142
bool programInitialized;
142
143
std::string sourceCode;
143
144
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 )
145
149
{
146
150
newIrFrame ();
147
151
newDepthFrame ();
@@ -350,7 +354,8 @@ class OpenCLDepthPacketProcessorImpl
350
354
std::cerr << OUT_NAME (" init" ) " ERROR: " << err.what () << " (" << err.err () << " )" << std::endl;
351
355
throw err;
352
356
}
353
- return true ;
357
+
358
+ return buildProgram (sourceCode);
354
359
}
355
360
356
361
bool initProgram ()
@@ -360,16 +365,13 @@ class OpenCLDepthPacketProcessorImpl
360
365
return false ;
361
366
}
362
367
368
+ if (!programBuilt)
369
+ if (!buildProgram (sourceCode))
370
+ return false ;
371
+
363
372
cl_int err = CL_SUCCESS;
364
373
try
365
374
{
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
-
373
375
queue = cl::CommandQueue (context, device, 0 , &err);
374
376
375
377
// Read only
@@ -455,13 +457,6 @@ class OpenCLDepthPacketProcessorImpl
455
457
{
456
458
std::cerr << OUT_NAME (" init" ) " ERROR: " << err.what () << " (" << err.err () << " )" << std::endl;
457
459
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
-
465
460
throw err;
466
461
}
467
462
programInitialized = true ;
@@ -518,6 +513,38 @@ class OpenCLDepthPacketProcessorImpl
518
513
return !source.empty ();
519
514
}
520
515
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
+
521
548
void startTiming ()
522
549
{
523
550
timing_current_start = cv::getTickCount ();
@@ -579,8 +606,23 @@ OpenCLDepthPacketProcessor::~OpenCLDepthPacketProcessor()
579
606
void OpenCLDepthPacketProcessor::setConfiguration (const libfreenect2::DepthPacketProcessor::Config &config)
580
607
{
581
608
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
+
582
625
impl_->config = config;
583
- impl_->programInitialized = false ;
584
626
}
585
627
586
628
void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse (unsigned char *buffer, size_t buffer_length)
0 commit comments