Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hello_xr: Make Vulkan's mirror window optional #395

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/sdk/pr.395.gh.OpenXR-SDK-Source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello_xr: Make Vulkan's mirror window optional
57 changes: 29 additions & 28 deletions src/tests/hello_xr/graphicsplugin_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
#include <shaderc/shaderc.hpp>
#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 {
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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>& options, std::shared_ptr<IPlatformPlugin> /*unused*/)
Expand Down Expand Up @@ -1359,14 +1354,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)

VkApplicationInfo appInfo{VK_STRUCTURE_TYPE_APPLICATION_INFO};
appInfo.pApplicationName = "hello_xr";
Expand Down Expand Up @@ -1438,8 +1431,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};
Expand Down Expand Up @@ -1532,15 +1527,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
}

Expand Down Expand Up @@ -1651,9 +1648,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.Present(m_vkQueue);
}
Expand All @@ -1662,7 +1659,10 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin {

uint32_t GetSupportedSwapchainSampleCount(const XrViewConfigurationView&) override { return VK_SAMPLE_COUNT_1_BIT; }

void UpdateOptions(const std::shared_ptr<Options>& options) override { m_clearColor = options->GetBackgroundClearColor(); }
void UpdateOptions(const std::shared_ptr<Options>& options) override {
m_clearColor = options->GetBackgroundClearColor();
m_enableMirrorWindow = options->EnableMirrorWindow;
}

protected:
XrGraphicsBindingVulkan2KHR m_graphicsBinding{XR_TYPE_GRAPHICS_BINDING_VULKAN2_KHR};
Expand All @@ -1683,8 +1683,9 @@ struct VulkanGraphicsPlugin : public IGraphicsPlugin {
PipelineLayout m_pipelineLayout{};
VertexBuffer<Geometry::Vertex> m_drawBuffer{};
std::array<float, 4> m_clearColor;
bool m_enableMirrorWindow = false;

#if defined(USE_MIRROR_WINDOW)
#if defined(VK_USE_PLATFORM_WIN32_KHR)
Swapchain m_swapchain{};
#endif

Expand Down
20 changes: 12 additions & 8 deletions src/tests/hello_xr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Graphics API> [--formfactor|-ff <Form factor>] [--viewconfig|-vc <View config>] "
"[--blendmode|-bm <Blend mode>] [--space|-s <Space>] [--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,
"HelloXr --graphics|-g <Graphics API> [--formfactor|-ff <Form factor>] [--viewconfig|-vc <View config>] "
"[--blendmode|-bm <Blend mode>] [--space|-s <Space>] [--mirrorwindow|-mw <Enable Vulkan Mirror Window>] [--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 Vulkan Mirror Window: true, false");
}

bool UpdateOptionsFromCommandLine(Options& options, int argc, char* argv[]) {
Expand All @@ -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")) {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/hello_xr/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct Options {

std::string AppSpace{"Local"};

bool EnableMirrorWindow = false;

struct {
XrFormFactor FormFactor{XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY};

Expand Down