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_d3d12.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
17QOpenXRGraphicsD3D12::QOpenXRGraphicsD3D12()
18{
19 m_graphicsBinding.type = XR_TYPE_GRAPHICS_BINDING_D3D12_KHR;
20 m_graphicsRequirements.type = XR_TYPE_GRAPHICS_REQUIREMENTS_D3D12_KHR;
21}
22
23bool QOpenXRGraphicsD3D12::initialize(const QVector<XrExtensionProperties> &extensions)
24{
25 return hasExtension(extensions, XR_KHR_D3D12_ENABLE_EXTENSION_NAME);
26}
27
28QVector<const char *> QOpenXRGraphicsD3D12::getRequiredExtensions() const
29{
30 return { XR_KHR_D3D12_ENABLE_EXTENSION_NAME };
31}
32
33
34const XrBaseInStructure *QOpenXRGraphicsD3D12::handle() const
35{
36 return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
37}
38
39
40bool QOpenXRGraphicsD3D12::setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &)
41{
42 PFN_xrGetD3D12GraphicsRequirementsKHR pfnGetD3D12GraphicsRequirementsKHR = nullptr;
43 OpenXRHelpers::checkXrResult(xrGetInstanceProcAddr(instance, "xrGetD3D12GraphicsRequirementsKHR",
44 reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetD3D12GraphicsRequirementsKHR)),
45 instance);
46
47 if (!pfnGetD3D12GraphicsRequirementsKHR) {
48 qWarning("Could not resolve xrGetD3D12GraphicsRequirementsKHR; perhaps the OpenXR implementation does not support D3D12?");
49 return false;
50 }
51
52 OpenXRHelpers::checkXrResult(pfnGetD3D12GraphicsRequirementsKHR(instance, systemId, &m_graphicsRequirements),
53 instance);
54 return true;
55}
56
57bool QOpenXRGraphicsD3D12::finializeGraphics(QRhi *rhi)
58{
59 const QRhiD3D12NativeHandles *d3d12Rhi = static_cast<const QRhiD3D12NativeHandles *>(rhi->nativeHandles());
60 m_graphicsBinding.device = reinterpret_cast<ID3D12Device*>(d3d12Rhi->dev);
61 m_graphicsBinding.queue = reinterpret_cast<ID3D12CommandQueue*>(d3d12Rhi->commandQueue);
62 m_rhi = rhi;
63
64 return true;
65}
66
67
68int64_t QOpenXRGraphicsD3D12::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 QOpenXRGraphicsD3D12::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*> QOpenXRGraphicsD3D12::allocateSwapchainImages(int count, XrSwapchain swapchain)
102{
103 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
104 QVector<XrSwapchainImageD3D12KHR> swapchainImageBuffer(count);
105 for (XrSwapchainImageD3D12KHR& image : swapchainImageBuffer) {
106 image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR;
107 swapchainImages.push_back(reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
108 }
109 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
110 return swapchainImages;
111}
112
113
114QQuickRenderTarget QOpenXRGraphicsD3D12::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 ID3D12Resource* const colorTexture = reinterpret_cast<const XrSwapchainImageD3D12KHR*>(swapchainImage)->texture;
123
124 DXGI_FORMAT viewFormat = DXGI_FORMAT(swapchainFormat);
125 switch (swapchainFormat) {
126 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
127 viewFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
128 break;
129 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
130 viewFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
131 break;
132 default:
133 break;
134 }
135
136 QQuickRenderTarget::Flags flags;
137 if (samples > 1)
138 flags |= QQuickRenderTarget::Flag::MultisampleResolve;
139
140 return QQuickRenderTarget::fromD3D12Texture(colorTexture,
141 0,
142 swapchainFormat,
143 viewFormat,
144 QSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height),
145 samples,
146 arraySize,
147 flags);
148
149 // No depthSwapchainImage support because ResolveDepthStencil will be
150 // unsupported with D3D11/12 no matter what.
151 Q_UNUSED(depthSwapchainImage);
152 Q_UNUSED(depthSwapchainFormat);
153}
154
155
156void QOpenXRGraphicsD3D12::setupWindow(QQuickWindow *quickWindow)
157{
158 quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromAdapter(m_graphicsRequirements.adapterLuid.LowPart,
159 m_graphicsRequirements.adapterLuid.HighPart,
160 m_graphicsRequirements.minFeatureLevel));
161}
162
163QT_END_NAMESPACE
Combined button and popup list for selecting options.