From 655f2cff737a10336f0704e87b58d1a4e35ccc9d Mon Sep 17 00:00:00 2001 From: utzcoz Date: Wed, 5 Apr 2023 10:59:34 +0800 Subject: [PATCH] hello_xr: Make Vulkan's mirror window optional The Vulkan's mirror window is disabled default, and we use "--mirrorwindow|-mw true" to enable it. Signed-off-by: utzcoz --- src/tests/hello_xr/graphicsplugin_vulkan.cpp | 53 +++++++++----------- src/tests/hello_xr/main.cpp | 5 +- src/tests/hello_xr/options.h | 2 + 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/tests/hello_xr/graphicsplugin_vulkan.cpp b/src/tests/hello_xr/graphicsplugin_vulkan.cpp index 46f718777..42caa902e 100644 --- a/src/tests/hello_xr/graphicsplugin_vulkan.cpp +++ b/src/tests/hello_xr/graphicsplugin_vulkan.cpp @@ -16,11 +16,6 @@ #include #endif -#if defined(VK_USE_PLATFORM_WIN32_KHR) -// Define USE_MIRROR_WINDOW to open a otherwise-unused window for e.g. RenderDoc -#define USE_MIRROR_WINDOW -#endif - // glslangValidator doesn't wrap its output in brackets if you don't have it define the whole array. #if defined(USE_GLSLANGVALIDATOR) #define SPV_PREFIX { @@ -1043,7 +1038,7 @@ struct SwapchainImageContext { VulkanDebugObjectNamer m_namer; }; -#if defined(USE_MIRROR_WINDOW) +#if defined(VK_USE_PLATFORM_WIN32_KHR) // Swapchain struct Swapchain { VkFormat format{VK_FORMAT_B8G8R8A8_SRGB}; @@ -1270,7 +1265,7 @@ void Swapchain::Present(VkQueue queue, VkSemaphore drawComplete) { } CHECK_VKRESULT(res, "vkQueuePresentKHR"); } -#endif // defined(USE_MIRROR_WINDOW) +#endif // defined(VK_USE_PLATFORM_WIN32_KHR) struct VulkanGraphicsPlugin : public IGraphicsPlugin { VulkanGraphicsPlugin(const std::shared_ptr& options, std::shared_ptr /*unused*/) @@ -1359,14 +1354,14 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { } // TODO add back VK_EXT_debug_report code for compatibility with older systems? (Android) } -#if defined(USE_MIRROR_WINDOW) - extensions.push_back("VK_KHR_surface"); + if (m_enableMirrorWindow) { + extensions.push_back("VK_KHR_surface"); #if defined(VK_USE_PLATFORM_WIN32_KHR) - extensions.push_back("VK_KHR_win32_surface"); + extensions.push_back("VK_KHR_win32_surface"); #else #error CreateSurface not supported on this OS #endif // defined(VK_USE_PLATFORM_WIN32_KHR) -#endif // defined(USE_MIRROR_WINDOW) + } VkApplicationInfo appInfo{VK_STRUCTURE_TYPE_APPLICATION_INFO}; appInfo.pApplicationName = "hello_xr"; @@ -1438,9 +1433,9 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { VkPhysicalDeviceFeatures features{}; // features.samplerAnisotropy = VK_TRUE; -#if defined(USE_MIRROR_WINDOW) - deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); -#endif + if (m_enableMirrorWindow) { + deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + } VkDeviceCreateInfo deviceInfo{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; deviceInfo.queueCreateInfoCount = 1; @@ -1532,16 +1527,16 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { m_drawBuffer.UpdateIndices(Geometry::c_cubeIndices, numCubeIdicies, 0); m_drawBuffer.UpdateVertices(Geometry::c_cubeVertices, numCubeVerticies, 0); -#if defined(USE_MIRROR_WINDOW) - m_swapchain.Create(m_vkInstance, m_vkPhysicalDevice, m_vkDevice, m_graphicsBinding.queueFamilyIndex); + if (m_enableMirrorWindow) { + m_swapchain.Create(m_vkInstance, m_vkPhysicalDevice, m_vkDevice, m_graphicsBinding.queueFamilyIndex); - m_cmdBuffer.Reset(); - m_cmdBuffer.Begin(); - m_swapchain.Prepare(m_cmdBuffer.buf); - m_cmdBuffer.End(); - m_cmdBuffer.Exec(m_vkQueue); - m_cmdBuffer.Wait(); -#endif + m_cmdBuffer.Reset(); + m_cmdBuffer.Begin(); + m_swapchain.Prepare(m_cmdBuffer.buf); + m_cmdBuffer.End(); + m_cmdBuffer.Exec(m_vkQueue); + m_cmdBuffer.Wait(); + } } int64_t SelectColorSwapchainFormat(const std::vector& runtimeFormats) const override { @@ -1651,18 +1646,19 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { m_cmdBuffer.End(); m_cmdBuffer.Exec(m_vkQueue); -#if defined(USE_MIRROR_WINDOW) // Cycle the window's swapchain on the last view rendered - if (swapchainContext == &m_swapchainImageContexts.back()) { + if (m_enableMirrorWindow && swapchainContext == &m_swapchainImageContexts.back()) { m_swapchain.Acquire(); m_swapchain.Present(m_vkQueue); } -#endif } uint32_t GetSupportedSwapchainSampleCount(const XrViewConfigurationView&) override { return VK_SAMPLE_COUNT_1_BIT; } - void UpdateOptions(const std::shared_ptr& options) override { m_clearColor = options->GetBackgroundClearColor(); } + void UpdateOptions(const std::shared_ptr& options) override { + m_clearColor = options->GetBackgroundClearColor(); + m_enableMirrorWindow = options->EnableMirrorWindow; + } protected: XrGraphicsBindingVulkan2KHR m_graphicsBinding{XR_TYPE_GRAPHICS_BINDING_VULKAN2_KHR}; @@ -1683,8 +1679,9 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { PipelineLayout m_pipelineLayout{}; VertexBuffer m_drawBuffer{}; std::array m_clearColor; + bool m_enableMirrorWindow = false; -#if defined(USE_MIRROR_WINDOW) +#if defined(VK_USE_PLATFORM_WIN32_KHR) Swapchain m_swapchain{}; #endif diff --git a/src/tests/hello_xr/main.cpp b/src/tests/hello_xr/main.cpp index af1b3e675..4506051d6 100644 --- a/src/tests/hello_xr/main.cpp +++ b/src/tests/hello_xr/main.cpp @@ -68,12 +68,13 @@ void ShowHelp() { // TODO: Improve/update when things are more settled. Log::Write(Log::Level::Info, "HelloXr --graphics|-g [--formfactor|-ff
] [--viewconfig|-vc ] " - "[--blendmode|-bm ] [--space|-s ] [--verbose|-v]"); + "[--blendmode|-bm ] [--space|-s ] [--mirrorwindow|-mw ] [--verbose|-v]"); Log::Write(Log::Level::Info, "Graphics APIs: D3D11, D3D12, OpenGLES, OpenGL, Vulkan2, Vulkan"); Log::Write(Log::Level::Info, "Form factors: Hmd, Handheld"); Log::Write(Log::Level::Info, "View configurations: Mono, Stereo"); Log::Write(Log::Level::Info, "Environment blend modes: Opaque, Additive, AlphaBlend"); Log::Write(Log::Level::Info, "Spaces: View, Local, Stage"); + Log::Write(Log::Level::Info, "Enable Mirror Window: true, false"); } bool UpdateOptionsFromCommandLine(Options& options, int argc, char* argv[]) { @@ -99,6 +100,8 @@ bool UpdateOptionsFromCommandLine(Options& options, int argc, char* argv[]) { options.EnvironmentBlendMode = getNextArg(); } else if (EqualsIgnoreCase(arg, "--space") || EqualsIgnoreCase(arg, "-s")) { options.AppSpace = getNextArg(); + } else if (EqualsIgnoreCase(arg, "--mirrorwindow") || EqualsIgnoreCase(arg, "-mw")) { + options.EnableMirrorWindow = EqualsIgnoreCase(getNextArg(), "true"); } else if (EqualsIgnoreCase(arg, "--verbose") || EqualsIgnoreCase(arg, "-v")) { Log::SetLevel(Log::Level::Verbose); } else if (EqualsIgnoreCase(arg, "--help") || EqualsIgnoreCase(arg, "-h")) { diff --git a/src/tests/hello_xr/options.h b/src/tests/hello_xr/options.h index 5ff7c912d..c7083f745 100644 --- a/src/tests/hello_xr/options.h +++ b/src/tests/hello_xr/options.h @@ -63,6 +63,8 @@ struct Options { std::string AppSpace{"Local"}; + bool EnableMirrorWindow = false; + struct { XrFormFactor FormFactor{XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY};