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