Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopenxrgraphics_d3d11.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5
7
8#include <QtQuick/QQuickWindow>
9#include <QtQuick/QQuickGraphicsDevice>
10
11#include <rhi/qrhi.h>
12
14
15QOpenXRGraphicsD3D11::QOpenXRGraphicsD3D11()
16{
17 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
18 m_graphicsRequirements.type = XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR;
19}
20
21bool QOpenXRGraphicsD3D11::isExtensionSupported(const QVector<XrExtensionProperties> &extensions) const
22{
23 for (const auto &extension : extensions) {
24 if (!strcmp(XR_KHR_D3D11_ENABLE_EXTENSION_NAME,
25 extension.extensionName))
26 return true;
27 }
28 return false;
29}
30
31
32const char *QOpenXRGraphicsD3D11::extensionName() const
33{
34 return XR_KHR_D3D11_ENABLE_EXTENSION_NAME;
35}
36
37
38const XrBaseInStructure *QOpenXRGraphicsD3D11::handle() const
39{
40 return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
41}
42
43
44bool QOpenXRGraphicsD3D11::setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &)
45{
46 PFN_xrGetD3D11GraphicsRequirementsKHR pfnGetD3D11GraphicsRequirementsKHR = nullptr;
47 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance, "xrGetD3D11GraphicsRequirementsKHR",
48 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetD3D11GraphicsRequirementsKHR)),
49 instance);
50
51 if (!pfnGetD3D11GraphicsRequirementsKHR) {
52 qWarning("Could not resolve xrGetD3D11GraphicsRequirementsKHR; perhaps the OpenXR implementation does not support D3D11?");
53 return false;
54 }
55
56 // Create the D3D11 device for the adapter associated with the system.
57 OpenXRHelpers::checkXrResult(pfnGetD3D11GraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
58 instance);
59 return true;
60}
61
62bool QOpenXRGraphicsD3D11::finializeGraphics(QRhi *rhi)
63{
64 const QRhiD3D11NativeHandles *d3d11Rhi = static_cast<const QRhiD3D11NativeHandles *>(rhi->nativeHandles());
65 m_graphicsBinding.device = reinterpret_cast<ID3D11Device*>(d3d11Rhi->dev);
66 m_rhi = rhi;
67
68 return true;
69}
70
71
72int64_t QOpenXRGraphicsD3D11::colorSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
73{
74 // List of supported color swapchain formats.
75 constexpr DXGI_FORMAT supportedColorSwapchainFormats[] = {
76 DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
77 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
78 DXGI_FORMAT_B8G8R8A8_UNORM,
79 DXGI_FORMAT_R8G8B8A8_UNORM
80 };
81
82 auto swapchainFormatIt = std::find_first_of(std::begin(supportedColorSwapchainFormats),
83 std::end(supportedColorSwapchainFormats),
84 swapchainFormats.begin(),
85 swapchainFormats.end());
86
87 return *swapchainFormatIt;
88}
89
90int64_t QOpenXRGraphicsD3D11::depthSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
91{
92 // in order of preference
93 constexpr int64_t supportedDepthSwapchainFormats[] = {
94 DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
95 DXGI_FORMAT_D32_FLOAT,
96 DXGI_FORMAT_D16_UNORM
97 };
98
99 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
100 std::end(supportedDepthSwapchainFormats),
101 swapchainFormats.begin(),
102 swapchainFormats.end());
103}
104
105QVector<XrSwapchainImageBaseHeader*> QOpenXRGraphicsD3D11::allocateSwapchainImages(int count, XrSwapchain swapchain)
106{
107 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
108 QVector<XrSwapchainImageD3D11KHR> swapchainImageBuffer(count);
109 for (XrSwapchainImageD3D11KHR& image : swapchainImageBuffer) {
110 image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR;
111 swapchainImages.push_back(reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
112 }
113 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
114 return swapchainImages;
115}
116
117
118QQuickRenderTarget QOpenXRGraphicsD3D11::renderTarget(const XrSwapchainSubImage &subImage,
119 const XrSwapchainImageBaseHeader *swapchainImage,
120 quint64 swapchainFormat,
121 int samples,
122 int arraySize,
123 const XrSwapchainImageBaseHeader *depthSwapchainImage,
124 quint64 depthSwapchainFormat) const
125{
126 ID3D11Texture2D* const colorTexture = reinterpret_cast<const XrSwapchainImageD3D11KHR*>(swapchainImage)->texture;
127
128 // No real view format support for D3D11 in QRhi, but can strip off the
129 // _SRGB and pass that in as the texture format, which in the end also
130 // avoids incorrect (unwanted) linear->sRGB conversions.
131 DXGI_FORMAT viewFormat = DXGI_FORMAT(swapchainFormat);
132 switch (swapchainFormat) {
133 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
134 viewFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
135 break;
136 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
137 viewFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
138 break;
139 default:
140 break;
141 }
142
143 if (arraySize > 1)
144 qWarning("Qt Quick 3D XR: The D3D11 integration has no support for multiview");
145
146 QQuickRenderTarget::Flags flags;
147 if (samples > 1)
148 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
149
150 return QQuickRenderTarget::fromD3D11Texture(colorTexture,
151 viewFormat,
152 QSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height),
153 samples,
154 flags);
155
156 // No depthSwapchainImage support because ResolveDepthStencil will be
157 // unsupported with D3D11/12 no matter what.
158 Q_UNUSED(depthSwapchainImage);
159 Q_UNUSED(depthSwapchainFormat);
160}
161
162
163void QOpenXRGraphicsD3D11::setupWindow(QQuickWindow *quickWindow)
164{
165 quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromAdapter(m_graphicsRequirements.adapterLuid.LowPart,
166 m_graphicsRequirements.adapterLuid.HighPart,
167 m_graphicsRequirements.minFeatureLevel));
168}
169
170QT_END_NAMESPACE
bool checkXrResult(XrResult result, XrInstance instance)