Skip to content

Commit e31d2af

Browse files
OpenXRUtils: implemented GetOpenXRGraphicsBindingGL on Win32
1 parent 192ad3e commit e31d2af

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

Graphics/GraphicsEngineOpenGL/include/GLContextWindows.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 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");
@@ -44,6 +44,9 @@ class GLContext
4444

4545
NativeGLContextType GetCurrentNativeGLContext();
4646

47+
HGLRC GetHandle() const { return m_Context; }
48+
HDC GetWindowHandleToDeviceContext() const { return m_WindowHandleToDeviceContext; }
49+
4750
private:
4851
HGLRC m_Context = NULL;
4952
HDC m_WindowHandleToDeviceContext = NULL;

Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ class RenderDeviceGLImpl : public RenderDeviceBase<EngineGLImplTraits>
190190
RESOURCE_DIMENSION Dimension,
191191
Uint32 SampleCount) const override final;
192192

193+
#if PLATFORM_WIN32
194+
virtual NativeGLContextAttribs DILIGENT_CALL_TYPE GetNativeGLContextAttribs() const override final;
195+
#endif
196+
193197
FBOCache& GetFBOCache(GLContext::NativeGLContextType Context);
194198
void OnReleaseTexture(ITexture* pTexture);
195199

Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h

Lines changed: 21 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");
@@ -48,6 +48,20 @@ static DILIGENT_CONSTEXPR INTERFACE_ID IID_RenderDeviceGL =
4848

4949
// clang-format off
5050

51+
#if PLATFORM_WIN32
52+
/// Native GL context attributes
53+
struct NativeGLContextAttribsWin32
54+
{
55+
/// Device context handle
56+
void* hDC DEFAULT_INITIALIZER(nullptr);
57+
58+
/// Rendering context handle
59+
void* hGLRC DEFAULT_INITIALIZER(nullptr);
60+
};
61+
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribsWin32;
62+
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribs;
63+
#endif
64+
5165
/// Exposes OpenGL-specific functionality of a render device.
5266
DILIGENT_BEGIN_INTERFACE(IRenderDeviceGL, IRenderDevice)
5367
{
@@ -113,6 +127,11 @@ DILIGENT_BEGIN_INTERFACE(IRenderDeviceGL, IRenderDevice)
113127
const TextureDesc REF TexDesc,
114128
RESOURCE_STATE InitialState,
115129
ITexture** ppTexture) PURE;
130+
131+
#if PLATFORM_WIN32
132+
/// Returns platform-specific GL context attributes
133+
VIRTUAL NativeGLContextAttribs METHOD(GetNativeGLContextAttribs)(THIS) CONST PURE;
134+
#endif
116135
};
117136
DILIGENT_END_INTERFACE
118137

@@ -125,6 +144,7 @@ DILIGENT_END_INTERFACE
125144
# define IRenderDeviceGL_CreateTextureFromGLHandle(This, ...)CALL_IFACE_METHOD(RenderDeviceGL, CreateTextureFromGLHandle, This, __VA_ARGS__)
126145
# define IRenderDeviceGL_CreateBufferFromGLHandle(This, ...) CALL_IFACE_METHOD(RenderDeviceGL, CreateBufferFromGLHandle, This, __VA_ARGS__)
127146
# define IRenderDeviceGL_CreateDummyTexture(This, ...) CALL_IFACE_METHOD(RenderDeviceGL, CreateDummyTexture, This, __VA_ARGS__)
147+
# define IRenderDeviceGL_GetNativeGLContextAttribs(This) CALL_IFACE_METHOD(RenderDeviceGL, GetNativeGLContextAttribs, This)
128148

129149
// clang-format on
130150

Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,4 +1732,14 @@ void RenderDeviceGLImpl::IdleGPU()
17321732
glFinish();
17331733
}
17341734

1735+
#if PLATFORM_WIN32
1736+
NativeGLContextAttribs RenderDeviceGLImpl::GetNativeGLContextAttribs() const
1737+
{
1738+
NativeGLContextAttribs Attribs;
1739+
Attribs.hDC = m_GLContext.GetWindowHandleToDeviceContext();
1740+
Attribs.hGLRC = m_GLContext.GetHandle();
1741+
return Attribs;
1742+
}
1743+
#endif
1744+
17351745
} // namespace Diligent

Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
#if GL_SUPPORTED
3333

34+
# if PLATFORM_WIN32
35+
36+
# include "WinHPreface.h"
37+
# include <Windows.h>
38+
# include <Unknwn.h>
39+
# include "WinHPostface.h"
40+
41+
# define XR_USE_PLATFORM_WIN32
42+
# endif
43+
3444
# define XR_USE_GRAPHICS_API_OPENGL
3545
# include <openxr/openxr_platform.h>
3646

@@ -60,7 +70,23 @@ void GetOpenXRGraphicsBindingGL(IRenderDevice* pDevice,
6070
IDeviceContext* pContext,
6171
IDataBlob** ppGraphicsBinding)
6272
{
63-
UNSUPPORTED("Not yet implemented");
73+
#if GL_SUPPORTED && PLATFORM_WIN32
74+
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingOpenGLWin32KHR))};
75+
76+
RefCntAutoPtr<IRenderDeviceGL> pDeviceGL{pDevice, IID_RenderDeviceGL};
77+
VERIFY_EXPR(pDeviceGL != nullptr);
78+
NativeGLContextAttribsWin32 GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs();
79+
80+
XrGraphicsBindingOpenGLWin32KHR& Binding = *reinterpret_cast<XrGraphicsBindingOpenGLWin32KHR*>(pDataBlob->GetDataPtr());
81+
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
82+
Binding.next = nullptr;
83+
Binding.hDC = static_cast<HDC>(GLCtxAttribs.hDC);
84+
Binding.hGLRC = static_cast<HGLRC>(GLCtxAttribs.hGLRC);
85+
86+
*ppGraphicsBinding = pDataBlob.Detach();
87+
#else
88+
UNEXPECTED("OpenXR GL bindings are not supported on this platform. The application should initialize the bindings manually.");
89+
#endif
6490
}
6591

6692
void AllocateOpenXRSwapchainImageDataGL(Uint32 ImageCount,

0 commit comments

Comments
 (0)