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  * @param height Height of Y plane
123  * @param width Widht of Y plane
124  * @param ctx A pointer to remote context
125  * @param nv12_surf A ID3D11Texture2D instance to create NV12 blob from
126  * @return NV12 remote blob
127  */
128 static inline Blob::Ptr make_shared_blob_nv12(size_t height, size_t width, RemoteContext::Ptr ctx, ID3D11Texture2D* nv12_surf) {
129  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
130  if (nullptr == casted) {
131  THROW_IE_EXCEPTION << "Invalid remote context passed";
132  }
133 
134  // despite of layout, blob dimensions always follow in N,C,H,W order
135  TensorDesc desc(Precision::U8, { 1, 1, height, width }, Layout::NHWC);
136 
137  ParamMap blobParams = {
138  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
139  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(nv12_surf) },
140  { GPU_PARAM_KEY(VA_PLANE), uint32_t(0) }
141  };
142  Blob::Ptr y_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, blobParams));
143 
144  TensorDesc uvdesc(Precision::U8, { 1, 2, height / 2, width / 2 }, Layout::NHWC);
145  blobParams[GPU_PARAM_KEY(MEM_HANDLE)] = static_cast<gpu_handle_param>(nv12_surf);
146  blobParams[GPU_PARAM_KEY(VA_PLANE)] = uint32_t(1);
147  Blob::Ptr uv_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(uvdesc, blobParams));
148 
149  return InferenceEngine::make_shared_blob<NV12Blob>(y_blob, uv_blob);
150 }
151 
152 /**
153  * @brief This function is used to obtain remote context object from ID3D11Device
154  * @param core Inference Engine Core object instance
155  * @param deviceName A name of to create a remote context for
156  * @param device A pointer to ID3D11Device to be used to create a remote context
157  * @return A shared remote context instance
158  */
159 static inline D3DContext::Ptr make_shared_context(Core& core, std::string deviceName, ID3D11Device* device) {
160  ParamMap contextParams = {
161  { GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(VA_SHARED) },
162  { GPU_PARAM_KEY(VA_DEVICE), static_cast<gpu_handle_param>(device) }
163  };
164  return std::dynamic_pointer_cast<D3DContext>(core.CreateContext(deviceName, contextParams));
165 }
166 
167 /**
168  * @brief This function is used to obtain remote blob object from ID3D11Buffer
169  * @param desc A tensor description which describes blob configuration
170  * @param ctx A shared pointer to a remote context
171  * @param buffer A pointer to ID3D11Buffer instance to create remote blob based on
172  * @return A remote blob instance
173  */
174 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, ID3D11Buffer* buffer) {
175  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
176  if (nullptr == casted) {
177  THROW_IE_EXCEPTION << "Invalid remote context passed";
178  }
179 
180  ParamMap params = {
181  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(DX_BUFFER) },
182  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(buffer) }
183  };
184  return std::dynamic_pointer_cast<D3DBufferBlob>(casted->CreateBlob(desc, params));
185 }
186 
187 /**
188  * @brief This function is used to obtain remote blob object from ID3D11Texture2D
189  * @param desc Tensor description
190  * @param ctx the RemoteContext object whuch owns context for the blob to be created
191  * @param surface Pointer to ID3D11Texture2D interface of the objects that owns NV12 texture
192  * @param plane ID of the plane to be shared (0 or 1)
193  * @return Smart pointer to created RemoteBlob object cast to base class
194  * @note The underlying ID3D11Texture2D can also be a plane of output surface of DXGI video decoder
195  */
196 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, ID3D11Texture2D* surface, uint32_t plane = 0) {
197  auto casted = std::dynamic_pointer_cast<D3DContext>(ctx);
198  if (nullptr == casted) {
199  THROW_IE_EXCEPTION << "Invalid remote context passed";
200  }
201  ParamMap params = {
202  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
203  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), static_cast<gpu_handle_param>(surface) },
204  { GPU_PARAM_KEY(VA_PLANE), plane }
205  };
206  return std::dynamic_pointer_cast<D3DSurface2DBlob>(casted->CreateBlob(desc, params));
207 }
208 
209 } // namespace gpu
210 } // namespace InferenceEngine
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:227
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
This class represents Inference Engine Core entity.
Definition: ie_core.hpp:29
@ U8
Definition: ie_precision.hpp:34
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
std::shared_ptr< RemoteContext > Ptr
A smart pointer to the RemoteContext object.
Definition: ie_remote_context.hpp:99
This class defines Tensor description.
Definition: ie_layouts.h:158
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:89
This class represents an abstraction for GPU plugin remote context which is shared with OpenCL contex...
Definition: gpu_context_api_ocl.hpp:33
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:132
This class represents an abstraction for GPU plugin remote blob which is shared with Direct3D 11 buff...
Definition: gpu_context_api_dx.hpp:55
std::shared_ptr< D3DBufferBlob > Ptr
A smart pointer to the D3DBufferBlob object.
Definition: gpu_context_api_dx.hpp:60
D3DBufferBlob(const TensorDesc &tensorDesc)
Creates a D3DBufferBlob object with the specified dimensions and layout.
Definition: gpu_context_api_dx.hpp:66
This class represents an abstraction for GPU plugin remote context which is shared with Direct3D 11 d...
Definition: gpu_context_api_dx.hpp:31
std::shared_ptr< D3DContext > Ptr
A smart pointer to the D3DContext object.
Definition: gpu_context_api_dx.hpp:36
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
std::shared_ptr< D3DSurface2DBlob > Ptr
A smart pointer to the D3DSurface2DBlob object.
Definition: gpu_context_api_dx.hpp:90
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
D3DSurface2DBlob(const TensorDesc &tensorDesc)
Creates a D3DSurface2DBlob object with the specified dimensions and layout.
Definition: gpu_context_api_dx.hpp:96
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:159
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:128
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:174
a header that defines wrappers for internal GPU plugin-specific OpenCL context and OpenCL shared memo...
void * gpu_handle_param
Shortcut for defining a handle parameter.
Definition: gpu_params.hpp:20
#define GPU_PARAM_KEY(name)
Shortcut for defining configuration keys.
Definition: gpu_params.hpp:27
#define GPU_PARAM_VALUE(name)
Shortcut for defining configuration values.
Definition: gpu_params.hpp:32
@ NHWC
"NHWC" layout
Definition: ie_c_api.h:144
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
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
Represents detailed information for an error.
Definition: ie_c_api.h:122