Skip to content

Commit 558b17d

Browse files
committed
Merge pull request opencv#10231 from alalek:ocl_refactor_program_api
2 parents 9665dde + 1625ffa commit 558b17d

File tree

5 files changed

+800
-204
lines changed

5 files changed

+800
-204
lines changed

modules/core/include/opencv2/core/ocl.hpp

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,20 +606,33 @@ class CV_EXPORTS Program
606606

607607
bool create(const ProgramSource& src,
608608
const String& buildflags, String& errmsg);
609-
bool read(const String& buf, const String& buildflags);
610-
bool write(String& buf) const;
611609

612-
const ProgramSource& source() const;
613610
void* ptr() const;
614611

615-
String getPrefix() const;
616-
static String getPrefix(const String& buildflags);
617-
612+
/**
613+
* @brief Query device-specific program binary.
614+
*
615+
* Returns RAW OpenCL executable binary without additional attachments.
616+
*
617+
* @sa ProgramSource::fromBinary
618+
*
619+
* @param[out] binary output buffer
620+
*/
621+
void getBinary(std::vector<char>& binary) const;
618622

619-
struct Impl;
623+
struct Impl; friend struct Impl;
620624
inline Impl* getImpl() const { return (Impl*)p; }
621625
protected:
622626
Impl* p;
627+
public:
628+
#ifndef OPENCV_REMOVE_DEPRECATED_API
629+
// TODO Remove this
630+
CV_DEPRECATED bool read(const String& buf, const String& buildflags); // removed, use ProgramSource instead
631+
CV_DEPRECATED bool write(String& buf) const; // removed, use getBinary() method instead (RAW OpenCL binary)
632+
CV_DEPRECATED const ProgramSource& source() const; // implementation removed
633+
CV_DEPRECATED String getPrefix() const; // deprecated, implementation replaced
634+
CV_DEPRECATED static String getPrefix(const String& buildflags); // deprecated, implementation replaced
635+
#endif
623636
};
624637

625638

@@ -636,10 +649,59 @@ class CV_EXPORTS ProgramSource
636649
ProgramSource(const ProgramSource& prog);
637650
ProgramSource& operator = (const ProgramSource& prog);
638651

639-
const String& source() const;
652+
const String& source() const; // deprecated
640653
hash_t hash() const; // deprecated
641654

642-
struct Impl;
655+
656+
/** @brief Describe OpenCL program binary.
657+
* Do not call clCreateProgramWithBinary() and/or clBuildProgram().
658+
*
659+
* Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
660+
*
661+
* This kind of binary is not portable between platforms in general - it is specific to OpenCL vendor / device / driver version.
662+
*
663+
* @param module name of program owner module
664+
* @param name unique name of program (module+name is used as key for OpenCL program caching)
665+
* @param binary buffer address. See buffer lifetime requirement in description.
666+
* @param size buffer size
667+
* @param buildOptions additional program-related build options passed to clBuildProgram()
668+
* @return created ProgramSource object
669+
*/
670+
static ProgramSource fromBinary(const String& module, const String& name,
671+
const unsigned char* binary, const size_t size,
672+
const cv::String& buildOptions = cv::String());
673+
674+
/** @brief Describe OpenCL program in SPIR format.
675+
* Do not call clCreateProgramWithBinary() and/or clBuildProgram().
676+
*
677+
* Supports SPIR 1.2 by default (pass '-spir-std=X.Y' in buildOptions to override this behavior)
678+
*
679+
* Caller should guarantee binary buffer lifetime greater than ProgramSource object (and any of its copies).
680+
*
681+
* Programs in this format are portable between OpenCL implementations with 'khr_spir' extension:
682+
* https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/cl_khr_spir.html
683+
* (but they are not portable between different platforms: 32-bit / 64-bit)
684+
*
685+
* Note: these programs can't support vendor specific extensions, like 'cl_intel_subgroups'.
686+
*
687+
* @param module name of program owner module
688+
* @param name unique name of program (module+name is used as key for OpenCL program caching)
689+
* @param binary buffer address. See buffer lifetime requirement in description.
690+
* @param size buffer size
691+
* @param buildOptions additional program-related build options passed to clBuildProgram()
692+
* (these options are added automatically: '-x spir' and '-spir-std=1.2')
693+
* @return created ProgramSource object.
694+
*/
695+
static ProgramSource fromSPIR(const String& module, const String& name,
696+
const unsigned char* binary, const size_t size,
697+
const cv::String& buildOptions = cv::String());
698+
699+
//OpenCL 2.1+ only
700+
//static Program fromSPIRV(const String& module, const String& name,
701+
// const unsigned char* binary, const size_t size,
702+
// const cv::String& buildOptions = cv::String());
703+
704+
struct Impl; friend struct Impl;
643705
inline Impl* getImpl() const { return (Impl*)p; }
644706
protected:
645707
Impl* p;

0 commit comments

Comments
 (0)