@@ -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 ;
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
@@ -454,14 +456,6 @@ class OpenCLDepthPacketProcessorImpl
454
456
catch (const cl::Error &err)
455
457
{
456
458
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
-
465
459
throw ;
466
460
}
467
461
programInitialized = true ;
@@ -517,6 +511,38 @@ class OpenCLDepthPacketProcessorImpl
517
511
return !source.empty ();
518
512
}
519
513
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
+
520
546
void startTiming ()
521
547
{
522
548
timing_current_start = cv::getTickCount ();
@@ -578,8 +604,23 @@ OpenCLDepthPacketProcessor::~OpenCLDepthPacketProcessor()
578
604
void OpenCLDepthPacketProcessor::setConfiguration (const libfreenect2::DepthPacketProcessor::Config &config)
579
605
{
580
606
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
+
581
623
impl_->config = config;
582
- impl_->programInitialized = false ;
583
624
}
584
625
585
626
void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse (unsigned char *buffer, size_t buffer_length)
0 commit comments