Skip to content

Commit 5a6dddd

Browse files
OpenGL/Win32: enabled OpenXR support
1 parent 34cdfd2 commit 5a6dddd

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

Graphics/GraphicsEngineOpenGL/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ set(INCLUDE
4545
include/TextureViewGLImpl.hpp
4646
include/VAOCache.hpp
4747
)
48+
if(DILIGENT_USE_OPENXR)
49+
list(APPEND INCLUDE include/OpenXR_GLHelpers.hpp)
50+
endif()
4851

4952
set(INTERFACE
5053
interface/BaseInterfacesGL.h
@@ -287,6 +290,11 @@ else()
287290
)
288291
endif()
289292

293+
if(DILIGENT_USE_OPENXR)
294+
target_link_libraries(Diligent-GraphicsEngineOpenGL-static PRIVATE OpenXR::headers)
295+
target_compile_definitions(Diligent-GraphicsEngineOpenGL-static PRIVATE DILIGENT_USE_OPENXR=1)
296+
endif()
297+
290298
add_library(Diligent-GLAdapterSelector OBJECT "src/GLAdapterSelector.cpp")
291299
target_link_libraries(Diligent-GLAdapterSelector PRIVATE Diligent-BuildSettings)
292300

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
#if GL_SUPPORTED
30+
31+
# define XR_USE_GRAPHICS_API_OPENGL
32+
# include <openxr/openxr_platform.h>
33+
34+
using XrGraphicsRequirementsGL = XrGraphicsRequirementsOpenGLKHR;
35+
using PFN_xrGetGLGraphicsRequirements = PFN_xrGetOpenGLGraphicsRequirementsKHR;
36+
static constexpr char xrGetGLGraphicsRequirementsFunctionName[] = "xrGetOpenGLGraphicsRequirementsKHR";
37+
static constexpr XrStructureType XR_TYPE_GRAPHICS_REQUIREMENTS_GL = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR;
38+
39+
40+
#else GLES_SUPPORTED
41+
42+
# define XR_USE_GRAPHICS_API_OPENGL_ES
43+
# include <openxr/openxr_platform.h>
44+
45+
using XrGraphicsRequirementsGL = XrGraphicsRequirementsOpenGLESKHR;
46+
using PFN_xrGetGLGraphicsRequirements = PFN_xrGetOpenGLESGraphicsRequirementsKHR;
47+
static constexpr char xrGetGLGraphicsRequirementsFunctionName[] = "xrGetOpenGLESGraphicsRequirementsKHR";
48+
static constexpr XrStructureType XR_TYPE_GRAPHICS_REQUIREMENTS_GL = XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_ES_KHR;
49+
50+
#endif
51+
52+
namespace Diligent
53+
{
54+
55+
static Version GetOpenXRRequiredGLVersion(const OpenXRAttribs* pXR) noexcept(false)
56+
{
57+
if (pXR == nullptr || pXR->Instance == 0)
58+
return {};
59+
60+
if (pXR->GetInstanceProcAddr == nullptr)
61+
LOG_ERROR_AND_THROW("xrGetInstanceProcAddr must not be null");
62+
63+
XrInstance xrInstance = XR_NULL_HANDLE;
64+
static_assert(sizeof(xrInstance) == sizeof(pXR->Instance), "XrInstance size mismatch");
65+
memcpy(&xrInstance, &pXR->Instance, sizeof(xrInstance));
66+
67+
XrSystemId xrSystemId = XR_NULL_SYSTEM_ID;
68+
static_assert(sizeof(xrSystemId) == sizeof(pXR->SystemId), "XrSystemId size mismatch");
69+
memcpy(&xrSystemId, &pXR->SystemId, sizeof(XrSystemId));
70+
71+
PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr = reinterpret_cast<PFN_xrGetInstanceProcAddr>(pXR->GetInstanceProcAddr);
72+
PFN_xrGetGLGraphicsRequirements xrGetOpenGLGraphicsRequirements = nullptr;
73+
if (XR_FAILED(xrGetInstanceProcAddr(xrInstance, xrGetGLGraphicsRequirementsFunctionName, reinterpret_cast<PFN_xrVoidFunction*>(&xrGetOpenGLGraphicsRequirements))))
74+
{
75+
LOG_ERROR_AND_THROW("Failed to get ", xrGetGLGraphicsRequirementsFunctionName, ". Make sure that XR_KHR_opengl_enable/XR_KHR_opengl_es_enable extension is enabled.");
76+
}
77+
78+
XrGraphicsRequirementsGL xrGraphicsRequirements{XR_TYPE_GRAPHICS_REQUIREMENTS_GL};
79+
if (XR_FAILED(xrGetOpenGLGraphicsRequirements(xrInstance, xrSystemId, &xrGraphicsRequirements)))
80+
{
81+
LOG_ERROR_AND_THROW("Failed to get OpenGL graphics requirements");
82+
}
83+
84+
return Version{
85+
XR_VERSION_MAJOR(xrGraphicsRequirements.minApiVersionSupported),
86+
XR_VERSION_MINOR(xrGraphicsRequirements.minApiVersionSupported),
87+
};
88+
}
89+
90+
} // namespace Diligent

Graphics/GraphicsEngineOpenGL/src/GLContextWindows.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,6 +32,10 @@
3232
#include "GLTypeConversions.hpp"
3333
#include "GraphicsAccessories.hpp"
3434

35+
#if DILIGENT_USE_OPENXR
36+
# include "OpenXR_GLHelpers.hpp"
37+
#endif
38+
3539
namespace Diligent
3640
{
3741

@@ -42,6 +46,15 @@ GLContext::GLContext(const EngineGLCreateInfo& InitAttribs,
4246
m_Context{0},
4347
m_WindowHandleToDeviceContext{0}
4448
{
49+
#if DILIGENT_USE_OPENXR
50+
Version OpenXRRequiredGLVersion;
51+
if (InitAttribs.Window.hWnd != nullptr)
52+
{
53+
// Check OpenXR requirements when not attaching to an existing context
54+
OpenXRRequiredGLVersion = GetOpenXRRequiredGLVersion(InitAttribs.pXRAttribs);
55+
}
56+
#endif
57+
4558
int MajorVersion = 0, MinorVersion = 0;
4659
if (InitAttribs.Window.hWnd != nullptr)
4760
{
@@ -202,6 +215,14 @@ GLContext::GLContext(const EngineGLCreateInfo& InitAttribs,
202215
APIVersion = Version{static_cast<Uint32>(MajorVersion), static_cast<Uint32>(MinorVersion)};
203216
VERIFY(static_cast<int>(APIVersion.Major) == MajorVersion && static_cast<int>(APIVersion.Minor) == MinorVersion,
204217
"Not enough bits to store version number");
218+
219+
#if DILIGENT_USE_OPENXR
220+
if (InitAttribs.Window.hWnd != nullptr && OpenXRRequiredGLVersion > APIVersion)
221+
{
222+
LOG_ERROR("OpenGL version ", APIVersion.Major, '.', APIVersion.Minor, " does not meet minimum required version for OpenXR: ",
223+
OpenXRRequiredGLVersion.Major, '.', OpenXRRequiredGLVersion.Minor);
224+
}
225+
#endif
205226
}
206227

207228
GLContext::~GLContext()

0 commit comments

Comments
 (0)