Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qopenxrgraphics_d3d12.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5
6#include "qopenxrhelpers_p.h"
7
8#include <QtQuick/QQuickWindow>
9#include <QtQuick/QQuickGraphicsDevice>
10
11#include <rhi/qrhi.h>
12
14
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
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
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 return true;
67}
68
69
70int64_t QOpenXRGraphicsD3D12::colorSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
71{
72 // List of supported color swapchain formats.
73 constexpr DXGI_FORMAT supportedColorSwapchainFormats[] = {
74 DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
75 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
76 DXGI_FORMAT_B8G8R8A8_UNORM,
77 DXGI_FORMAT_R8G8B8A8_UNORM
78 };
79
80 auto swapchainFormatIt = std::find_first_of(std::begin(supportedColorSwapchainFormats),
81 std::end(supportedColorSwapchainFormats),
82 swapchainFormats.begin(),
83 swapchainFormats.end());
84
85 return *swapchainFormatIt;
86}
87
88int64_t QOpenXRGraphicsD3D12::depthSwapchainFormat(const QVector<int64_t> &swapchainFormats) const
89{
90 // in order of preference
91 constexpr int64_t supportedDepthSwapchainFormats[] = {
92 DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
93 DXGI_FORMAT_D32_FLOAT,
94 DXGI_FORMAT_D16_UNORM
95 };
96
97 return *std::find_first_of(std::begin(supportedDepthSwapchainFormats),
98 std::end(supportedDepthSwapchainFormats),
99 swapchainFormats.begin(),
100 swapchainFormats.end());
101}
102
103QVector<XrSwapchainImageBaseHeader*> QOpenXRGraphicsD3D12::allocateSwapchainImages(int count, XrSwapchain swapchain)
104{
105 QVector<XrSwapchainImageBaseHeader*> swapchainImages;
106 QVector<XrSwapchainImageD3D12KHR> swapchainImageBuffer(count);
107 for (XrSwapchainImageD3D12KHR& image : swapchainImageBuffer) {
108 image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR;
109 swapchainImages.push_back(reinterpret_cast<XrSwapchainImageBaseHeader*>(&image));
110 }
111 m_swapchainImageBuffer.insert(swapchain, swapchainImageBuffer);
112 return swapchainImages;
113}
114
115
116QQuickRenderTarget QOpenXRGraphicsD3D12::renderTarget(const XrSwapchainSubImage &subImage,
117 const XrSwapchainImageBaseHeader *swapchainImage,
118 quint64 swapchainFormat,
119 int samples,
120 int arraySize,
121 const XrSwapchainImageBaseHeader *depthSwapchainImage,
122 quint64 depthSwapchainFormat) const
123{
124 ID3D12Resource* const colorTexture = reinterpret_cast<const XrSwapchainImageD3D12KHR*>(swapchainImage)->texture;
125
126 DXGI_FORMAT viewFormat = DXGI_FORMAT(swapchainFormat);
127 switch (swapchainFormat) {
128 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
129 viewFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
130 break;
131 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
132 viewFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
133 break;
134 default:
135 break;
136 }
137
138 QQuickRenderTarget::Flags flags;
139 if (samples > 1)
141
142 return QQuickRenderTarget::fromD3D12Texture(colorTexture,
143 0,
144 swapchainFormat,
145 viewFormat,
146 QSize(subImage.imageRect.extent.width, subImage.imageRect.extent.height),
147 samples,
148 arraySize,
149 flags);
150
151 // No depthSwapchainImage support because ResolveDepthStencil will be
152 // unsupported with D3D11/12 no matter what.
153 Q_UNUSED(depthSwapchainImage);
155}
156
157
159{
160 quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromAdapter(m_graphicsRequirements.adapterLuid.LowPart,
161 m_graphicsRequirements.adapterLuid.HighPart,
162 m_graphicsRequirements.minFeatureLevel));
163}
164
QQuickRenderTarget renderTarget(const XrSwapchainSubImage &subImage, const XrSwapchainImageBaseHeader *swapchainImage, quint64 swapchainFormat, int samples, int arraySize, const XrSwapchainImageBaseHeader *depthSwapchainImage, quint64 depthSwapchainFormat) const override
const char * extensionName() const override
int64_t colorSwapchainFormat(const QVector< int64_t > &swapchainFormats) const override
QVector< XrSwapchainImageBaseHeader * > allocateSwapchainImages(int count, XrSwapchain swapchain) override
bool finializeGraphics(QRhi *rhi) override
bool isExtensionSupported(const QVector< XrExtensionProperties > &extensions) const override
bool setupGraphics(const XrInstance &instance, XrSystemId &systemId, const QQuickGraphicsConfiguration &quickConfig) override
const XrBaseInStructure * handle() const override
int64_t depthSwapchainFormat(const QVector< int64_t > &swapchainFormats) const override
void setupWindow(QQuickWindow *quickWindow) override
QQuickGraphicsConfiguration controls lower level graphics settings for the QQuickWindow.
The QQuickRenderTarget class provides an opaque container for native graphics resources specifying a ...
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
const QRhiNativeHandles * nativeHandles()
Definition qrhi.cpp:10137
\inmodule QtCore
Definition qsize.h:25
void extension()
[6]
Definition dialogs.cpp:230
bool checkXrResult(XrResult result, XrInstance instance)
Combined button and popup list for selecting options.
Definition image.cpp:4
#define qWarning
Definition qlogging.h:166
GLsizei samples
GLenum GLenum GLsizei count
GLbitfield flags
GLenum GLuint texture
#define Q_UNUSED(x)
unsigned long long quint64
Definition qtypes.h:61