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 e26f8f8eea..39a45a12bd 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) @@ -451,8 +452,9 @@ 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: 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/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 109a79fdde..0e9620d474 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; @@ -114,10 +115,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 +128,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 +152,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 +171,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(); @@ -231,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/GPU/Vulkan/VulkanQueueRunner.h b/Common/GPU/Vulkan/VulkanQueueRunner.h index 90e8abf76f..dbd0419052 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.h +++ b/Common/GPU/Vulkan/VulkanQueueRunner.h @@ -281,7 +281,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..a6b150c011 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]; @@ -191,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); @@ -523,6 +532,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; } @@ -542,6 +590,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); } @@ -610,6 +662,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); @@ -620,6 +676,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 +692,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..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)); @@ -42,6 +45,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 @@ -50,6 +55,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..503476fea0 100644 --- a/Common/VR/VRBase.cpp +++ b/Common/VR/VRBase.cpp @@ -1,4 +1,5 @@ #include "VRBase.h" +#include "VRInput.h" #include #include @@ -39,9 +40,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 @@ -144,6 +166,7 @@ void VR_Destroy( engine_t* engine ) { if (engine == &vr_engine) { xrDestroyInstance(engine->appState.Instance); ovrApp_Destroy(&engine->appState); + vr_initialized = 0; } } @@ -171,6 +194,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; @@ -193,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/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..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 @@ -20,6 +43,7 @@ #include #include #include +#include #if !defined(_WIN32) #include @@ -50,7 +74,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 +267,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 +288,14 @@ 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])); + // 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; } @@ -184,6 +311,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 +333,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 +361,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 +384,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/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 86df230f05..5fcab45d2f 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)); @@ -183,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); } @@ -237,7 +261,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}; @@ -257,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)); @@ -272,6 +306,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 +367,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 +384,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 +409,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 +420,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 +482,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 +523,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 +545,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 +557,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 +582,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 +618,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 +640,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..dc214581c1 --- /dev/null +++ b/Common/VR/VRVulkan.cpp @@ -0,0 +1,284 @@ +// 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); +} + +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) { + 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; +} + +bool VR_VulkanUnsupported() { + return true; +} + +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..b12be6eecb --- /dev/null +++ b/Common/VR/VRVulkan.h @@ -0,0 +1,47 @@ +// 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(); +// 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(); +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..e2290f6927 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; } @@ -149,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/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..dc0640a2c0 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,97 @@ 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; + } + + 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. + { + 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 f8c5d60165..af5b7b4c40 100644 --- a/GPU/Common/VertexShaderGenerator.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -180,7 +180,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); @@ -544,8 +544,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) { @@ -558,11 +563,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"); } @@ -1043,13 +1047,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/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index eb66cb27a4..b3cfe624a3 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 e5b2670cf8..fc76630bf9 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -394,7 +394,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; @@ -553,7 +553,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..a63654c2c8 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,15 @@ u32 GPU_Vulkan::CheckGPUFeatures() const { } } - if (g_Config.bStereoRendering && draw_->GetDeviceCaps().multiViewSupported) { + 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 (IsVREnabled() && 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 c9a57bb33f..efbb93c402 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 7d3eaa4243..c8f8e86df8 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1405,6 +1405,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..2b0f450f7f 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) { @@ -943,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 new file mode 100644 index 0000000000..3819de58fa --- /dev/null +++ b/docs/vr-openxr-vulkan.md @@ -0,0 +1,125 @@ +# 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. + +### The OpenGL-only parts, and their Vulkan versions + +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 + 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. + +### 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). +- **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 + +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 + +- [x] Phase 1: runs on a Quest 2 (menus, Burnout Legends, GTA LCS in flat mode, stereo and 6DoF on) + at 72 fps, backend switching both ways. Skyplane not checked specifically yet. +- [ ] 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 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; }