core: added DirectX interop implementation (OpenCL) with samples

This commit is contained in:
Alexander Alekhin
2013-12-02 01:50:24 +04:00
parent cc69d4631a
commit 11b9d5bf4d
15 changed files with 2474 additions and 39 deletions

View File

@@ -1262,5 +1262,4 @@ template<> struct ParamType<uchar>
#include "opencv2/core/operations.hpp"
#include "opencv2/core/cvstd.inl.hpp"
#endif /*__OPENCV_CORE_HPP__*/

View File

@@ -223,19 +223,26 @@ enum {
CV_EXPORTS void error(int _code, const String& _err, const char* _func, const char* _file, int _line);
#ifdef __GNUC__
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Winvalid-noreturn"
# endif
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Winvalid-noreturn"
# endif
#endif
CV_INLINE CV_NORETURN void errorNoReturn(int _code, const String& _err, const char* _func, const char* _file, int _line)
{
error(_code, _err, _func, _file, _line);
#ifdef __GNUC__
# if !defined __clang__ && !defined __APPLE__
// this suppresses this warning: "noreturn" function does return [enabled by default]
__builtin_trap();
// or use infinite loop: for (;;) {}
# endif
#endif
}
#ifdef __GNUC__
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic pop
# endif
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic pop
# endif
#endif

View File

@@ -0,0 +1,95 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the copyright holders or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_CORE_DIRECTX_HPP__
#define __OPENCV_CORE_DIRECTX_HPP__
#include "mat.hpp"
#include "ocl.hpp"
#if !defined(__d3d11_h__)
struct ID3D11Device;
struct ID3D11Texture2D;
#endif
#if !defined(__d3d10_h__)
struct ID3D10Device;
struct ID3D10Texture2D;
#endif
#if !defined(_D3D9_H_)
struct IDirect3DDevice9;
struct IDirect3DDevice9Ex;
struct IDirect3DSurface9;
#endif
namespace cv { namespace directx {
namespace ocl {
using namespace cv::ocl;
// TODO static functions in the Context class
CV_EXPORTS Context2& initializeContextFromD3D11Device(ID3D11Device* pD3D11Device);
CV_EXPORTS Context2& initializeContextFromD3D10Device(ID3D10Device* pD3D10Device);
CV_EXPORTS Context2& initializeContextFromDirect3DDevice9Ex(IDirect3DDevice9Ex* pDirect3DDevice9Ex);
CV_EXPORTS Context2& initializeContextFromDirect3DDevice9(IDirect3DDevice9* pDirect3DDevice9);
} // namespace cv::directx::ocl
CV_EXPORTS void convertToD3D11Texture2D(InputArray src, ID3D11Texture2D* pD3D11Texture2D);
CV_EXPORTS void convertFromD3D11Texture2D(ID3D11Texture2D* pD3D11Texture2D, OutputArray dst);
CV_EXPORTS void convertToD3D10Texture2D(InputArray src, ID3D10Texture2D* pD3D10Texture2D);
CV_EXPORTS void convertFromD3D10Texture2D(ID3D10Texture2D* pD3D10Texture2D, OutputArray dst);
CV_EXPORTS void convertToDirect3DSurface9(InputArray src, IDirect3DSurface9* pDirect3DSurface9, void* surfaceSharedHandle = NULL);
CV_EXPORTS void convertFromDirect3DSurface9(IDirect3DSurface9* pDirect3DSurface9, OutputArray dst, void* surfaceSharedHandle = NULL);
// Get OpenCV type from DirectX type, return -1 if there is no equivalent
CV_EXPORTS int getTypeFromDXGI_FORMAT(const int iDXGI_FORMAT); // enum DXGI_FORMAT for D3D10/D3D11
// Get OpenCV type from DirectX type, return -1 if there is no equivalent
CV_EXPORTS int getTypeFromD3DFORMAT(const int iD3DFORMAT); // enum D3DTYPE for D3D9
} } // namespace cv::directx
#endif // __OPENCV_CORE_DIRECTX_HPP__

View File

@@ -214,10 +214,33 @@ public:
Program getProg(const ProgramSource2& prog,
const String& buildopt, String& errmsg);
static Context2& getDefault();
static Context2& getDefault(bool initialize = true);
void* ptr() const;
protected:
struct Impl;
inline struct Impl* _getImpl() const { return p; };
protected:
Impl* p;
};
// TODO Move to internal header
void initializeContextFromHandle(Context2& ctx, void* platform, void* context, void* device);
class CV_EXPORTS Platform
{
public:
Platform();
~Platform();
Platform(const Platform& p);
Platform& operator = (const Platform& p);
void* ptr() const;
static Platform& getDefault();
struct Impl;
inline struct Impl* _getImpl() const { return p; };
protected:
Impl* p;
};

