Skip to content

Commit a50a3aa

Browse files
GraphicsTools: added OpenXR utilities
1 parent 59a0d8d commit a50a3aa

File tree

7 files changed

+450
-0
lines changed

7 files changed

+450
-0
lines changed

Graphics/GraphicsTools/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(INTERFACE
1313
interface/GraphicsUtilities.h
1414
interface/MapHelper.hpp
1515
interface/OffScreenSwapChain.hpp
16+
interface/OpenXRUtilities.h
1617
interface/ResourceRegistry.hpp
1718
interface/ScopedDebugGroup.hpp
1819
interface/GPUCompletionAwaitQueue.hpp
@@ -43,6 +44,7 @@ set(SOURCE
4344
src/GraphicsUtilitiesVk.cpp
4445
src/GraphicsUtilitiesWebGPU.cpp
4546
src/OffScreenSwapChain.cpp
47+
src/OpenXRUtilities.cpp
4648
src/ScopedQueryHelper.cpp
4749
src/ScreenCapture.cpp
4850
src/ShaderSourceFactoryUtils.cpp
@@ -81,14 +83,23 @@ if(D3D11_SUPPORTED)
8183
list(APPEND SOURCE src/TextureUploaderD3D11.cpp)
8284
list(APPEND INTERFACE interface/TextureUploaderD3D11.hpp)
8385
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D11Interface)
86+
if(DILIGENT_USE_OPENXR)
87+
list(APPEND SOURCE src/OpenXRUtilitiesD3D11.cpp)
88+
endif()
8489
endif()
8590

8691
if(D3D12_SUPPORTED)
8792
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D12Interface)
93+
if(DILIGENT_USE_OPENXR)
94+
list(APPEND SOURCE src/OpenXRUtilitiesD3D12.cpp)
95+
endif()
8896
endif()
8997

9098
if(VULKAN_SUPPORTED)
9199
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVkInterface Vulkan::Headers)
100+
if(DILIGENT_USE_OPENXR)
101+
list(APPEND SOURCE src/OpenXRUtilitiesVk.cpp)
102+
endif()
92103
endif()
93104

94105
if(D3D12_SUPPORTED OR VULKAN_SUPPORTED)
@@ -100,6 +111,9 @@ if(GL_SUPPORTED OR GLES_SUPPORTED)
100111
list(APPEND SOURCE src/TextureUploaderGL.cpp)
101112
list(APPEND INTERFACE interface/TextureUploaderGL.hpp)
102113
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGLInterface)
114+
if(DILIGENT_USE_OPENXR)
115+
list(APPEND SOURCE src/OpenXRUtilitiesGL.cpp)
116+
endif()
103117
endif()
104118

105119
if(WEBGPU_SUPPORTED)
@@ -151,6 +165,11 @@ if(RENDER_STATE_CACHE_SUPPORTED)
151165
target_compile_definitions(Diligent-GraphicsTools PUBLIC DILIGENT_RENDER_STATE_CACHE_SUPPORTED=1)
152166
endif()
153167

168+
if(DILIGENT_USE_OPENXR)
169+
target_link_libraries(Diligent-GraphicsTools PRIVATE OpenXR::headers)
170+
target_compile_definitions(Diligent-GraphicsTools PUBLIC DILIGENT_USE_OPENXR=1)
171+
endif()
172+
154173
if(D3D11_SUPPORTED OR D3D12_SUPPORTED)
155174
target_link_libraries(Diligent-GraphicsTools
156175
PRIVATE
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#pragma once
28+
29+
/// \file
30+
/// OpenXR utilities
31+
32+
#include "../../GraphicsEngine/interface/RenderDevice.h"
33+
#include "../../GraphicsEngine/interface/DeviceContext.h"
34+
#include "../../Primitives/interface/DataBlob.h"
35+
36+
DILIGENT_BEGIN_NAMESPACE(Diligent)
37+
38+
#include "../../../Primitives/interface/DefineRefMacro.h"
39+
40+
/// Prepares OpenXR graphics binding for the specified device and context.
41+
///
42+
/// \param [in] pDevice - Pointer to the render device.
43+
/// \param [in] pContext - Pointer to the device context.
44+
/// \param [out] ppGraphicsBinding - Address of the memory location where the pointer to the data blob
45+
/// containing the graphics binding will be stored.
46+
///
47+
/// \remarks The function returns the data blob that contains the OpenXR graphics binding structure
48+
/// (XrGraphicsBindingVulkanKHR, XrGraphicsBindingD3D11KHR, etc.).
49+
/// The data blob should be used to create the OpenXR session, for example:
50+
///
51+
/// RefCntAutoPtr<IDataBlob> pGraphicsBinding;
52+
/// GetOpenXRGraphicsBinding(m_pDevice, m_pImmediateContext, &pGraphicsBinding);
53+
///
54+
/// XrSessionCreateInfo sessionCI{XR_TYPE_SESSION_CREATE_INFO};
55+
/// sessionCI.next = pGraphicsBinding->GetConstDataPtr();
56+
/// sessionCI.systemId = m_SystemId;
57+
/// xrCreateSession(m_xrInstance, &sessionCI, &m_xrSession);
58+
///
59+
void DILIGENT_GLOBAL_FUNCTION(GetOpenXRGraphicsBinding)(IRenderDevice* pDevice,
60+
IDeviceContext* pContext,
61+
IDataBlob** ppGraphicsBinding);
62+
63+
#include "../../../Primitives/interface/UndefRefMacro.h"
64+
65+
DILIGENT_END_NAMESPACE // namespace Diligent
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "OpenXRUtilities.h"
28+
29+
#include "DebugUtilities.hpp"
30+
31+
namespace Diligent
32+
{
33+
34+
#if DILIGENT_USE_OPENXR
35+
36+
# if D3D11_SUPPORTED
37+
void GetOpenXRGraphicsBindingD3D11(IRenderDevice* pDevice,
38+
IDeviceContext* pContext,
39+
IDataBlob** ppGraphicsBinding);
40+
# endif
41+
42+
# if D3D12_SUPPORTED
43+
void GetOpenXRGraphicsBindingD3D12(IRenderDevice* pDevice,
44+
IDeviceContext* pContext,
45+
IDataBlob** ppGraphicsBinding);
46+
# endif
47+
48+
# if GL_SUPPORTED || GLES_SUPPORTED
49+
void GetOpenXRGraphicsBindingGL(IRenderDevice* pDevice,
50+
IDeviceContext* pContext,
51+
IDataBlob** ppGraphicsBinding);
52+
# endif
53+
54+
# if VULKAN_SUPPORTED
55+
void GetOpenXRGraphicsBindingVk(IRenderDevice* pDevice,
56+
IDeviceContext* pContext,
57+
IDataBlob** ppGraphicsBinding);
58+
# endif
59+
60+
#endif
61+
62+
void GetOpenXRGraphicsBinding(IRenderDevice* pDevice,
63+
IDeviceContext* pContext,
64+
IDataBlob** ppGraphicsBinding)
65+
{
66+
#if DILIGENT_USE_OPENXR
67+
if (pDevice == nullptr)
68+
{
69+
UNEXPECTED("pDevice must not be null");
70+
return;
71+
}
72+
73+
if (pContext == nullptr)
74+
{
75+
UNEXPECTED("pContext must not be null");
76+
return;
77+
}
78+
79+
if (ppGraphicsBinding == nullptr)
80+
{
81+
UNEXPECTED("ppGraphicsBinding must not be null");
82+
return;
83+
}
84+
85+
RENDER_DEVICE_TYPE DevType = pDevice->GetDeviceInfo().Type;
86+
switch (DevType)
87+
{
88+
# if D3D11_SUPPORTED
89+
case RENDER_DEVICE_TYPE_D3D11:
90+
GetOpenXRGraphicsBindingD3D11(pDevice, pContext, ppGraphicsBinding);
91+
break;
92+
# endif
93+
94+
# if D3D12_SUPPORTED
95+
case RENDER_DEVICE_TYPE_D3D12:
96+
GetOpenXRGraphicsBindingD3D12(pDevice, pContext, ppGraphicsBinding);
97+
break;
98+
# endif
99+
100+
# if GL_SUPPORTED || GLES_SUPPORTED
101+
case RENDER_DEVICE_TYPE_GL:
102+
case RENDER_DEVICE_TYPE_GLES:
103+
GetOpenXRGraphicsBindingGL(pDevice, pContext, ppGraphicsBinding);
104+
break;
105+
# endif
106+
107+
# if VULKAN_SUPPORTED
108+
case RENDER_DEVICE_TYPE_VULKAN:
109+
GetOpenXRGraphicsBindingVk(pDevice, pContext, ppGraphicsBinding);
110+
break;
111+
# endif
112+
113+
default:
114+
UNSUPPORTED("Unsupported device type");
115+
}
116+
#else
117+
UNSUPPORTED("OpenXR is not supported");
118+
#endif
119+
}
120+
121+
} // namespace Diligent
122+
123+
extern "C"
124+
{
125+
void Diligent_GetOpenXRGraphicsBinding(Diligent::IRenderDevice* pDevice,
126+
Diligent::IDeviceContext* pContext,
127+
Diligent::IDataBlob** ppGraphicsBinding)
128+
{
129+
Diligent::GetOpenXRGraphicsBinding(pDevice, pContext, ppGraphicsBinding);
130+
}
131+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "OpenXRUtilities.h"
28+
29+
#include "DebugUtilities.hpp"
30+
#include "DataBlobImpl.hpp"
31+
32+
#include "WinHPreface.h"
33+
#include <d3d11.h>
34+
#include <atlbase.h>
35+
#include "WinHPostface.h"
36+
37+
#include "RenderDeviceD3D11.h"
38+
39+
#define XR_USE_GRAPHICS_API_D3D11
40+
#include <openxr/openxr_platform.h>
41+
42+
namespace Diligent
43+
{
44+
45+
void GetOpenXRGraphicsBindingD3D11(IRenderDevice* pDevice,
46+
IDeviceContext* pContext,
47+
IDataBlob** ppGraphicsBinding)
48+
{
49+
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingD3D11KHR))};
50+
51+
RefCntAutoPtr<IRenderDeviceD3D11> pDeviceD3D11{pDevice, IID_RenderDeviceD3D11};
52+
VERIFY_EXPR(pDeviceD3D11 != nullptr);
53+
54+
XrGraphicsBindingD3D11KHR& Binding = *reinterpret_cast<XrGraphicsBindingD3D11KHR*>(pDataBlob->GetDataPtr());
55+
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
56+
Binding.next = nullptr;
57+
Binding.device = pDeviceD3D11->GetD3D11Device();
58+
59+
*ppGraphicsBinding = pDataBlob.Detach();
60+
}
61+
62+
} // namespace Diligent
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "OpenXRUtilities.h"
28+
29+
#include "DebugUtilities.hpp"
30+
#include "DataBlobImpl.hpp"
31+
32+
#include "WinHPreface.h"
33+
#include <d3d12.h>
34+
#include <atlbase.h>
35+
#include "WinHPostface.h"
36+
37+
#include "RenderDeviceD3D12.h"
38+
#include "CommandQueueD3D12.h"
39+
40+
41+
#define XR_USE_GRAPHICS_API_D3D12
42+
#include <openxr/openxr_platform.h>
43+
44+
namespace Diligent
45+
{
46+
47+
void GetOpenXRGraphicsBindingD3D12(IRenderDevice* pDevice,
48+
IDeviceContext* pContext,
49+
IDataBlob** ppGraphicsBinding)
50+
{
51+
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingD3D12KHR))};
52+
53+
RefCntAutoPtr<IRenderDeviceD3D12> pDeviceD3D12{pDevice, IID_RenderDeviceD3D12};
54+
VERIFY_EXPR(pDeviceD3D12 != nullptr);
55+
RefCntAutoPtr<ICommandQueueD3D12> pQueueD3D12{pContext->LockCommandQueue(), IID_CommandQueueD3D12};
56+
VERIFY_EXPR(pQueueD3D12 != nullptr);
57+
58+
XrGraphicsBindingD3D12KHR& Binding = *reinterpret_cast<XrGraphicsBindingD3D12KHR*>(pDataBlob->GetDataPtr());
59+
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D12_KHR;
60+
Binding.next = nullptr;
61+
Binding.device = pDeviceD3D12->GetD3D12Device();
62+
Binding.queue = pQueueD3D12->GetD3D12CommandQueue();
63+
64+
*ppGraphicsBinding = pDataBlob.Detach();
65+
}
66+
67+
} // namespace Diligent

0 commit comments

Comments
 (0)