Skip to content

Commit 5626546

Browse files
authored
[GPU] Add OPENCL extension d3d11 sharing checking (from fork) (openvinotoolkit#7856)
1 parent 4cce01c commit 5626546

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

inference-engine/tests/functional/plugin/gpu/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ if(LIBVA_FOUND)
4242
target_include_directories(${TARGET_NAME} PRIVATE ${LIBVA_INCLUDE_DIRS})
4343
target_link_libraries(${TARGET_NAME} PRIVATE ${LIBVA_LINK_LIBRARIES})
4444
endif()
45+
46+
if(WIN32)
47+
target_compile_definitions(${TARGET_NAME} PRIVATE ENABLE_DX11)
48+
target_link_libraries(${TARGET_NAME} PRIVATE d3d11 dxgi)
49+
endif()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (C) 2021 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
#include <string>
6+
#include <utility>
7+
#include <vector>
8+
#include <tuple>
9+
#include <memory>
10+
11+
#include <ie_compound_blob.h>
12+
13+
#include <gpu/gpu_config.hpp>
14+
#include <common_test_utils/test_common.hpp>
15+
#include <common_test_utils/test_constants.hpp>
16+
17+
#ifdef _WIN32
18+
#ifdef ENABLE_DX11
19+
20+
#ifndef D3D11_NO_HELPERS
21+
#define D3D11_NO_HELPERS
22+
#define D3D11_NO_HELPERS_DEFINED_CTX_UT
23+
#endif
24+
25+
#ifndef NOMINMAX
26+
#define NOMINMAX
27+
#define NOMINMAX_DEFINED_CTX_UT
28+
#endif
29+
30+
#include <gpu/gpu_context_api_dx.hpp>
31+
#include <atlbase.h>
32+
#include <d3d11.h>
33+
#include <d3d11_4.h>
34+
35+
#ifdef NOMINMAX_DEFINED_CTX_UT
36+
#undef NOMINMAX
37+
#undef NOMINMAX_DEFINED_CTX_UT
38+
#endif
39+
40+
#ifdef D3D11_NO_HELPERS_DEFINED_CTX_UT
41+
#undef D3D11_NO_HELPERS
42+
#undef D3D11_NO_HELPERS_DEFINED_CTX_UT
43+
#endif
44+
45+
using namespace ::testing;
46+
using namespace InferenceEngine;
47+
using namespace InferenceEngine::gpu;
48+
49+
class DX11RemoteCtx_Test : public CommonTestUtils::TestsCommon {
50+
protected:
51+
CComPtr<IDXGIFactory> factory;
52+
std::vector<CComPtr<IDXGIAdapter>> intel_adapters;
53+
std::vector<CComPtr<IDXGIAdapter>> other_adapters;
54+
55+
void SetUp() override {
56+
IDXGIFactory* out_factory = nullptr;
57+
HRESULT err = CreateDXGIFactory(__uuidof(IDXGIFactory),
58+
reinterpret_cast<void**>(&out_factory));
59+
if (FAILED(err)) {
60+
throw std::runtime_error("Cannot create CreateDXGIFactory, error: " + std::to_string(HRESULT_CODE(err)));
61+
}
62+
63+
factory.Attach(out_factory);
64+
65+
UINT adapter_index = 0;
66+
const unsigned int refIntelVendorID = 0x8086;
67+
IDXGIAdapter* out_adapter = nullptr;
68+
while (factory->EnumAdapters(adapter_index, &out_adapter) != DXGI_ERROR_NOT_FOUND) {
69+
CComPtr<IDXGIAdapter> adapter(out_adapter);
70+
71+
DXGI_ADAPTER_DESC desc{};
72+
adapter->GetDesc(&desc);
73+
if (desc.VendorId == refIntelVendorID) {
74+
intel_adapters.push_back(adapter);
75+
} else {
76+
other_adapters.push_back(adapter);
77+
}
78+
++adapter_index;
79+
}
80+
}
81+
82+
std::tuple<CComPtr<ID3D11Device>, CComPtr<ID3D11DeviceContext>>
83+
create_device_with_ctx(CComPtr<IDXGIAdapter> adapter) {
84+
UINT flags = 0;
85+
D3D_FEATURE_LEVEL feature_levels[] = { D3D_FEATURE_LEVEL_11_1,
86+
D3D_FEATURE_LEVEL_11_0,
87+
};
88+
D3D_FEATURE_LEVEL featureLevel;
89+
ID3D11Device* ret_device_ptr = nullptr;
90+
ID3D11DeviceContext* ret_ctx_ptr = nullptr;
91+
HRESULT err = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
92+
nullptr, flags,
93+
feature_levels,
94+
ARRAYSIZE(feature_levels),
95+
D3D11_SDK_VERSION, &ret_device_ptr,
96+
&featureLevel, &ret_ctx_ptr);
97+
if (FAILED(err)) {
98+
throw std::runtime_error("Cannot create D3D11CreateDevice, error: " +
99+
std::to_string(HRESULT_CODE(err)));
100+
}
101+
102+
return std::make_tuple(ret_device_ptr, ret_ctx_ptr);
103+
}
104+
};
105+
106+
TEST_F(DX11RemoteCtx_Test, smoke_make_shared_context) {
107+
auto ie = InferenceEngine::Core();
108+
109+
for (auto adapter : intel_adapters) {
110+
CComPtr<ID3D11Device> device_ptr;
111+
CComPtr<ID3D11DeviceContext> ctx_ptr;
112+
113+
ASSERT_NO_THROW(std::tie(device_ptr, ctx_ptr) =
114+
create_device_with_ctx(adapter));
115+
auto remote_context = make_shared_context(ie,
116+
CommonTestUtils::DEVICE_GPU,
117+
device_ptr);
118+
ASSERT_TRUE(remote_context);
119+
}
120+
121+
for (auto adapter : other_adapters) {
122+
CComPtr<ID3D11Device> device_ptr;
123+
CComPtr<ID3D11DeviceContext> ctx_ptr;
124+
125+
ASSERT_NO_THROW(std::tie(device_ptr, ctx_ptr) =
126+
create_device_with_ctx(adapter));
127+
ASSERT_THROW(make_shared_context(ie, CommonTestUtils::DEVICE_GPU,
128+
device_ptr),
129+
std::runtime_error);
130+
}
131+
}
132+
133+
#endif // ENABLE_DX11
134+
#endif // WIN32

inference-engine/thirdparty/clDNN/runtime/ocl/ocl_device_detector.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ return true;
4444
namespace cldnn {
4545
namespace ocl {
4646
static constexpr auto INTEL_PLATFORM_VENDOR = "Intel(R) Corporation";
47+
#ifdef _WIN32
48+
static constexpr auto INTEL_D3D11_SHARING_EXT_NAME = "cl_khr_d3d11_sharing";
49+
#endif // _WIN32
4750

4851
static std::vector<cl::Device> getSubDevices(cl::Device& rootDevice) {
4952
cl_uint maxSubDevices;
@@ -205,6 +208,18 @@ std::vector<device::ptr> ocl_device_detector::create_device_list_from_user_devi
205208

206209
std::vector<cl::Device> devices;
207210
#ifdef _WIN32
211+
// From OpenCL Docs:
212+
// A non-NULL return value for clGetExtensionFunctionAddressForPlatform
213+
// does not guarantee that an extension function is actually supported
214+
// by the platform. The application must also make a corresponding query
215+
// using clGetPlatformInfo (platform, CL_PLATFORM_EXTENSIONS, …​ ) or
216+
// clGetDeviceInfo (device,CL_DEVICE_EXTENSIONS, …​ )
217+
// to determine if an extension is supported by the OpenCL implementation.
218+
const std::string& ext = platform.getInfo<CL_PLATFORM_EXTENSIONS>();
219+
if (ext.empty() || ext.find(INTEL_D3D11_SHARING_EXT_NAME) == std::string::npos) {
220+
continue;
221+
}
222+
208223
platform.getDevices(CL_D3D11_DEVICE_KHR,
209224
user_device,
210225
CL_PREFERRED_DEVICES_FOR_D3D11_KHR,

0 commit comments

Comments
 (0)