Skyscraper 2.0
OgreOpenXRSwapchain.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.0 - OpenXR Swapchain
3 Portions Copyright (C)2024 Ryan Thoryk
4 MIT license - see LICENSE file
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8*/
9
10/*
11 Original work produced by Glastonbridge Software Limited. This code is provided under the MIT license.
12 https://github.com/glastonbridge/OgreOpenXRRenderWindow
13*/
14
15#include <d3d11.h>
16#include "OgreOpenXRSwapchain.h"
17#include "OgreOpenXRState.h"
19
21 DXGI_FORMAT format,
22 uint32_t width,
23 uint32_t height,
24 uint32_t arraySize,
25 uint32_t sampleCount,
26 XrSwapchainCreateFlags createFlags,
27 XrSwapchainUsageFlags usageFlags) {
28 SwapchainD3D11 swapchain;
29 swapchain.Format = format;
30 swapchain.Width = width;
31 swapchain.Height = height;
32 swapchain.ArraySize = arraySize;
33
34 XrSwapchainCreateInfo swapchainCreateInfo{ XR_TYPE_SWAPCHAIN_CREATE_INFO };
35 swapchainCreateInfo.arraySize = arraySize;
36 swapchainCreateInfo.format = format;
37 swapchainCreateInfo.width = width;
38 swapchainCreateInfo.height = height;
39 swapchainCreateInfo.mipCount = 1;
40 swapchainCreateInfo.faceCount = 1;
41 swapchainCreateInfo.sampleCount = sampleCount;
42 swapchainCreateInfo.createFlags = createFlags;
43 swapchainCreateInfo.usageFlags = usageFlags;
44
45 CHECK_XRCMD(xrCreateSwapchain(session, &swapchainCreateInfo, swapchain.Handle.Put(xrDestroySwapchain)));
46
47 uint32_t chainLength;
48 CHECK_XRCMD(xrEnumerateSwapchainImages(swapchain.Handle.Get(), 0, &chainLength, nullptr));
49
50 swapchain.Images.resize(chainLength, { XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR });
51 CHECK_XRCMD(xrEnumerateSwapchainImages(swapchain.Handle.Get(),
52 (uint32_t)swapchain.Images.size(),
53 &chainLength,
54 reinterpret_cast<XrSwapchainImageBaseHeader*>(swapchain.Images.data())));
55
56 return swapchain;
57}
58
59uint32_t AcquireAndWaitForSwapchainImage(XrSwapchain handle) {
60 uint32_t swapchainImageIndex;
61 XrSwapchainImageAcquireInfo acquireInfo{ XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO };
62 CHECK_XRCMD(xrAcquireSwapchainImage(handle, &acquireInfo, &swapchainImageIndex));
63
64 XrSwapchainImageWaitInfo waitInfo{ XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO };
65 waitInfo.timeout = XR_INFINITE_DURATION;
66 CHECK_XRCMD(xrWaitSwapchainImage(handle, &waitInfo));
67
68 return swapchainImageIndex;
69}
70
72{
73}
74
76{
77 XrSystemProperties systemProperties{ XR_TYPE_SYSTEM_PROPERTIES };
78
79 state->GetSystemProperties(systemProperties);
80
81 // Select color and depth swapchain pixel formats.
82 const auto [colorSwapchainFormat, depthSwapchainFormat] = state->SelectSwapchainPixelFormats();
83 ColorSwapchainPixelFormat = colorSwapchainFormat;
84 DepthSwapchainPixelFormat = depthSwapchainFormat;
85
86 // Query and cache view configuration views.
87 uint32_t viewCount;
89 xrEnumerateViewConfigurationViews(state->GetInstanceHandle().Get(), state->GetSystemId(), state->primaryViewConfigType, 0, &viewCount, nullptr));
90 CHECK(viewCount == 2);
91
92 // Create swapchains with texture array for color and depth images.
93 // The texture array has the size of viewCount, and they are rendered in a single pass using VPRT.
94 const uint32_t textureArraySize = 1;
95 ColorSwapchain =
97 colorSwapchainFormat,
98 viewProjection->getWidth(),
99 viewProjection->getHeight(),
100 textureArraySize,
101 viewProjection->getRecommendedSampleCount(),
102 0 /*createFlags*/,
103 XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT);
104
105 DepthSwapchain =
107 depthSwapchainFormat,
108 viewProjection->getWidth(),
109 viewProjection->getHeight(),
110 textureArraySize,
111 viewProjection->getRecommendedSampleCount(),
112 0 /*createFlags*/,
113 XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
114}
115
117{
118 CHECK(ColorSwapchain.Width == DepthSwapchain.Width);
119 CHECK(ColorSwapchain.Height == DepthSwapchain.Height);
120
121 colorSwapchainImageIndex = AcquireAndWaitForSwapchainImage(ColorSwapchain.Handle.Get());
122 depthSwapchainImageIndex = AcquireAndWaitForSwapchainImage(DepthSwapchain.Handle.Get());
123 isAcquired=true;
124}
125
127{
128 //isAcquired = false;
129 if (ColorSwapchain.Handle.Get()) {
130 XrSwapchainImageReleaseInfo releaseInfo{ XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO };
131 CHECK_XRCMD(xrReleaseSwapchainImage(ColorSwapchain.Handle.Get(), &releaseInfo));
132 CHECK_XRCMD(xrReleaseSwapchainImage(DepthSwapchain.Handle.Get(), &releaseInfo));
133 }
134}
135
137{
138 return { {0, 0}, {(int32_t)ColorSwapchain.Width, (int32_t)ColorSwapchain.Height} };
139}
140
141ID3D11Texture2D* Ogre::OpenXRSwapchain::getSurface(size_t index) const
142{
143 return ColorSwapchain.Images[index].texture;
144}
145
147{
148 return ColorSwapchain.Handle.Get();
149}
150
152{
153 return DepthSwapchain.Handle.Get();
154}
155
157{
158 if (!isAcquired) return nullptr;
159 return ColorSwapchain.Images[colorSwapchainImageIndex].texture;
160}
161
163{
164 if (!isAcquired) return nullptr;
165 return DepthSwapchain.Images[depthSwapchainImageIndex].texture;
166}
SwapchainD3D11 CreateSwapchainD3D11(XrSession session, DXGI_FORMAT format, uint32_t width, uint32_t height, uint32_t arraySize, uint32_t sampleCount, XrSwapchainCreateFlags createFlags, XrSwapchainUsageFlags usageFlags)
uint32_t AcquireAndWaitForSwapchainImage(XrSwapchain handle)
#define CHECK(exp)
Definition XrError.h:59
#define CHECK_XRCMD(cmd)
Definition XrError.h:14
void GetSystemProperties(XrSystemProperties &systemProperties)
const uint64_t GetSystemId()
static constexpr XrViewConfigurationType primaryViewConfigType
const xr::InstanceHandle & GetInstanceHandle()
std::tuple< DXGI_FORMAT, DXGI_FORMAT > SelectSwapchainPixelFormats()
xr::SessionHandle & GetSession()
void Initialize(OpenXRState *state, OpenXRViewProjection *viewProjection)
ID3D11Texture2D * getSurface(size_t index) const
ID3D11Texture2D * getDepthTexture()
ID3D11Texture2D * getColorTexture()
HandleType Get() const noexcept
Definition XrHandle.h:51
HandleType * Put(PFN_DestroyFunction destroyFunction) noexcept
Definition XrHandle.h:56
std::vector< XrSwapchainImageD3D11KHR > Images
xr::SwapchainHandle Handle