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