47std::atomic<QQuickWindow*> s_connectedWindow{
nullptr};
51std::array<QMetaObject::Connection, 3> s_connections;
53std::atomic<void*> s_qtEglDisplay{
nullptr};
54std::atomic<void (*)()> s_publishedHandler{
nullptr};
56#if defined(QGC_RHI_CAPTURE_PROBES_EGL)
57void* eglDisplayOf(
const QOpenGLContext* context)
59 const auto* egl = context ? context->nativeInterface<QNativeInterface::QEGLContext>() :
nullptr;
60 return egl ?
static_cast<void*
>(egl->display()) :
nullptr;
66 s_snapshot.
backend.store(-1, std::memory_order_release);
67 s_snapshot.
d3d11Device.store(
nullptr, std::memory_order_release);
68 s_snapshot.
adapterLuid.store(0, std::memory_order_release);
70 s_snapshot.
vkVendorId.store(0, std::memory_order_release);
71 s_snapshot.
vkDeviceId.store(0, std::memory_order_release);
73 half.store(0, std::memory_order_release);
77#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
82bool protectImmediateContext(ID3D11DeviceContext* context)
84 ID3D11Multithread* multithread =
nullptr;
86 FAILED(context->QueryInterface(__uuidof(ID3D11Multithread),
reinterpret_cast<void**
>(&multithread))) ||
90 (void) multithread->SetMultithreadProtected(TRUE);
91 const bool isProtected = multithread->GetMultithreadProtected();
92 multithread->Release();
97void populateSnapshot(QRhi* rhi)
103 const int backend =
static_cast<int>(rhi->backend());
104 void* d3d11 =
nullptr;
106 quint32 vkVendorId = 0;
107 quint32 vkDeviceId = 0;
108 std::array<quint64, 2> vkPipelineCacheUuid{};
110#if defined(QGC_HAS_GST_D3D11_GPU_PATH)
111 if (rhi->backend() == QRhi::D3D11) {
112 if (
auto* h =
static_cast<const QRhiD3D11NativeHandles*
>(rhi->nativeHandles())) {
113 if (protectImmediateContext(
static_cast<ID3D11DeviceContext*
>(h->context))) {
116 qCWarning(QGCRhiCaptureLog)
117 <<
"Qt's D3D11 immediate context isn't multithread-protected; GStreamer gets its own device";
120 luid = (
static_cast<qint64
>(h->adapterLuidHigh) << 32) |
121 (
static_cast<qint64
>(h->adapterLuidLow) & 0xFFFFFFFFLL);
125#if defined(QGC_HAS_GST_D3D12_GPU_PATH)
126 if (rhi->backend() == QRhi::D3D12) {
127 if (
auto* h =
static_cast<const QRhiD3D12NativeHandles*
>(rhi->nativeHandles())) {
128 luid = (
static_cast<qint64
>(h->adapterLuidHigh) << 32) |
129 (
static_cast<qint64
>(h->adapterLuidLow) & 0xFFFFFFFFLL);
133#if defined(QGC_RHI_CAPTURE_PROBES_EGL)
134 if (rhi->backend() == QRhi::OpenGLES2) {
135 if (
auto* h =
static_cast<const QRhiGles2NativeHandles*
>(rhi->nativeHandles())) {
136 if (
void*
const display = eglDisplayOf(h->context)) {
137 s_qtEglDisplay.store(display, std::memory_order_release);
142#if defined(QGC_HAS_GST_VULKAN_GPU_PATH) && QT_CONFIG(vulkan)
143 if (rhi->backend() == QRhi::Vulkan) {
144 const auto* h =
static_cast<const QRhiVulkanNativeHandles*
>(rhi->nativeHandles());
145 if (h && h->inst && (h->physDev != VK_NULL_HANDLE)) {
147 h->inst->functions()->vkGetPhysicalDeviceProperties(h->physDev, &
properties);
150 static_assert(
sizeof(vkPipelineCacheUuid) ==
sizeof(
properties.pipelineCacheUUID));
151 std::memcpy(vkPipelineCacheUuid.data(),
properties.pipelineCacheUUID,
sizeof(vkPipelineCacheUuid));
157 if (path->probeDevice) {
158 path->probeDevice(*rhi);
164 s_snapshot.
d3d11Device.store(d3d11, std::memory_order_release);
165 s_snapshot.
adapterLuid.store(luid, std::memory_order_release);
166 s_snapshot.
vkVendorId.store(vkVendorId, std::memory_order_release);
167 s_snapshot.
vkDeviceId.store(vkDeviceId, std::memory_order_release);
168 for (std::size_t i = 0; i < vkPipelineCacheUuid.size(); ++i) {
169 s_snapshot.
vkPipelineCacheUuid[i].store(vkPipelineCacheUuid[i], std::memory_order_release);
171 s_snapshot.
backend.store(backend, std::memory_order_release);
176 path->bridge().serveWaitingPipelines();
179 if (
void (*
const handler)() = s_publishedHandler.load(std::memory_order_acquire)) {
185void publishRhi(QQuickWindow* window)
187 populateSnapshot(window->rhi());
204#ifdef QGC_UNITTEST_BUILD
205void publishDeviceSnapshotForTest(QRhi* rhi)
207 populateSnapshot(rhi);
213 void* display = s_qtEglDisplay.load(std::memory_order_acquire);
214#if defined(QGC_RHI_CAPTURE_PROBES_EGL)
220 if (
const QOpenGLContext*
const shareContext = QOpenGLContext::globalShareContext()) {
221 display = eglDisplayOf(shareContext);
222 }
else if (QThread::isMainThread() &&
qGuiApp) {
223 if (QPlatformNativeInterface*
const native = QGuiApplication::platformNativeInterface()) {
224 display = native->nativeResourceForIntegration(QByteArrayLiteral(
"egldisplay"));
228 s_qtEglDisplay.store(display, std::memory_order_release);
236 s_publishedHandler.store(handler, std::memory_order_release);
243 if (s_connectedWindow.load(std::memory_order_acquire) == window)
248 if (s_connectedWindow.load(std::memory_order_acquire) !=
nullptr) {
254 for (std::size_t i = 0; i < s_connections.size(); ++i) {
255 auto& conn = s_connections[i];
256 QObject::disconnect(conn);
257 conn = QMetaObject::Connection();
260 s_connectedWindow.store(window, std::memory_order_release);
263 s_connections[0] = QObject::connect(
264 window, &QQuickWindow::sceneGraphInitialized, window, [window]() { publishRhi(window); }, Qt::DirectConnection);
266 QObject::connect(window, &QQuickWindow::sceneGraphInvalidated, window, &unpublishRhi, Qt::DirectConnection);
268 s_connections[2] = QObject::connect(
269 window, &QQuickWindow::destroyed, window,
271 s_connectedWindow.store(
nullptr, std::memory_order_release);
274 Qt::DirectConnection);
277 if (window->isSceneGraphInitialized()) {
278 window->scheduleRenderJob(QRunnable::create([window]() { publishRhi(window); }),
279 QQuickWindow::BeforeSynchronizingStage);