Debian Codegraph @

tests/manual/rhi/multiwindow_threaded/multiwindow_threaded.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QApplication>
5 #include <QWidget>
6 #include <QLabel>
7 #include <QPlainTextEdit>
8 #include <QPushButton>
9 #include <QCheckBox>
10 #include <QVBoxLayout>
11
12 #include <QThread>
13 #include <QMutex>
14 #include <QWaitCondition>
15 #include <QQueue>
16 #include <QEvent>
17 #include <QCommandLineParser>
18 #include <QElapsedTimer>
19 #include <QFile>
20 #include <QLoggingCategory>
21 #include <QOffscreenSurface>
22 #include <rhi/qrhi.h>
23
24 #ifdef Q_OS_DARWIN
25 #include <QtCore/private/qcore_mac_p.h>
26 #endif
27
28 #include "window.h"
29
30 #include "../shared/cube.h"
31
32 static QShader getShader(const QString &name)
33 {
34 QFile f(name);
35 if (f.open(QIODevice::ReadOnly))
36 return QShader::fromSerialized(f.readAll());
37
38 return QShader();
39 }
40
41 static GraphicsApi graphicsApi;
42
43 static QString graphicsApiName()
44 {
45 switch (graphicsApi) {
46 case OpenGL:
47 return QLatin1String("OpenGL 2.x");
48 case Vulkan:
49 return QLatin1String("Vulkan");
50 case D3D11:
51 return QLatin1String("Direct3D 11");
52 case D3D12:
53 return QLatin1String("Direct3D 12");
54 case Metal:
55 return QLatin1String("Metal");
56 default:
57 break;
58 }
59 return QString();
60 }
61
62 #if QT_CONFIG(vulkan)
63 QVulkanInstance *instance = nullptr;
64 #endif
65
66 // Window (main thread) emit signals -> Renderer::send* (main thread) -> event queue (add on main, process on render thread) -> Renderer::renderEvent (render thread)
67
68 // event queue is taken from the Qt Quick scenegraph as-is
69 // all this below is conceptually the same as the QSG threaded render loop
70 class RenderThreadEventQueue : public QQueue<QEvent *>
71 {
72 public:
73 RenderThreadEventQueue()
74 : waiting(false)
75 {
76 }
77
78 void addEvent(QEvent *e) {
79 mutex.lock();
80 enqueue(e);
81 if (waiting)
82 condition.wakeOne();
83 mutex.unlock();
84 }
85
86 QEvent *takeEvent(bool wait) {
87 mutex.lock();
88 if (isEmpty() && wait) {
89 waiting = true;
90 condition.wait(&mutex);
91 waiting = false;
92 }
93 QEvent *e = dequeue();
94 mutex.unlock();
95 return e;
96 }
97
98 bool hasMoreEvents() {
99 mutex.lock();
100 bool has = !isEmpty();
101 mutex.unlock();
102 return has;
103 }
104
105 private:
106 QMutex mutex;
107 QWaitCondition condition;
108 bool waiting;
109 };
110
111 struct Renderer;
112
113 struct Thread : public QThread
114 {
115 Thread(Renderer *renderer_)
116 : renderer(renderer_)
117 {
118 active = true;
119 start();
120 }
121 void run() override;
122
123 Renderer *renderer;
124 bool active;
125 RenderThreadEventQueue eventQueue;
126 bool sleeping = false;
127 bool stopEventProcessing = false;
128 bool pendingRender = false;
129 bool pendingRenderIsNewExpose = false;
130 // mutex and cond used to allow the main thread waiting until something completes on the render thread
131 QMutex mutex;
132 QWaitCondition cond;
133 };
134
135 class RenderThreadEvent : public QEvent
136 {
137 public:
138 RenderThreadEvent(QEvent::Type type) : QEvent(type) { }
139 };
140
141 class InitEvent : public RenderThreadEvent
142 {
143 public:
144 static const QEvent::Type TYPE = QEvent::Type(QEvent::User + 1);
145 InitEvent() : RenderThreadEvent(TYPE)
146 { }
147 };
148
149 class RequestRenderEvent : public RenderThreadEvent
150 {
151 public:
152 static const QEvent::Type TYPE = QEvent::Type(QEvent::User + 2);
153 RequestRenderEvent(bool newlyExposed_) : RenderThreadEvent(TYPE), newlyExposed(newlyExposed_)
154 { }
155 bool newlyExposed;
156 };
157
158 class SurfaceCleanupEvent : public RenderThreadEvent
159 {
160 public:
161 static const QEvent::Type TYPE = QEvent::Type(QEvent::User + 3);
162 SurfaceCleanupEvent() : RenderThreadEvent(TYPE)
163 { }
164 };
165
166 class CloseEvent : public RenderThreadEvent
167 {
168 public:
169 static const QEvent::Type TYPE = QEvent::Type(QEvent::User + 4);
170 CloseEvent() : RenderThreadEvent(TYPE)
171 { }
172 };
173
174 class SyncSurfaceSizeEvent : public RenderThreadEvent
175 {
176 public:
177 static const QEvent::Type TYPE = QEvent::Type(QEvent::User + 5);
178 SyncSurfaceSizeEvent() : RenderThreadEvent(TYPE)
179 { }
180 };
181
182 struct Renderer
183 {
184 // ctor and dtor and send* are called main thread, rest on the render thread
185
186 Renderer(QWindow *w, const QColor &bgColor, int rotationAxis);
187 ~Renderer();
188
189 void sendInit();
190 void sendRender(bool newlyExposed);
191 void sendSurfaceGoingAway();
192 void sendSyncSurfaceSize();
193
194 QWindow *window;
195 Thread *thread;
196 QRhi *r = nullptr;
197 #ifndef QT_NO_OPENGL
198 QOffscreenSurface *fallbackSurface = nullptr;
199 #endif
200
201 void createRhi();
202 void destroyRhi();
203 void renderEvent(QEvent *e);
204 void init();
205 void releaseSwapChain();
206 void releaseResources();
207 void render(bool newlyExposed, bool wakeBeforePresent);
208
209 QColor m_bgColor;
210 int m_rotationAxis;
211
212 QList<QRhiResource *> m_releasePool;
213 bool m_hasSwapChain = false;
214 QRhiSwapChain *m_sc = nullptr;
215 QRhiRenderBuffer *m_ds = nullptr;
216 QRhiRenderPassDescriptor *m_rp = nullptr;
217
218 QRhiBuffer *m_vbuf = nullptr;
219 QRhiBuffer *m_ubuf = nullptr;
220 QRhiTexture *m_tex = nullptr;
221 QRhiSampler *m_sampler = nullptr;
222 QRhiShaderResourceBindings *m_srb = nullptr;
223 QRhiGraphicsPipeline *m_ps = nullptr;
224 QRhiResourceUpdateBatch *m_initialUpdates = nullptr;
225
226 QMatrix4x4 m_proj;
227 float m_rotation = 0;
228 int m_frameCount = 0;
229 };
230
231 void Thread::run()
232 {
233 while (active) {
234 #ifdef Q_OS_DARWIN
235 QMacAutoReleasePool autoReleasePool;
236 #endif
237
238 if (pendingRender) {
239 pendingRender = false;
240 renderer->render(pendingRenderIsNewExpose, false);
241 }
242
243 while (eventQueue.hasMoreEvents()) {
244 QEvent *e = eventQueue.takeEvent(false);
245 renderer->renderEvent(e);
246 delete e;
247 }
248
249 if (active && !pendingRender) {
250 sleeping = true;
251 stopEventProcessing = false;
252 while (!stopEventProcessing) {
253 QEvent *e = eventQueue.takeEvent(true);
254 renderer->renderEvent(e);
255 delete e;
256 }
257 sleeping = false;
258 }
259 }
260 }
261
262 Renderer::Renderer(QWindow *w, const QColor &bgColor, int rotationAxis)
263 : window(w),
264 m_bgColor(bgColor),
265 m_rotationAxis(rotationAxis)
266 { // main thread
267 thread = new Thread(this);
268
269 #ifndef QT_NO_OPENGL
270 if (graphicsApi == OpenGL)
271 fallbackSurface = QRhiGles2InitParams::newFallbackSurface();
272 #endif
273 }
274
275 Renderer::~Renderer()
276 { // main thread
277 thread->eventQueue.addEvent(new CloseEvent);
278 thread->wait();
279 delete thread;
280
281 #ifndef QT_NO_OPENGL
282 delete fallbackSurface;
283 #endif
284 }
285
286 void Renderer::createRhi()
287 {
288 if (r)
289 return;
290
291 qDebug() << "renderer" << this << "creating rhi";
292 QRhi::Flags rhiFlags;
293
294 #ifndef QT_NO_OPENGL
295 if (graphicsApi == OpenGL) {
296 QRhiGles2InitParams params;
297 params.fallbackSurface = fallbackSurface;
298 params.window = window;
299 r = QRhi::create(QRhi::OpenGLES2, &params, rhiFlags);
300 }
301 #endif
302
303 #if QT_CONFIG(vulkan)
304 if (graphicsApi == Vulkan) {
305 QRhiVulkanInitParams params;
306 params.inst = instance;
307 params.window = window;
308 r = QRhi::create(QRhi::Vulkan, &params, rhiFlags);
309 }
310 #endif
311
312 #ifdef Q_OS_WIN
313 if (graphicsApi == D3D11) {
314 QRhiD3D11InitParams params;
315 params.enableDebugLayer = true;
316 r = QRhi::create(QRhi::D3D11, &params, rhiFlags);
317 } else if (graphicsApi == D3D12) {
318 QRhiD3D12InitParams params;
319 params.enableDebugLayer = true;
320 r = QRhi::create(QRhi::D3D12, &params, rhiFlags);
321 }
322 #endif
323
324 #if QT_CONFIG(metal)
325 if (graphicsApi == Metal) {
326 QRhiMetalInitParams params;
327 r = QRhi::create(QRhi::Metal, &params, rhiFlags);
328 }
329 #endif
330
331 if (!r)
332 qFatal("Failed to create RHI backend");
333 }
334
335 void Renderer::destroyRhi()
336 {
337 qDebug() << "renderer" << this << "destroying rhi";
338 delete r;
339 r = nullptr;
340 }
341
342 void Renderer::renderEvent(QEvent *e)
343 {
344 Q_ASSERT(QThread::currentThread() == thread);
345
346 if (thread->sleeping)
347 thread->stopEventProcessing = true;
348
349 switch (int(e->type())) {
350 case InitEvent::TYPE:
351 qDebug() << "renderer" << this << "for window" << window << "is initializing";
352 createRhi();
353 init();
354 break;
355 case RequestRenderEvent::TYPE:
356 thread->pendingRender = true;
357 thread->pendingRenderIsNewExpose = static_cast<RequestRenderEvent *>(e)->newlyExposed;
358 break;
359 case SurfaceCleanupEvent::TYPE: // when the QWindow is closed, before QPlatformWindow goes away
360 thread->mutex.lock();
361 qDebug() << "renderer" << this << "for window" << window << "is destroying swapchain";
362 thread->pendingRender = false;
363 releaseSwapChain();
364 thread->cond.wakeOne();
365 thread->mutex.unlock();
366 break;
367 case CloseEvent::TYPE: // when destroying the window+renderer (NB not the same as hitting X on the window, that's just QWindow close)
368 qDebug() << "renderer" << this << "for window" << window << "is shutting down";
369 thread->pendingRender = false;
370 thread->active = false;
371 thread->stopEventProcessing = true;
372 releaseResources();
373 destroyRhi();
374 break;
375 case SyncSurfaceSizeEvent::TYPE:
376 thread->mutex.lock();
377 thread->pendingRender = false;
378 render(false, true);
379 break;
380 default:
381 break;
382 }
383 }
384
385 void Renderer::init()
386 {
387 m_sc = r->newSwapChain();
388 m_ds = r->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
389 QSize(),
390 1,
391 QRhiRenderBuffer::UsedWithSwapChainOnly);
392 m_releasePool << m_ds;
393 m_sc->setWindow(window);
394 m_sc->setDepthStencil(m_ds);
395 m_rp = m_sc->newCompatibleRenderPassDescriptor();
396 m_releasePool << m_rp;
397 m_sc->setRenderPassDescriptor(m_rp);
398
399 m_vbuf = r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
400 m_releasePool << m_vbuf;
401 m_vbuf->create();
402
403 m_ubuf = r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4);
404 m_releasePool << m_ubuf;
405 m_ubuf->create();
406
407 QImage image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888);
408 m_tex = r->newTexture(QRhiTexture::RGBA8, image.size());
409 m_releasePool << m_tex;
410 m_tex->create();
411
412 m_sampler = r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
413 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
414 m_releasePool << m_sampler;
415 m_sampler->create();
416
417 m_srb = r->newShaderResourceBindings();
418 m_releasePool << m_srb;
419 m_srb->setBindings({
420 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf),
421 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, m_tex, m_sampler)
422 });
423 m_srb->create();
424
425 m_ps = r->newGraphicsPipeline();
426 m_releasePool << m_ps;
427
428 m_ps->setDepthTest(true);
429 m_ps->setDepthWrite(true);
430 m_ps->setDepthOp(QRhiGraphicsPipeline::Less);
431
432 m_ps->setCullMode(QRhiGraphicsPipeline::Back);
433 m_ps->setFrontFace(QRhiGraphicsPipeline::CCW);
434
435 m_ps->setShaderStages({
436 { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
437 { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
438 });
439
440 QRhiVertexInputLayout inputLayout;
441 inputLayout.setBindings({
442 { 3 * sizeof(float) },
443 { 2 * sizeof(float) }
444 });
445 inputLayout.setAttributes({
446 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
447 { 1, 1, QRhiVertexInputAttribute::Float2, 0 }
448 });
449
450 m_ps->setVertexInputLayout(inputLayout);
451 m_ps->setShaderResourceBindings(m_srb);
452 m_ps->setRenderPassDescriptor(m_rp);
453
454 m_ps->create();
455
456 m_initialUpdates = r->nextResourceUpdateBatch();
457
458 m_initialUpdates->uploadStaticBuffer(m_vbuf, cube);
459
460 qint32 flip = 0;
461 m_initialUpdates->updateDynamicBuffer(m_ubuf, 64, 4, &flip);
462
463 m_initialUpdates->uploadTexture(m_tex, image);
464 }
465
466 void Renderer::releaseSwapChain()
467 {
468 if (m_hasSwapChain) {
469 m_hasSwapChain = false;
470 m_sc->destroy();
471 }
472 }
473
474 void Renderer::releaseResources()
475 {
476 qDeleteAll(m_releasePool);
477 m_releasePool.clear();
478
479 delete m_sc;
480 m_sc = nullptr;
481 }
482
483 void Renderer::render(bool newlyExposed, bool wakeBeforePresent)
484 {
485 // This function handles both resizing and rendering. Resizes have some
486 // complications due to the threaded model (check exposeEvent and
487 // sendSyncSurfaceSize) but don't have to worry about that in here.
488
489 auto buildOrResizeSwapChain = [this] {
490 qDebug() << "renderer" << this << "build or resize swapchain for window" << window;
491 m_hasSwapChain = m_sc->createOrResize();
492 const QSize outputSize = m_sc->currentPixelSize();
493 qDebug() << " size is" << outputSize;
494 m_proj = r->clipSpaceCorrMatrix();
495 m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 100.0f);
496 m_proj.translate(0, 0, -4);
497 };
498
499 auto wakeUpIfNeeded = [wakeBeforePresent, this] {
500 // make sure the main/gui thread is not blocked when issuing the Present (or equivalent)
501 if (wakeBeforePresent) {
502 thread->cond.wakeOne();
503 thread->mutex.unlock();
504 }
505 };
506
507 const QSize surfaceSize = m_sc->surfacePixelSize();
508 if (surfaceSize.isEmpty()) {
509 wakeUpIfNeeded();
510 return;
511 }
512
513 if (newlyExposed || m_sc->currentPixelSize() != surfaceSize)
514 buildOrResizeSwapChain();
515
516 if (!m_hasSwapChain) {
517 wakeUpIfNeeded();
518 return;
519 }
520
521 QRhi::FrameOpResult result = r->beginFrame(m_sc);
522 if (result == QRhi::FrameOpSwapChainOutOfDate) {
523 buildOrResizeSwapChain();
524 if (!m_hasSwapChain) {
525 wakeUpIfNeeded();
526 return;
527 }
528 result = r->beginFrame(m_sc);
529 }
530 if (result != QRhi::FrameOpSuccess) {
531 wakeUpIfNeeded();
532 return;
533 }
534
535 QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
536 const QSize outputSize = m_sc->currentPixelSize();
537
538 QRhiResourceUpdateBatch *u = r->nextResourceUpdateBatch();
539 if (m_initialUpdates) {
540 u->merge(m_initialUpdates);
541 m_initialUpdates->release();
542 m_initialUpdates = nullptr;
543 }
544
545 m_rotation += 1.0f;
546 QMatrix4x4 mvp = m_proj;
547 mvp.scale(0.5f);
548 mvp.rotate(m_rotation, m_rotationAxis == 0 ? 1 : 0, m_rotationAxis == 1 ? 1 : 0, m_rotationAxis == 2 ? 1 : 0);
549 u->updateDynamicBuffer(m_ubuf, 0, 64, mvp.constData());
550
551 cb->beginPass(m_sc->currentFrameRenderTarget(),
552 QColor::fromRgbF(float(m_bgColor.redF()), float(m_bgColor.greenF()), float(m_bgColor.blueF()), 1.0f),
553 { 1.0f, 0 },
554 u);
555
556 cb->setGraphicsPipeline(m_ps);
557 cb->setViewport(QRhiViewport(0, 0, outputSize.width(), outputSize.height()));
558 cb->setShaderResources();
559 const QRhiCommandBuffer::VertexInput vbufBindings[] = {
560 { m_vbuf, 0 },
561 { m_vbuf, quint32(36 * 3 * sizeof(float)) }
562 };
563 cb->setVertexInput(0, 2, vbufBindings);
564 cb->draw(36);
565
566 cb->endPass();
567
568 wakeUpIfNeeded();
569
570 r->endFrame(m_sc);
571
572 m_frameCount += 1;
573 }
574
575 void Renderer::sendInit()
576 { // main thread
577 InitEvent *e = new InitEvent;
578 thread->eventQueue.addEvent(e);
579 }
580
581 void Renderer::sendRender(bool newlyExposed)
582 { // main thread
583 RequestRenderEvent *e = new RequestRenderEvent(newlyExposed);
584 thread->eventQueue.addEvent(e);
585 }
586
587 void Renderer::sendSurfaceGoingAway()
588 { // main thread
589 SurfaceCleanupEvent *e = new SurfaceCleanupEvent;
590
591 // cannot let this thread to proceed with tearing down the native window
592 // before the render thread completes the swapchain release
593 thread->mutex.lock();
594
595 thread->eventQueue.addEvent(e);
596
597 thread->cond.wait(&thread->mutex);
598 thread->mutex.unlock();
599 }
600
601 void Renderer::sendSyncSurfaceSize()
602 { // main thread
603 SyncSurfaceSizeEvent *e = new SyncSurfaceSizeEvent;
604 // must block to prevent surface size mess. the render thread will do a
605 // full rendering round before it unlocks which is good since it can thus
606 // pick up and the surface (window) size atomically.
607 thread->mutex.lock();
608 thread->eventQueue.addEvent(e);
609 thread->cond.wait(&thread->mutex);
610 thread->mutex.unlock();
611 }
612
613 struct WindowAndRenderer
614 {
615 QWindow *window;
616 Renderer *renderer;
617 };
618
619 QList<WindowAndRenderer> windows;
620
621 void createWindow()
622 {
623 static QColor colors[] = { Qt::red, Qt::green, Qt::blue, Qt::yellow, Qt::cyan, Qt::gray };
624 const int n = windows.count();
625 Window *w = new Window(QString::asprintf("Window+Thread #%d (%s)", n, qPrintable(graphicsApiName())), graphicsApi);
626 Renderer *renderer = new Renderer(w, colors[n % 6], n % 3);
627 QObject::connect(w, &Window::initRequested, w, [renderer] {
628 renderer->sendInit();
629 });
630 QObject::connect(w, &Window::renderRequested, w, [w, renderer](bool newlyExposed) {
631 renderer->sendRender(newlyExposed);
632 w->requestUpdate();
633 });
634 QObject::connect(w, &Window::surfaceGoingAway, w, [renderer] {
635 renderer->sendSurfaceGoingAway();
636 });
637 QObject::connect(w, &Window::syncSurfaceSizeRequested, w, [renderer] {
638 renderer->sendSyncSurfaceSize();
639 });
640 windows.append({ w, renderer });
641 w->show();
642 }
643
644 void closeWindow()
645 {
646 WindowAndRenderer wr = windows.takeLast();
647 delete wr.renderer;
648 delete wr.window;
649 }
650
651 int main(int argc, char **argv)
652 {
653 QApplication app(argc, argv);
654
655 #if defined(Q_OS_WIN)
656 graphicsApi = D3D11;
657 #elif QT_CONFIG(metal)
658 graphicsApi = Metal;
659 #elif QT_CONFIG(vulkan)
660 graphicsApi = Vulkan;
661 #else
662 graphicsApi = OpenGL;
663 #endif
664
665 QCommandLineParser cmdLineParser;
666 cmdLineParser.addHelpOption();
667 QCommandLineOption glOption({ "g", "opengl" }, QLatin1String("OpenGL (2.x)"));
668 cmdLineParser.addOption(glOption);
669 QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan"));
670 cmdLineParser.addOption(vkOption);
671 QCommandLineOption d3dOption({ "d", "d3d11" }, QLatin1String("Direct3D 11"));
672 cmdLineParser.addOption(d3dOption);
673 QCommandLineOption d3d12Option({ "D", "d3d12" }, QLatin1String("Direct3D 12"));
674 cmdLineParser.addOption(d3d12Option);
675 QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal"));
676 cmdLineParser.addOption(mtlOption);
677 cmdLineParser.process(app);
678 if (cmdLineParser.isSet(glOption))
679 graphicsApi = OpenGL;
680 if (cmdLineParser.isSet(vkOption))
681 graphicsApi = Vulkan;
682 if (cmdLineParser.isSet(d3dOption))
683 graphicsApi = D3D11;
684 if (cmdLineParser.isSet(d3d12Option))
685 graphicsApi = D3D12;
686 if (cmdLineParser.isSet(mtlOption))
687 graphicsApi = Metal;
688
689 qDebug("Selected graphics API is %s", qPrintable(graphicsApiName()));
690 qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText()));
691
692 QSurfaceFormat fmt;
693 fmt.setDepthBufferSize(24);
694 QSurfaceFormat::setDefaultFormat(fmt);
695
696 #if QT_CONFIG(vulkan)
697 instance = new QVulkanInstance;
698 if (graphicsApi == Vulkan) {
699 instance->setLayers({ "VK_LAYER_KHRONOS_validation" });
700 if (!instance->create()) {
701 qWarning("Failed to create Vulkan instance, switching to OpenGL");
702 graphicsApi = OpenGL;
703 }
704 }
705 #endif
706
707 int winCount = 0;
708 QWidget w;
709 w.resize(800, 600);
710 w.setWindowTitle(QCoreApplication::applicationName() + QLatin1String(" - ") + graphicsApiName());
711 QVBoxLayout *layout = new QVBoxLayout(&w);
712
713 QPlainTextEdit *info = new QPlainTextEdit(
714 QLatin1String("This application tests rendering on a separate thread per window, with dedicated QRhi instances and resources. "
715 "\n\nThis is the same concept as the Qt Quick Scenegraph's threaded render loop. This should allow rendering to the different windows "
716 "without unintentionally throttling each other's threads."
717 "\n\nUsing API: ") + graphicsApiName());
718 info->setReadOnly(true);
719 layout->addWidget(info);
720 QLabel *label = new QLabel(QLatin1String("Window and thread count: 0"));
721 layout->addWidget(label);
722 QPushButton *btn = new QPushButton(QLatin1String("New window"));
723 QObject::connect(btn, &QPushButton::clicked, btn, [label, &winCount] {
724 winCount += 1;
725 label->setText(QString::asprintf("Window count: %d", winCount));
726 createWindow();
727 });
728 layout->addWidget(btn);
729 btn = new QPushButton(QLatin1String("Close window"));
730 QObject::connect(btn, &QPushButton::clicked, btn, [label, &winCount] {
731 if (winCount > 0) {
732 winCount -= 1;
733 label->setText(QString::asprintf("Window count: %d", winCount));
734 closeWindow();
735 }
736 });
737 layout->addWidget(btn);
738 w.show();
739
740 int result = app.exec();
741
742 for (const WindowAndRenderer &wr : windows) {
743 delete wr.renderer;
744 delete wr.window;
745 }
746
747 #if QT_CONFIG(vulkan)
748 delete instance;
749 #endif
750
751 return result;
752 }