Skip to content

Commit

Permalink
OpenXRUtils: implemented GetOpenXRGraphicsBindingGL on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 16, 2024
1 parent 72da691 commit 7d7142e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ class GLContext
int32_t GetScreenWidth() const { return screen_width_; }
int32_t GetScreenHeight() const { return screen_height_; }

EGLDisplay GetDisplay() const { return display_; }
EGLSurface GetSurface() const { return surface_; }
EGLContext GetEGLCtx() const { return context_; }
EGLConfig GetConfig() const { return config_; }

private:
//EGL configurations
ANativeWindow* window_ = nullptr;
EGLDisplay display_ = EGL_NO_DISPLAY;
EGLSurface surface_ = EGL_NO_SURFACE;
EGLContext context_ = EGL_NO_CONTEXT;
EGLConfig config_;
EGLConfig config_ = nullptr;

#if DILIGENT_USE_OPENXR
std::unique_ptr<OpenXRAttribs> openxr_attribs_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class RenderDeviceGLImpl : public RenderDeviceBase<EngineGLImplTraits>
RESOURCE_DIMENSION Dimension,
Uint32 SampleCount) const override final;

#if PLATFORM_WIN32
#if PLATFORM_WIN32 || PLATFORM_ANDROID
virtual NativeGLContextAttribs DILIGENT_CALL_TYPE GetNativeGLContextAttribs() const override final;
#endif

Expand Down
22 changes: 20 additions & 2 deletions Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static DILIGENT_CONSTEXPR INTERFACE_ID IID_RenderDeviceGL =
// clang-format off

#if PLATFORM_WIN32
/// Native GL context attributes
/// Win32 native GL context attributes
struct NativeGLContextAttribsWin32
{
/// Device context handle
Expand All @@ -60,6 +60,24 @@ struct NativeGLContextAttribsWin32
};
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribsWin32;
typedef struct NativeGLContextAttribsWin32 NativeGLContextAttribs;
#elif PLATFORM_ANDROID
/// Android native GL context attributes
struct NativeGLContextAttribsAndroid
{
/// EGL display
void* Display DEFAULT_INITIALIZER(nullptr);

// EGL surface
void* Surface DEFAULT_INITIALIZER(nullptr);

/// EGL context
void* Context DEFAULT_INITIALIZER(nullptr);

/// EGL config
void* Config DEFAULT_INITIALIZER(nullptr);
};
typedef struct NativeGLContextAttribsAndroid NativeGLContextAttribsAndroid;
typedef struct NativeGLContextAttribsAndroid NativeGLContextAttribs;
#endif

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

#if PLATFORM_WIN32
#if PLATFORM_WIN32 || PLATFORM_ANDROID
/// Returns platform-specific GL context attributes
VIRTUAL NativeGLContextAttribs METHOD(GetNativeGLContextAttribs)(THIS) CONST PURE;
#endif
Expand Down
10 changes: 10 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,16 @@ NativeGLContextAttribs RenderDeviceGLImpl::GetNativeGLContextAttribs() const
Attribs.hGLRC = m_GLContext.GetHandle();
return Attribs;
}
#elif PLATFORM_ANDROID
NativeGLContextAttribs RenderDeviceGLImpl::GetNativeGLContextAttribs() const
{
NativeGLContextAttribs Attribs;
Attribs.Display = m_GLContext.GetDisplay();
Attribs.Surface = m_GLContext.GetSurface();
Attribs.Context = m_GLContext.GetEGLCtx();
Attribs.Config = m_GLContext.GetConfig();
return Attribs;
}
#endif

} // namespace Diligent
32 changes: 27 additions & 5 deletions Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ constexpr XrStructureType XR_TYPE_SWAPCHAIN_IMAGE_GL = XR_TYPE_SWAPCHAIN_IMAGE_O

#elif GLES_SUPPORTED

# if PLATFORM_ANDROID
# include <jni.h>
# include <EGL/egl.h>

# define XR_USE_PLATFORM_ANDROID
# endif

typedef unsigned int EGLenum;

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

XrGraphicsBindingOpenGLWin32KHR& Binding = *reinterpret_cast<XrGraphicsBindingOpenGLWin32KHR*>(pDataBlob->GetDataPtr());
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
Binding.next = nullptr;
Binding.hDC = static_cast<HDC>(GLCtxAttribs.hDC);
Binding.hGLRC = static_cast<HGLRC>(GLCtxAttribs.hGLRC);
XrGraphicsBindingOpenGLWin32KHR& Binding{*pDataBlob->GetDataPtr<XrGraphicsBindingOpenGLWin32KHR>()};
Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
Binding.next = nullptr;
Binding.hDC = static_cast<HDC>(GLCtxAttribs.hDC);
Binding.hGLRC = static_cast<HGLRC>(GLCtxAttribs.hGLRC);

*ppGraphicsBinding = pDataBlob.Detach();
#elif GLES_SUPPORTED && PLATFORM_ANDROID
RefCntAutoPtr<DataBlobImpl> pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingOpenGLESAndroidKHR))};

RefCntAutoPtr<IRenderDeviceGL> pDeviceGL{pDevice, IID_RenderDeviceGL};
VERIFY_EXPR(pDeviceGL != nullptr);
NativeGLContextAttribsAndroid GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs();

XrGraphicsBindingOpenGLESAndroidKHR& Binding{*pDataBlob->GetDataPtr<XrGraphicsBindingOpenGLESAndroidKHR>()};
Binding.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR;
Binding.next = nullptr;
Binding.display = GLCtxAttribs.Display;
Binding.config = GLCtxAttribs.Config;
Binding.context = GLCtxAttribs.Context;

*ppGraphicsBinding = pDataBlob.Detach();
#else
Expand Down

0 comments on commit 7d7142e

Please sign in to comment.