From 4fcc69242ecabaa6bab4a070ff88b877a1bc7c6c Mon Sep 17 00:00:00 2001 From: Lubos Date: Thu, 9 Mar 2023 20:42:49 +0100 Subject: [PATCH] OpenXR - Foveation implemented (Quest only) --- Common/VR/PPSSPPVR.cpp | 1 + Common/VR/VRBase.cpp | 9 ++++++ Common/VR/VRBase.h | 1 + Common/VR/VRFramebuffer.cpp | 59 +++++++++++++++++++++++++++++++++++++ Common/VR/VRFramebuffer.h | 3 ++ Common/VR/VRRenderer.cpp | 5 ++++ 6 files changed, 78 insertions(+) diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index 657f3e1420..f39f1bcc1d 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -153,6 +153,7 @@ void InitVROnAndroid(void* vm, void* activity, const char* system, int version, VR_SetPlatformFLag(VR_PLATFORM_EXTENSION_INSTANCE, true); } else if ((strcmp(vendor, "META") == 0) || (strcmp(vendor, "OCULUS") == 0)) { VR_SetPlatformFLag(VR_PLATFORM_CONTROLLER_QUEST, true); + VR_SetPlatformFLag(VR_PLATFORM_EXTENSION_FOVEATION, true); VR_SetPlatformFLag(VR_PLATFORM_EXTENSION_PERFORMANCE, true); } VR_SetPlatformFLag(VR_PLATFORM_RENDERER_VULKAN, (GPUBackend)g_Config.iGPUBackend == GPUBackend::VULKAN); diff --git a/Common/VR/VRBase.cpp b/Common/VR/VRBase.cpp index 6b795f4245..715ea6644e 100644 --- a/Common/VR/VRBase.cpp +++ b/Common/VR/VRBase.cpp @@ -49,6 +49,15 @@ void VR_Init( void* system, const char* name, int version ) { } extensions.push_back(XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME); #ifdef ANDROID + if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { + extensions.push_back(XR_FB_SWAPCHAIN_UPDATE_STATE_EXTENSION_NAME); + extensions.push_back(XR_FB_SWAPCHAIN_UPDATE_STATE_OPENGL_ES_EXTENSION_NAME); + extensions.push_back(XR_FB_FOVEATION_EXTENSION_NAME); + extensions.push_back(XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME); + if (VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN)) { + extensions.push_back(XR_FB_SWAPCHAIN_UPDATE_STATE_VULKAN_EXTENSION_NAME); + } + } if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_INSTANCE)) { extensions.push_back(XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME); } diff --git a/Common/VR/VRBase.h b/Common/VR/VRBase.h index 157ae7c2b3..2d3405f8a7 100644 --- a/Common/VR/VRBase.h +++ b/Common/VR/VRBase.h @@ -120,6 +120,7 @@ typedef struct { enum VRPlatformFlag { VR_PLATFORM_CONTROLLER_PICO, VR_PLATFORM_CONTROLLER_QUEST, + VR_PLATFORM_EXTENSION_FOVEATION, VR_PLATFORM_EXTENSION_INSTANCE, VR_PLATFORM_EXTENSION_PERFORMANCE, VR_PLATFORM_RENDERER_VULKAN, diff --git a/Common/VR/VRFramebuffer.cpp b/Common/VR/VRFramebuffer.cpp index 0450712114..4f53b9eb25 100644 --- a/Common/VR/VRFramebuffer.cpp +++ b/Common/VR/VRFramebuffer.cpp @@ -105,6 +105,15 @@ static bool ovrFramebuffer_CreateGLES(XrSession session, ovrFramebuffer* frameBu swapChainCreateInfo.mipCount = 1; swapChainCreateInfo.arraySize = multiview ? 2 : 1; +#ifdef ANDROID + if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { + XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; + memset(&swapChainFoveationCreateInfo, 0, sizeof(swapChainFoveationCreateInfo)); + swapChainFoveationCreateInfo.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB; + swapChainCreateInfo.next = &swapChainFoveationCreateInfo; + } +#endif + frameBuffer->ColorSwapChain.Width = swapChainCreateInfo.width; frameBuffer->ColorSwapChain.Height = swapChainCreateInfo.height; @@ -198,6 +207,15 @@ static bool ovrFramebuffer_CreateVK(XrSession session, ovrFramebuffer* frameBuff swapChainCreateInfo.mipCount = 1; swapChainCreateInfo.arraySize = multiview ? 2 : 1; +#ifdef ANDROID + if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { + XrSwapchainCreateInfoFoveationFB swapChainFoveationCreateInfo; + memset(&swapChainFoveationCreateInfo, 0, sizeof(swapChainFoveationCreateInfo)); + swapChainFoveationCreateInfo.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB; + swapChainCreateInfo.next = &swapChainFoveationCreateInfo; + } +#endif + frameBuffer->ColorSwapChain.Width = swapChainCreateInfo.width; frameBuffer->ColorSwapChain.Height = swapChainCreateInfo.height; @@ -407,6 +425,47 @@ void ovrRenderer_MouseCursor(ovrRenderer* renderer, int x, int y, int sx, int sy } } +#ifdef ANDROID +void ovrRenderer_SetFoveation(XrInstance* instance, XrSession* session, ovrRenderer* renderer, XrFoveationLevelFB level, float verticalOffset, XrFoveationDynamicFB dynamic) { + PFN_xrCreateFoveationProfileFB pfnCreateFoveationProfileFB; + OXR(xrGetInstanceProcAddr(*instance, "xrCreateFoveationProfileFB", (PFN_xrVoidFunction*)(&pfnCreateFoveationProfileFB))); + + PFN_xrDestroyFoveationProfileFB pfnDestroyFoveationProfileFB; + OXR(xrGetInstanceProcAddr(*instance, "xrDestroyFoveationProfileFB", (PFN_xrVoidFunction*)(&pfnDestroyFoveationProfileFB))); + + PFN_xrUpdateSwapchainFB pfnUpdateSwapchainFB; + OXR(xrGetInstanceProcAddr(*instance, "xrUpdateSwapchainFB", (PFN_xrVoidFunction*)(&pfnUpdateSwapchainFB))); + + int instances = renderer->Multiview ? 1 : ovrMaxNumEyes; + for (int eye = 0; eye < instances; eye++) { + XrFoveationLevelProfileCreateInfoFB levelProfileCreateInfo; + memset(&levelProfileCreateInfo, 0, sizeof(levelProfileCreateInfo)); + levelProfileCreateInfo.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB; + levelProfileCreateInfo.level = level; + levelProfileCreateInfo.verticalOffset = verticalOffset; + levelProfileCreateInfo.dynamic = dynamic; + + XrFoveationProfileCreateInfoFB profileCreateInfo; + memset(&profileCreateInfo, 0, sizeof(profileCreateInfo)); + profileCreateInfo.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB; + profileCreateInfo.next = &levelProfileCreateInfo; + + XrFoveationProfileFB foveationProfile; + + pfnCreateFoveationProfileFB(*session, &profileCreateInfo, &foveationProfile); + + XrSwapchainStateFoveationFB foveationUpdateState; + memset(&foveationUpdateState, 0, sizeof(foveationUpdateState)); + foveationUpdateState.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB; + foveationUpdateState.profile = foveationProfile; + + pfnUpdateSwapchainFB(renderer->FrameBuffer[eye].ColorSwapChain.Handle, (XrSwapchainStateBaseHeaderFB*)(&foveationUpdateState)); + + pfnDestroyFoveationProfileFB(foveationProfile); + } +} +#endif + /* ================================================================================ diff --git a/Common/VR/VRFramebuffer.h b/Common/VR/VRFramebuffer.h index 07a5c19834..49f7484c99 100644 --- a/Common/VR/VRFramebuffer.h +++ b/Common/VR/VRFramebuffer.h @@ -13,3 +13,6 @@ void* ovrFramebuffer_SetCurrent(ovrFramebuffer* frameBuffer); void ovrRenderer_Create(XrSession session, ovrRenderer* renderer, int width, int height, bool multiview, void* vulkanContext); void ovrRenderer_Destroy(ovrRenderer* renderer); void ovrRenderer_MouseCursor(ovrRenderer* renderer, int x, int y, int sx, int sy); +#ifdef ANDROID +void ovrRenderer_SetFoveation(XrInstance* instance, XrSession* session, ovrRenderer* renderer, XrFoveationLevelFB level, float verticalOffset, XrFoveationDynamicFB dynamic); +#endif diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 8a3cc8ef90..8a4efd8657 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -216,6 +216,11 @@ void VR_InitRenderer( engine_t* engine, bool multiview ) { engine->appState.ViewConfigurationView[0].recommendedImageRectWidth, engine->appState.ViewConfigurationView[0].recommendedImageRectHeight, multiview, vulkanContext); +#ifdef ANDROID + if (VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_FOVEATION)) { + ovrRenderer_SetFoveation(&engine->appState.Instance, &engine->appState.Session, &engine->appState.Renderer, XR_FOVEATION_LEVEL_HIGH_FB, 0, XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB); + } +#endif initialized = true; }