diff --git a/changes/sdk/pr.395.gh.OpenXR-SDK-Source.md b/changes/sdk/pr.395.gh.OpenXR-SDK-Source.md new file mode 100644 index 000000000..ca97d6101 --- /dev/null +++ b/changes/sdk/pr.395.gh.OpenXR-SDK-Source.md @@ -0,0 +1 @@ +hello_xr: Make Vulkan's mirror window optional diff --git a/src/tests/hello_xr/graphicsplugin_vulkan.cpp b/src/tests/hello_xr/graphicsplugin_vulkan.cpp index f120a8b57..6de142b95 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}; @@ -1280,7 +1275,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*/) @@ -1369,14 +1364,12 @@ 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 defined(VK_USE_PLATFORM_WIN32_KHR) - extensions.push_back("VK_KHR_win32_surface"); -#else -#error CreateSurface not supported on this OS + if (m_enableMirrorWindow) { + extensions.push_back("VK_KHR_surface"); + extensions.push_back("VK_KHR_win32_surface"); + } #endif // defined(VK_USE_PLATFORM_WIN32_KHR) -#endif // defined(USE_MIRROR_WINDOW) VkDebugUtilsMessengerCreateInfoEXT debugInfo{VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT}; debugInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; @@ -1446,8 +1439,10 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { VkPhysicalDeviceFeatures features{}; // features.samplerAnisotropy = VK_TRUE; -#if defined(USE_MIRROR_WINDOW) - deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); +#if defined(VK_USE_PLATFORM_WIN32_KHR) + if (m_enableMirrorWindow) { + deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + } #endif VkDeviceCreateInfo deviceInfo{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; @@ -1540,15 +1535,17 @@ 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); - - m_cmdBuffer.Reset(); - m_cmdBuffer.Begin(); - m_swapchain.Prepare(m_cmdBuffer.buf); - m_cmdBuffer.End(); - m_cmdBuffer.Exec(m_vkQueue); - m_cmdBuffer.Wait(); +#if defined(VK_USE_PLATFORM_WIN32_KHR) + 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 } @@ -1658,9 +1655,9 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { m_cmdBuffer.End(); m_cmdBuffer.Exec(m_vkQueue); -#if defined(USE_MIRROR_WINDOW) +#if defined(VK_USE_PLATFORM_WIN32_KHR) // 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.Wait(); m_swapchain.Present(m_vkQueue); @@ -1670,7 +1667,10 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin { 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}; @@ -1691,8 +1691,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 fdee60e37..0cc606fa6 100644 --- a/src/tests/hello_xr/main.cpp +++ b/src/tests/hello_xr/main.cpp @@ -66,14 +66,16 @@ bool UpdateOptionsFromSystemProperties(Options& options) { #else 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]"); - Log::Write(Log::Level::Info, "Graphics APIs: D3D11, D3D12, OpenGLES, OpenGL, Vulkan2, Vulkan, Metal"); - 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, + "HelloXr --graphics|-g [--formfactor|-ff ] [--viewconfig|-vc ] " + "[--blendmode|-bm ] [--space|-s ] [--mirrorwindow|-mw ] [--verbose|-v]"); + Log::Write(Log::Level::Info, "Graphics APIs: D3D11, D3D12, OpenGLES, OpenGL, Vulkan2, Vulkan, Metal"); + 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 Vulkan Mirror Window: true, false"); } bool UpdateOptionsFromCommandLine(Options& options, int argc, char* argv[]) { @@ -99,6 +101,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 058260641..bcadb708a 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};