Skip to content

Commit d8451c5

Browse files
D3D12: enabled OpenXR support
1 parent a7b8722 commit d8451c5

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

Graphics/GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool CheckAdapterD3D11Compatibility(IDXGIAdapter1* pDXGIAdapter, D3D_FEATURE_LEV
7070
}
7171

7272
#if DILIGENT_USE_OPENXR
73-
void GetOpenXRAdapterRequirements(const OpenXRAttribs& XR, LUID& AdapterLUID, D3D_FEATURE_LEVEL& d3dFeatureLevel) noexcept(false)
73+
static void GetOpenXRAdapterRequirements(const OpenXRAttribs& XR, LUID& AdapterLUID, D3D_FEATURE_LEVEL& d3dFeatureLevel) noexcept(false)
7474
{
7575
if (XR.Instance == 0)
7676
return;

Graphics/GraphicsEngineD3D12/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ if(NOT ${USE_D3D12_LOADER})
206206
target_link_libraries(Diligent-GraphicsEngineD3D12-static PRIVATE d3d12.lib)
207207
endif()
208208

209+
if(DILIGENT_USE_OPENXR)
210+
target_link_libraries(Diligent-GraphicsEngineD3D12-static PRIVATE OpenXR::headers)
211+
target_compile_definitions(Diligent-GraphicsEngineD3D12-static PRIVATE DILIGENT_USE_OPENXR=1)
212+
endif()
213+
209214
target_link_libraries(Diligent-GraphicsEngineD3D12-shared
210215
PRIVATE
211216
Diligent-BuildSettings

Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
#include <dxgi1_4.h>
5252
#include "WinHPostface.h"
5353

54+
#if DILIGENT_USE_OPENXR
55+
# define XR_USE_GRAPHICS_API_D3D12
56+
# include <openxr/openxr_platform.h>
57+
#endif
58+
5459
namespace Diligent
5560
{
5661

@@ -61,6 +66,41 @@ bool CheckAdapterD3D12Compatibility(IDXGIAdapter1* pDXGIAdapter,
6166
return SUCCEEDED(hr);
6267
}
6368

69+
#if DILIGENT_USE_OPENXR
70+
static void GetOpenXRAdapterRequirements(const OpenXRAttribs& XR, LUID& AdapterLUID, D3D_FEATURE_LEVEL& d3dFeatureLevel) noexcept(false)
71+
{
72+
if (XR.Instance == 0)
73+
return;
74+
75+
if (XR.GetInstanceProcAddr == nullptr)
76+
LOG_ERROR_AND_THROW("xrGetInstanceProcAddr must not be null");
77+
78+
XrInstance xrInstance = XR_NULL_HANDLE;
79+
static_assert(sizeof(xrInstance) == sizeof(XR.Instance), "XrInstance size mismatch");
80+
memcpy(&xrInstance, &XR.Instance, sizeof(xrInstance));
81+
82+
XrSystemId xrSystemId = XR_NULL_SYSTEM_ID;
83+
static_assert(sizeof(xrSystemId) == sizeof(XR.SystemId), "XrSystemId size mismatch");
84+
memcpy(&xrSystemId, &XR.SystemId, sizeof(XrSystemId));
85+
86+
PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr = reinterpret_cast<PFN_xrGetInstanceProcAddr>(XR.GetInstanceProcAddr);
87+
PFN_xrGetD3D12GraphicsRequirementsKHR xrGetD3D12GraphicsRequirementsKHR = nullptr;
88+
if (XR_FAILED(xrGetInstanceProcAddr(xrInstance, "xrGetD3D12GraphicsRequirementsKHR", reinterpret_cast<PFN_xrVoidFunction*>(&xrGetD3D12GraphicsRequirementsKHR))))
89+
{
90+
LOG_ERROR_AND_THROW("Failed to get xrGetD3D12GraphicsRequirementsKHR. Make sure that XR_KHR_D3D12_enable extension is enabled.");
91+
}
92+
93+
XrGraphicsRequirementsD3D12KHR xrGraphicsRequirementsD3D12KHR{XR_TYPE_GRAPHICS_REQUIREMENTS_D3D12_KHR};
94+
if (XR_FAILED(xrGetD3D12GraphicsRequirementsKHR(xrInstance, xrSystemId, &xrGraphicsRequirementsD3D12KHR)))
95+
{
96+
LOG_ERROR_AND_THROW("Failed to get D3D12 graphics requirements");
97+
}
98+
99+
AdapterLUID = xrGraphicsRequirementsD3D12KHR.adapterLuid;
100+
d3dFeatureLevel = (std::max)(d3dFeatureLevel, xrGraphicsRequirementsD3D12KHR.minFeatureLevel);
101+
}
102+
#endif
103+
64104
/// Engine factory for D3D12 implementation
65105
class EngineFactoryD3D12Impl : public EngineFactoryD3DBase<IEngineFactoryD3D12, RENDER_DEVICE_TYPE_D3D12>
66106
{
@@ -296,24 +336,38 @@ void EngineFactoryD3D12Impl::CreateDeviceAndContextsD3D12(const EngineD3D12Creat
296336
HRESULT hr = CreateDXGIFactory1(__uuidof(factory), reinterpret_cast<void**>(static_cast<IDXGIFactory4**>(&factory)));
297337
CHECK_D3D_RESULT_THROW(hr, "Failed to create DXGI factory");
298338

339+
LUID AdapterLUID{};
299340
// Direct3D12 does not allow feature levels below 11.0 (D3D12CreateDevice fails to create a device).
300-
const auto MinimumFeatureLevel = Version::Max(EngineCI.GraphicsAPIVersion, Version{11, 0});
341+
D3D_FEATURE_LEVEL MinFeatureLevel = GetD3DFeatureLevel((std::max)(EngineCI.GraphicsAPIVersion, Version{11, 0}));
342+
Uint32 AdapterId = EngineCI.AdapterId;
343+
#if DILIGENT_USE_OPENXR
344+
if (EngineCI.pXRAttribs != nullptr && EngineCI.pXRAttribs->Instance != 0)
345+
{
346+
GetOpenXRAdapterRequirements(*EngineCI.pXRAttribs, AdapterLUID, MinFeatureLevel);
347+
if (AdapterId != DEFAULT_ADAPTER_ID)
348+
{
349+
LOG_WARNING_MESSAGE("AdapterId is ignored when OpenXR is used as the suitable adapter is selected by OpenXR runtime");
350+
}
351+
// There should be only one adapter
352+
AdapterId = 0;
353+
}
354+
#endif
301355

302356
CComPtr<IDXGIAdapter1> hardwareAdapter;
303-
if (EngineCI.AdapterId == DEFAULT_ADAPTER_ID)
357+
if (AdapterId == DEFAULT_ADAPTER_ID)
304358
{
305-
GetHardwareAdapter(factory, &hardwareAdapter, GetD3DFeatureLevel(MinimumFeatureLevel));
359+
GetHardwareAdapter(factory, &hardwareAdapter, MinFeatureLevel);
306360
if (hardwareAdapter == nullptr)
307361
LOG_ERROR_AND_THROW("No suitable hardware adapter found");
308362
}
309363
else
310364
{
311-
auto Adapters = FindCompatibleAdapters(MinimumFeatureLevel);
312-
if (EngineCI.AdapterId < Adapters.size())
313-
hardwareAdapter = Adapters[EngineCI.AdapterId];
365+
auto Adapters = FindCompatibleAdapters(MinFeatureLevel, AdapterLUID);
366+
if (AdapterId < Adapters.size())
367+
hardwareAdapter = Adapters[AdapterId];
314368
else
315369
{
316-
LOG_ERROR_AND_THROW(EngineCI.AdapterId, " is not a valid hardware adapter id. Total number of compatible adapters available on this system: ", Adapters.size());
370+
LOG_ERROR_AND_THROW(AdapterId, " is not a valid hardware adapter id. Total number of compatible adapters available on this system: ", Adapters.size());
317371
}
318372
}
319373

0 commit comments

Comments
 (0)