1134
modules/core/src/directx.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,55 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the copyright holders or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#if defined(HAVE_DIRECTX)
#include <d3d11.h>
#include <d3d10.h>
#include <d3d9.h>
#ifdef HAVE_OPENCL
#include "opencv2/core/opencl/runtime/opencl_core.hpp"
#include <CL/cl_d3d11.h>
#include <CL/cl_d3d10.h>
#include <CL/cl_dx9_media_sharing.h>
#endif // HAVE_OPENCL
#endif // HAVE_DIRECTX

View File

@@ -1363,21 +1363,6 @@ void finish2()
void release() { if( CV_XADD(&refcount, -1) == 1 ) delete this; } \
int refcount
class Platform
{
public:
Platform();
~Platform();
Platform(const Platform& p);
Platform& operator = (const Platform& p);
void* ptr() const;
static Platform& getDefault();
protected:
struct Impl;
Impl* p;
};
struct Platform::Impl
{
Impl()
@@ -1773,6 +1758,12 @@ const Device& Device::getDefault()
struct Context2::Impl
{
Impl()
{
refcount = 1;
handle = 0;
}
Impl(int dtype0)
{
refcount = 1;
@@ -1855,7 +1846,6 @@ struct Context2::Impl
cl_context handle;
std::vector<Device> devices;
bool initialized;
typedef ProgramSource2::hash_t hash_t;
@@ -1937,22 +1927,29 @@ const Device& Context2::device(size_t idx) const
return !p || idx >= p->devices.size() ? dummy : p->devices[idx];
}
Context2& Context2::getDefault()
Context2& Context2::getDefault(bool initialize)
{
static Context2 ctx;
if( !ctx.p && haveOpenCL() )
if(!ctx.p && haveOpenCL())
{
// do not create new Context2 right away.
// First, try to retrieve existing context of the same type.
// In its turn, Platform::getContext() may call Context2::create()
// if there is no such context.
ctx.create(Device::TYPE_ACCELERATOR);
if(!ctx.p)
ctx.create(Device::TYPE_DGPU);
if(!ctx.p)
ctx.create(Device::TYPE_IGPU);
if(!ctx.p)
ctx.create(Device::TYPE_CPU);
if (initialize)
{
// do not create new Context2 right away.
// First, try to retrieve existing context of the same type.
// In its turn, Platform::getContext() may call Context2::create()
// if there is no such context.
ctx.create(Device::TYPE_ACCELERATOR);
if(!ctx.p)
ctx.create(Device::TYPE_DGPU);
if(!ctx.p)
ctx.create(Device::TYPE_IGPU);
if(!ctx.p)
ctx.create(Device::TYPE_CPU);
}
else
{
ctx.p = new Impl();
}
}
return ctx;
@@ -1964,6 +1961,30 @@ Program Context2::getProg(const ProgramSource2& prog,
return p ? p->getProg(prog, buildopts, errmsg) : Program();
}
void initializeContextFromHandle(Context2& ctx, void* platform, void* _context, void* _device)
{
cl_context context = (cl_context)_context;
cl_device_id device = (cl_device_id)_device;
// cleanup old context
Context2::Impl* impl = ctx._getImpl();
if (impl->handle)
{
cl_int status = clReleaseContext(impl->handle);
(void)status;
}
impl->devices.clear();
impl->handle = context;
impl->devices.resize(1);
impl->devices[0].set(device);
Platform& p = Platform::getDefault();
Platform::Impl* pImpl = p._getImpl();
pImpl->handle = (cl_platform_id)platform;
}
struct Queue::Impl
{
Impl(const Context2& c, const Device& d)

View File

@@ -43,6 +43,9 @@
#ifndef __OPENCV_PRECOMP_H__
#define __OPENCV_PRECOMP_H__
#include "opencv2/opencv_modules.hpp"
#include "cvconfig.h"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/core/cuda.hpp"