From 7d7142ef3eaea8fbc04c23c790ec57790238ef55 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 9 Dec 2024 19:15:07 -0800 Subject: [PATCH] OpenXRUtils: implemented GetOpenXRGraphicsBindingGL on Android --- .../include/GLContextAndroid.hpp | 7 +++- .../include/RenderDeviceGLImpl.hpp | 2 +- .../interface/RenderDeviceGL.h | 22 +++++++++++-- .../src/RenderDeviceGLImpl.cpp | 10 ++++++ .../GraphicsTools/src/OpenXRUtilitiesGL.cpp | 32 ++++++++++++++++--- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp b/Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp index 3698c746c..8abdf9e74 100644 --- a/Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/GLContextAndroid.hpp @@ -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 openxr_attribs_; diff --git a/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp b/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp index 3045fd84d..94a0a93c1 100644 --- a/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/RenderDeviceGLImpl.hpp @@ -190,7 +190,7 @@ class RenderDeviceGLImpl : public RenderDeviceBase 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 diff --git a/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h b/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h index 17f21f55e..06290406a 100644 --- a/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h +++ b/Graphics/GraphicsEngineOpenGL/interface/RenderDeviceGL.h @@ -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 @@ -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. @@ -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 diff --git a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp b/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp index 09cf96ef1..d5e4387b8 100644 --- a/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp @@ -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 diff --git a/Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp b/Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp index 5d0879251..5382d29af 100644 --- a/Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp +++ b/Graphics/GraphicsTools/src/OpenXRUtilitiesGL.cpp @@ -49,6 +49,13 @@ constexpr XrStructureType XR_TYPE_SWAPCHAIN_IMAGE_GL = XR_TYPE_SWAPCHAIN_IMAGE_O #elif GLES_SUPPORTED +# if PLATFORM_ANDROID +# include +# include + +# define XR_USE_PLATFORM_ANDROID +# endif + typedef unsigned int EGLenum; # define XR_USE_GRAPHICS_API_OPENGL_ES @@ -77,11 +84,26 @@ void GetOpenXRGraphicsBindingGL(IRenderDevice* pDevice, VERIFY_EXPR(pDeviceGL != nullptr); NativeGLContextAttribsWin32 GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs(); - XrGraphicsBindingOpenGLWin32KHR& Binding = *reinterpret_cast(pDataBlob->GetDataPtr()); - Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR; - Binding.next = nullptr; - Binding.hDC = static_cast(GLCtxAttribs.hDC); - Binding.hGLRC = static_cast(GLCtxAttribs.hGLRC); + XrGraphicsBindingOpenGLWin32KHR& Binding{*pDataBlob->GetDataPtr()}; + Binding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR; + Binding.next = nullptr; + Binding.hDC = static_cast(GLCtxAttribs.hDC); + Binding.hGLRC = static_cast(GLCtxAttribs.hGLRC); + + *ppGraphicsBinding = pDataBlob.Detach(); +#elif GLES_SUPPORTED && PLATFORM_ANDROID + RefCntAutoPtr pDataBlob{DataBlobImpl::Create(sizeof(XrGraphicsBindingOpenGLESAndroidKHR))}; + + RefCntAutoPtr pDeviceGL{pDevice, IID_RenderDeviceGL}; + VERIFY_EXPR(pDeviceGL != nullptr); + NativeGLContextAttribsAndroid GLCtxAttribs = pDeviceGL->GetNativeGLContextAttribs(); + + XrGraphicsBindingOpenGLESAndroidKHR& Binding{*pDataBlob->GetDataPtr()}; + 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