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