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