Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
11b9d5bf
Commit
11b9d5bf
authored
Dec 01, 2013
by
Alexander Alekhin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: added DirectX interop implementation (OpenCL) with samples
parent
cc69d463
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
883 additions
and
39 deletions
+883
-39
core.hpp
modules/core/include/opencv2/core.hpp
+0
-1
base.hpp
modules/core/include/opencv2/core/base.hpp
+14
-7
directx.hpp
modules/core/include/opencv2/core/directx.hpp
+95
-0
ocl.hpp
modules/core/include/opencv2/core/ocl.hpp
+24
-1
directx.cpp
modules/core/src/directx.cpp
+0
-0
directx.inc.hpp
modules/core/src/directx.inc.hpp
+55
-0
ocl.cpp
modules/core/src/ocl.cpp
+51
-30
precomp.hpp
modules/core/src/precomp.hpp
+3
-0
CMakeLists.txt
samples/CMakeLists.txt
+8
-0
CMakeLists.txt
samples/directx/CMakeLists.txt
+45
-0
d3d10_interop.cpp
samples/directx/d3d10_interop.cpp
+138
-0
d3d11_interop.cpp
samples/directx/d3d11_interop.cpp
+143
-0
d3d9_interop.cpp
samples/directx/d3d9_interop.cpp
+149
-0
d3d9ex_interop.cpp
samples/directx/d3d9ex_interop.cpp
+158
-0
d3d_base.inl.hpp
samples/directx/d3d_base.inl.hpp
+0
-0
No files found.
modules/core/include/opencv2/core.hpp
View file @
11b9d5bf
...
...
@@ -1262,5 +1262,4 @@ template<> struct ParamType<uchar>
#include "opencv2/core/operations.hpp"
#include "opencv2/core/cvstd.inl.hpp"
#endif
/*__OPENCV_CORE_HPP__*/
modules/core/include/opencv2/core/base.hpp
View file @
11b9d5bf
...
...
@@ -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
...
...
modules/core/include/opencv2/core/directx.hpp
0 → 100644
View file @
11b9d5bf
/*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__
modules/core/include/opencv2/core/ocl.hpp
View file @
11b9d5bf
...
...
@@ -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
;
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
;
};
...
...
modules/core/src/directx.cpp
0 → 100644
View file @
11b9d5bf
This diff is collapsed.
Click to expand it.
modules/core/src/directx.inc.hpp
0 → 100644
View file @
11b9d5bf
/*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
modules/core/src/ocl.cpp
View file @
11b9d5bf
...
...
@@ -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
()
)
{
// 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
(
!
ctx
.
p
&&
haveOpenCL
())
{
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
)
...
...
modules/core/src/precomp.hpp
View file @
11b9d5bf
...
...
@@ -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"
...
...
samples/CMakeLists.txt
View file @
11b9d5bf
...
...
@@ -15,6 +15,10 @@ add_subdirectory(cpp)
add_subdirectory
(
gpu
)
add_subdirectory
(
ocl
)
if
(
WIN32 AND HAVE_DIRECTX
)
add_subdirectory
(
directx
)
endif
()
if
(
ANDROID AND BUILD_ANDROID_EXAMPLES
)
add_subdirectory
(
android
)
endif
()
...
...
@@ -62,6 +66,10 @@ add_subdirectory(cpp)
add_subdirectory
(
ocl
)
# FIXIT: can't use cvconfig.h in samples: add_subdirectory(gpu)
if
(
WIN32
)
add_subdirectory
(
directx
)
endif
()
#
# END OF BUILD CASE 2: Build samples with library binaries
#
...
...
samples/directx/CMakeLists.txt
0 → 100644
View file @
11b9d5bf
SET
(
OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_highgui
)
ocv_check_dependencies
(
${
OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS
}
)
if
(
BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND
)
set
(
project
"directx"
)
string
(
TOUPPER
"
${
project
}
"
project_upper
)
project
(
"
${
project
}
_samples"
)
ocv_include_modules
(
${
OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS
}
)
# ---------------------------------------------
# Define executable targets
# ---------------------------------------------
MACRO
(
OPENCV_DEFINE_DIRECTX_EXAMPLE name srcs
)
set
(
the_target
"example_
${
project
}
_
${
name
}
"
)
add_executable
(
${
the_target
}
${
srcs
}
)
target_link_libraries
(
${
the_target
}
${
OPENCV_LINKER_LIBS
}
${
OPENCV_DIRECTX_SAMPLES_REQUIRED_DEPS
}
)
set_target_properties
(
${
the_target
}
PROPERTIES
OUTPUT_NAME
"
${
project
}
-example-
${
name
}
"
PROJECT_LABEL
"(EXAMPLE_
${
project_upper
}
)
${
name
}
"
)
if
(
ENABLE_SOLUTION_FOLDERS
)
set_target_properties
(
${
the_target
}
PROPERTIES FOLDER
"samples//
${
project
}
"
)
endif
()
if
(
WIN32
)
if
(
MSVC AND NOT BUILD_SHARED_LIBS
)
set_target_properties
(
${
the_target
}
PROPERTIES LINK_FLAGS
"/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG"
)
endif
()
install
(
TARGETS
${
the_target
}
RUNTIME DESTINATION
"
${
OPENCV_SAMPLES_BIN_INSTALL_PATH
}
/
${
project
}
"
COMPONENT main
)
endif
()
ENDMACRO
()
file
(
GLOB all_samples RELATIVE
${
CMAKE_CURRENT_SOURCE_DIR
}
*.cpp
)
foreach
(
sample_filename
${
all_samples
}
)
get_filename_component
(
sample
${
sample_filename
}
NAME_WE
)
file
(
GLOB sample_srcs RELATIVE
${
CMAKE_CURRENT_SOURCE_DIR
}
${
sample
}
.*
)
OPENCV_DEFINE_DIRECTX_EXAMPLE
(
${
sample
}
${
sample_srcs
}
)
endforeach
()
endif
()
samples/directx/d3d10_interop.cpp
0 → 100644
View file @
11b9d5bf
#include <windows.h>
#include <d3d10.h>
#pragma comment (lib, "d3d10.lib")
#define USE_D3D10
#define WINDOW_NAME "OpenCV Direct3D 10 Sample"
IDXGISwapChain
*
swapchain
=
NULL
;
ID3D10Device
*
dev
=
NULL
;
ID3D10Texture2D
*
pBackBufferTexture
=
NULL
;
ID3D10Texture2D
*
pCPUWriteTexture
=
NULL
;
ID3D10Texture2D
*
pInputTexture
=
NULL
;
ID3D10RenderTargetView
*
backbuffer
=
NULL
;
#include "d3d_base.inl.hpp"
bool
initDirect3D
()
{
DXGI_SWAP_CHAIN_DESC
scd
;
ZeroMemory
(
&
scd
,
sizeof
(
DXGI_SWAP_CHAIN_DESC
));
scd
.
BufferCount
=
1
;
// one back buffer
scd
.
BufferDesc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
// use 32-bit color
scd
.
BufferDesc
.
Width
=
WIDTH
;
// set the back buffer width
scd
.
BufferDesc
.
Height
=
HEIGHT
;
// set the back buffer height
scd
.
BufferUsage
=
DXGI_USAGE_RENDER_TARGET_OUTPUT
;
// how swap chain is to be used
scd
.
OutputWindow
=
hWnd
;
// the window to be used
scd
.
SampleDesc
.
Count
=
1
;
// how many multisamples
scd
.
Windowed
=
TRUE
;
// windowed/full-screen mode
scd
.
SwapEffect
=
DXGI_SWAP_EFFECT_DISCARD
;
scd
.
Flags
=
DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
;
// allow full-screen switching
if
(
FAILED
(
D3D10CreateDeviceAndSwapChain
(
NULL
,
D3D10_DRIVER_TYPE_HARDWARE
,
NULL
,
0
,
D3D10_SDK_VERSION
,
&
scd
,
&
swapchain
,
&
dev
)))
{
return
false
;
}
if
(
FAILED
(
swapchain
->
GetBuffer
(
0
,
__uuidof
(
ID3D10Texture2D
),
(
LPVOID
*
)
&
pBackBufferTexture
)))
{
return
false
;
}
if
(
FAILED
(
dev
->
CreateRenderTargetView
(
pBackBufferTexture
,
NULL
,
&
backbuffer
)))
{
return
false
;
}
dev
->
OMSetRenderTargets
(
1
,
&
backbuffer
,
NULL
);
D3D10_VIEWPORT
viewport
;
ZeroMemory
(
&
viewport
,
sizeof
(
D3D10_VIEWPORT
));
viewport
.
Width
=
WIDTH
;
viewport
.
Height
=
HEIGHT
;
viewport
.
MinDepth
=
0.0
f
;
viewport
.
MaxDepth
=
0.0
f
;
dev
->
RSSetViewports
(
1
,
&
viewport
);
return
true
;
}
bool
initDirect3DTextures
()
{
{
// Create texture for demo 0
D3D10_TEXTURE2D_DESC
desc
=
{
0
};
desc
.
Width
=
WIDTH
;
desc
.
Height
=
HEIGHT
;
desc
.
MipLevels
=
desc
.
ArraySize
=
1
;
desc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
desc
.
SampleDesc
.
Count
=
1
;
desc
.
BindFlags
=
D3D10_BIND_SHADER_RESOURCE
;
desc
.
Usage
=
D3D10_USAGE_DYNAMIC
;
desc
.
CPUAccessFlags
=
D3D10_CPU_ACCESS_WRITE
;
if
(
FAILED
(
dev
->
CreateTexture2D
(
&
desc
,
NULL
,
&
pCPUWriteTexture
)))
{
std
::
cerr
<<
"Can't create texture for CPU write sample"
<<
std
::
endl
;
return
false
;
}
}
{
// Create Read-only texture
cv
::
Mat
inputMat
=
getInputTexture
();
D3D10_TEXTURE2D_DESC
desc
=
{
0
};
desc
.
Width
=
inputMat
.
size
().
width
;
desc
.
Height
=
inputMat
.
size
().
height
;
desc
.
MipLevels
=
desc
.
ArraySize
=
1
;
desc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
desc
.
SampleDesc
.
Count
=
1
;
desc
.
BindFlags
=
D3D10_BIND_SHADER_RESOURCE
;
desc
.
Usage
=
D3D10_USAGE_IMMUTABLE
;
desc
.
CPUAccessFlags
=
cv
::
ocl
::
useOpenCL
()
?
0
:
D3D10_CPU_ACCESS_READ
;
D3D10_SUBRESOURCE_DATA
srInitData
;
srInitData
.
pSysMem
=
inputMat
.
data
;
srInitData
.
SysMemPitch
=
(
UINT
)
inputMat
.
step
[
0
];
if
(
FAILED
(
dev
->
CreateTexture2D
(
&
desc
,
&
srInitData
,
&
pInputTexture
)))
{
std
::
cerr
<<
"Can't create texture with input image"
<<
std
::
endl
;
return
false
;
}
}
return
true
;
}
void
cleanUp
(
void
)
{
if
(
swapchain
)
swapchain
->
SetFullscreenState
(
FALSE
,
NULL
);
// switch to windowed mode
SAFE_RELEASE
(
swapchain
);
SAFE_RELEASE
(
pCPUWriteTexture
);
SAFE_RELEASE
(
pInputTexture
);
SAFE_RELEASE
(
pBackBufferTexture
);
SAFE_RELEASE
(
backbuffer
);
SAFE_RELEASE
(
dev
);
}
void
render
(
void
)
{
// check to make sure you have a valid Direct3D device
CV_Assert
(
dev
);
renderToD3DObject
();
// switch the back buffer and the front buffer
swapchain
->
Present
(
0
,
0
);
}
samples/directx/d3d11_interop.cpp
0 → 100644
View file @
11b9d5bf
#include <windows.h>
#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
#define USE_D3D11
#define WINDOW_NAME "OpenCV Direct3D 11 Sample"
IDXGISwapChain
*
swapchain
=
NULL
;
ID3D11Device
*
dev
=
NULL
;
ID3D11DeviceContext
*
devcon
=
NULL
;
ID3D11Texture2D
*
pBackBufferTexture
=
NULL
;
ID3D11Texture2D
*
pCPUWriteTexture
=
NULL
;
ID3D11Texture2D
*
pInputTexture
=
NULL
;
ID3D11RenderTargetView
*
backbuffer
=
NULL
;
#include "d3d_base.inl.hpp"
bool
initDirect3D
()
{
DXGI_SWAP_CHAIN_DESC
scd
;
ZeroMemory
(
&
scd
,
sizeof
(
DXGI_SWAP_CHAIN_DESC
));
scd
.
BufferCount
=
1
;
// one back buffer
scd
.
BufferDesc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
// use 32-bit color
scd
.
BufferDesc
.
Width
=
WIDTH
;
// set the back buffer width
scd
.
BufferDesc
.
Height
=
HEIGHT
;
// set the back buffer height
scd
.
BufferUsage
=
DXGI_USAGE_RENDER_TARGET_OUTPUT
;
// how swap chain is to be used
scd
.
OutputWindow
=
hWnd
;
// the window to be used
scd
.
SampleDesc
.
Count
=
1
;
// how many multisamples
scd
.
Windowed
=
TRUE
;
// windowed/full-screen mode
scd
.
SwapEffect
=
DXGI_SWAP_EFFECT_DISCARD
;
scd
.
Flags
=
DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
;
// allow full-screen switching
if
(
FAILED
(
D3D11CreateDeviceAndSwapChain
(
NULL
,
D3D_DRIVER_TYPE_HARDWARE
,
NULL
,
0
,
NULL
,
0
,
D3D11_SDK_VERSION
,
&
scd
,
&
swapchain
,
&
dev
,
NULL
,
&
devcon
)))
{
return
false
;
}
if
(
FAILED
(
swapchain
->
GetBuffer
(
0
,
__uuidof
(
ID3D11Texture2D
),
(
LPVOID
*
)
&
pBackBufferTexture
)))
{
return
false
;
}
if
(
FAILED
(
dev
->
CreateRenderTargetView
(
pBackBufferTexture
,
NULL
,
&
backbuffer
)))
{
return
false
;
}
devcon
->
OMSetRenderTargets
(
1
,
&
backbuffer
,
NULL
);
D3D11_VIEWPORT
viewport
=
{
0
};
viewport
.
Width
=
WIDTH
;
viewport
.
Height
=
HEIGHT
;
viewport
.
MinDepth
=
0.0
f
;
viewport
.
MaxDepth
=
0.0
f
;
devcon
->
RSSetViewports
(
1
,
&
viewport
);
return
true
;
}
bool
initDirect3DTextures
()
{
{
// Create texture for demo 0
D3D11_TEXTURE2D_DESC
desc
=
{
0
};
desc
.
Width
=
WIDTH
;
desc
.
Height
=
HEIGHT
;
desc
.
MipLevels
=
desc
.
ArraySize
=
1
;
desc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
desc
.
SampleDesc
.
Count
=
1
;
desc
.
BindFlags
=
D3D11_BIND_SHADER_RESOURCE
;
desc
.
Usage
=
D3D11_USAGE_DYNAMIC
;
desc
.
CPUAccessFlags
=
D3D11_CPU_ACCESS_WRITE
;
if
(
FAILED
(
dev
->
CreateTexture2D
(
&
desc
,
NULL
,
&
pCPUWriteTexture
)))
{
std
::
cerr
<<
"Can't create texture for CPU write sample"
<<
std
::
endl
;
return
false
;
}
}
{
// Create Read-only texture
cv
::
Mat
inputMat
=
getInputTexture
();
D3D11_TEXTURE2D_DESC
desc
=
{
0
};
desc
.
Width
=
inputMat
.
size
().
width
;
desc
.
Height
=
inputMat
.
size
().
height
;
desc
.
MipLevels
=
desc
.
ArraySize
=
1
;
desc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
desc
.
SampleDesc
.
Count
=
1
;
desc
.
BindFlags
=
D3D11_BIND_SHADER_RESOURCE
;
desc
.
Usage
=
D3D11_USAGE_IMMUTABLE
;
desc
.
CPUAccessFlags
=
cv
::
ocl
::
useOpenCL
()
?
0
:
D3D11_CPU_ACCESS_READ
;
D3D11_SUBRESOURCE_DATA
srInitData
;
srInitData
.
pSysMem
=
inputMat
.
data
;
srInitData
.
SysMemPitch
=
(
UINT
)
inputMat
.
step
[
0
];
if
(
FAILED
(
dev
->
CreateTexture2D
(
&
desc
,
&
srInitData
,
&
pInputTexture
)))
{
std
::
cerr
<<
"Can't create texture with input image"
<<
std
::
endl
;
return
false
;
}
}
return
true
;
}
void
cleanUp
(
void
)
{
if
(
swapchain
)
swapchain
->
SetFullscreenState
(
FALSE
,
NULL
);
// switch to windowed mode
SAFE_RELEASE
(
swapchain
);
SAFE_RELEASE
(
pCPUWriteTexture
);
SAFE_RELEASE
(
pInputTexture
);
SAFE_RELEASE
(
pBackBufferTexture
);
SAFE_RELEASE
(
backbuffer
);
SAFE_RELEASE
(
dev
);
SAFE_RELEASE
(
devcon
);
}
void
render
(
void
)
{
// check to make sure you have a valid Direct3D device
CV_Assert
(
dev
);
renderToD3DObject
();
// switch the back buffer and the front buffer
swapchain
->
Present
(
0
,
0
);
}
samples/directx/d3d9_interop.cpp
0 → 100644
View file @
11b9d5bf
#include <windows.h>
#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")
#define USE_D3D9
#define WINDOW_NAME "OpenCV Direct3D 9 Sample"
IDirect3D9
*
pD3D
=
NULL
;
IDirect3DDevice9
*
dev
=
NULL
;
IDirect3DSurface9
*
pBackBuffer
=
NULL
;
IDirect3DSurface9
*
pCPUWriteSurface
=
NULL
;
// required name
IDirect3DSurface9
*
pReadOnlySurface
=
NULL
;
// required name
HANDLE
readOnlySurfaceShared
=
0
;
// required name
IDirect3DSurface9
*
pSurface
=
NULL
;
// required name
HANDLE
surfaceShared
=
0
;
// required name
#include "d3d_base.inl.hpp"
bool
initDirect3D
(
void
)
{
if
(
NULL
==
(
pD3D
=
Direct3DCreate9
(
D3D_SDK_VERSION
)))
{
return
false
;
}
D3DPRESENT_PARAMETERS
d3dpp
;
ZeroMemory
(
&
d3dpp
,
sizeof
(
D3DPRESENT_PARAMETERS
));
DWORD
flags
=
D3DCREATE_HARDWARE_VERTEXPROCESSING
|
D3DCREATE_PUREDEVICE
|
D3DCREATE_NOWINDOWCHANGES
|
D3DCREATE_MULTITHREADED
;
d3dpp
.
Windowed
=
true
;
d3dpp
.
Flags
=
0
;
d3dpp
.
BackBufferCount
=
0
;
d3dpp
.
BackBufferFormat
=
D3DFMT_A8R8G8B8
;
d3dpp
.
BackBufferHeight
=
HEIGHT
;
d3dpp
.
BackBufferWidth
=
WIDTH
;
d3dpp
.
MultiSampleType
=
D3DMULTISAMPLE_NONE
;
d3dpp
.
SwapEffect
=
D3DSWAPEFFECT_DISCARD
;
d3dpp
.
hDeviceWindow
=
hWnd
;
d3dpp
.
PresentationInterval
=
D3DPRESENT_INTERVAL_IMMEDIATE
;
d3dpp
.
FullScreen_RefreshRateInHz
=
D3DPRESENT_RATE_DEFAULT
;
if
(
FAILED
(
pD3D
->
CreateDevice
(
D3DADAPTER_DEFAULT
,
D3DDEVTYPE_HAL
,
hWnd
,
flags
,
&
d3dpp
,
&
dev
)))
{
return
false
;
}
if
(
FAILED
(
dev
->
GetBackBuffer
(
0
,
0
,
D3DBACKBUFFER_TYPE_MONO
,
&
pBackBuffer
)))
{
return
false
;
}
return
true
;
}
bool
initDirect3DTextures
()
{
// Note: sharing is not supported on some platforms
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pSurface
,
NULL
/*&surfaceShared*/
)))
{
std
::
cerr
<<
"Can't create surface for result"
<<
std
::
endl
;
return
false
;
}
// Note: sharing is not supported on some platforms
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pReadOnlySurface
,
NULL
/*&readOnlySurfaceShared*/
)))
{
std
::
cerr
<<
"Can't create read only surface"
<<
std
::
endl
;
return
false
;
}
else
{
IDirect3DSurface9
*
pTmpSurface
;
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pTmpSurface
,
NULL
)))
{
std
::
cerr
<<
"Can't create temp surface for CPU write"
<<
std
::
endl
;
return
false
;
}
D3DLOCKED_RECT
memDesc
=
{
0
,
NULL
};
RECT
rc
=
{
0
,
0
,
WIDTH
,
HEIGHT
};
if
(
SUCCEEDED
(
pTmpSurface
->
LockRect
(
&
memDesc
,
&
rc
,
0
)))
{
cv
::
Mat
m
(
cv
::
Size
(
WIDTH
,
HEIGHT
),
CV_8UC4
,
memDesc
.
pBits
,
(
int
)
memDesc
.
Pitch
);
getInputTexture
().
copyTo
(
m
);
pTmpSurface
->
UnlockRect
();
dev
->
StretchRect
(
pTmpSurface
,
NULL
,
pReadOnlySurface
,
NULL
,
D3DTEXF_NONE
);
}
else
{
std
::
cerr
<<
"Can't LockRect() on surface"
<<
std
::
endl
;
}
pTmpSurface
->
Release
();
}
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pCPUWriteSurface
,
NULL
)))
{
std
::
cerr
<<
"Can't create surface for CPU write"
<<
std
::
endl
;
return
false
;
}
return
true
;
}
void
render
(
void
)
{
// check to make sure you have a valid Direct3D device
CV_Assert
(
dev
);
renderToD3DObject
();
if
(
g_sampleType
==
0
)
{
// nothing
}
else
if
(
g_sampleType
==
1
)
{
if
(
FAILED
(
dev
->
StretchRect
(
pCPUWriteSurface
,
NULL
,
pBackBuffer
,
NULL
,
D3DTEXF_NONE
)))
{
std
::
cerr
<<
"Can't StretchRect()"
<<
std
::
endl
;
}
}
else
{
if
(
FAILED
(
dev
->
StretchRect
(
pSurface
,
NULL
,
pBackBuffer
,
NULL
,
D3DTEXF_NONE
)))
{
std
::
cerr
<<
"Can't StretchRect()"
<<
std
::
endl
;
}
}
if
(
SUCCEEDED
(
dev
->
BeginScene
()))
{
// end the scene
dev
->
EndScene
();
}
// present the back buffer contents to the display
dev
->
Present
(
NULL
,
NULL
,
NULL
,
NULL
);
}
void
cleanUp
(
void
)
{
SAFE_RELEASE
(
pCPUWriteSurface
);
SAFE_RELEASE
(
pReadOnlySurface
);
SAFE_RELEASE
(
pSurface
);
SAFE_RELEASE
(
pBackBuffer
);
SAFE_RELEASE
(
dev
);
SAFE_RELEASE
(
pD3D
);}
samples/directx/d3d9ex_interop.cpp
0 → 100644
View file @
11b9d5bf
#include <windows.h>
#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")
#define USE_D3DEX
#define WINDOW_NAME "OpenCV Direct3D 9 Ex Sample"
IDirect3D9Ex
*
pD3D
=
NULL
;
IDirect3DDevice9Ex
*
dev
=
NULL
;
IDirect3DSurface9
*
pBackBuffer
=
NULL
;
IDirect3DSurface9
*
pCPUWriteSurface
=
NULL
;
// required name
IDirect3DSurface9
*
pReadOnlySurface
=
NULL
;
// required name
HANDLE
readOnlySurfaceShared
=
0
;
// required name
IDirect3DSurface9
*
pSurface
=
NULL
;
// required name
HANDLE
surfaceShared
=
0
;
// required name
#include "d3d_base.inl.hpp"
bool
initDirect3D
(
void
)
{
if
(
FAILED
(
Direct3DCreate9Ex
(
D3D_SDK_VERSION
,
&
pD3D
)))
{
return
false
;
}
D3DDISPLAYMODEEX
ddm
;
ZeroMemory
(
&
ddm
,
sizeof
(
ddm
));
ddm
.
Size
=
sizeof
(
D3DDISPLAYMODEEX
);
D3DDISPLAYROTATION
rotation
;
if
(
FAILED
(
pD3D
->
GetAdapterDisplayModeEx
(
D3DADAPTER_DEFAULT
,
&
ddm
,
&
rotation
)))
{
return
false
;
}
D3DPRESENT_PARAMETERS
d3dpp
;
ZeroMemory
(
&
d3dpp
,
sizeof
(
D3DPRESENT_PARAMETERS
));
DWORD
flags
=
D3DCREATE_HARDWARE_VERTEXPROCESSING
|
D3DCREATE_PUREDEVICE
|
D3DCREATE_NOWINDOWCHANGES
|
D3DCREATE_MULTITHREADED
;
d3dpp
.
Windowed
=
true
;
d3dpp
.
Flags
=
0
;
d3dpp
.
BackBufferCount
=
0
;
d3dpp
.
BackBufferFormat
=
ddm
.
Format
;
d3dpp
.
BackBufferHeight
=
HEIGHT
;
d3dpp
.
BackBufferWidth
=
WIDTH
;
d3dpp
.
MultiSampleType
=
D3DMULTISAMPLE_NONE
;
d3dpp
.
SwapEffect
=
D3DSWAPEFFECT_DISCARD
;
d3dpp
.
hDeviceWindow
=
hWnd
;
d3dpp
.
PresentationInterval
=
D3DPRESENT_INTERVAL_IMMEDIATE
;
d3dpp
.
FullScreen_RefreshRateInHz
=
D3DPRESENT_RATE_DEFAULT
;
if
(
FAILED
(
pD3D
->
CreateDeviceEx
(
D3DADAPTER_DEFAULT
,
D3DDEVTYPE_HAL
,
hWnd
,
flags
,
&
d3dpp
,
NULL
,
&
dev
)))
{
return
false
;
}
if
(
FAILED
(
dev
->
GetBackBuffer
(
0
,
0
,
D3DBACKBUFFER_TYPE_MONO
,
&
pBackBuffer
)))
{
return
false
;
}
return
true
;
}
bool
initDirect3DTextures
()
{
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pSurface
,
&
surfaceShared
)))
{
std
::
cerr
<<
"Can't create surface for result"
<<
std
::
endl
;
return
false
;
}
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pReadOnlySurface
,
&
readOnlySurfaceShared
)))
{
std
::
cerr
<<
"Can't create read only surface"
<<
std
::
endl
;
return
false
;
}
else
{
IDirect3DSurface9
*
pTmpSurface
;
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pTmpSurface
,
NULL
)))
{
std
::
cerr
<<
"Can't create temp surface for CPU write"
<<
std
::
endl
;
return
false
;
}
D3DLOCKED_RECT
memDesc
=
{
0
,
NULL
};
RECT
rc
=
{
0
,
0
,
WIDTH
,
HEIGHT
};
if
(
SUCCEEDED
(
pTmpSurface
->
LockRect
(
&
memDesc
,
&
rc
,
0
)))
{
cv
::
Mat
m
(
cv
::
Size
(
WIDTH
,
HEIGHT
),
CV_8UC4
,
memDesc
.
pBits
,
(
int
)
memDesc
.
Pitch
);
getInputTexture
().
copyTo
(
m
);
pTmpSurface
->
UnlockRect
();
dev
->
StretchRect
(
pTmpSurface
,
NULL
,
pReadOnlySurface
,
NULL
,
D3DTEXF_NONE
);
}
else
{
std
::
cerr
<<
"Can't LockRect() on surface"
<<
std
::
endl
;
}
pTmpSurface
->
Release
();
}
if
(
FAILED
(
dev
->
CreateOffscreenPlainSurface
(
WIDTH
,
HEIGHT
,
D3DFMT_A8R8G8B8
,
D3DPOOL_DEFAULT
,
&
pCPUWriteSurface
,
NULL
)))
{
std
::
cerr
<<
"Can't create surface for CPU write"
<<
std
::
endl
;
return
false
;
}
return
true
;
}
void
render
(
void
)
{
// check to make sure you have a valid Direct3D device
CV_Assert
(
dev
);
renderToD3DObject
();
if
(
g_sampleType
==
0
)
{
// nothing
}
else
if
(
g_sampleType
==
1
)
{
if
(
FAILED
(
dev
->
StretchRect
(
pCPUWriteSurface
,
NULL
,
pBackBuffer
,
NULL
,
D3DTEXF_NONE
)))
{
std
::
cerr
<<
"Can't StretchRect()"
<<
std
::
endl
;
}
}
else
{
if
(
FAILED
(
dev
->
StretchRect
(
pSurface
,
NULL
,
pBackBuffer
,
NULL
,
D3DTEXF_NONE
)))
{
std
::
cerr
<<
"Can't StretchRect()"
<<
std
::
endl
;
}
}
if
(
SUCCEEDED
(
dev
->
BeginScene
()))
{
// end the scene
dev
->
EndScene
();
}
// present the back buffer contents to the display
dev
->
Present
(
NULL
,
NULL
,
NULL
,
NULL
);
}
void
cleanUp
(
void
)
{
SAFE_RELEASE
(
pCPUWriteSurface
);
SAFE_RELEASE
(
pReadOnlySurface
);
SAFE_RELEASE
(
pSurface
);
SAFE_RELEASE
(
pBackBuffer
);
SAFE_RELEASE
(
dev
);
SAFE_RELEASE
(
pD3D
);
}
samples/directx/d3d_base.inl.hpp
0 → 100644
View file @
11b9d5bf
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment