gpu_context_api_dx.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief a header that defines wrappers for internal GPU plugin-specific
7  * shared Video Acceleration device contexts
8  * and shared memory blobs which contain Video Acceleration surfaces
9  *
10  * @file gpu_context_api_dx.hpp
11  */
12 #pragma once
13 
14 #include <memory>
15 #include <string>
16 
18 
19 #include <d3d11.h>
20 
21 namespace InferenceEngine {
22 
23 namespace gpu {
24 /**
25 * @brief This class represents an abstraction for GPU plugin remote context
26 * which is shared with Direct3D 11 device.
27 * The plugin object derived from this class can be obtained either with
28 * GetContext() method of Executable network or using CreateContext() Core call.
29 * @note User can also obtain OpenCL context handle from this class.
30 */
31 class D3DContext : public ClContext {
32 public:
33  /**
34  * @brief A smart pointer to the D3DContext object
35  */
36  using Ptr = std::shared_ptr<D3DContext>;
37 
38  /**
39  * @brief ID3D11Device conversion operator for the D3DContext object.
40  * @return Pointer to underlying ID3D11Device interface
41  */
42  operator ID3D11Device*() {
43  return _ObjFromParams<ID3D11Device*, gpu_handle_param>(getParams(),
44  GPU_PARAM_KEY(VA_DEVICE),
45  GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(VA_SHARED));
46  }
47 };
48 
49 /**
50 * @brief This class represents an abstraction for GPU plugin remote blob
51 * which is shared with Direct3D 11 buffer.
52 * The plugin object derived from this class can be obtained with CreateBlob() call.
53 * @note User can also obtain OpenCL buffer handle from this class.
54 */
55 class D3DBufferBlob : public ClBufferBlob {
56 public:
57  /**
58  * @brief A smart pointer to the D3DBufferBlob object
59  */
60  using Ptr = std::shared_ptr<D3DBufferBlob>;
61 
62  /**
63  * @brief Creates a D3DBufferBlob object with the specified dimensions and layout.
64  * @param tensorDesc Tensor description
65  */
67 
68  /**
69  * @brief ID3D11Buffer conversion operator for the D3DContext object.
70  * @return Pointer to underlying ID3D11Buffer interface
71  */
72  operator ID3D11Buffer*() {
73  return _ObjFromParams<ID3D11Buffer*, gpu_handle_param>(getParams(),
74  GPU_PARAM_KEY(DEV_OBJECT_HANDLE),
75  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(DX_BUFFER));
76  }
77 };
78 
79 /**
80 * @brief This class represents an abstraction for GPU plugin remote blob
81 * which is shared with Direct3D 11 2D texture.
82 * The plugin object derived from this class can be obtained with CreateBlob() call.
83 * @note User can also obtain OpenCL 2D image handle from this class.
84 */
86 public:
87  /**
88  * @brief A smart pointer to the D3DSurface2DBlob object
89  */
90  using Ptr = std::shared_ptr<D3DSurface2DBlob>;
91 
92  /**
93  * @brief Creates a D3DSurface2DBlob object with the specified dimensions and layout.
94  * @param tensorDesc Tensor description
95  */
97 
98  /**
99  * @brief ID3D11Texture2D conversion operator for the D3DContext object.
100  * @return Pointer to underlying ID3D11Texture2D interface
101  */
102  operator ID3D11Texture2D*() {
103  return _ObjFromParams<ID3D11Texture2D*, gpu_handle_param>(getParams(),
104  GPU_PARAM_KEY(DEV_OBJECT_HANDLE),
105  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE));
106  }
107 
108  /**
109  * @brief Returns plane ID of underlying video decoder surface,
110  * or 0 if no video surface was shared.
111  */
112  uint32_t plane() {
113  return _ObjFromParams<uint32_t, uint32_t>(getParams(),
114  GPU_PARAM_KEY(VA_PLANE),
115  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE));
116  }
117 };
118 
119 /**
120 * @brief This function is used to obtain a NV12 compound blob object from NV12 DXGI video decoder output.
121 * The resulting compound contains two remote blobs for Y and UV planes of the surface.
122 */
123 static inline Blob::Ptr make_shared_blob_nv12(size_t height, size_t width, RemoteContext::Ptr ctx, ID3D11Texture2D* nv12_surf) {
124  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
125  if (nullptr == casted) {
126  THROW_IE_EXCEPTION << "Invalid remote context passed";
127  }
128 
129  // despite of layout, blob dimensions always follow in N,C,H,W order
130  TensorDesc desc(Precision::U8, { 1, 1, height, width }, Layout::NHWC);
131 
132  ParamMap blobParams = {
133  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
134  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(nv12_surf) },
135  { GPU_PARAM_KEY(VA_PLANE), uint32_t(0) }
136  };
137  Blob::Ptr y_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, blobParams));
138 
139  TensorDesc uvdesc(Precision::U8, { 1, 2, height / 2, width / 2 }, Layout::NHWC);
140  blobParams[GPU_PARAM_KEY(MEM_HANDLE)] = static_cast<gpu_handle_param>(nv12_surf);
141  blobParams[GPU_PARAM_KEY(VA_PLANE)] = uint32_t(1);
142  Blob::Ptr uv_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(uvdesc, blobParams));
143 
144  return InferenceEngine::make_shared_blob<NV12Blob>(y_blob, uv_blob);
145 }
146 
147 /**
148 * @brief This function is used to obtain remote context object from ID3D11Device
149 */
150 static inline D3DContext::Ptr make_shared_context(Core& core, std::string deviceName, ID3D11Device* device) {
151  ParamMap contextParams = {
152  { GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(VA_SHARED) },
153  { GPU_PARAM_KEY(VA_DEVICE), static_cast<gpu_handle_param>(device) }
154  };
155  return std::dynamic_pointer_cast<D3DContext>(core.CreateContext(deviceName, contextParams));
156 }
157 
158 /**
159 * @brief This function is used to obtain remote blob object from ID3D11Buffer
160 */
161 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, ID3D11Buffer* buffer) {
162  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
163  if (nullptr == casted) {
164  THROW_IE_EXCEPTION << "Invalid remote context passed";
165  }
166 
167  ParamMap params = {
168  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(DX_BUFFER) },
169  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(buffer) }
170  };
171  return std::dynamic_pointer_cast<D3DBufferBlob>(casted->CreateBlob(desc, params));
172 }
173 
174 /**
175 * @brief This function is used to obtain remote blob object from ID3D11Texture2D
176 * @param desc Tensor description
177 * @param ctx the RemoteContext object whuch owns context for the blob to be created
178 * @param surface Pointer to ID3D11Texture2D interface of the objects that owns NV12 texture
179 * @param plane ID of the plane to be shared (0 or 1)
180 * @return Smart pointer to created RemoteBlob object cast to base class
181 * @note The underlying ID3D11Texture2D can also be a plane of output surface of DXGI video decoder
182 */
183 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, ID3D11Texture2D* surface, uint32_t plane = 0) {
184  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
185  if (nullptr == casted) {
186  THROW_IE_EXCEPTION << "Invalid remote context passed";
187  }
188  ParamMap params = {
189  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
190  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(surface) },
191  { GPU_PARAM_KEY(VA_PLANE), plane }
192  };
193  return std::dynamic_pointer_cast<D3DSurface2DBlob>(casted->CreateBlob(desc, params));
194 }
195 
196 } // namespace gpu
197 } // namespace InferenceEngine
InferenceEngine::gpu::D3DContext
This class represents an abstraction for GPU plugin remote context which is shared with Direct3D 11 d...
Definition: gpu_context_api_dx.hpp:31
InferenceEngine::RemoteContext::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::Core
This class represents Inference Engine Core entity.
Definition: ie_core.hpp:29
InferenceEngine::Blob::tensorDesc
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:218
InferenceEngine::TensorDesc
This class defines Tensor description.
Definition: ie_layouts.h:153
InferenceEngine::gpu::ClImage2DBlob
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:132
InferenceEngine::gpu::D3DBufferBlob::D3DBufferBlob
D3DBufferBlob(const TensorDesc &tensorDesc)
Creates a D3DBufferBlob object with the specified dimensions and layout.
Definition: gpu_context_api_dx.hpp:66
InferenceEngine::gpu::D3DBufferBlob::Ptr
std::shared_ptr< D3DBufferBlob > Ptr
A smart pointer to the D3DBufferBlob object.
Definition: gpu_context_api_dx.hpp:60
GPU_PARAM_VALUE
#define GPU_PARAM_VALUE(name)
Shortcut for defining configuration values.
Definition: gpu_params.hpp:32
InferenceEngine::gpu::D3DSurface2DBlob::D3DSurface2DBlob
D3DSurface2DBlob(const TensorDesc &tensorDesc)
Creates a D3DSurface2DBlob object with the specified dimensions and layout.
Definition: gpu_context_api_dx.hpp:96
InferenceEngine::gpu::ClBufferBlob
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:89
InferenceEngine::RemoteBlob::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::ParamMap
std::map< std::string, Parameter > ParamMap
An std::map object containing low-level object parameters of classes that are derived from RemoteBlob...
Definition: ie_remote_context.hpp:26
InferenceEngine::gpu::D3DSurface2DBlob::plane
uint32_t plane()
Returns plane ID of underlying video decoder surface, or 0 if no video surface was shared.
Definition: gpu_context_api_dx.hpp:112
InferenceEngine::RemoteContext::Ptr
std::shared_ptr< RemoteContext > Ptr
A smart pointer to the RemoteContext object.
Definition: ie_remote_context.hpp:99
InferenceEngine::Blob::Ptr
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
GPU_PARAM_KEY
#define GPU_PARAM_KEY(name)
Shortcut for defining configuration keys.
Definition: gpu_params.hpp:27
InferenceEngine::gpu::D3DBufferBlob
This class represents an abstraction for GPU plugin remote blob which is shared with Direct3D 11 buff...
Definition: gpu_context_api_dx.hpp:55
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
InferenceEngine::gpu::D3DSurface2DBlob
This class represents an abstraction for GPU plugin remote blob which is shared with Direct3D 11 2D t...
Definition: gpu_context_api_dx.hpp:85
desc
Represents detailed information for an error.
Definition: ie_c_api.h:122
InferenceEngine::gpu::D3DContext::Ptr
std::shared_ptr< D3DContext > Ptr
A smart pointer to the D3DContext object.
Definition: gpu_context_api_dx.hpp:36
InferenceEngine::gpu::D3DSurface2DBlob::Ptr
std::shared_ptr< D3DSurface2DBlob > Ptr
A smart pointer to the D3DSurface2DBlob object.
Definition: gpu_context_api_dx.hpp:90
InferenceEngine::gpu::make_shared_blob_nv12
static Blob::Ptr make_shared_blob_nv12(size_t height, size_t width, RemoteContext::Ptr ctx, ID3D11Texture2D *nv12_surf)
This function is used to obtain a NV12 compound blob object from NV12 DXGI video decoder output....
Definition: gpu_context_api_dx.hpp:123
InferenceEngine::Precision::U8
@ U8
Definition: ie_precision.hpp:34
InferenceEngine::gpu::make_shared_blob
static Blob::Ptr make_shared_blob(const TensorDesc &desc, RemoteContext::Ptr ctx, ID3D11Buffer *buffer)
This function is used to obtain remote blob object from ID3D11Buffer.
Definition: gpu_context_api_dx.hpp:161
InferenceEngine::gpu::make_shared_context
static D3DContext::Ptr make_shared_context(Core &core, std::string deviceName, ID3D11Device *device)
This function is used to obtain remote context object from ID3D11Device.
Definition: gpu_context_api_dx.hpp:150
InferenceEngine::gpu::ClContext
This class represents an abstraction for GPU plugin remote context which is shared with OpenCL contex...
Definition: gpu_context_api_ocl.hpp:33
gpu_context_api_ocl.hpp
a header that defines wrappers for internal GPU plugin-specific OpenCL context and OpenCL shared memo...
InferenceEngine::gpu_handle_param
void * gpu_handle_param
Shortcut for defining a handle parameter.
Definition: gpu_params.hpp:20