Skip to content

Commit adc3da7

Browse files
OpenXRUtils: implemented GetOpenXRGraphicsBindingGL on Android
1 parent ba94953 commit adc3da7

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,18 @@ class GLContext
6565
int32_t GetScreenWidth() const { return screen_width_; }
6666
int32_t GetScreenHeight() const { return screen_height_; }
6767

68+
EGLDisplay GetDisplay() const { return display_; }
69+
EGLSurface GetSurface() const { return surface_; }
70+
EGLContext GetEGLCtx() const { return context_; }
71+
EGLConfig GetConfig() const { return config_; }
72+
6873
private:
6974
//EGL configurations
7075
ANativeWindow* window_ = nullptr;
7176
EGLDisplay display_ = EGL_NO_DISPLAY;
7277
EGLSurface surface_ = EGL_NO_SURFACE;
7378
EGLContext context_ = EGL_NO_CONTEXT;
74-
EGLConfig config_;
79+
EGLConfig config_ = nullptr;
7580

7681
#if DILIGENT_USE_OPENXR
7782
std::unique_ptr<OpenXRAttribs> openxr_attribs_;

Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class RenderDeviceGLImpl : public RenderDeviceBase<EngineGLImplTraits>
190190
RESOURCE_DIMENSION Dimension,
191191
Uint32 SampleCount) const override final;
192192

193-
#if PLATFORM_WIN32
193+
#if PLATFORM_WIN32 || PLATFORM_ANDROID
194194
virtual NativeGLContextAttribs DILIGENT_CALL_TYPE GetNativeGLContextAttribs() const override final;
195195
#endif
196196

Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static DILIGENT_CONSTEXPR INTERFACE_ID IID_RenderDeviceGL =
4949
// clang-format off
5050

5151
#if PLATFORM_WIN32
52-
/// Native GL context attributes
52+
/// Win32 native GL context attributes
5353
struct NativeGLContextAttribsWin32
5454
{
5555
/// Device context handle
@@ -60,6 +60,24 @@ struct NativeGLContextAttribsWin32
6060
};
6161
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribsWin32;
6262
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribs;
63+
#elif PLATFORM_ANDROID
64+
/// Android native GL context attributes
65+
struct NativeGLContextAttribsAndroid
66+
{
67+
/// EGL display
68+
void* Display DEFAULT_INITIALIZER(nullptr);
69+
70+
// EGL surface
71+
void* Surface DEFAULT_INITIALIZER(nullptr);
72+
73+
/// EGL context
74+
void* Context DEFAULT_INITIALIZER(nullptr);
75+
76+
/// EGL config
77+
void* Config DEFAULT_INITIALIZER(nullptr);
78+
};
79+
typedef struct NativeGLContextAttribsAndroid NativeGLContextAttribsAndroid;
80+
typedef struct NativeGLContextAttribsAndroid NativeGLContextAttribs;
6381
#endif
6482

6583
/// Exposes OpenGL-specific functionality of a render device.
@@ -128,7 +146,7 @@ DILIGENT_BEGIN_INTERFACE(IRenderDeviceGL, IRenderDevice)
128146
RESOURCE_STATE InitialState,
129147
ITexture** ppTexture) PURE;
130148

131-
#if PLATFORM_WIN32
149+
#if PLATFORM_WIN32 || PLATFORM_ANDROID
132150
/// Returns platform-specific GL context attributes
133151
VIRTUAL NativeGLContextAttribs METHOD(GetNativeGLContextAttribs)(THIS) CONST PURE;
134152
#endif

Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,16 @@ NativeGLContextAttribs RenderDeviceGLImpl::GetNativeGLContextAttribs() const
17401740
Attribs.hGLRC = m_GLContext.GetHandle();
17411741
return Attribs;
17421742
}
1743+
#elif PLATFORM_ANDROID
1744+
NativeGLContextAttribs RenderDeviceGLImpl::GetNativeGLContextAttribs() const
1745+
{
1746+
NativeGLContextAttribs Attribs;
1747+
Attribs.Display = m_GLContext.GetDisplay();
1748+
Attribs.Surface = m_GLContext.GetSurface();
1749+
Attribs.Context = m_GLContext.GetEGLCtx();
1750+
Attribs.Config = m_GLContext.GetConfig();
1751+
return Attribs;
1752+
}
17431753
#endif
17441754

17451755
} // namespace Diligent

Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ constexpr XrStructureType XR_TYPE_SWAPCHAIN_IMAGE_GL = XR_TYPE_SWAPCHAIN_IMAGE_O
4949

5050
#elif GLES_SUPPORTED
5151

52+
# if PLATFORM_ANDROID
53+
# include <jni.h>
54+
# include <EGL/egl.h>
55+
56+
# define XR_USE_PLATFORM_ANDROID
57+
# endif
58+
5259
typedef unsigned int EGLenum;
5360

5461
# define XR_USE_GRAPHICS_API_OPENGL_ES
@@ -77,11 +84,26 @@ void GetOpenXRGraphicsBindingGL(IRenderDevice* pDevice,
7784
VERIFY_EXPR(pDeviceGL != nullptr);
7885
NativeGLContextAttribsWin32 GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs();
7986

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);
87+
XrGraphicsBindingOpenGLWin32KHR& Binding{*pDataBlob->GetDataPtr<XrGraphicsBindingOpenGLWin32KHR>()};
88+
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
89+
Binding.next = nullptr;
90+
Binding.hDC = static_cast<HDC>(GLCtxAttribs.hDC);
91+
Binding.hGLRC = static_cast<HGLRC>(GLCtxAttribs.hGLRC);
92+
93+
*ppGraphicsBinding = pDataBlob.Detach();
94+
#elif GLES_SUPPORTED && PLATFORM_ANDROID
95+
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingOpenGLESAndroidKHR))};
96+
97+
RefCntAutoPtr<IRenderDeviceGL> pDeviceGL{pDevice, IID_RenderDeviceGL};
98+
VERIFY_EXPR(pDeviceGL != nullptr);
99+
NativeGLContextAttribsAndroid GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs();
100+
101+
XrGraphicsBindingOpenGLESAndroidKHR& Binding{*pDataBlob->GetDataPtr<XrGraphicsBindingOpenGLESAndroidKHR>()};
102+
Binding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR;
103+
Binding.next = nullptr;
104+
Binding.display = GLCtxAttribs.Display;
105+
Binding.config = GLCtxAttribs.Config;
106+
Binding.context = GLCtxAttribs.Context;
85107

86108
*ppGraphicsBinding = pDataBlob.Detach();
87109
#else

0 commit comments

Comments
 (0)