From a81e6d61c98adfc5d9047c04cbd67101fac163bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 01:05:01 +0100 Subject: [PATCH 1/5] VR: Run OpenXR on Vulkan, with both eyes in one multiview pass The XR swapchain (one image layer per eye) replaces the main swapchain as a VulkanPresentation, and the backbuffer pass is multiview in VR. The emu thread begins each XR frame before NativeFrame, the render thread ends it after submitting. VR uniforms (lens projection, right eye view, HUD scale) follow the base uniform block on Vulkan, recomputed for every draw since they depend on the draw being 2D and on the head pose. VR now defaults to Vulkan. On the Quest the swapchain is plain UNORM: a mutable sRGB one viewed as UNORM showed snow along edges in its compositor. Poses that aren't valid (no tracking yet) become identity, so layers aren't rejected. See docs/vr-openxr-vulkan.md. Co-Authored-By: Claude Opus 5.5 --- Common/CMakeLists.txt | 2 + Common/Common.vcxproj | 2 + Common/Common.vcxproj.filters | 6 + Common/GPU/Vulkan/VulkanContext.cpp | 14 + Common/GPU/Vulkan/VulkanContext.h | 4 + Common/GPU/Vulkan/VulkanFramebuffer.cpp | 3 +- Common/GPU/Vulkan/VulkanGraphicsContext.cpp | 34 ++- Common/GPU/Vulkan/VulkanGraphicsContext.h | 2 + Common/GPU/Vulkan/VulkanLoader.cpp | 5 +- Common/GPU/Vulkan/VulkanPresentation.h | 2 + Common/GPU/Vulkan/VulkanQueueRunner.cpp | 11 +- Common/GPU/Vulkan/VulkanQueueRunner.h | 2 +- Common/GPU/Vulkan/VulkanRenderManager.cpp | 7 +- Common/VR/OpenXRLoader.h | 6 + Common/VR/PPSSPPVR.cpp | 30 +++ Common/VR/PPSSPPVR.h | 3 + Common/VR/VRBase.cpp | 27 ++ Common/VR/VRBase.h | 12 + Common/VR/VRFramebuffer.cpp | 132 +++++++++- Common/VR/VRFramebuffer.h | 3 +- Common/VR/VRRenderer.cpp | 162 +++++++++++- Common/VR/VRRenderer.h | 10 + Common/VR/VRVulkan.cpp | 276 ++++++++++++++++++++ Common/VR/VRVulkan.h | 44 ++++ Core/Config.cpp | 3 +- Core/EmuThread.cpp | 18 +- GPU/Common/PresentationCommon.cpp | 40 ++- GPU/Common/PresentationCommon.h | 3 + GPU/Common/ShaderUniforms.cpp | 88 +++++++ GPU/Common/ShaderUniforms.h | 20 ++ GPU/Common/VertexShaderGenerator.cpp | 11 +- GPU/GLES/ShaderManagerGLES.cpp | 45 ---- GPU/Vulkan/DrawEngineVulkan.cpp | 4 +- GPU/Vulkan/GPU_Vulkan.cpp | 10 +- GPU/Vulkan/ShaderManagerVulkan.cpp | 8 + GPU/Vulkan/ShaderManagerVulkan.h | 7 +- UI/NativeApp.cpp | 7 + UWP/CommonUWP/CommonUWP.vcxproj | 2 + UWP/CommonUWP/CommonUWP.vcxproj.filters | 6 + android/jni/Android.mk | 3 +- android/jni/app-android.cpp | 14 +- docs/vr-openxr-vulkan.md | 91 +++++++ libretro/Makefile.common | 3 +- 43 files changed, 1080 insertions(+), 102 deletions(-) create mode 100644 Common/VR/VRVulkan.cpp create mode 100644 Common/VR/VRVulkan.h create mode 100644 docs/vr-openxr-vulkan.md diff --git a/Common/CMakeLists.txt b/Common/CMakeLists.txt index 29b2a2ed3d..cbda41b1bb 100644 --- a/Common/CMakeLists.txt +++ b/Common/CMakeLists.txt @@ -60,6 +60,8 @@ set(CommonVR VR/VRMath.h VR/VRRenderer.cpp VR/VRRenderer.h + VR/VRVulkan.cpp + VR/VRVulkan.h ) add_library(Common STATIC diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 7442925798..3ddb87b34e 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -593,6 +593,7 @@ + @@ -1147,6 +1148,7 @@ + diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 204ca4f9f9..9e6839c942 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -424,6 +424,9 @@ VR + + VR + VR @@ -1221,6 +1224,9 @@ VR + + VR + VR diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index 97a015324f..43c93db5c7 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -214,6 +214,20 @@ VkResult VulkanContext::CreateInstance(const CreateInfo &info) { extensionsLookup_.EXT_swapchain_colorspace = true; } + // createInfo_ owns the strings. + for (const std::string &ext : createInfo_.extraInstanceExtensions) { + bool found = false; + for (const char *enabled : instance_extensions_enabled_) { + if (ext == enabled) { + found = true; + break; + } + } + if (!found) { + instance_extensions_enabled_.push_back(ext.c_str()); + } + } + // Validate that all the instance extensions we ask for are actually available. for (auto ext : instance_extensions_enabled_) { if (!IsInstanceExtensionAvailable(ext)) { diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index cd9b54b61e..cf22971e51 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -185,6 +185,8 @@ public: int app_ver; VulkanInitFlags flags; std::string customDriver; + // Required on top of our own, by OpenXR for example. + std::vector extraInstanceExtensions; }; VkResult CreateInstance(const CreateInfo &info); @@ -428,6 +430,8 @@ public: int GetBackbufferWidth() { return (int)(presentation_ ? presentation_->GetExtent().width : swapChainExtent_.width); } int GetBackbufferHeight() { return (int)(presentation_ ? presentation_->GetExtent().height : swapChainExtent_.height); } + // More than one means a multiview (stereo) backbuffer pass. + int GetBackbufferLayers() const { return presentation_ ? (int)presentation_->GetImageLayers() : 1; } void SetProfilerEnabledPtr(bool *enabled) { for (auto &frame : frame_) { diff --git a/Common/GPU/Vulkan/VulkanFramebuffer.cpp b/Common/GPU/Vulkan/VulkanFramebuffer.cpp index 9a95f9bcbf..d568bb3b9c 100644 --- a/Common/GPU/Vulkan/VulkanFramebuffer.cpp +++ b/Common/GPU/Vulkan/VulkanFramebuffer.cpp @@ -299,7 +299,8 @@ VkRenderPass CreateRenderPass(VulkanContext *vulkan, const RPKey &key, RenderPas _dbg_assert_(key.stencilLoadAction != VKRRenderPassLoadAction::KEEP); } bool hasDepth = RenderPassTypeHasDepth(rpType); - bool multiview = RenderPassTypeHasMultiView(rpType); + // A layered backbuffer (VR) is drawn to with multiview, so every pipeline compiled for it matches. + bool multiview = RenderPassTypeHasMultiView(rpType) || (isBackbuffer && vulkan->GetBackbufferLayers() > 1); bool multisample = RenderPassTypeHasMultisample(rpType); _dbg_assert_(!(isBackbuffer && multisample)); diff --git a/Common/GPU/Vulkan/VulkanGraphicsContext.cpp b/Common/GPU/Vulkan/VulkanGraphicsContext.cpp index 71ea559d9e..e61703c849 100644 --- a/Common/GPU/Vulkan/VulkanGraphicsContext.cpp +++ b/Common/GPU/Vulkan/VulkanGraphicsContext.cpp @@ -56,6 +56,8 @@ #include "Common/GPU/Vulkan/VulkanLoader.h" #include "Common/GPU/Vulkan/VulkanContext.h" #include "Common/GPU/Vulkan/VulkanPresentation.h" +#include "Common/VR/PPSSPPVR.h" +#include "Common/VR/VRVulkan.h" #include "Common/GPU/thin3d.h" #include "Common/GPU/thin3d_create.h" @@ -183,8 +185,13 @@ bool VulkanGraphicsContext::InitAPI(void *wnd, std::string *deviceName, std::str vulkan_ = new VulkanContext(); + vr_ = offscreenWidth_ == 0 && IsVREnabled() && VR_VulkanSupported(); + VulkanContext::CreateInfo info{}; InitVulkanCreateInfoFromConfig(&info); + if (vr_) { + info.extraInstanceExtensions = VR_GetVulkanInstanceExtensions(); + } if (VK_SUCCESS != vulkan_->CreateInstance(info)) { *errorMessage = vulkan_->InitError(); delete vulkan_; @@ -192,6 +199,13 @@ bool VulkanGraphicsContext::InitAPI(void *wnd, std::string *deviceName, std::str return false; } int deviceNum = vulkan_->GetPhysicalDeviceByName(*deviceName); + if (vr_) { + // The runtime decides, it has to be the GPU the headset is on. + int xrDevice = VR_GetVulkanPhysicalDevice(vulkan_); + if (xrDevice >= 0) { + deviceNum = xrDevice; + } + } if (deviceNum < 0) { deviceNum = vulkan_->GetBestPhysicalDevice(); if (!deviceName->empty()) { @@ -199,14 +213,15 @@ bool VulkanGraphicsContext::InitAPI(void *wnd, std::string *deviceName, std::str } } - if (vulkan_->CreateDevice(deviceNum) != VK_SUCCESS) { + static const std::vector noExtensions; + if (vulkan_->CreateDevice(deviceNum, vr_ ? VR_GetVulkanDeviceExtensions() : noExtensions) != VK_SUCCESS) { *errorMessage = vulkan_->InitError(); delete vulkan_; vulkan_ = nullptr; return false; } // Normally the queue is picked along with the surface, which must be able to present from it. - if (offscreenWidth_ > 0 && !vulkan_->ChooseGraphicsQueueWithoutSurface()) { + if ((offscreenWidth_ > 0 || vr_) && !vulkan_->ChooseGraphicsQueueWithoutSurface()) { *errorMessage = "No graphics queue"; return false; } @@ -222,6 +237,13 @@ bool VulkanGraphicsContext::InitSurface(WindowSystem winsys, void *data1, void * return false; } vulkan_->SetPresentation(std::move(presentation)); + } else if (vr_) { + // The headset is the display. The window's surface isn't used. + std::unique_ptr presentation = VR_CreateVulkanPresentation(vulkan_, errorMessage); + if (!presentation) { + return false; + } + vulkan_->SetPresentation(std::move(presentation)); } else { // Don't proceed on failure - without a surface there's no present mode, no swapchain and no queue, // so everything below would just fail in more confusing ways further down (it used to assert deep @@ -243,7 +265,7 @@ bool VulkanGraphicsContext::InitSurface(WindowSystem winsys, void *data1, void * draw_ = Draw::T3DCreateVulkanContext(vulkan_, useMultiThreading); - if (offscreenWidth_ == 0) { + if (!vulkan_->GetPresentation()) { VkPresentModeKHR presentMode = ConfigPresentModeToVulkan(draw_); #ifdef VK_EXT_full_screen_exclusive @@ -297,6 +319,10 @@ void VulkanGraphicsContext::ShutdownSurface() { } void VulkanGraphicsContext::ShutdownAPI() { + if (vr_) { + // The session goes before the device it was created with. + VR_DestroyVulkanSession(); + } vulkan_->DestroyDevice(); vulkan_->DestroyInstance(); @@ -308,7 +334,7 @@ void VulkanGraphicsContext::ShutdownAPI() { } void VulkanGraphicsContext::Resize() { - if (offscreenWidth_ > 0) { + if (offscreenWidth_ > 0 || vr_) { // The images have a fixed size. return; } diff --git a/Common/GPU/Vulkan/VulkanGraphicsContext.h b/Common/GPU/Vulkan/VulkanGraphicsContext.h index 9fad4efa31..3b583b43d8 100644 --- a/Common/GPU/Vulkan/VulkanGraphicsContext.h +++ b/Common/GPU/Vulkan/VulkanGraphicsContext.h @@ -55,4 +55,6 @@ private: bool windowRestored_ = false; int offscreenWidth_ = 0; int offscreenHeight_ = 0; + // Presenting to an OpenXR swapchain instead of the window. + bool vr_ = false; }; diff --git a/Common/GPU/Vulkan/VulkanLoader.cpp b/Common/GPU/Vulkan/VulkanLoader.cpp index 6906331f4c..e3da6a3450 100644 --- a/Common/GPU/Vulkan/VulkanLoader.cpp +++ b/Common/GPU/Vulkan/VulkanLoader.cpp @@ -29,6 +29,7 @@ #include "Common/StringUtils.h" #include "Common/System/System.h" #include "Common/VR/PPSSPPVR.h" +#include "Common/VR/VRVulkan.h" #include "Common/File/FileUtil.h" #if !PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(SWITCH) @@ -452,8 +453,8 @@ bool VulkanMayBeAvailable() { g_vulkanMayBeAvailable = true; return true; #else - // Unsupported in VR at the moment - if (IsVREnabled()) { + // VR needs a runtime that takes a Vulkan session (not cached, OpenXR may not be up yet). + if (IsVREnabled() && !VR_VulkanSupported()) { return false; } diff --git a/Common/GPU/Vulkan/VulkanPresentation.h b/Common/GPU/Vulkan/VulkanPresentation.h index 4d6891896f..001252b9b9 100644 --- a/Common/GPU/Vulkan/VulkanPresentation.h +++ b/Common/GPU/Vulkan/VulkanPresentation.h @@ -40,6 +40,8 @@ public: virtual VkImage GetImage(uint32_t index) const = 0; virtual VkExtent2D GetExtent() const = 0; virtual VkFormat GetFormat() const = 0; + // Array layers per image. Two makes the backbuffer pass multiview, one view per layer (VR). + virtual uint32_t GetImageLayers() const { return 1; } // The layout backbuffer images should be left in after rendering, since there's no real presentation // engine to hand them to via a VK_IMAGE_LAYOUT_PRESENT_SRC_KHR transition. diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.cpp b/Common/GPU/Vulkan/VulkanQueueRunner.cpp index c70cc79406..64119ab399 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.cpp +++ b/Common/GPU/Vulkan/VulkanQueueRunner.cpp @@ -114,10 +114,10 @@ bool VulkanQueueRunner::InitBackbufferFramebuffers(int width, int height, FrameD bool VulkanQueueRunner::InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrierBatch *barriers) { depth_.format = vulkan_->GetDeviceInfo().preferredDepthStencilFormat; - return CreateDepthStencilImage(vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight(), &depth_.image, &depth_.alloc, &depth_.view, barriers, "BackbufferDepth"); + return CreateDepthStencilImage(vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight(), vulkan_->GetBackbufferLayers(), &depth_.image, &depth_.alloc, &depth_.view, barriers, "BackbufferDepth"); } -bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag) { +bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, int layers, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag) { const VkFormat depth_format = vulkan_->GetDeviceInfo().preferredDepthStencilFormat; int aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; VkImageCreateInfo image_info = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; @@ -127,7 +127,7 @@ bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, VkImage * image_info.extent.height = height; image_info.extent.depth = 1; image_info.mipLevels = 1; - image_info.arrayLayers = 1; + image_info.arrayLayers = layers; image_info.samples = VK_SAMPLE_COUNT_1_BIT; image_info.queueFamilyIndexCount = 0; image_info.pQueueFamilyIndices = nullptr; @@ -151,6 +151,7 @@ bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, VkImage * VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, 0); barrier->subresourceRange.aspectMask = aspectMask; + barrier->subresourceRange.layerCount = layers; barrier->oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; barrier->newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; barrier->srcAccessMask = 0; @@ -169,8 +170,8 @@ bool VulkanQueueRunner::CreateDepthStencilImage(int width, int height, VkImage * depth_view_info.subresourceRange.baseMipLevel = 0; depth_view_info.subresourceRange.levelCount = 1; depth_view_info.subresourceRange.baseArrayLayer = 0; - depth_view_info.subresourceRange.layerCount = 1; - depth_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; + depth_view_info.subresourceRange.layerCount = layers; + depth_view_info.viewType = layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; depth_view_info.flags = 0; VkDevice device = vulkan_->GetDevice(); diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.h b/Common/GPU/Vulkan/VulkanQueueRunner.h index 21309001ff..352d734a7d 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.h +++ b/Common/GPU/Vulkan/VulkanQueueRunner.h @@ -275,7 +275,7 @@ public: bool InitDepthStencilBuffer(VkCommandBuffer cmd, VulkanBarrierBatch *barriers); // Used for non-buffered rendering. // For additional swapchains too. The depth image is transitioned by the barriers. VkFramebuffer CreateBackbufferFramebuffer(VkImageView colorView, VkImageView depthView, int width, int height); - bool CreateDepthStencilImage(int width, int height, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag); + bool CreateDepthStencilImage(int width, int height, int layers, VkImage *image, VmaAllocation *alloc, VkImageView *view, VulkanBarrierBatch *barriers, const char *tag); private: VKRRenderPass *PerformBindFramebufferAsRenderTarget(const VKRStep &pass, VkCommandBuffer cmd); diff --git a/Common/GPU/Vulkan/VulkanRenderManager.cpp b/Common/GPU/Vulkan/VulkanRenderManager.cpp index 069cf961c9..e3f67e77b7 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.cpp +++ b/Common/GPU/Vulkan/VulkanRenderManager.cpp @@ -380,8 +380,9 @@ static void InitSwapchainImageData(VulkanContext *vulkan, VkImage image, Swapcha color_image_view.subresourceRange.baseMipLevel = 0; color_image_view.subresourceRange.levelCount = 1; color_image_view.subresourceRange.baseArrayLayer = 0; - color_image_view.subresourceRange.layerCount = 1; // TODO: Investigate hw-assisted stereo. - color_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D; + const int layers = vulkan->GetBackbufferLayers(); + color_image_view.subresourceRange.layerCount = layers; + color_image_view.viewType = layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; color_image_view.flags = 0; color_image_view.image = image; @@ -660,7 +661,7 @@ bool VulkanRenderManager::InitExtraSwapchain(VKRSwapchain *swapchain, std::strin std::vector images(count); vkGetSwapchainImagesKHR(vulkan_->GetDevice(), swapchain->swapchain, &count, images.data()); - if (!queueRunner_.CreateDepthStencilImage(extent.width, extent.height, &swapchain->depthImage, &swapchain->depthAlloc, &swapchain->depthView, &postInitBarrier_, "ExtraSwapchainDepth")) { + if (!queueRunner_.CreateDepthStencilImage(extent.width, extent.height, 1, &swapchain->depthImage, &swapchain->depthAlloc, &swapchain->depthView, &postInitBarrier_, "ExtraSwapchainDepth")) { *error = "Failed to create the depth buffer"; return false; } diff --git a/Common/VR/OpenXRLoader.h b/Common/VR/OpenXRLoader.h index 1cc6cf67ba..f0a8940d15 100644 --- a/Common/VR/OpenXRLoader.h +++ b/Common/VR/OpenXRLoader.h @@ -32,6 +32,12 @@ #define XR_NO_PROTOTYPES 1 #endif +#if defined(ANDROID) || (defined(_WIN32) && defined(XR_USE_PLATFORM_WIN32)) +// Brings in the Vulkan types (no prototypes, we load Vulkan ourselves). +#include "Common/GPU/Vulkan/VulkanLoader.h" +#define XR_USE_GRAPHICS_API_VULKAN 1 +#endif + #include #include diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index e587bdfdb4..677ed5405e 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -542,6 +542,10 @@ void* BindVRFramebuffer() { bool StartVRRender() { #ifndef __LIBRETRO__ if (!VR_GetConfig(VR_CONFIG_VIEWPORT_VALID)) { + if (VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN)) { + // The presentation creates the swapchain with the surface. There isn't one now. + return false; + } VR_InitRenderer(VR_GetEngine()); VR_SetConfig(VR_CONFIG_VIEWPORT_VALID, true); } @@ -620,6 +624,10 @@ bool StartVRRender() { } void FinishVRRender() { + if (VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN)) { + // The render thread ends the frame once it's submitted. + return; + } VR_FinishFrame(VR_GetEngine()); } @@ -632,6 +640,28 @@ void PostVRFrameRender() { VR_EndFrame(VR_GetEngine()); } +bool GetVRMouseCursorRect(int *x, int *y, int *w, int *h) { +#ifdef __LIBRETRO__ + return false; +#else + if (!VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN)) { + return false; + } + // The same rect VR_EndFrame clears on OpenGL, where y counts from the bottom. + const int vrMode = VR_GetConfig(VR_CONFIG_MODE); + const bool screenMode = (vrMode == VR_MODE_MONO_SCREEN) || (vrMode == VR_MODE_STEREO_SCREEN); + const int size = VR_GetConfig(VR_CONFIG_MOUSE_SIZE); + if (!screenMode || size <= 0) { + return false; + } + *w = size; + *h = (int)((float)size * VR_GetConfigFloat(VR_CONFIG_CANVAS_ASPECT)); + *x = VR_GetConfig(VR_CONFIG_MOUSE_X); + *y = VR_GetConfig(VR_CONFIG_VIEWPORT_HEIGHT) - VR_GetConfig(VR_CONFIG_MOUSE_Y) - *h; + return true; +#endif +} + int GetVRFBOIndex() { #ifdef __LIBRETRO__ return Libretro::VREye(); diff --git a/Common/VR/PPSSPPVR.h b/Common/VR/PPSSPPVR.h index d2a8f087eb..1d2463bdf9 100644 --- a/Common/VR/PPSSPPVR.h +++ b/Common/VR/PPSSPPVR.h @@ -50,6 +50,9 @@ bool StartVRRender(); void FinishVRRender(); void PreVRFrameRender(int fboIndex); void PostVRFrameRender(); +// The pointer for the menus, in backbuffer pixels from the top left, if the app has to draw it +// (Vulkan; OpenGL draws it into the eye buffers itself). +bool GetVRMouseCursorRect(int *x, int *y, int *w, int *h); int GetVRFBOIndex(); int GetVRPassesCount(); bool IsPassthroughSupported(); diff --git a/Common/VR/VRBase.cpp b/Common/VR/VRBase.cpp index c305bf160f..d92c506b2b 100644 --- a/Common/VR/VRBase.cpp +++ b/Common/VR/VRBase.cpp @@ -39,9 +39,30 @@ void VR_Init( void* system, const char* name, int version ) { } #endif + std::vector available; + uint32_t availableCount = 0; + if (XR_SUCCEEDED(xrEnumerateInstanceExtensionProperties(nullptr, 0, &availableCount, nullptr)) && availableCount) { + available.resize(availableCount, { XR_TYPE_EXTENSION_PROPERTIES }); + xrEnumerateInstanceExtensionProperties(nullptr, availableCount, &availableCount, available.data()); + } + auto isAvailable = [&](const char *name) { + for (const XrExtensionProperties &ext : available) { + if (!strcmp(ext.extensionName, name)) { + return true; + } + } + return false; + }; + std::vector extensions; #ifdef XR_USE_GRAPHICS_API_OPENGL_ES extensions.push_back(XR_KHR_OPENGL_ES_ENABLE_EXTENSION_NAME); +#endif +#ifdef XR_USE_GRAPHICS_API_VULKAN + if (isAvailable(XR_KHR_VULKAN_ENABLE_EXTENSION_NAME)) { + extensions.push_back(XR_KHR_VULKAN_ENABLE_EXTENSION_NAME); + VR_SetPlatformFLag(VR_PLATFORM_EXTENSION_VULKAN, true); + } #endif extensions.push_back(XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME); #ifdef ANDROID @@ -171,6 +192,12 @@ void VR_EnterVR( engine_t* engine ) { sessionCreateInfo.next = &graphicsBindingGL; #else //TODO:PCVR definition +#endif +#ifdef XR_USE_GRAPHICS_API_VULKAN + if (engine->appState.VulkanBinding.type == XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR) { + sessionCreateInfo.next = &engine->appState.VulkanBinding; + VR_SetPlatformFLag(VR_PLATFORM_RENDERER_VULKAN, true); + } #endif sessionCreateInfo.type = XR_TYPE_SESSION_CREATE_INFO; sessionCreateInfo.createFlags = 0; diff --git a/Common/VR/VRBase.h b/Common/VR/VRBase.h index 83aa372c6b..c1061d7ceb 100644 --- a/Common/VR/VRBase.h +++ b/Common/VR/VRBase.h @@ -69,8 +69,13 @@ typedef struct { void* ColorSwapChainImage; unsigned int* GLDepthBuffers; unsigned int* GLFrameBuffers; + // Vulkan: one swapchain whose images have a layer per eye. + int ArraySize; + int64_t SwapchainFormat; // As created (sRGB, on Vulkan we render through a UNORM view). bool Acquired; + // At least one image has been released, so the swapchain can be put in a layer. + bool EverReleased; } ovrFramebuffer; typedef struct { @@ -99,6 +104,11 @@ typedef struct { int LayerCount; ovrRenderer Renderer; + +#ifdef XR_USE_GRAPHICS_API_VULKAN + // Set before VR_EnterVR to create the session for Vulkan instead of OpenGL. + XrGraphicsBindingVulkanKHR VulkanBinding; +#endif } ovrApp; #ifdef ANDROID @@ -122,6 +132,8 @@ enum VRPlatformFlag { VR_PLATFORM_EXTENSION_PASSTHROUGH, VR_PLATFORM_EXTENSION_PERFORMANCE, VR_PLATFORM_TRACKING_FLOOR, + VR_PLATFORM_EXTENSION_VULKAN, // The runtime has XR_KHR_vulkan_enable. + VR_PLATFORM_RENDERER_VULKAN, // The session is a Vulkan one. VR_PLATFORM_MAX }; diff --git a/Common/VR/VRFramebuffer.cpp b/Common/VR/VRFramebuffer.cpp index 21e5d3ed0e..d94d24f675 100644 --- a/Common/VR/VRFramebuffer.cpp +++ b/Common/VR/VRFramebuffer.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #if !defined(_WIN32) #include @@ -50,7 +51,96 @@ void ovrFramebuffer_Clear(ovrFramebuffer* frameBuffer) { frameBuffer->GLDepthBuffers = NULL; frameBuffer->GLFrameBuffers = NULL; + frameBuffer->ArraySize = 0; + frameBuffer->SwapchainFormat = 0; frameBuffer->Acquired = false; + frameBuffer->EverReleased = false; +} + +// Both APIs can be compiled in (Android), so this is a runtime choice. +static bool UseGL() { +#if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL + return !VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN); +#else + return false; +#endif +} + +#ifdef XR_USE_GRAPHICS_API_VULKAN + +static bool ovrFramebuffer_CreateVK(XrSession session, ovrFramebuffer* frameBuffer, int width, int height, int layers) { + frameBuffer->Width = width; + frameBuffer->Height = height; + frameBuffer->ArraySize = layers; + + // We write sRGB-encoded values and want them shown as they are. The Quest shows UNORM images as + // they are. By the spec, an sRGB image drawn through a UNORM view (mutable) would be the way, but + // the Quest's compositor shows that with snow along edges, even with the view formats listed + // (XR_KHR_vulkan_swapchain_format_list). So sRGB only where there's no UNORM. + uint32_t formatCount = 0; + OXR(xrEnumerateSwapchainFormats(session, 0, &formatCount, nullptr)); + std::vector formats(formatCount); + OXR(xrEnumerateSwapchainFormats(session, formatCount, &formatCount, formats.data())); + const int64_t preferred[] = { VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_B8G8R8A8_SRGB }; + int64_t format = 0; + for (int64_t want : preferred) { + for (int64_t have : formats) { + if (have == want) { + format = want; + break; + } + } + if (format) { + break; + } + } + if (!format) { + ALOGE("No usable Vulkan swapchain format"); + return false; + } + frameBuffer->SwapchainFormat = format; + + XrSwapchainCreateInfo swapChainCreateInfo{ XR_TYPE_SWAPCHAIN_CREATE_INFO }; + swapChainCreateInfo.usageFlags = XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT; + if (format == VK_FORMAT_R8G8B8A8_SRGB || format == VK_FORMAT_B8G8R8A8_SRGB) { + // Drawn through a UNORM view. + swapChainCreateInfo.usageFlags |= XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT; + } + swapChainCreateInfo.format = format; + swapChainCreateInfo.sampleCount = 1; + swapChainCreateInfo.width = width; + swapChainCreateInfo.height = height; + swapChainCreateInfo.faceCount = 1; + swapChainCreateInfo.arraySize = layers; + swapChainCreateInfo.mipCount = 1; + + frameBuffer->ColorSwapChain.Width = width; + frameBuffer->ColorSwapChain.Height = height; + + XrResult res; + OXR(res = xrCreateSwapchain(session, &swapChainCreateInfo, &frameBuffer->ColorSwapChain.Handle)); + if (XR_FAILED(res)) { + return false; + } + OXR(xrEnumerateSwapchainImages(frameBuffer->ColorSwapChain.Handle, 0, &frameBuffer->TextureSwapChainLength, nullptr)); + XrSwapchainImageVulkanKHR *images = (XrSwapchainImageVulkanKHR *)calloc(frameBuffer->TextureSwapChainLength, sizeof(XrSwapchainImageVulkanKHR)); + for (uint32_t i = 0; i < frameBuffer->TextureSwapChainLength; i++) { + images[i].type = XR_TYPE_SWAPCHAIN_IMAGE_VULKAN_KHR; + } + OXR(xrEnumerateSwapchainImages(frameBuffer->ColorSwapChain.Handle, frameBuffer->TextureSwapChainLength, &frameBuffer->TextureSwapChainLength, (XrSwapchainImageBaseHeader *)images)); + frameBuffer->ColorSwapChainImage = images; + return true; +} + +#endif + +void *ovrFramebuffer_GetVulkanImage(ovrFramebuffer* frameBuffer, uint32_t index) { +#ifdef XR_USE_GRAPHICS_API_VULKAN + if (frameBuffer->ColorSwapChainImage && index < frameBuffer->TextureSwapChainLength) { + return (void *)((XrSwapchainImageVulkanKHR *)frameBuffer->ColorSwapChainImage)[index].image; + } +#endif + return nullptr; } #if XR_USE_GRAPHICS_API_OPENGL || XR_USE_GRAPHICS_API_OPENGL_ES @@ -154,9 +244,16 @@ static bool ovrFramebuffer_CreateGL(XrSession session, ovrFramebuffer* frameBuff #endif void ovrFramebuffer_Destroy(ovrFramebuffer* frameBuffer) { + if (frameBuffer->ColorSwapChain.Handle == XR_NULL_HANDLE) { + // Never created (the second eye on Vulkan). + ovrFramebuffer_Clear(frameBuffer); + return; + } #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL - GL(glDeleteRenderbuffers(frameBuffer->TextureSwapChainLength, frameBuffer->GLDepthBuffers)); - GL(glDeleteFramebuffers(frameBuffer->TextureSwapChainLength, frameBuffer->GLFrameBuffers)); + if (UseGL()) { + GL(glDeleteRenderbuffers(frameBuffer->TextureSwapChainLength, frameBuffer->GLDepthBuffers)); + GL(glDeleteFramebuffers(frameBuffer->TextureSwapChainLength, frameBuffer->GLFrameBuffers)); + } free(frameBuffer->GLDepthBuffers); free(frameBuffer->GLFrameBuffers); #endif @@ -168,7 +265,9 @@ void ovrFramebuffer_Destroy(ovrFramebuffer* frameBuffer) { void* ovrFramebuffer_SetCurrent(ovrFramebuffer* frameBuffer) { #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL - GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->GLFrameBuffers[frameBuffer->TextureSwapChainIndex])); + if (UseGL()) { + GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->GLFrameBuffers[frameBuffer->TextureSwapChainIndex])); + } #endif return nullptr; } @@ -184,6 +283,10 @@ void ovrFramebuffer_Acquire(ovrFramebuffer* frameBuffer) { XrResult res = xrWaitSwapchainImage(frameBuffer->ColorSwapChain.Handle, &waitInfo); frameBuffer->Acquired = res == XR_SUCCESS; + if (!UseGL()) { + return; + } + ovrFramebuffer_SetCurrent(frameBuffer); #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL @@ -202,13 +305,16 @@ void ovrFramebuffer_Release(ovrFramebuffer* frameBuffer) { XrSwapchainImageReleaseInfo releaseInfo = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO, NULL}; OXR(xrReleaseSwapchainImage(frameBuffer->ColorSwapChain.Handle, &releaseInfo)); frameBuffer->Acquired = false; + frameBuffer->EverReleased = true; // Clear the alpha channel, other way OpenXR would not transfer the framebuffer fully #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL - GL(glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE)); - GL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); - GL(glClear(GL_COLOR_BUFFER_BIT)); - GL(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); + if (UseGL()) { + GL(glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE)); + GL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); + GL(glClear(GL_COLOR_BUFFER_BIT)); + GL(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); + } #endif } } @@ -227,12 +333,19 @@ void ovrRenderer_Clear(ovrRenderer* renderer) { } } -void ovrRenderer_Create(XrSession session, ovrRenderer* renderer, int width, int height) { +bool ovrRenderer_Create(XrSession session, ovrRenderer* renderer, int width, int height) { +#ifdef XR_USE_GRAPHICS_API_VULKAN + if (!UseGL()) { + // Both eyes in one swapchain, a layer each, drawn with multiview. + return ovrFramebuffer_CreateVK(session, &renderer->FrameBuffer[0], width, height, ovrMaxNumEyes); + } +#endif for (int i = 0; i < ovrMaxNumEyes; i++) { #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL ovrFramebuffer_CreateGL(session, &renderer->FrameBuffer[i], width, height); #endif } + return true; } void ovrRenderer_Destroy(ovrRenderer* renderer) { @@ -243,6 +356,9 @@ void ovrRenderer_Destroy(ovrRenderer* renderer) { void ovrRenderer_MouseCursor(ovrRenderer* renderer, int x, int y, int sx, int sy) { #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL + if (!UseGL()) { + return; + } GL(glEnable(GL_SCISSOR_TEST)); GL(glScissor(x, y, sx, sy)); GL(glViewport(x, y, sx, sy)); diff --git a/Common/VR/VRFramebuffer.h b/Common/VR/VRFramebuffer.h index fc7a550778..b7cbffbb69 100644 --- a/Common/VR/VRFramebuffer.h +++ b/Common/VR/VRFramebuffer.h @@ -10,6 +10,7 @@ void ovrFramebuffer_Acquire(ovrFramebuffer* frameBuffer); void ovrFramebuffer_Release(ovrFramebuffer* frameBuffer); void* ovrFramebuffer_SetCurrent(ovrFramebuffer* frameBuffer); -void ovrRenderer_Create(XrSession session, ovrRenderer* renderer, int width, int height); +bool ovrRenderer_Create(XrSession session, ovrRenderer* renderer, int width, int height); +void *ovrFramebuffer_GetVulkanImage(ovrFramebuffer* frameBuffer, uint32_t index); void ovrRenderer_Destroy(ovrRenderer* renderer); void ovrRenderer_MouseCursor(ovrRenderer* renderer, int x, int y, int sx, int sy); diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 86df230f05..ba18bb26aa 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -8,6 +8,21 @@ #include #include +#include +#include + +// On Vulkan, VR_InitFrame (emu thread) begins the frame and VR_FinishFrame (render thread, after the +// submit) ends it. The next frame can't begin before that. +static std::mutex g_frameMutex; +static std::condition_variable g_frameCond; +static bool g_frameOpen = false; +static bool g_presentQueued = false; +// The runtime may use the VkQueue in xrBeginFrame, xrEndFrame and the swapchain calls. +static std::recursive_mutex g_queueMutex; + +static bool IsVulkan() { + return VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN); +} XrFovf fov; XrView* projections; @@ -141,8 +156,16 @@ void VR_Recenter(engine_t* engine) { if (engine->appState.CurrentSpace != XR_NULL_HANDLE) { XrSpaceLocation loc = {}; loc.type = XR_TYPE_SPACE_LOCATION; - OXR(xrLocateSpace(engine->appState.HeadSpace, engine->appState.CurrentSpace, engine->predictedDisplayTime, &loc)); - hmdorientation = XrQuaternionf_ToEulerAngles(loc.pose.orientation); + XrResult locateResult = XR_ERROR_TIME_INVALID; + if (engine->predictedDisplayTime != 0) { + OXR(locateResult = xrLocateSpace(engine->appState.HeadSpace, engine->appState.CurrentSpace, engine->predictedDisplayTime, &loc)); + } + if (XR_SUCCEEDED(locateResult) && (loc.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT)) { + hmdorientation = XrQuaternionf_ToEulerAngles(loc.pose.orientation); + } else { + // Before the first frame, or not tracking. A zero quaternion would make every layer's pose invalid. + hmdorientation = {}; + } VR_SetConfigFloat(VR_CONFIG_RECENTER_YAW, VR_GetConfigFloat(VR_CONFIG_RECENTER_YAW) + hmdorientation.y); float recenterYaw = ToRadians(VR_GetConfigFloat(VR_CONFIG_RECENTER_YAW)); @@ -237,7 +260,9 @@ void VR_InitRenderer( engine_t* engine ) { projections[eye].type = XR_TYPE_VIEW; } - ovrRenderer_Create(engine->appState.Session, &engine->appState.Renderer, eyeW, eyeH); + if (!ovrRenderer_Create(engine->appState.Session, &engine->appState.Renderer, eyeW, eyeH)) { + ALOGE("Failed to create the XR swapchains"); + } if (VR_GetPlatformFlag(VRPlatformFlag::VR_PLATFORM_EXTENSION_PASSTHROUGH)) { XrPassthroughCreateInfoFB ptci = {XR_TYPE_PASSTHROUGH_CREATE_INFO_FB}; @@ -272,6 +297,18 @@ void VR_DestroyRenderer( engine_t* engine ) { } bool VR_InitFrame( engine_t* engine ) { + if (IsVulkan()) { + // Before the events: they can end the session or recreate the space the last frame ends in. + std::unique_lock lock(g_frameMutex); + if (g_frameOpen && !g_presentQueued) { + // The last frame never got to the render thread, so nothing there will end it. + lock.unlock(); + VR_FinishFrame(engine); + lock.lock(); + } + g_frameCond.wait(lock, [] { return !g_frameOpen; }); + } + bool stageBoundsDirty = true; if (ovrApp_HandleXrEvents(&engine->appState)) { VR_Recenter(engine); @@ -321,6 +358,16 @@ bool VR_InitFrame( engine_t* engine ) { &projectionCountOutput, projections)); + // Not tracking (yet). Layers with these poses would be rejected. + for (int eye = 0; eye < ovrMaxNumEyes; eye++) { + if (!(viewState.viewStateFlags & XR_VIEW_STATE_ORIENTATION_VALID_BIT)) { + projections[eye].pose.orientation = { 0.0f, 0.0f, 0.0f, 1.0f }; + } + if (!(viewState.viewStateFlags & XR_VIEW_STATE_POSITION_VALID_BIT)) { + projections[eye].pose.position = { 0.0f, 0.0f, 0.0f }; + } + } + // Get the HMD pose, predicted for the middle of the time period during which // the new eye images will be displayed. The number of frames predicted ahead // depends on the pipeline depth of the engine and the synthesis rate. @@ -328,7 +375,15 @@ bool VR_InitFrame( engine_t* engine ) { XrFrameBeginInfo beginFrameDesc = {}; beginFrameDesc.type = XR_TYPE_FRAME_BEGIN_INFO; beginFrameDesc.next = NULL; - OXR(xrBeginFrame(engine->appState.Session, &beginFrameDesc)); + if (IsVulkan()) { + std::lock_guard queueLock(g_queueMutex); + OXR(xrBeginFrame(engine->appState.Session, &beginFrameDesc)); + std::lock_guard lock(g_frameMutex); + g_frameOpen = true; + g_presentQueued = false; + } else { + OXR(xrBeginFrame(engine->appState.Session, &beginFrameDesc)); + } fov = {}; for (int eye = 0; eye < ovrMaxNumEyes; eye++) { @@ -345,8 +400,10 @@ bool VR_InitFrame( engine_t* engine ) { for (int i = 0; i < ovrMaxNumEyes; i++) { ovrFramebuffer* frameBuffer = &engine->appState.Renderer.FrameBuffer[i]; - frameBuffer->TextureSwapChainIndex++; - frameBuffer->TextureSwapChainIndex %= frameBuffer->TextureSwapChainLength; + if (frameBuffer->TextureSwapChainLength) { + frameBuffer->TextureSwapChainIndex++; + frameBuffer->TextureSwapChainIndex %= frameBuffer->TextureSwapChainLength; + } } engine->appState.LayerCount = 0; @@ -354,6 +411,44 @@ bool VR_InitFrame( engine_t* engine ) { return true; } +void VR_NotifyPresentQueued() { + std::lock_guard lock(g_frameMutex); + g_presentQueued = true; +} + +void VR_LockQueue() { + g_queueMutex.lock(); +} + +void VR_UnlockQueue() { + g_queueMutex.unlock(); +} + +bool VR_AcquireVulkanImage(engine_t *engine, uint32_t *index) { + ovrFramebuffer *frameBuffer = &engine->appState.Renderer.FrameBuffer[0]; + if (frameBuffer->ColorSwapChain.Handle == XR_NULL_HANDLE) { + return false; + } + { + std::lock_guard lock(g_frameMutex); + if (!g_frameOpen) { + return false; + } + } + std::lock_guard queueLock(g_queueMutex); + ovrFramebuffer_Acquire(frameBuffer); + if (!frameBuffer->Acquired) { + return false; + } + *index = frameBuffer->TextureSwapChainIndex; + return true; +} + +void VR_ReleaseVulkanImage(engine_t *engine) { + std::lock_guard queueLock(g_queueMutex); + ovrFramebuffer_Release(&engine->appState.Renderer.FrameBuffer[0]); +} + void VR_BeginFrame( engine_t* engine, int fboIndex ) { vrConfig[VR_CONFIG_CURRENT_FBO] = fboIndex; ovrFramebuffer_Acquire(&engine->appState.Renderer.FrameBuffer[fboIndex]); @@ -378,6 +473,29 @@ void VR_EndFrame( engine_t* engine ) { } void VR_FinishFrame( engine_t* engine ) { + const bool vulkan = IsVulkan(); + if (vulkan) { + std::lock_guard lock(g_frameMutex); + if (!g_frameOpen) { + return; + } + } + if (vulkan && engine->appState.Renderer.FrameBuffer[0].Acquired) { + // A frame that never got to present. + VR_ReleaseVulkanImage(engine); + } + // On Vulkan both eyes are layers of FrameBuffer[0]. Its alpha isn't cleared to 1 like the GL path's, + // so the layers are opaque instead. + ovrFramebuffer *eyeBuffers[ovrMaxNumEyes]; + uint32_t eyeLayers[ovrMaxNumEyes]; + for (int eye = 0; eye < ovrMaxNumEyes; eye++) { + eyeBuffers[eye] = &engine->appState.Renderer.FrameBuffer[vulkan ? 0 : eye]; + eyeLayers[eye] = vulkan ? eye : 0; + } + const XrCompositionLayerFlags alphaFlag = vulkan ? 0 : XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT; + // A swapchain that has never had an image released can't be shown. + const bool canShow = !vulkan || engine->appState.Renderer.FrameBuffer[0].EverReleased; + if (VR_GetPlatformFlag(VRPlatformFlag::VR_PLATFORM_EXTENSION_PASSTHROUGH) && VR_GetConfig(VR_CONFIG_PASSTHROUGH)) { if (passthroughLayer != XR_NULL_HANDLE) { XrCompositionLayerPassthroughFB passthrough_layer = {XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB}; @@ -396,13 +514,15 @@ void VR_FinishFrame( engine_t* engine ) { VR_SetConfigFloat(VR_CONFIG_MENU_YAW, hmdorientation.y); for (int eye = 0; eye < ovrMaxNumEyes; eye++) {; - ovrFramebuffer* frameBuffer = &engine->appState.Renderer.FrameBuffer[0]; + ovrFramebuffer* frameBuffer = eyeBuffers[0]; + uint32_t arrayIndex = eyeLayers[0]; XrPosef pose = invViewTransform[0]; if (vrMode != VR_MODE_MONO_6DOF) { pose = invViewTransform[eye]; } if (vrMode == VR_MODE_STEREO_6DOF) { - frameBuffer = &engine->appState.Renderer.FrameBuffer[eye]; + frameBuffer = eyeBuffers[eye]; + arrayIndex = eyeLayers[eye]; } memset(&projection_layer_elements[eye], 0, sizeof(XrCompositionLayerProjectionView)); @@ -416,7 +536,7 @@ void VR_FinishFrame( engine_t* engine ) { projection_layer_elements[eye].subImage.imageRect.offset.y = 0; projection_layer_elements[eye].subImage.imageRect.extent.width = frameBuffer->ColorSwapChain.Width; projection_layer_elements[eye].subImage.imageRect.extent.height = frameBuffer->ColorSwapChain.Height; - projection_layer_elements[eye].subImage.imageArrayIndex = 0; + projection_layer_elements[eye].subImage.imageArrayIndex = arrayIndex; if (vrMode == VR_MODE_SBS_6DOF) { projection_layer_elements[eye].subImage.imageRect.extent.width /= 2; @@ -428,7 +548,7 @@ void VR_FinishFrame( engine_t* engine ) { XrCompositionLayerProjection projection_layer = {}; projection_layer.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION; - projection_layer.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT; + projection_layer.layerFlags = alphaFlag; projection_layer.layerFlags |= XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT; projection_layer.space = engine->appState.CurrentSpace; projection_layer.viewCount = ovrMaxNumEyes; @@ -453,7 +573,7 @@ void VR_FinishFrame( engine_t* engine ) { // Setup the cylinder layer XrCompositionLayerCylinderKHR cylinder_layer = {}; cylinder_layer.type = XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR; - cylinder_layer.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT; + cylinder_layer.layerFlags = alphaFlag; cylinder_layer.space = engine->appState.CurrentSpace; memset(&cylinder_layer.subImage, 0, sizeof(XrSwapchainSubImage)); cylinder_layer.subImage.imageRect.offset.x = 0; @@ -489,11 +609,16 @@ void VR_FinishFrame( engine_t* engine ) { cylinder_layer.eyeVisibility = XR_EYE_VISIBILITY_LEFT; engine->appState.Layers[engine->appState.LayerCount++].Cylinder = cylinder_layer; cylinder_layer.eyeVisibility = XR_EYE_VISIBILITY_RIGHT; - cylinder_layer.subImage.swapchain = engine->appState.Renderer.FrameBuffer[1].ColorSwapChain.Handle; + cylinder_layer.subImage.swapchain = eyeBuffers[1]->ColorSwapChain.Handle; + cylinder_layer.subImage.imageArrayIndex = eyeLayers[1]; engine->appState.Layers[engine->appState.LayerCount++].Cylinder = cylinder_layer; } } + if (!canShow) { + engine->appState.LayerCount = 0; + } + // Compose the layers for this frame. const XrCompositionLayerBaseHeader* layers[ovrMaxLayerCount] = {}; for (int i = 0; i < engine->appState.LayerCount; i++) { @@ -506,7 +631,18 @@ void VR_FinishFrame( engine_t* engine ) { endFrameInfo.environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; endFrameInfo.layerCount = engine->appState.LayerCount; endFrameInfo.layers = layers; - OXR(xrEndFrame(engine->appState.Session, &endFrameInfo)); + if (!vulkan) { + OXR(xrEndFrame(engine->appState.Session, &endFrameInfo)); + return; + } + + { + std::lock_guard queueLock(g_queueMutex); + OXR(xrEndFrame(engine->appState.Session, &endFrameInfo)); + } + std::lock_guard lock(g_frameMutex); + g_frameOpen = false; + g_frameCond.notify_all(); } int VR_GetConfig( VRConfig config ) { diff --git a/Common/VR/VRRenderer.h b/Common/VR/VRRenderer.h index 0c7fe4edb5..1f9feb6c24 100644 --- a/Common/VR/VRRenderer.h +++ b/Common/VR/VRRenderer.h @@ -52,5 +52,15 @@ float VR_GetConfigFloat( VRConfigFloat config ); void VR_SetConfigFloat( VRConfigFloat config, float value ); void* VR_BindFramebuffer(engine_t *engine); + +// Vulkan. VR_InitFrame begins the frame on the emu thread, and the render thread ends it with +// VR_FinishFrame after submitting. Present() calls VR_NotifyPresentQueued so the next VR_InitFrame +// knows to wait for that, instead of ending the frame itself. +void VR_NotifyPresentQueued(); +bool VR_AcquireVulkanImage(engine_t *engine, uint32_t *index); +void VR_ReleaseVulkanImage(engine_t *engine); +// Around every use of the VkQueue the session was created with. +void VR_LockQueue(); +void VR_UnlockQueue(); XrView VR_GetView(int eye); XrVector3f VR_GetHMDAngles(); diff --git a/Common/VR/VRVulkan.cpp b/Common/VR/VRVulkan.cpp new file mode 100644 index 0000000000..229bf34439 --- /dev/null +++ b/Common/VR/VRVulkan.cpp @@ -0,0 +1,276 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include + +#include "Common/VR/VRVulkan.h" +#include "Common/VR/VRBase.h" +#include "Common/GPU/Vulkan/VulkanPresentation.h" + +#ifdef XR_USE_GRAPHICS_API_VULKAN + +#include "Common/Log.h" +#include "Common/GPU/Vulkan/VulkanContext.h" +#include "Common/VR/VRFramebuffer.h" +#include "Common/VR/VRInput.h" +#include "Common/VR/VRRenderer.h" + +static std::vector SplitExtensionList(const std::string &list) { + std::vector result; + size_t start = 0; + while (start < list.size()) { + size_t end = list.find(' ', start); + if (end == std::string::npos) { + end = list.size(); + } + if (end > start) { + result.push_back(list.substr(start, end - start)); + } + start = end + 1; + } + return result; +} + +// The runtime's extension queries return one space-separated string. +static std::vector GetExtensionList(const char *funcName) { + engine_t *engine = VR_GetEngine(); + PFN_xrGetVulkanInstanceExtensionsKHR func = nullptr; // The device one has the same signature. + if (XR_FAILED(xrGetInstanceProcAddr(engine->appState.Instance, funcName, (PFN_xrVoidFunction *)&func)) || !func) { + return {}; + } + uint32_t size = 0; + if (XR_FAILED(func(engine->appState.Instance, engine->appState.SystemId, 0, &size, nullptr)) || size == 0) { + return {}; + } + std::string buffer(size, '\0'); + if (XR_FAILED(func(engine->appState.Instance, engine->appState.SystemId, size, &size, &buffer[0]))) { + return {}; + } + buffer.resize(strlen(buffer.c_str())); + return SplitExtensionList(buffer); +} + +bool VR_VulkanSupported() { + return VR_GetEngine()->appState.Instance != XR_NULL_HANDLE && VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_VULKAN); +} + +std::vector VR_GetVulkanInstanceExtensions() { + std::vector list = GetExtensionList("xrGetVulkanInstanceExtensionsKHR"); + for (const std::string &ext : list) { + INFO_LOG(Log::G3D, "OpenXR wants Vulkan instance extension %s", ext.c_str()); + } + return list; +} + +const std::vector &VR_GetVulkanDeviceExtensions() { + static std::vector storage; + static std::vector pointers; + storage = GetExtensionList("xrGetVulkanDeviceExtensionsKHR"); + pointers.clear(); + for (const std::string &ext : storage) { + INFO_LOG(Log::G3D, "OpenXR wants Vulkan device extension %s", ext.c_str()); + pointers.push_back(ext.c_str()); + } + return pointers; +} + +int VR_GetVulkanPhysicalDevice(VulkanContext *vulkan) { + engine_t *engine = VR_GetEngine(); + PFN_xrGetVulkanGraphicsDeviceKHR getDevice = nullptr; + if (XR_FAILED(xrGetInstanceProcAddr(engine->appState.Instance, "xrGetVulkanGraphicsDeviceKHR", (PFN_xrVoidFunction *)&getDevice)) || !getDevice) { + return -1; + } + VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; + if (XR_FAILED(getDevice(engine->appState.Instance, engine->appState.SystemId, vulkan->GetInstance(), &physicalDevice))) { + return -1; + } + for (int i = 0; i < vulkan->GetNumPhysicalDevices(); i++) { + if (vulkan->GetPhysicalDevice(i) == physicalDevice) { + return i; + } + } + return -1; +} + +static bool CreateSession(VulkanContext *vulkan, std::string *error) { + engine_t *engine = VR_GetEngine(); + if (engine->appState.Session != XR_NULL_HANDLE) { + return true; + } + + // Required before xrCreateSession, even when we don't care about the answer. + PFN_xrGetVulkanGraphicsRequirementsKHR getRequirements = nullptr; + OXR(xrGetInstanceProcAddr(engine->appState.Instance, "xrGetVulkanGraphicsRequirementsKHR", (PFN_xrVoidFunction *)&getRequirements)); + if (!getRequirements) { + *error = "No xrGetVulkanGraphicsRequirementsKHR"; + return false; + } + XrGraphicsRequirementsVulkanKHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_VULKAN_KHR }; + OXR(getRequirements(engine->appState.Instance, engine->appState.SystemId, &requirements)); + INFO_LOG(Log::G3D, "OpenXR Vulkan API version: min %d.%d, max %d.%d", + (int)XR_VERSION_MAJOR(requirements.minApiVersionSupported), (int)XR_VERSION_MINOR(requirements.minApiVersionSupported), + (int)XR_VERSION_MAJOR(requirements.maxApiVersionSupported), (int)XR_VERSION_MINOR(requirements.maxApiVersionSupported)); + + XrGraphicsBindingVulkanKHR &binding = engine->appState.VulkanBinding; + binding = { XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR }; + binding.instance = vulkan->GetInstance(); + binding.physicalDevice = vulkan->GetCurrentPhysicalDevice(); + binding.device = vulkan->GetDevice(); + binding.queueFamilyIndex = vulkan->GetGraphicsQueueFamilyIndex(); + binding.queueIndex = 0; + + VR_EnterVR(engine); + if (engine->appState.Session == XR_NULL_HANDLE) { + *error = "xrCreateSession failed"; + return false; + } + IN_VRInit(engine); + return true; +} + +static VkFormat ToUnormFormat(VkFormat format) { + switch (format) { + case VK_FORMAT_R8G8B8A8_SRGB: return VK_FORMAT_R8G8B8A8_UNORM; + case VK_FORMAT_B8G8R8A8_SRGB: return VK_FORMAT_B8G8R8A8_UNORM; + default: return format; + } +} + +// The XR swapchain in place of the main swapchain. Acquire and present signal and wait on the +// frame's semaphores with empty submits (like the offscreen presentation), around the XR calls. +class OpenXRVulkanPresentation : public VulkanPresentation { +public: + bool Create() { + engine_ = VR_GetEngine(); + VR_InitRenderer(engine_); + VR_SetConfig(VR_CONFIG_VIEWPORT_VALID, true); + fb_ = &engine_->appState.Renderer.FrameBuffer[0]; + return fb_->ColorSwapChain.Handle != XR_NULL_HANDLE && fb_->TextureSwapChainLength > 0; + } + + void Destroy(VulkanContext *vulkan) override { + if (!engine_) { + return; + } + // End a frame that's still open, while its swapchain exists. + VR_FinishFrame(engine_); + VR_DestroyRenderer(engine_); + VR_SetConfig(VR_CONFIG_VIEWPORT_VALID, false); + engine_ = nullptr; + fb_ = nullptr; + } + + VkResult AcquireNextImage(VulkanContext *vulkan, VkSemaphore signalSemaphore, uint32_t *imageIndex) override { + if (!VR_AcquireVulkanImage(engine_, imageIndex)) { + // No frame to draw into (the session isn't running). Processed but not presented. + return VK_NOT_READY; + } + VkSubmitInfo submit{ VK_STRUCTURE_TYPE_SUBMIT_INFO }; + submit.signalSemaphoreCount = 1; + submit.pSignalSemaphores = &signalSemaphore; + VR_LockQueue(); + VkResult res = vkQueueSubmit(vulkan->GetGraphicsQueue(), 1, &submit, VK_NULL_HANDLE); + VR_UnlockQueue(); + return res; + } + + VkResult QueuePresent(VulkanContext *vulkan, VkQueue queue, uint32_t imageIndex, VkSemaphore waitSemaphore) override { + VkPipelineStageFlags stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; + VkSubmitInfo submit{ VK_STRUCTURE_TYPE_SUBMIT_INFO }; + submit.waitSemaphoreCount = 1; + submit.pWaitSemaphores = &waitSemaphore; + submit.pWaitDstStageMask = &stage; + VR_LockQueue(); + VkResult res = vkQueueSubmit(queue, 1, &submit, VK_NULL_HANDLE); + VR_UnlockQueue(); + // The rendering is submitted, so the runtime can have the image. + VR_ReleaseVulkanImage(engine_); + return res; + } + + // Emu thread, from Present(). + void BeginPresent() override { + VR_NotifyPresentQueued(); + } + // Render thread, after the present (or the skipped one). + void EndPresent() override { + VR_FinishFrame(engine_); + } + + void LockQueue() override { VR_LockQueue(); } + void UnlockQueue() override { VR_UnlockQueue(); } + + uint32_t GetImageCount() const override { return fb_->TextureSwapChainLength; } + VkImage GetImage(uint32_t index) const override { return (VkImage)ovrFramebuffer_GetVulkanImage(fb_, index); } + VkExtent2D GetExtent() const override { return VkExtent2D{ (uint32_t)fb_->Width, (uint32_t)fb_->Height }; } + VkFormat GetFormat() const override { return ToUnormFormat((VkFormat)fb_->SwapchainFormat); } + uint32_t GetImageLayers() const override { return (uint32_t)fb_->ArraySize; } + // What OpenXR expects a released color image to be in. + VkImageLayout GetPresentLayout() const override { return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; } + +private: + engine_t *engine_ = nullptr; + ovrFramebuffer *fb_ = nullptr; +}; + +std::unique_ptr VR_CreateVulkanPresentation(VulkanContext *vulkan, std::string *error) { + if (!CreateSession(vulkan, error)) { + return nullptr; + } + auto presentation = std::make_unique(); + if (!presentation->Create()) { + presentation->Destroy(vulkan); + *error = "Failed to create the XR swapchain"; + return nullptr; + } + return presentation; +} + +void VR_DestroyVulkanSession() { + engine_t *engine = VR_GetEngine(); + VR_LeaveVR(engine); + engine->appState.VulkanBinding = {}; + VR_SetPlatformFLag(VR_PLATFORM_RENDERER_VULKAN, false); +} + +#else + +bool VR_VulkanSupported() { + return false; +} + +std::vector VR_GetVulkanInstanceExtensions() { + return {}; +} + +const std::vector &VR_GetVulkanDeviceExtensions() { + static const std::vector empty; + return empty; +} + +int VR_GetVulkanPhysicalDevice(VulkanContext *vulkan) { + return -1; +} + +std::unique_ptr VR_CreateVulkanPresentation(VulkanContext *vulkan, std::string *error) { + *error = "No OpenXR Vulkan support in this build"; + return nullptr; +} + +void VR_DestroyVulkanSession() {} + +#endif diff --git a/Common/VR/VRVulkan.h b/Common/VR/VRVulkan.h new file mode 100644 index 0000000000..5f57105f44 --- /dev/null +++ b/Common/VR/VRVulkan.h @@ -0,0 +1,44 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#pragma once + +// OpenXR with a Vulkan session (XR_KHR_vulkan_enable). See docs/vr-openxr-vulkan.md. + +#include +#include +#include + +class VulkanContext; +class VulkanPresentation; + +// True if OpenXR is up and the runtime can take a Vulkan session. +bool VR_VulkanSupported(); + +// What the runtime needs enabled on our VkInstance and VkDevice. The device list's strings stay valid. +std::vector VR_GetVulkanInstanceExtensions(); +const std::vector &VR_GetVulkanDeviceExtensions(); + +// The physical device the headset is driven by, as an index for VulkanContext::CreateDevice. -1 if unknown. +int VR_GetVulkanPhysicalDevice(VulkanContext *vulkan); + +// Creates the session the first time, then a presentation that renders into the XR swapchain. +// The device and graphics queue must exist. +std::unique_ptr VR_CreateVulkanPresentation(VulkanContext *vulkan, std::string *error); + +// Before the device goes. +void VR_DestroyVulkanSession(); diff --git a/Core/Config.cpp b/Core/Config.cpp index 4151f2ff4e..134a57b89b 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -610,7 +610,8 @@ const char * const vulkanDefaultBlacklist[] = { static int DefaultGPUBackend() { if (IsVREnabled()) { - return (int)GPUBackend::OPENGL; + // Falls back to OpenGL if the runtime can't take a Vulkan session (VulkanMayBeAvailable). + return (int)GPUBackend::VULKAN; } #if PPSSPP_PLATFORM(UWP) diff --git a/Core/EmuThread.cpp b/Core/EmuThread.cpp index 90483fdb81..91f71ce30c 100644 --- a/Core/EmuThread.cpp +++ b/Core/EmuThread.cpp @@ -133,7 +133,23 @@ void RunGraphicsLoop(GraphicsContext *graphicsContext, Application *application, if (!graphicsContext->NeedsSeparateEmuThread()) { // The backend spawns its own render thread, so this thread is simply where emulation runs. SetCurrentThreadName("EmuThread"); - RunMainLoop(graphicsContext, application, frame); + if (!IsVREnabled()) { + RunMainLoop(graphicsContext, application, frame); + return; + } + // Vulkan VR. The frame begins here, so its view poses are there for the emulator's uniforms, + // and the render thread ends it once it's submitted. While the session is idle, nothing runs. + RunMainLoop(graphicsContext, application, [&](GraphicsContext *ctx) { + while (!StartVRRender()) { + if (shouldExit && shouldExit()) { + return false; + } + sleep_ms(16, "vr-session-idle"); + } + const bool result = frame(ctx); + UpdateVRInput(g_Config.bHapticFeedback, g_display.dpi_scale_x, g_display.dpi_scale_y); + return result; + }); return; } diff --git a/GPU/Common/PresentationCommon.cpp b/GPU/Common/PresentationCommon.cpp index 392ea6d045..5cec928b6e 100644 --- a/GPU/Common/PresentationCommon.cpp +++ b/GPU/Common/PresentationCommon.cpp @@ -22,6 +22,7 @@ #include #include #include "Common/GPU/thin3d.h" +#include "Common/GPU/ShaderWriter.h" #include "Common/System/Display.h" #include "Common/System/System.h" @@ -598,6 +599,30 @@ void PresentationCommon::CreateDeviceObjects() { texColor_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), draw_->GetFshaderPreset(FS_TEXTURE_COLOR_2D) }, false, &vsTexColBufDesc); texColorRBSwizzle_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), draw_->GetFshaderPreset(FS_TEXTURE_COLOR_2D_RB_SWIZZLE) }, false, &vsTexColBufDesc); texColorBlend_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), draw_->GetFshaderPreset(FS_TEXTURE_COLOR_2D) }, false, &vsTexColBufDesc, true); + if (IsVREnabled() && lang_ == GLSL_VULKAN && draw_->GetDeviceCaps().multiViewSupported) { + // FS_TEXTURE_COLOR_2D, sampling the layer of the eye being drawn. + static const VaryingDef varyings[] = { + { "vec4", "oColor0", SEM_COLOR0, 0, "lowp" }, + { "vec2", "oTexCoord0", SEM_TEXCOORD0, 1, "highp" }, + }; + static const SamplerDef samplers[] = { + { 0, "tex", SamplerFlags::ARRAY_ON_VULKAN }, + }; + char code[2048]; + ShaderWriter writer(code, draw_->GetShaderLanguageDesc(), ShaderStage::Fragment, Slice::empty(), ShaderWriterFlags::FS_AUTO_STEREO); + writer.DeclareSamplers(samplers); + writer.BeginFSMain(Slice::empty(), varyings); + writer.C("vec4 col = "); + writer.SampleTexture2D("tex", "oTexCoord0"); + writer.C(" * oColor0;\n"); + writer.C("col.rgb *= oColor0.a;\n"); + writer.EndFSMain("col"); + ShaderModule *fs = draw_->CreateShaderModule(ShaderStage::Fragment, lang_, (const uint8_t *)code, strlen(code), "present_stereo"); + if (fs) { + texColorStereo_ = CreatePipeline({ draw_->GetVshaderPreset(VS_TEXTURE_COLOR_2D), fs }, false, &vsTexColBufDesc); + fs->Release(); + } + } overlayVdata_ = draw_->CreateBuffer(sizeof(Vertex) * 4, BufferUsageFlag::DYNAMIC | BufferUsageFlag::VERTEXDATA); // Not resetting restorePostShader_: after a device loss, BeginFrame uses it to bring the post-shader back. } @@ -623,6 +648,7 @@ static void DoReleaseVector(std::vector &list) { void PresentationCommon::DestroyDeviceObjects() { DoRelease(texColor_); DoRelease(texColorRBSwizzle_); + DoRelease(texColorStereo_); DoRelease(texColorBlend_); DoRelease(overlayVdata_); DoRelease(overlayTexture_); @@ -698,6 +724,10 @@ void PresentationCommon::SourceFramebuffer(Draw::Framebuffer *fb, int bufferWidt srcHeight_ = bufferHeight; } +bool PresentationCommon::UseVRStereoSource() const { + return texColorStereo_ && !srcTexture_ && srcFramebuffer_ && srcFramebuffer_->Layers() > 1; +} + // Return value is if stereo binding succeeded. bool PresentationCommon::BindSource(int binding, bool bindStereo) { if (srcTexture_) { @@ -739,8 +769,10 @@ void PresentationCommon::RunPostshaderPasses(const DisplayLayoutConfig &config, bool useNearest = flags & OutputFlags::NEAREST; bool useStereo = gstate_c.Use(GPU_USE_SIMPLE_STEREO_PERSPECTIVE) && stereoPipeline_ != nullptr; // TODO: Also check that the backend has support for it. + // Post shaders output one layer, which would show the left eye's view to both. + const bool vrStereo = UseVRStereoSource(); - const bool usePostShader = usePostShader_ && !useStereo && !(flags & (OutputFlags::RB_SWIZZLE | OutputFlags::NO_POST_SHADER)); + const bool usePostShader = usePostShader_ && !useStereo && !vrStereo && !(flags & (OutputFlags::RB_SWIZZLE | OutputFlags::NO_POST_SHADER)); const bool isFinalAtOutputResolution = usePostShader && postShaderFramebuffers_.size() < postShaderPipelines_.size(); int lastWidth = srcWidth_; int lastHeight = srcHeight_; @@ -1032,8 +1064,9 @@ void PresentationCommon::DrawOverlay(const OverlayLayout &layout, const OutputPa void PresentationCommon::CopyToOutput(const DisplayLayoutConfig &config) { bool useNearest = outputFlags_ & OutputFlags::NEAREST; bool useStereo = gstate_c.Use(GPU_USE_SIMPLE_STEREO_PERSPECTIVE) && stereoPipeline_ != nullptr; // TODO: Also check that the backend has support for it. + const bool vrStereo = UseVRStereoSource(); - const bool usePostShader = usePostShader_ && !useStereo && !(outputFlags_ & (OutputFlags::RB_SWIZZLE | OutputFlags::NO_POST_SHADER)); + const bool usePostShader = usePostShader_ && !useStereo && !vrStereo && !(outputFlags_ & (OutputFlags::RB_SWIZZLE | OutputFlags::NO_POST_SHADER)); const bool isFinalAtOutputResolution = usePostShader && postShaderFramebuffers_.size() < postShaderPipelines_.size(); int lastWidth = srcWidth_; int lastHeight = srcHeight_; @@ -1071,6 +1104,9 @@ void PresentationCommon::CopyToOutput(const DisplayLayoutConfig &config) { draw_->BindPipeline(texColor_); useStereo = false; // Otherwise we end up uploading the wrong uniforms } + } else if (vrStereo) { + draw_->BindPipeline(texColorStereo_); + BindSource(0, true); } else { if (isFinalAtOutputResolution && previousFramebuffers_.empty()) { pipeline = postShaderPipelines_.back(); diff --git a/GPU/Common/PresentationCommon.h b/GPU/Common/PresentationCommon.h index e3cab6d581..6f3faaea05 100644 --- a/GPU/Common/PresentationCommon.h +++ b/GPU/Common/PresentationCommon.h @@ -193,6 +193,7 @@ protected: Draw::ShaderModule *CompileShaderModule(ShaderStage stage, ShaderLanguage lang, const std::string &src, std::string *errorString) const; Draw::Pipeline *CreatePipeline(std::vector shaders, bool postShader, const UniformBufferDesc *uniformDesc, bool alphaBlend = false) const; + bool UseVRStereoSource() const; bool CompilePostShader(const ShaderInfo *shaderInfo, Draw::Pipeline **outPipeline) const; bool BuildPostShader(const DisplayLayoutConfig &config, const ShaderInfo *shaderInfo, const ShaderInfo *next, Draw::Pipeline **outPipeline); bool AllocateFramebuffer(int w, int h); @@ -209,6 +210,8 @@ protected: Draw::DrawContext *draw_; Draw::Pipeline *texColor_ = nullptr; Draw::Pipeline *texColorRBSwizzle_ = nullptr; + // VR with a stereo (two-layer) source: each view of the multiview backbuffer samples its own layer. + Draw::Pipeline *texColorStereo_ = nullptr; Draw::SamplerState *samplerNearest_ = nullptr; Draw::SamplerState *samplerLinear_ = nullptr; Draw::Buffer *vdata_ = nullptr; diff --git a/GPU/Common/ShaderUniforms.cpp b/GPU/Common/ShaderUniforms.cpp index 7bb4f6b92a..182c2fcd19 100644 --- a/GPU/Common/ShaderUniforms.cpp +++ b/GPU/Common/ShaderUniforms.cpp @@ -9,6 +9,8 @@ #include "Common/Math/CrossSIMD.h" #include "Common/Math/lin/vec3.h" #include "Common/TimeUtil.h" +#include "Common/VR/PPSSPPVR.h" +#include "Core/Config.h" #include "GPU/GPUState.h" #include "GPU/Common/FramebufferManagerCommon.h" #include "GPU/Common/GPUStateUtils.h" @@ -180,6 +182,92 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool useBuffe } } +bool GuessVRDrawingHUD(bool is2D, bool flatScreen) { + + bool hud = true; + //HUD shouldn't be modified in nonVR mode + if (IsBigScreenVRMode()) hud = false; + //HUD can be disabled in settings + else if (!g_Config.bRescaleHUD) hud = false; + //HUD cannot be rendered in flatscreen + else if (flatScreen) hud = false; + //HUD has to be 2D + else if (!is2D) hud = false; + //HUD has to be blended + else if (!gstate.isAlphaBlendEnabled()) hud = false; +#ifdef __LIBRETRO__ + // Untextured transition fades must cover the view, not be scaled with the HUD. + else if (!gstate.isTextureMapEnabled()) hud = false; +#endif + //HUD cannot be rendered with clear color mask + else if (gstate.isClearModeColorMask()) hud = false; + //HUD cannot be rendered with depth color mask + else if (gstate.isClearModeDepthMask()) hud = false; + //HUD texture has to contain alpha channel + else if (!gstate.isTextureAlphaUsed()) hud = false; + //HUD texture cannot be in 5551 format + else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_5551) hud = false; + //HUD texture cannot be in CLUT16 format + else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT16) hud = false; + //HUD texture cannot be in CLUT32 format + else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT32) hud = false; + //HUD cannot have full texture alpha + else if (gstate_c.textureSolidAlpha && gstate.getTextureFormat() != GETextureFormat::GE_TFMT_CLUT4) hud = false; + //HUD must have full vertex alpha + else if (!gstate_c.vertexFullAlpha && gstate.getDepthTestFunction() == GE_COMP_NEVER) hud = false; + //HUD cannot render FB screenshot + else if (gstate_c.curTextureHeight % 68 <= 1) hud = false; + //HUD cannot be rendered with add function + else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_ADD) hud = false; + //HUD cannot be rendered with replace function + else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_REPLACE) hud = false; + //HUD cannot be rendered with full clear color mask + else if ((gstate.getClearModeColorMask() == 0xFFFFFF) && (gstate.getColorMask() == 0xFFFFFF)) hud = false; + + return hud; +} + +void VRUpdateUniforms(UB_VS_FS_Base *base, UB_VS_VR *vr, uint64_t dirtyUniforms) { + // Also counts the 3D draws that decide whether the scene is 3D, so once per draw. + const bool is2D = Is2DVRObject(gstate.projMatrix, gstate.isModeThrough()); + const bool flatScreen = IsFlatVRScene(); + + if (GuessVRDrawingHUD(is2D, flatScreen)) { + float aspect = 480.0f / 272.0f * (IsImmersiveVRMode() ? 0.5f : 1.0f); + vr->scaleX = g_Config.fHeadUpDisplayScale * aspect; + vr->scaleY = g_Config.fHeadUpDisplayScale; + } else { + vr->scaleX = 1.0f; + vr->scaleY = 1.0f; + } + + // Every draw, not only when the game changes the matrices: the result also depends on whether this + // draw is 2D and on the head pose. GL gets away with dirty flags because its uniforms are per program + // (2D and 3D draws rarely share one), while this buffer is shared by all draws. + { + if (flatScreen || is2D) { + memcpy(vr->projLens, gstate.projMatrix, sizeof(vr->projLens)); + } else { + UpdateVRProjection(gstate.projMatrix, vr->projLens); + } + UpdateVRParams(gstate.projMatrix); + } + + { + float leftEyeView[16]; + float rightEyeView[16]; + ConvertMatrix4x3To4x4Transposed(leftEyeView, gstate.viewMatrix); + ConvertMatrix4x3To4x4Transposed(rightEyeView, gstate.viewMatrix); + if (!is2D) { + UpdateVRView(leftEyeView, rightEyeView); + } + // The head pose is a rotation and a translation, so the last row stays 0 0 0 1 and the + // first three are the 3x4 the shaders take. + memcpy(base->view, leftEyeView, sizeof(base->view)); + memcpy(vr->viewRight, rightEyeView, sizeof(vr->viewRight)); + } +} + void DirtyDepalIfPixelMappedChanged(bool pixelMapped) { if (gstate_c.shaderDepalMode != ShaderDepalMode::OFF && pixelMapped != gstate_c.depalPixelMapped) { gstate_c.Dirty(DIRTY_DEPAL); diff --git a/GPU/Common/ShaderUniforms.h b/GPU/Common/ShaderUniforms.h index e85fc16b83..1c1d5835a0 100644 --- a/GPU/Common/ShaderUniforms.h +++ b/GPU/Common/ShaderUniforms.h @@ -104,9 +104,29 @@ R"( vec4 u_ambient; vec3 u_lightspecular[4]; )"; +// VR (GPU_USE_VIRTUAL_REALITY) on Vulkan. Pushed right after UB_VS_FS_Base, as the end of the +// vertex shader's base block, so other backends and non-VR drawing don't pay for it. +struct alignas(16) UB_VS_VR { + float projLens[16]; // The headset's projection, with the game's signs. + float viewRight[12]; // The right eye's view. The left eye's (or the only one) is in the base view. + float scaleX; float scaleY; float padding[2]; // HUD scaling. +}; +static_assert(sizeof(UB_VS_VR) == 128); + +static const char * const ub_vrStr = +R"( mat4 u_proj_lens; + mat3x4 u_viewR; + float u_scaleX; float u_scaleY; vec2 u_vrPadding; +)"; + // useBufferedRendering is only used to determine the rotation uniform. void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool useBufferedRendering, bool pixelMapped); void LightUpdateUniforms(UB_VS_Lights *ub, uint64_t dirtyUniforms); +// Call for every draw after BaseUpdateUniforms: the HUD guess depends on state that dirties nothing. +// Replaces the base view with the left eye's. +void VRUpdateUniforms(UB_VS_FS_Base *base, UB_VS_VR *vr, uint64_t dirtyUniforms); +// Whether a draw looks like part of the HUD, which VR scales down to keep in view. +bool GuessVRDrawingHUD(bool is2D, bool flatScreen); uint32_t PackLightControlBits(); uint32_t PackDepalBits(bool pixelMapped); diff --git a/GPU/Common/VertexShaderGenerator.cpp b/GPU/Common/VertexShaderGenerator.cpp index 4b5464d9ef..122d7240ba 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -178,7 +178,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (compat.shaderLanguage == GLSL_VULKAN) { WRITE(p, "\n"); - WRITE(p, "layout (std140, set = 0, binding = %d) uniform baseVars {\n%s};\n", DRAW_BINDING_DYNUBO_BASE, ub_baseStr); + WRITE(p, "layout (std140, set = 0, binding = %d) uniform baseVars {\n%s%s};\n", DRAW_BINDING_DYNUBO_BASE, ub_baseStr, gstate_c.Use(GPU_USE_VIRTUAL_REALITY) ? ub_vrStr : ""); if (enableLighting || doShadeMapping || readUnusedLights) WRITE(p, "layout (std140, set = 0, binding = %d) uniform lightVars {\n%s};\n", DRAW_BINDING_DYNUBO_LIGHT, ub_vs_lightsStr); @@ -525,8 +525,13 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag } } - WRITE(p, " vec4 viewPos = vec4(mul(vec4(worldpos, 1.0), u_view).xyz, 1.0);\n"); - if (useSimpleStereo) { + if (useSimpleStereo && gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { + // Each eye's view comes from the headset (multiview, Vulkan). + WRITE(p, " vec4 viewPos = vec4(mul(vec4(worldpos, 1.0), gl_ViewIndex == 0 ? u_view : u_viewR).xyz, 1.0);\n"); + } else { + WRITE(p, " vec4 viewPos = vec4(mul(vec4(worldpos, 1.0), u_view).xyz, 1.0);\n"); + } + if (useSimpleStereo && !gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { float ipd = 0.065f; float scale = 1.0f; if (PSP_CoreParameter().compat.vrCompat().UnitsPerMeter > 0) { diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 5d1191af4f..df7e4288f2 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -301,51 +301,6 @@ static void SetMatrix4x3(GLRenderManager *render, GLint *uniform, const float *m render->SetUniformM4x4(uniform, m4x4); } -static inline bool GuessVRDrawingHUD(bool is2D, bool flatScreen) { - - bool hud = true; - //HUD shouldn't be modified in nonVR mode - if (IsBigScreenVRMode()) hud = false; - //HUD can be disabled in settings - else if (!g_Config.bRescaleHUD) hud = false; - //HUD cannot be rendered in flatscreen - else if (flatScreen) hud = false; - //HUD has to be 2D - else if (!is2D) hud = false; - //HUD has to be blended - else if (!gstate.isAlphaBlendEnabled()) hud = false; -#ifdef __LIBRETRO__ - // Untextured transition fades must cover the view, not be scaled with the HUD. - else if (!gstate.isTextureMapEnabled()) hud = false; -#endif - //HUD cannot be rendered with clear color mask - else if (gstate.isClearModeColorMask()) hud = false; - //HUD cannot be rendered with depth color mask - else if (gstate.isClearModeDepthMask()) hud = false; - //HUD texture has to contain alpha channel - else if (!gstate.isTextureAlphaUsed()) hud = false; - //HUD texture cannot be in 5551 format - else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_5551) hud = false; - //HUD texture cannot be in CLUT16 format - else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT16) hud = false; - //HUD texture cannot be in CLUT32 format - else if (gstate.getTextureFormat() == GETextureFormat::GE_TFMT_CLUT32) hud = false; - //HUD cannot have full texture alpha - else if (gstate_c.textureSolidAlpha && gstate.getTextureFormat() != GETextureFormat::GE_TFMT_CLUT4) hud = false; - //HUD must have full vertex alpha - else if (!gstate_c.vertexFullAlpha && gstate.getDepthTestFunction() == GE_COMP_NEVER) hud = false; - //HUD cannot render FB screenshot - else if (gstate_c.curTextureHeight % 68 <= 1) hud = false; - //HUD cannot be rendered with add function - else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_ADD) hud = false; - //HUD cannot be rendered with replace function - else if (gstate.getTextureFunction() == GETexFunc::GE_TEXFUNC_REPLACE) hud = false; - //HUD cannot be rendered with full clear color mask - else if ((gstate.getClearModeColorMask() == 0xFFFFFF) && (gstate.getColorMask() == 0xFFFFFF)) hud = false; - - return hud; -} - void LinkedShader::use(const ShaderID &VSID) const { render_->BindProgram(program); // Note that we no longer track attr masks here - we do it for the input layouts instead. diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 163420f617..95521b7ee8 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -349,7 +349,7 @@ void DrawEngineVulkan::Flush() { descriptors[2].image.sampler = (boundDepal_ && boundDepalSmoothed_) ? samplerSecondaryLinear_ : samplerSecondaryNearest_; descriptors[3].buffer.buffer = baseBuf; - descriptors[3].buffer.range = sizeof(UB_VS_FS_Base); + descriptors[3].buffer.range = ShaderManagerVulkan::BaseBufferSize(); descriptors[3].buffer.offset = 0; descriptors[4].buffer.buffer = lightBuf; @@ -506,7 +506,7 @@ void DrawEngineVulkan::Flush() { descriptors[2].image.view = boundDepal_; descriptors[2].image.sampler = (boundDepal_ && boundDepalSmoothed_) ? samplerSecondaryLinear_ : samplerSecondaryNearest_; descriptors[3].buffer.buffer = baseBuf; - descriptors[3].buffer.range = sizeof(UB_VS_FS_Base); + descriptors[3].buffer.range = ShaderManagerVulkan::BaseBufferSize(); descriptors[3].buffer.offset = 0; descriptors[4].buffer.buffer = lightBuf; descriptors[4].buffer.range = sizeof(UB_VS_Lights); diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index 98920d0465..2afb635988 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -26,6 +26,7 @@ #include "Common/GPU/GraphicsContext.h" #include "Common/StringUtils.h" #include "Common/Data/Text/StringWriter.h" +#include "Common/VR/PPSSPPVR.h" #include "Core/Config.h" #include "Core/Reporting.h" @@ -232,7 +233,14 @@ u32 GPU_Vulkan::CheckGPUFeatures() const { } } - if (g_Config.bStereoRendering && draw_->GetDeviceCaps().multiViewSupported) { + if (IsVREnabled()) { + features |= GPU_USE_VIRTUAL_REALITY; + features &= ~GPU_USE_VS_RANGE_CULLING; + // Both eyes in one pass. The presentation copies each layer to its eye, so no stereo-to-mono shader. + if (GetVRPassesCount() > 1 && draw_->GetDeviceCaps().multiViewSupported) { + features |= GPU_USE_SINGLE_PASS_STEREO; + } + } else if (g_Config.bStereoRendering && draw_->GetDeviceCaps().multiViewSupported) { features |= GPU_USE_SINGLE_PASS_STEREO; features |= GPU_USE_SIMPLE_STEREO_PERSPECTIVE; } diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index 7aec3872c7..6e2070e669 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -19,6 +19,8 @@ //#define SHADERLOG #endif +#include + #include "Common/LogReporting.h" #include "Common/Profiler/Profiler.h" #include "Common/GPU/thin3d.h" @@ -176,6 +178,7 @@ ShaderManagerVulkan::ShaderManagerVulkan(Draw::DrawContext *draw) _assert_(uniforms_); static_assert(sizeof(uniforms_->ub_base) <= 512, "ub_base grew too big"); + static_assert(offsetof(Uniforms, ub_vr) == sizeof(UB_VS_FS_Base), "ub_vr must follow ub_base"); static_assert(sizeof(uniforms_->ub_lights) <= 512, "ub_lights grew too big"); } @@ -226,6 +229,11 @@ uint64_t ShaderManagerVulkan::UpdateUniforms(bool useBufferedRendering, bool pix if (dirty & DIRTY_LIGHT_UNIFORMS) LightUpdateUniforms(&uniforms_->ub_lights, dirty); } + if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { + VRUpdateUniforms(&uniforms_->ub_base, &uniforms_->ub_vr, dirty); + // It rewrote the view and the VR block for this draw, so the base buffer is pushed again. + dirty |= DIRTY_VIEWMATRIX; + } gstate_c.CleanUniforms(); return dirty; } diff --git a/GPU/Vulkan/ShaderManagerVulkan.h b/GPU/Vulkan/ShaderManagerVulkan.h index 86848b60ad..319827fa55 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.h +++ b/GPU/Vulkan/ShaderManagerVulkan.h @@ -92,6 +92,7 @@ protected: struct Uniforms { // Uniform block scratchpad. These (the relevant ones) are copied to the current pushbuffer at draw time. UB_VS_FS_Base ub_base{}; + UB_VS_VR ub_vr{}; // Right after ub_base, pushed with it in VR. UB_VS_Lights ub_lights{}; }; @@ -126,7 +127,11 @@ public: bool IsBoneDirty() { return true; } uint32_t PushBaseBuffer(VulkanPushPool *dest, VkBuffer *buf) const { - return dest->Push(&uniforms_->ub_base, sizeof(uniforms_->ub_base), uboAlignment_, buf); + return dest->Push(&uniforms_->ub_base, BaseBufferSize(), uboAlignment_, buf); + } + // With VR, the base buffer has the VR uniforms at the end. + static uint32_t BaseBufferSize() { + return gstate_c.Use(GPU_USE_VIRTUAL_REALITY) ? (uint32_t)(sizeof(UB_VS_FS_Base) + sizeof(UB_VS_VR)) : (uint32_t)sizeof(UB_VS_FS_Base); } uint32_t PushLightBuffer(VulkanPushPool *dest, VkBuffer *buf) const { return dest->Push(&uniforms_->ub_lights, sizeof(uniforms_->ub_lights), uboAlignment_, buf); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index ad91aa7a6b..8e4db63f89 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1402,6 +1402,13 @@ void NativeFrame(GraphicsContext *graphicsContext) { runImDebugger(g_draw); }); renderImDebugger(g_draw); + int cursorX, cursorY, cursorW, cursorH; + if (IsVREnabled() && GetVRMouseCursorRect(&cursorX, &cursorY, &cursorW, &cursorH)) { + UIContext *uiContext = g_screenManager->getUIContext(); + uiContext->Begin(); + uiContext->FillRect(UI::Drawable(0xFFFFFFFF), Bounds(cursorX * g_display.dpi_scale_x, cursorY * g_display.dpi_scale_y, cursorW * g_display.dpi_scale_x, cursorH * g_display.dpi_scale_y)); + uiContext->Flush(); + } // Last, since the readback ends the backbuffer pass. Nothing can draw after it. ScreenshotNotifyEndOfFrame(g_draw); if (g_screenManager->getUIContext()->Text()) { diff --git a/UWP/CommonUWP/CommonUWP.vcxproj b/UWP/CommonUWP/CommonUWP.vcxproj index 071431a7ca..eb589e4d12 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj +++ b/UWP/CommonUWP/CommonUWP.vcxproj @@ -236,6 +236,7 @@ + @@ -435,6 +436,7 @@ + diff --git a/UWP/CommonUWP/CommonUWP.vcxproj.filters b/UWP/CommonUWP/CommonUWP.vcxproj.filters index cec0a7a1c5..1cfaaab833 100644 --- a/UWP/CommonUWP/CommonUWP.vcxproj.filters +++ b/UWP/CommonUWP/CommonUWP.vcxproj.filters @@ -419,6 +419,9 @@ VR + + VR + VR @@ -969,6 +972,9 @@ VR + + VR + VR diff --git a/android/jni/Android.mk b/android/jni/Android.mk index c149e59b19..ef19e8f138 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -291,7 +291,8 @@ VR_FILES := \ $(SRC)/Common/VR/VRFramebuffer.cpp \ $(SRC)/Common/VR/VRInput.cpp \ $(SRC)/Common/VR/VRMath.cpp \ - $(SRC)/Common/VR/VRRenderer.cpp + $(SRC)/Common/VR/VRRenderer.cpp \ + $(SRC)/Common/VR/VRVulkan.cpp EXT_FILES := \ $(SRC)/ext/cityhash/city.cpp \ diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 19b55e9a33..637002fc4e 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -793,6 +793,14 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_init bFirstResume = true; + // Before the graphics context: Vulkan has to know what OpenXR needs from its instance and device, + // and VulkanMayBeAvailable whether the runtime takes a Vulkan session at all. + if (IsVREnabled()) { + Version gitVer(PPSSPP_GIT_VERSION); + InitVROnAndroid(gJvm, ppssppActivity, systemName.c_str(), gitVer.ToInteger(), "PPSSPP"); + SetVRCallbacks(NativeAxis, NativeKey, NativeTouch); + } + // In debug mode, don't allow creating software Vulkan devices (reject by VulkanMaybeAvailable). // Needed for #16931. #ifdef NDEBUG @@ -838,12 +846,6 @@ retry: g_Config.iGPUBackend = (int)GPUBackend::OPENGL; goto retry; } - - if (IsVREnabled()) { - Version gitVer(PPSSPP_GIT_VERSION); - InitVROnAndroid(gJvm, ppssppActivity, systemName.c_str(), gitVer.ToInteger(), "PPSSPP"); - SetVRCallbacks(NativeAxis, NativeKey, NativeTouch); - } } extern "C" void Java_org_ppsspp_ppsspp_NativeApp_setAchievementsHostOverride(JNIEnv *env, jclass, jstring jhost) { diff --git a/docs/vr-openxr-vulkan.md b/docs/vr-openxr-vulkan.md new file mode 100644 index 0000000000..1c4c05eaa3 --- /dev/null +++ b/docs/vr-openxr-vulkan.md @@ -0,0 +1,91 @@ +# OpenXR on Vulkan (design and status) + +VR used to be OpenGL ES only: `Common/VR` creates the OpenXR session with an EGL binding, makes one +GL swapchain per eye, and `GLRenderManager` replays the whole frame once per eye, swapping +`u_view` per pass (`UNIFORMSTEREOMATRIX`). This moves it to Vulkan, where both eyes are drawn in one +pass with multiview (the existing "stereo rendering" path, `GPU_USE_SINGLE_PASS_STEREO`), and then to +PC VR, where the desktop window stays the primary swapchain and the headset is a second output. + +## Phase 1: Quest (standalone Android) on Vulkan + +### Instance, device, session + +- `XR_KHR_vulkan_enable` (version 1: we create the `VkInstance`/`VkDevice` ourselves, which keeps + `VulkanContext` in charge). `VR_Init` enables it next to the GLES binding when the runtime has + it, so the backend can be chosen later. +- Before `vkCreateInstance`/`vkCreateDevice`, `VulkanContext` asks a hook for extra extensions + (`xrGetVulkanInstanceExtensionsKHR`, `xrGetVulkanDeviceExtensionsKHR`), and the physical device + is the one `xrGetVulkanGraphicsDeviceKHR` names. +- The session is created with `XrGraphicsBindingVulkanKHR` once the device exists (in + `VulkanGraphicsContext::InitSurface`), with our graphics queue family and index. + +### Presenting: the XR swapchain is a `VulkanPresentation` + +There's no Android window to present to in VR, so the main swapchain is replaced the same way +libretro and the offscreen mode replace it (`VulkanPresentation`): + +- One XR swapchain, `arraySize = 2`. We write sRGB-encoded values and want them shown as they are. + On the Quest it's plain `R8G8B8A8_UNORM`, which its compositor shows as it is. The spec's way, + `R8G8B8A8_SRGB` with `XR_SWAPCHAIN_USAGE_MUTABLE_FORMAT_BIT` drawn through a UNORM view, makes the + Quest compositor show "snow" along edges (worst when it reprojects a late frame), with or without + the view formats listed through `XR_KHR_vulkan_swapchain_format_list`, and with clean + synchronization validation on our side. +- **The backbuffer pass is multiview** (view mask 0b11), always, in VR. The backbuffer views are + 2D arrays, the depth buffer has two layers. Everything that draws to the backbuffer (UI, + presentation) is the same in both views unless its shader reads `gl_ViewIndex`. That costs a + second layer of fill for the menus, which is small next to the game, and means the backbuffer + render pass and all pipelines compiled against it never change with the VR mode. +- Presenting the game samples layer `gl_ViewIndex` of the game framebuffer when that has two + layers (stereo on), else layer 0. +- Compositor layers, as before: in a flat mode (menus, flat games) a cylinder layer showing layer 0 + (mono) or one cylinder per eye on layers 0 and 1 (stereo); in a 6DoF mode, a projection layer with + one view per layer. + +### Frame loop and threads + +OpenXR wants `xrWaitFrame` → `xrBeginFrame` → acquire/wait/release → `xrEndFrame` per frame, with +`xrBeginFrame` of the next frame after `xrEndFrame` of this one. With Vulkan, NativeFrame runs on +the emu thread and the render manager's thread submits and presents: + +- **Emu thread,** before NativeFrame (`StartVRRender`): wait for the previous frame's + `xrEndFrame`, then `xrWaitFrame`, `xrBeginFrame`, locate the views, sync input. The view poses + are then used for this frame's uniforms. While the session isn't running, skip the frame. +- **Render thread:** acquiring the backbuffer is `xrAcquireSwapchainImage` + + `xrWaitSwapchainImage`. Presenting releases it, then `xrEndFrame` with the layers. A frame that + never drew to the backbuffer ends with no layers, so the emu thread is never left waiting. +- The runtime may use the `VkQueue` inside `xrBeginFrame`, `xrEndFrame` and the swapchain calls, so + they and our submits take the presentation's queue lock. + +This serializes the XR frames (the emu thread can't begin frame N+1 until frame N is submitted), +which is what the OpenGL path did too. Pipelining `xrWaitFrame` is a later optimization. + +### Shaders + +`GPU_USE_VIRTUAL_REALITY` on Vulkan. The base uniform buffer gets the VR fields at the end: +`u_proj_lens`, the right eye's view matrix and the HUD scale. The vertex shader picks the view +matrix by `gl_ViewIndex` when multiview is on, instead of the simple stereo's fixed IPD offset. The +HUD heuristics (`GuessVRDrawingHUD`, `Is2DVRObject`, `UpdateVRProjection`, `UpdateVRView`) move out +of `ShaderManagerGLES` so both backends use them. + +In VR the view and lens matrices are recomputed and the base buffer re-uploaded on every draw. They +depend on whether the draw is 2D and on the head pose, neither of which sets a dirty flag. GL gets away +with dirty flags because its uniforms are per program (2D and 3D draws rarely share one). Recomputing +only on dirty split GTA's scene into two layers moving at different speeds. + +### Not ported (yet) + +- The skyplane compat hack (`PreprocessStepVR`) edits GL render steps. It stays GL-only for now. +- The mouse cursor was a GL scissored clear after the frame. On Vulkan it's drawn by the UI. + +## Phase 2: PC VR + +The desktop window (Windows, then Linux) stays the primary swapchain and shows a mirror. The +headset is an extra output through the multi-swapchain API (`docs/multi-swapchain.md`): its pass +renders into the XR swapchain images, acquired with `xrAcquireSwapchainImage` instead of +`vkAcquireNextImageKHR`. The runtime paces the frame (`xrWaitFrame`), so the desktop swapchain +becomes the non-pacing one. The OpenXR loader on desktop is our dynamic one (`OpenXRLoader.cpp`). + +## Status + +- [ ] Phase 1 +- [ ] Phase 2 diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 02bb845a59..c42d7278b3 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -544,7 +544,8 @@ SOURCES_CXX += \ $(COMMONDIR)/VR/VRMath.cpp \ $(COMMONDIR)/VR/VRFramebuffer.cpp \ $(COMMONDIR)/VR/VRInput.cpp \ - $(COMMONDIR)/VR/VRRenderer.cpp + $(COMMONDIR)/VR/VRRenderer.cpp \ + $(COMMONDIR)/VR/VRVulkan.cpp SOURCES_C += \ $(COMMONDIR)/GPU/OpenGL/gl3stub.c From abe220365767acb976537628559ba7f25976a414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 01:10:22 +0100 Subject: [PATCH 2/5] VR: Port the skyplane hack and VRForce to Vulkan The Vulkan version edits the frame's steps in PreprocessSteps, like the GL one does at replay: the first pass that doesn't sample another framebuffer clears to the fog color, and indexed draws without depth test are dropped. The shader generator test also builds the vertex shaders with the VR uniforms now, for Vulkan and GLSL. Co-Authored-By: Claude Opus 5.5 --- Common/GPU/Vulkan/VulkanQueueRunner.cpp | 8 ++++ Common/VR/PPSSPPVR.cpp | 52 ++++++++++++++++++++++--- Common/VR/PPSSPPVR.h | 2 + GPU/Common/ShaderUniforms.cpp | 5 +++ GPU/Vulkan/GPU_Vulkan.cpp | 5 ++- docs/vr-openxr-vulkan.md | 24 ++++++++++-- unittest/TestShaderGenerators.cpp | 18 ++++++++- 7 files changed, 101 insertions(+), 13 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.cpp b/Common/GPU/Vulkan/VulkanQueueRunner.cpp index 64119ab399..caed7594dc 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.cpp +++ b/Common/GPU/Vulkan/VulkanQueueRunner.cpp @@ -6,6 +6,7 @@ #include "Common/GPU/Vulkan/VulkanRenderManager.h" #include "Common/Log.h" #include "Common/TimeUtil.h" +#include "Common/VR/PPSSPPVR.h" using namespace PPSSPP_VK; @@ -232,6 +233,13 @@ void VulkanQueueRunner::PreprocessSteps(std::vector &steps) { // as early as possible in the frame (Wipeout billboards). This will require taking over more of descriptor management so we can // substitute descriptors, alternatively using texture array layers creatively. + // Before passes get merged, so it sees them as the game drew them. + if (IsVREnabled()) { + for (VKRStep *step : steps) { + PreprocessVulkanStepVR(step); + } + } + for (int j = 0; j < (int)steps.size(); j++) { decltype(steps[j]->render) &render = steps[j]->render; if (steps[j]->stepType == VKRStepType::RENDER && diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index 677ed5405e..e04a77b68b 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -1,10 +1,11 @@ +#include #include #include "Common/VR/PPSSPPVR.h" #ifdef __LIBRETRO__ -#include #include "libretro/LibretroVR.h" #endif +#include "Common/GPU/Vulkan/VulkanRenderManager.h" #include "Common/VR/VRBase.h" #if XR_USE_GRAPHICS_API_OPENGL || XR_USE_GRAPHICS_API_OPENGL_ES || defined(__LIBRETRO__) @@ -55,12 +56,8 @@ static std::map > pspAxis; static std::map pspKeys; // key can be virtual, so not using the enum. static int vr3DGeometryCount = 0; -#ifdef __LIBRETRO__ -// Fog colour is recorded on the emulation thread and consumed during GL replay. +// Fog colour is recorded on the emulation thread and consumed during replay, on the render thread. static std::atomic vrCompat[VR_COMPAT_MAX]{}; -#else -static long vrCompat[VR_COMPAT_MAX]; -#endif static bool vrFlatForced = false; static bool vrFlatGame = false; static float vrMatrix[VR_MATRIX_COUNT][16]; @@ -523,6 +520,45 @@ void PreprocessStepVR(void* step) {} #endif +// PreprocessSkyplane on a Vulkan step. Depth test lives in the pipelines, and a framebuffer bound +// as a texture shows up as a dependency on a framebuffer other than the target. +static void PreprocessSkyplaneVulkan(VKRStep *step) { + if (step->stepType != VKRStepType::RENDER || IsFlatVRScene()) { + return; + } + for (size_t i = 0; i < step->dependencies.size(); i++) { + if (step->dependencies[i] != step->render.framebuffer) { + return; + } + } + + // Clear sky with the fog color, unless the pass clears anyway (it would win over our clear on GL). + if (!vrCompat[VR_COMPAT_FBO_CLEAR]) { + if (step->render.colorLoad != VKRRenderPassLoadAction::CLEAR) { + step->render.colorLoad = VKRRenderPassLoadAction::CLEAR; + step->render.clearColor = (uint32_t)vrCompat[VR_COMPAT_FOG_COLOR]; + } + vrCompat[VR_COMPAT_FBO_CLEAR] = true; + } + + // Remove original sky plane. + bool depthEnabled = false; + for (VkRenderData &command : step->commands) { + if (command.cmd == VKRRenderCommand::BIND_GRAPHICS_PIPELINE) { + const VKRGraphicsPipelineDesc *desc = command.graphics_pipeline.pipeline->desc; + depthEnabled = desc && desc->dss.depthTestEnable; + } else if (command.cmd == VKRRenderCommand::DRAW_INDEXED && !depthEnabled) { + command.cmd = VKRRenderCommand::REMOVED; + } + } +} + +void PreprocessVulkanStepVR(VKRStep *step) { + if (vrCompat[VR_COMPAT_SKYPLANE]) { + PreprocessSkyplaneVulkan(step); + } +} + void SetVRCompat(VRCompatFlag flag, long value) { vrCompat[flag] = value; } @@ -614,6 +650,10 @@ bool StartVRRender() { // Set compatibility vrCompat[VR_COMPAT_SKYPLANE] = PSP_CoreParameter().compat.vrCompat().Skyplane; + if (VR_GetPlatformFlag(VR_PLATFORM_RENDERER_VULKAN)) { + // The last frame is done on the render thread, so its steps are too. (On OpenGL, this is per eye in PreVRFrameRender.) + vrCompat[VR_COMPAT_FBO_CLEAR] = false; + } // Set customizations VR_SetConfigFloat(VR_CONFIG_CANVAS_DISTANCE, !IsBigScreenVRMode() && (appMode == VR_GAME_MODE) ? g_Config.fCanvas3DDistance : g_Config.fCanvasDistance); diff --git a/Common/VR/PPSSPPVR.h b/Common/VR/PPSSPPVR.h index 1d2463bdf9..3dde8b3871 100644 --- a/Common/VR/PPSSPPVR.h +++ b/Common/VR/PPSSPPVR.h @@ -42,6 +42,8 @@ bool UpdateVRKeys(const KeyInput &key); // VR games compatibility void PreprocessStepVR(void* step); +struct VKRStep; +void PreprocessVulkanStepVR(VKRStep *step); void SetVRCompat(VRCompatFlag flag, long value); // VR rendering integration diff --git a/GPU/Common/ShaderUniforms.cpp b/GPU/Common/ShaderUniforms.cpp index 182c2fcd19..dc0640a2c0 100644 --- a/GPU/Common/ShaderUniforms.cpp +++ b/GPU/Common/ShaderUniforms.cpp @@ -241,6 +241,11 @@ void VRUpdateUniforms(UB_VS_FS_Base *base, UB_VS_VR *vr, uint64_t dirtyUniforms) vr->scaleY = 1.0f; } + if (dirtyUniforms & DIRTY_FOGCOLOR) { + // For the skyplane hack's clear. + SetVRCompat(VR_COMPAT_FOG_COLOR, gstate.fogcolor); + } + // Every draw, not only when the game changes the matrices: the result also depends on whether this // draw is 2D and on the head pose. GL gets away with dirty flags because its uniforms are per program // (2D and 3D draws rarely share one), while this buffer is shared by all draws. diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index 2afb635988..a63654c2c8 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -233,11 +233,12 @@ u32 GPU_Vulkan::CheckGPUFeatures() const { } } - if (IsVREnabled()) { + if (IsVREnabled() || g_Config.bForceVR) { + // bForceVR runs the VR shaders without a headset, for testing. features |= GPU_USE_VIRTUAL_REALITY; features &= ~GPU_USE_VS_RANGE_CULLING; // Both eyes in one pass. The presentation copies each layer to its eye, so no stereo-to-mono shader. - if (GetVRPassesCount() > 1 && draw_->GetDeviceCaps().multiViewSupported) { + if (IsVREnabled() && GetVRPassesCount() > 1 && draw_->GetDeviceCaps().multiViewSupported) { features |= GPU_USE_SINGLE_PASS_STEREO; } } else if (g_Config.bStereoRendering && draw_->GetDeviceCaps().multiViewSupported) { diff --git a/docs/vr-openxr-vulkan.md b/docs/vr-openxr-vulkan.md index 1c4c05eaa3..22e4a29dda 100644 --- a/docs/vr-openxr-vulkan.md +++ b/docs/vr-openxr-vulkan.md @@ -72,10 +72,28 @@ depend on whether the draw is 2D and on the head pose, neither of which sets a d with dirty flags because its uniforms are per program (2D and 3D draws rarely share one). Recomputing only on dirty split GTA's scene into two layers moving at different speeds. -### Not ported (yet) +### The OpenGL-only parts, and their Vulkan versions -- The skyplane compat hack (`PreprocessStepVR`) edits GL render steps. It stays GL-only for now. -- The mouse cursor was a GL scissored clear after the frame. On Vulkan it's drawn by the UI. +OpenGL VR goes once these are confirmed on a headset: + +- **Skyplane** (`compatvr.ini`, the GTA Stories games): `PreprocessSkyplane` edits GL render steps at + replay, `PreprocessSkyplaneVulkan` does the same to Vulkan steps in `PreprocessSteps`, before + passes are merged. A pass that samples another framebuffer is left alone (GL: a + `BIND_FB_TEXTURE` command; Vulkan: a dependency on a framebuffer other than the target). The + frame's first other pass is cleared to the fog color (GL inserts a `CLEAR`; Vulkan turns the + load op into a clear, unless it clears already). Indexed draws with the depth test off are + dropped (GL tracks `DEPTH` commands; Vulkan reads the bound pipeline's depth state). GL resets + the once-per-frame clear per eye, Vulkan once per frame in `StartVRRender`. +- **Per-eye view matrices:** GL replays the frame per eye and `UNIFORMSTEREOMATRIX` picks the + eye's `u_view`. Vulkan has `u_viewR` in the VR block and picks by `gl_ViewIndex`. +- **The pointer:** a scissored GL clear after the frame. On Vulkan the UI draws it + (`GetVRMouseCursorRect`). +- **Alpha:** GL clears the eye buffers' alpha to 1 after the frame. Vulkan submits opaque layers instead. +- **`VRForce`** (developer setting, VR shaders without a headset) works on both. + +The other VR compat flags (`ForceMono`, `ForceFlatScreen`, `IdentityViewHack`, `MirroringVariant`, +`ProjectionHack`, `UnitsPerMeter`) are read in shared code (`PPSSPPVR.cpp`, the shader generator), +so they don't depend on the backend. ## Phase 2: PC VR diff --git a/unittest/TestShaderGenerators.cpp b/unittest/TestShaderGenerators.cpp index 70358e5806..b898b380e7 100644 --- a/unittest/TestShaderGenerators.cpp +++ b/unittest/TestShaderGenerators.cpp @@ -360,7 +360,8 @@ const ShaderLanguage languages[] = { }; const int numLanguages = ARRAY_SIZE(languages); -bool TestVertexShaders() { +// vr: GPU_USE_VIRTUAL_REALITY is set, which only the GLSL backends (OpenGL, Vulkan) use. +bool TestVertexShaders(bool vr) { char *buffer[numLanguages]; for (int i = 0; i < numLanguages; i++) { @@ -393,6 +394,10 @@ bool TestVertexShaders() { std::string genErrorString[numLanguages]; for (int j = 0; j < numLanguages; j++) { + if (vr && languages[j] == ShaderLanguage::HLSL_D3D11) { + buffer[j][0] = '\0'; + continue; + } generateSuccess[j] = GenerateVShader(id, buffer[j], languages[j], bugs, &genErrorString[j]); if (!genErrorString[j].empty()) { if (g_testLog) { @@ -538,7 +543,16 @@ bool TestShaderGenerators() { return false; } - if (!TestVertexShaders()) { + if (!TestVertexShaders(false)) { + return false; + } + + // Again with the VR uniforms and the per-eye view (with VS_BIT_SIMPLE_STEREO on Vulkan). + const u32 oldFlags = gstate_c.GetUseFlags(); + gstate_c.SetUseFlags(oldFlags | GPU_USE_VIRTUAL_REALITY); + const bool vrResult = TestVertexShaders(true); + gstate_c.SetUseFlags(oldFlags); + if (!vrResult) { return false; } From 249b6539629a8cd784b91741817ddfffb3cbf04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 12:48:32 +0100 Subject: [PATCH 3/5] VR: Fix inverted depth and GL gamma, keep the flat screen upright - Depth was inverted on both backends: the VR shader replaced Z with the game's clip-space Z after the viewport transform, skipping its Z scale. Since the viewport transform moved into the shader, that's wrong. Replace it before the transform instead, as libretro already did. - GL eye buffers are sRGB, and GLES encoded our sRGB colors again (too bright). Disable the encoding with GL_EXT_sRGB_write_control. - Keep the flat screen upright instead of tilting it to the head's pitch at recenter time. Co-Authored-By: Claude Opus 5.5 --- Common/VR/VRFramebuffer.cpp | 28 ++++++++++++++++++++++++++++ Common/VR/VRRenderer.cpp | 5 +++-- GPU/Common/VertexShaderGenerator.cpp | 12 ++---------- docs/vr-openxr-vulkan.md | 16 ++++++++++++++-- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Common/VR/VRFramebuffer.cpp b/Common/VR/VRFramebuffer.cpp index d94d24f675..8d6b6766da 100644 --- a/Common/VR/VRFramebuffer.cpp +++ b/Common/VR/VRFramebuffer.cpp @@ -3,6 +3,29 @@ #if XR_USE_GRAPHICS_API_OPENGL || XR_USE_GRAPHICS_API_OPENGL_ES #include "Common/GPU/OpenGL/GLCommon.h" +#include + +#ifndef GL_FRAMEBUFFER_SRGB_EXT +#define GL_FRAMEBUFFER_SRGB_EXT 0x8DB9 +#endif + +// GLFeatures.h clashes with the EGL macros the OpenXR headers bring in, so this asks GL itself. +static bool HasSRGBWriteControl() { + static int has = -1; + if (has < 0) { + GLint count = 0; + glGetIntegerv(GL_NUM_EXTENSIONS, &count); + has = 0; + for (GLint i = 0; i < count; i++) { + const char *ext = (const char *)glGetStringi(GL_EXTENSIONS, i); + if (ext && !strcmp(ext, "GL_EXT_sRGB_write_control")) { + has = 1; + break; + } + } + } + return has == 1; +} #endif @@ -267,6 +290,11 @@ void* ovrFramebuffer_SetCurrent(ovrFramebuffer* frameBuffer) { #if XR_USE_GRAPHICS_API_OPENGL_ES || XR_USE_GRAPHICS_API_OPENGL if (UseGL()) { GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->GLFrameBuffers[frameBuffer->TextureSwapChainIndex])); + // The swapchain is sRGB and our colors are sRGB-encoded already: GLES would encode them again on + // write (too bright). Context state, but the other framebuffers aren't sRGB, so it only affects this. + if (HasSRGBWriteControl()) { + GL(glDisable(GL_FRAMEBUFFER_SRGB_EXT)); + } } #endif return nullptr; diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index ba18bb26aa..4d16c89917 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -206,8 +206,9 @@ void VR_Recenter(engine_t* engine) { } } - // Update menu orientation - VR_SetConfigFloat(VR_CONFIG_MENU_PITCH, hmdorientation.x); + // Update menu orientation. The screen stays upright: tilting it to the head's pitch at recenter + // time (often looking down while putting the headset on) left it at an angle for the session. + VR_SetConfigFloat(VR_CONFIG_MENU_PITCH, 0.0f); VR_SetConfigFloat(VR_CONFIG_MENU_YAW, 0.0f); } diff --git a/GPU/Common/VertexShaderGenerator.cpp b/GPU/Common/VertexShaderGenerator.cpp index 122d7240ba..097af96a19 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -544,11 +544,10 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { WRITE(p, " vec4 outPos = mul(u_proj_lens, viewPos);\n"); WRITE(p, " vec4 orgPos = mul(u_proj, viewPos);\n"); -#ifdef __LIBRETRO__ // Preserve game depth before clipping and viewport conversion. The VR lens - // changes XY, but depth must still agree with through-mode draws and clears. + // changes XY, but depth must still agree with through-mode draws and clears, + // and go through the viewport's Z scale (often negative on the PSP). WRITE(p, " outPos.z = orgPos.z * (outPos.w / orgPos.w);\n"); -#endif } else { WRITE(p, " vec4 outPos = mul(u_proj, viewPos);\n"); } @@ -1020,13 +1019,6 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag WRITE(p, " %sgl_Position = outPos;\n", compat.vsOutPrefix); if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { -#ifndef __LIBRETRO__ - // Z correction for the depth buffer - if (useHWTransform) { - WRITE(p, " %sgl_Position.z = orgPos.z / abs(orgPos.w) * abs(outPos.w);\n", compat.vsOutPrefix); - } -#endif - // HUD scaling WRITE(p, " %sgl_Position.x *= u_scaleX;\n", compat.vsOutPrefix); WRITE(p, " %sgl_Position.y *= u_scaleY;\n", compat.vsOutPrefix); diff --git a/docs/vr-openxr-vulkan.md b/docs/vr-openxr-vulkan.md index 22e4a29dda..1bc953c2b2 100644 --- a/docs/vr-openxr-vulkan.md +++ b/docs/vr-openxr-vulkan.md @@ -74,7 +74,7 @@ only on dirty split GTA's scene into two layers moving at different speeds. ### The OpenGL-only parts, and their Vulkan versions -OpenGL VR goes once these are confirmed on a headset: +OpenGL VR stays an option for a while. What it does its own way, and the Vulkan equivalent: - **Skyplane** (`compatvr.ini`, the GTA Stories games): `PreprocessSkyplane` edits GL render steps at replay, `PreprocessSkyplaneVulkan` does the same to Vulkan steps in `PreprocessSteps`, before @@ -95,6 +95,17 @@ The other VR compat flags (`ForceMono`, `ForceFlatScreen`, `IdentityViewHack`, ` `ProjectionHack`, `UnitsPerMeter`) are read in shared code (`PPSSPPVR.cpp`, the shader generator), so they don't depend on the backend. +### Fixes found on the headset + +- **Depth** was inverted, on GL too: the shader replaced the lens projection's Z with the game's + (`orgPos`) at the very end, skipping the viewport's Z scale, which broke when the viewport transform + moved into the shader. It's replaced before the transform now (libretro already did this). +- **Gamma on GL:** the GL eye swapchains are `GL_SRGB8_ALPHA8`, and GLES encodes writes to an sRGB + framebuffer, so our already sRGB-encoded colors came out too bright. Writes skip the encoding now + (`GL_EXT_sRGB_write_control`). +- **The flat screen** stays upright. It used to take the head's pitch at recenter time, often looking + down while putting the headset on. Recentering is the system gesture (hold the Meta button). + ## Phase 2: PC VR The desktop window (Windows, then Linux) stays the primary swapchain and shows a mirror. The @@ -105,5 +116,6 @@ becomes the non-pacing one. The OpenXR loader on desktop is our dynamic one (`Op ## Status -- [ ] Phase 1 +- [x] Phase 1: runs on a Quest 2 (menus, Burnout Legends, GTA LCS in flat mode, stereo and 6DoF on) + at 72 fps. Skyplane not checked specifically yet. - [ ] Phase 2 From 5a1d143ac2968ed06e8d8de7e9f4c721b4650c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 12:24:30 +0100 Subject: [PATCH 4/5] VR: Restart in-process when changing the backend A graphics restart recreates the activity, but on VR devices onDestroy called System.exit, so the app just quit. The XR instance (created for the old activity), the GL session (entered once per process) and the controller input (bound to the first session) all outlived the activity. Now the native shutdown takes OpenXR down (ShutdownVR: renderer, session, instance), the new activity sets it up again, and the exit is skipped for restarts. Input is created per session, which also keeps the controllers working when Vulkan recreates its session with the graphics context. The passthrough layer is destroyed with the renderer. Co-Authored-By: Claude Opus 5.5 --- Common/VR/PPSSPPVR.cpp | 18 ++++++++++++++--- Common/VR/PPSSPPVR.h | 5 ++++- Common/VR/VRBase.cpp | 3 +++ Common/VR/VRInput.cpp | 20 +++++++++++++++++++ Common/VR/VRInput.h | 1 + Common/VR/VRRenderer.cpp | 8 ++++++++ Core/EmuThread.cpp | 4 +--- android/jni/app-android.cpp | 5 +++++ .../src/org/ppsspp/ppsspp/PpssppActivity.java | 8 ++++++-- docs/vr-openxr-vulkan.md | 6 +++++- 10 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index e04a77b68b..a6b150c011 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -188,15 +188,27 @@ void InitVROnAndroid(void* vm, void* activity, const char* system, int version, } #endif -void EnterVR(bool firstStart) { - if (firstStart) { - engine_t* engine = VR_GetEngine(); +void EnterVR() { + engine_t* engine = VR_GetEngine(); + // The session outlives the render loop (it restarts when the surface comes back), not the native side. + if (engine->appState.Session == XR_NULL_HANDLE) { VR_EnterVR(engine); IN_VRInit(engine); } VR_SetConfig(VR_CONFIG_VIEWPORT_VALID, false); } +void ShutdownVR() { + engine_t* engine = VR_GetEngine(); + if (engine->appState.Instance == XR_NULL_HANDLE) { + return; + } + // GL's swapchains (Vulkan's go with its presentation). + VR_DestroyRenderer(engine); + VR_LeaveVR(engine); + VR_Destroy(engine); +} + void GetVRResolutionPerEye(int* width, int* height) { if (VR_GetEngine()->appState.Instance) { VR_GetResolution(VR_GetEngine(), width, height); diff --git a/Common/VR/PPSSPPVR.h b/Common/VR/PPSSPPVR.h index 3dde8b3871..0ce5ef0773 100644 --- a/Common/VR/PPSSPPVR.h +++ b/Common/VR/PPSSPPVR.h @@ -30,7 +30,10 @@ enum VRAppMode { // VR app flow integration bool IsVREnabled(); void InitVROnAndroid(void* vm, void* activity, const char* system, int version, const char* name); -void EnterVR(bool firstStart); +// GL: creates the session (once per instance, on the GL thread). +void EnterVR(); +// Tears down what InitVROnAndroid and EnterVR set up, so a new activity can start over in this process. +void ShutdownVR(); void GetVRResolutionPerEye(int* width, int* height); void SetVRCallbacks(void(*axis)(const AxisInput *axis, size_t count), bool(*key)(const KeyInput &key), void(*touch)(const TouchInput &touch)); diff --git a/Common/VR/VRBase.cpp b/Common/VR/VRBase.cpp index d92c506b2b..503476fea0 100644 --- a/Common/VR/VRBase.cpp +++ b/Common/VR/VRBase.cpp @@ -1,4 +1,5 @@ #include "VRBase.h" +#include "VRInput.h" #include #include @@ -165,6 +166,7 @@ void VR_Destroy( engine_t* engine ) { if (engine == &vr_engine) { xrDestroyInstance(engine->appState.Instance); ovrApp_Destroy(&engine->appState); + vr_initialized = 0; } } @@ -220,6 +222,7 @@ void VR_EnterVR( engine_t* engine ) { void VR_LeaveVR( engine_t* engine ) { if (engine->appState.Session) { + IN_VRShutdown(engine); OXR(xrDestroySpace(engine->appState.HeadSpace)); // StageSpace is optional. if (engine->appState.StageSpace != XR_NULL_HANDLE) { diff --git a/Common/VR/VRInput.cpp b/Common/VR/VRInput.cpp index 9a55562c37..f3be56b2fc 100644 --- a/Common/VR/VRInput.cpp +++ b/Common/VR/VRInput.cpp @@ -375,6 +375,26 @@ void IN_VRInit( engine_t *engine ) { inputInitialized = 1; } +void IN_VRShutdown( engine_t* engine ) { + if (!inputInitialized) { + return; + } + // The aim spaces belong to the session and the action set is attached to it, so a new session + // (a new graphics context or activity) needs new ones. + if (leftControllerAimSpace != XR_NULL_HANDLE) { + OXR(xrDestroySpace(leftControllerAimSpace)); + leftControllerAimSpace = XR_NULL_HANDLE; + } + if (rightControllerAimSpace != XR_NULL_HANDLE) { + OXR(xrDestroySpace(rightControllerAimSpace)); + rightControllerAimSpace = XR_NULL_HANDLE; + } + // Destroys its actions too. + OXR(xrDestroyActionSet(runningActionSet)); + runningActionSet = XR_NULL_HANDLE; + inputInitialized = 0; +} + void IN_VRInputFrame( engine_t* engine ) { // sync action data diff --git a/Common/VR/VRInput.h b/Common/VR/VRInput.h index 126cc56eaa..b882a627e9 100644 --- a/Common/VR/VRInput.h +++ b/Common/VR/VRInput.h @@ -29,6 +29,7 @@ typedef enum ovrButton_ { } ovrButton; void IN_VRInit( engine_t *engine ); +void IN_VRShutdown( engine_t *engine ); void IN_VRInputFrame( engine_t* engine ); uint32_t IN_VRGetButtonState( int controllerIndex ); XrVector2f IN_VRGetJoystickState( int controllerIndex ); diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 4d16c89917..5fcab45d2f 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -283,9 +283,17 @@ void VR_InitRenderer( engine_t* engine ) { } void VR_DestroyRenderer( engine_t* engine ) { + if (!initialized) { + return; + } if (VR_GetPlatformFlag(VRPlatformFlag::VR_PLATFORM_EXTENSION_PASSTHROUGH)) { if (passthroughRunning) { OXR(xrPassthroughLayerPauseFB(passthroughLayer)); + passthroughRunning = false; + } + if (passthroughLayer != XR_NULL_HANDLE) { + OXR(xrDestroyPassthroughLayerFB(passthroughLayer)); + passthroughLayer = XR_NULL_HANDLE; } OXR(xrPassthroughPauseFB(passthrough)); OXR(xrDestroyPassthroughFB(passthrough)); diff --git a/Core/EmuThread.cpp b/Core/EmuThread.cpp index 91f71ce30c..e2290f6927 100644 --- a/Core/EmuThread.cpp +++ b/Core/EmuThread.cpp @@ -165,9 +165,7 @@ void RunGraphicsLoop(GraphicsContext *graphicsContext, Application *application, std::thread emuThread = EmuThread_Start(graphicsContext, application, frame); if (IsVREnabled()) { - static bool vrFirstStart = true; - EnterVR(vrFirstStart); - vrFirstStart = false; + EnterVR(); } // Normally ThreadFrame() returning false is what gets us out of here - the frame callback diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 637002fc4e..2b0f450f7f 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -945,6 +945,11 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_shutdown(JNIEnv *, jclass) { INFO_LOG(Log::G3D, "Not shutting down renderer - not initialized"); } + // The instance belongs to this activity. A restart (recreate) creates a new one in this process. + if (IsVREnabled()) { + ShutdownVR(); + } + NativeShutdown(); g_VFS.Clear(); } diff --git a/android/src/org/ppsspp/ppsspp/PpssppActivity.java b/android/src/org/ppsspp/ppsspp/PpssppActivity.java index 006866e06c..25e3e0f5b6 100644 --- a/android/src/org/ppsspp/ppsspp/PpssppActivity.java +++ b/android/src/org/ppsspp/ppsspp/PpssppActivity.java @@ -148,6 +148,8 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList // This is to avoid losing the game/menu state etc when we are just // switched-away from or rotated etc. private boolean shuttingDown; + // Shutting down for a graphics restart (recreate), which comes back up in this process. + private boolean restarting; // There's one render loop thread. Whether it runs, and on which activity's surface (activityNumber). // surfaceChanged also fires for rotations and resizes, where the thread just keeps going. @@ -1133,8 +1135,9 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList Log.i(TAG, "in onDestroy, but not shutting down."); } } - // Really ugly workaround for VR issues when PPSSPP restarts - if (isVRDevice()) { + // Really ugly workaround for VR issues when PPSSPP restarts. A graphics restart is fine, since the + // native shutdown takes OpenXR down too and the new activity sets it up again. + if (isVRDevice() && !restarting) { System.exit(0); } Log.i(TAG, "onDestroy end"); @@ -1942,6 +1945,7 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList overrideShortcutParam = params; } shuttingDown = true; + restarting = true; recreate(); return true; } else if (command.equals("ask_permission") && params.equals("storage")) { diff --git a/docs/vr-openxr-vulkan.md b/docs/vr-openxr-vulkan.md index 1bc953c2b2..3819de58fa 100644 --- a/docs/vr-openxr-vulkan.md +++ b/docs/vr-openxr-vulkan.md @@ -105,6 +105,10 @@ so they don't depend on the backend. (`GL_EXT_sRGB_write_control`). - **The flat screen** stays upright. It used to take the head's pitch at recenter time, often looking down while putting the headset on. Recentering is the system gesture (hold the Meta button). +- **Restarting** (changing the backend): the native shutdown takes OpenXR down (renderer, input, + session, instance, `ShutdownVR`), and the recreated activity sets it up again in the same process. + It used to `System.exit` in `onDestroy` instead, which killed the restart. Controller input is + created per session, since its spaces and attached action set belong to one. ## Phase 2: PC VR @@ -117,5 +121,5 @@ becomes the non-pacing one. The OpenXR loader on desktop is our dynamic one (`Op ## Status - [x] Phase 1: runs on a Quest 2 (menus, Burnout Legends, GTA LCS in flat mode, stereo and 6DoF on) - at 72 fps. Skyplane not checked specifically yet. + at 72 fps, backend switching both ways. Skyplane not checked specifically yet. - [ ] Phase 2 From c5a51c084c8e819a150d1789dc89ce72aa28707b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 14:21:24 +0100 Subject: [PATCH 5/5] VR: Don't reject a saved Vulkan backend before OpenXR is up The config is validated before OpenXR is initialized, and VulkanMayBeAvailable said no in VR until it was, so a saved Vulkan setting was always replaced by the default ("Backend 3 not enabled"). Only say no once OpenXR is up without Vulkan support. app-android checks again after initializing it. Co-Authored-By: Claude Opus 5.5 --- Common/GPU/Vulkan/VulkanLoader.cpp | 5 +++-- Common/VR/VRVulkan.cpp | 8 ++++++++ Common/VR/VRVulkan.h | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanLoader.cpp b/Common/GPU/Vulkan/VulkanLoader.cpp index e3da6a3450..9182a2c55d 100644 --- a/Common/GPU/Vulkan/VulkanLoader.cpp +++ b/Common/GPU/Vulkan/VulkanLoader.cpp @@ -453,8 +453,9 @@ bool VulkanMayBeAvailable() { g_vulkanMayBeAvailable = true; return true; #else - // VR needs a runtime that takes a Vulkan session (not cached, OpenXR may not be up yet). - if (IsVREnabled() && !VR_VulkanSupported()) { + // VR needs a runtime that takes a Vulkan session. Not cached: the config is checked before OpenXR is + // up, when only the driver can say no, and app-android checks again after. + if (IsVREnabled() && VR_VulkanUnsupported()) { return false; } diff --git a/Common/VR/VRVulkan.cpp b/Common/VR/VRVulkan.cpp index 229bf34439..dc214581c1 100644 --- a/Common/VR/VRVulkan.cpp +++ b/Common/VR/VRVulkan.cpp @@ -68,6 +68,10 @@ bool VR_VulkanSupported() { return VR_GetEngine()->appState.Instance != XR_NULL_HANDLE && VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_VULKAN); } +bool VR_VulkanUnsupported() { + return VR_GetEngine()->appState.Instance != XR_NULL_HANDLE && !VR_GetPlatformFlag(VR_PLATFORM_EXTENSION_VULKAN); +} + std::vector VR_GetVulkanInstanceExtensions() { std::vector list = GetExtensionList("xrGetVulkanInstanceExtensionsKHR"); for (const std::string &ext : list) { @@ -253,6 +257,10 @@ bool VR_VulkanSupported() { return false; } +bool VR_VulkanUnsupported() { + return true; +} + std::vector VR_GetVulkanInstanceExtensions() { return {}; } diff --git a/Common/VR/VRVulkan.h b/Common/VR/VRVulkan.h index 5f57105f44..b12be6eecb 100644 --- a/Common/VR/VRVulkan.h +++ b/Common/VR/VRVulkan.h @@ -28,6 +28,9 @@ class VulkanPresentation; // True if OpenXR is up and the runtime can take a Vulkan session. bool VR_VulkanSupported(); +// True if we know there will be no Vulkan session: OpenXR is up without it, or this build can't have one. +// Before OpenXR is up (the config is checked before it), false. +bool VR_VulkanUnsupported(); // What the runtime needs enabled on our VkInstance and VkDevice. The device list's strings stay valid. std::vector VR_GetVulkanInstanceExtensions();