Skip to content

Commit

Permalink
hello_xr: Make Vulkan's mirror window optional
Browse files Browse the repository at this point in the history
The Vulkan's mirror window is disabled default, and we use
"--mirrorwindow|-mw true" to enable it.

Signed-off-by: utzcoz <[email protected]>
  • Loading branch information
utzcoz committed Jul 2, 2023
1 parent f122f9f commit 655f2cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
53 changes: 25 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,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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<int64_t>& runtimeFormats) const override {
Expand Down Expand Up @@ -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>& 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 +1679,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
5 changes: 4 additions & 1 deletion src/tests/hello_xr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ 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]");
"[--blendmode|-bm <Blend mode>] [--space|-s <Space>] [--mirrorwindow|-mw <Enable 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 Mirror Window: true, false");
}

bool UpdateOptionsFromCommandLine(Options& options, int argc, char* argv[]) {
Expand All @@ -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")) {
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

0 comments on commit 655f2cf

Please sign in to comment.