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
qrhid3dhelpers.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qrhid3dhelpers_p.h"
5#include <QtCore/private/qsystemlibrary_p.h>
6#include <QtCore/private/qsystemerror_p.h>
7
9
10namespace QRhiD3D {
11
12bool output6ForWindow(QWindow *w, IDXGIAdapter1 *adapter, IDXGIOutput6 **result)
13{
14 bool ok = false;
15 QRect wr = w->geometry();
16 wr = QRect(wr.topLeft() * w->devicePixelRatio(), wr.size() * w->devicePixelRatio());
17 const QPoint center = wr.center();
18 IDXGIOutput *currentOutput = nullptr;
19 IDXGIOutput *output = nullptr;
20 for (UINT i = 0; adapter->EnumOutputs(i, &output) != DXGI_ERROR_NOT_FOUND; ++i) {
21 DXGI_OUTPUT_DESC desc;
22 output->GetDesc(&desc);
23 const RECT r = desc.DesktopCoordinates;
24 const QRect dr(QPoint(r.left, r.top), QPoint(r.right - 1, r.bottom - 1));
25 if (dr.contains(center)) {
26 currentOutput = output;
27 break;
28 } else {
29 output->Release();
30 }
31 }
32 if (currentOutput) {
33 ok = SUCCEEDED(currentOutput->QueryInterface(__uuidof(IDXGIOutput6), reinterpret_cast<void **>(result)));
34 currentOutput->Release();
35 }
36 return ok;
37}
38
39bool outputDesc1ForWindow(QWindow *w, IDXGIAdapter1 *adapter, DXGI_OUTPUT_DESC1 *result)
40{
41 bool ok = false;
42 IDXGIOutput6 *out6 = nullptr;
43 if (output6ForWindow(w, adapter, &out6)) {
44 ok = SUCCEEDED(out6->GetDesc1(result));
45 out6->Release();
46 }
47 return ok;
48}
49
50float sdrWhiteLevelInNits(const DXGI_OUTPUT_DESC1 &outputDesc)
51{
52 QVector<DISPLAYCONFIG_PATH_INFO> pathInfos;
53 uint32_t pathInfoCount, modeInfoCount;
54 LONG result;
55 do {
56 if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathInfoCount, &modeInfoCount) == ERROR_SUCCESS) {
57 pathInfos.resize(pathInfoCount);
58 QVector<DISPLAYCONFIG_MODE_INFO> modeInfos(modeInfoCount);
59 result = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathInfoCount, pathInfos.data(), &modeInfoCount, modeInfos.data(), nullptr);
60 } else {
61 return 200.0f;
62 }
63 } while (result == ERROR_INSUFFICIENT_BUFFER);
64
65 MONITORINFOEX monitorInfo = {};
66 monitorInfo.cbSize = sizeof(monitorInfo);
67 GetMonitorInfo(outputDesc.Monitor, &monitorInfo);
68
69 for (const DISPLAYCONFIG_PATH_INFO &info : pathInfos) {
70 DISPLAYCONFIG_SOURCE_DEVICE_NAME deviceName = {};
71 deviceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
72 deviceName.header.size = sizeof(deviceName);
73 deviceName.header.adapterId = info.sourceInfo.adapterId;
74 deviceName.header.id = info.sourceInfo.id;
75 if (DisplayConfigGetDeviceInfo(&deviceName.header) == ERROR_SUCCESS) {
76 if (!wcscmp(monitorInfo.szDevice, deviceName.viewGdiDeviceName)) {
77 DISPLAYCONFIG_SDR_WHITE_LEVEL whiteLevel = {};
78 whiteLevel.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL;
79 whiteLevel.header.size = sizeof(DISPLAYCONFIG_SDR_WHITE_LEVEL);
80 whiteLevel.header.adapterId = info.targetInfo.adapterId;
81 whiteLevel.header.id = info.targetInfo.id;
82 if (DisplayConfigGetDeviceInfo(&whiteLevel.header) == ERROR_SUCCESS)
83 return whiteLevel.SDRWhiteLevel * 80 / 1000.0f;
84 }
85 }
86 }
87
88 return 200.0f;
89}
90
91pD3DCompile resolveD3DCompile()
92{
93 for (const wchar_t *libraryName : {L"D3DCompiler_47", L"D3DCompiler_43"}) {
94 QSystemLibrary library(libraryName);
95 if (library.load()) {
96 if (auto symbol = library.resolve("D3DCompile"))
97 return reinterpret_cast<pD3DCompile>(symbol);
98 } else {
99 qWarning("Failed to load D3DCompiler_47/43.dll");
100 }
101 }
102 return nullptr;
103}
104
105IDCompositionDevice *createDirectCompositionDevice()
106{
107 QSystemLibrary dcomplib(QStringLiteral("dcomp"));
108 typedef HRESULT (__stdcall *DCompositionCreateDeviceFuncPtr)(
109 _In_opt_ IDXGIDevice *dxgiDevice,
110 _In_ REFIID iid,
111 _Outptr_ void **dcompositionDevice);
112 DCompositionCreateDeviceFuncPtr func = reinterpret_cast<DCompositionCreateDeviceFuncPtr>(
113 dcomplib.resolve("DCompositionCreateDevice"));
114 if (!func) {
115 qWarning("Unable to resolve DCompositionCreateDevice, perhaps dcomp.dll is missing?");
116 return nullptr;
117 }
118 IDCompositionDevice *device = nullptr;
119 HRESULT hr = func(nullptr, __uuidof(IDCompositionDevice), reinterpret_cast<void **>(&device));
120 if (FAILED(hr)) {
121 qWarning("Failed to create Direct Composition device: %s",
122 qPrintable(QSystemError::windowsComString(hr)));
123 return nullptr;
124 }
125 return device;
126}
127
128#ifdef QRHI_D3D12_HAS_DXC
129std::pair<IDxcCompiler *, IDxcLibrary *> createDxcCompiler()
130{
131 QSystemLibrary dxclib(QStringLiteral("dxcompiler"));
132 // this will not be in the system library location, hence onlySystemDirectory==false
133 if (!dxclib.load(false)) {
134 qWarning("Failed to load dxcompiler.dll");
135 return {};
136 }
137 DxcCreateInstanceProc func = reinterpret_cast<DxcCreateInstanceProc>(dxclib.resolve("DxcCreateInstance"));
138 if (!func) {
139 qWarning("Unable to resolve DxcCreateInstance");
140 return {};
141 }
142 IDxcCompiler *compiler = nullptr;
143 HRESULT hr = func(CLSID_DxcCompiler, __uuidof(IDxcCompiler), reinterpret_cast<void**>(&compiler));
144 if (FAILED(hr)) {
145 qWarning("Failed to create dxc compiler instance: %s",
146 qPrintable(QSystemError::windowsComString(hr)));
147 return {};
148 }
149 IDxcLibrary *library = nullptr;
150 hr = func(CLSID_DxcLibrary, __uuidof(IDxcLibrary), reinterpret_cast<void**>(&library));
151 if (FAILED(hr)) {
152 qWarning("Failed to create dxc library instance: %s",
153 qPrintable(QSystemError::windowsComString(hr)));
154 return {};
155 }
156 return { compiler, library };
157}
158#endif
159
160void fillDriverInfo(QRhiDriverInfo *info, const DXGI_ADAPTER_DESC1 &desc)
161{
162 const QString name = QString::fromUtf16(reinterpret_cast<const char16_t *>(desc.Description));
163 info->deviceName = name.toUtf8();
164 info->deviceId = desc.DeviceId;
165 info->vendorId = desc.VendorId;
166 info->deviceType = (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) ? QRhiDriverInfo::CpuDevice
168}
169
170} // namespace
171
IOBluetoothDevice * device
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:855
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:6045
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
\inmodule QtGui
Definition qwindow.h:63
float sdrWhiteLevelInNits(const DXGI_OUTPUT_DESC1 &outputDesc)
bool output6ForWindow(QWindow *w, IDXGIAdapter1 *adapter, IDXGIOutput6 **result)
pD3DCompile resolveD3DCompile()
bool outputDesc1ForWindow(QWindow *w, IDXGIAdapter1 *adapter, DXGI_OUTPUT_DESC1 *result)
IDCompositionDevice * createDirectCompositionDevice()
void fillDriverInfo(QRhiDriverInfo *info, const DXGI_ADAPTER_DESC1 &desc)
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
GLfloat GLfloat GLfloat w
[0]
GLboolean r
[2]
GLuint name
GLenum func
Definition qopenglext.h:663
GLuint64EXT * result
[6]
#define DXGI_ADAPTER_FLAG_SOFTWARE
\variable QRhiD3D11NativeHandles::dev
#define qPrintable(string)
Definition qstring.h:1531
#define QStringLiteral(str)
QT_BEGIN_NAMESPACE typedef uchar * output
long HRESULT
QHostInfo info
[0]
\inmodule QtGui
Definition qrhi.h:1759