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
qrhiwidget.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// Qt-Security score:significant reason:default
4
5#include "qrhiwidget_p.h"
6#include <private/qguiapplication_p.h>
7#include <qpa/qplatformintegration.h>
8#include <private/qwidgetrepaintmanager_p.h>
9
11
12/*!
13 \class QRhiWidget
14 \inmodule QtWidgets
15 \since 6.7
16
17 \brief The QRhiWidget class is a widget for rendering 3D graphics via an
18 accelerated grapics API, such as Vulkan, Metal, or Direct 3D.
19
20 QRhiWidget provides functionality for displaying 3D content rendered
21 through the \l QRhi APIs within a QWidget-based application. In many ways
22 it is the portable equivalent of \l QOpenGLWidget that is not tied to a
23 single 3D graphics API, but rather can function with all the APIs QRhi
24 supports (such as, Direct 3D 11/12, Vulkan, Metal, and OpenGL).
25
26 QRhiWidget is expected to be subclassed. To render into the 2D texture that
27 is implicitly created and managed by the QRhiWidget, subclasses should
28 reimplement the virtual functions initialize() and render().
29
30 The size of the texture will by default adapt to the size of the widget. If
31 a fixed size is preferred, set a fixed size specified in pixels by calling
32 setFixedColorBufferSize().
33
34 In addition to the texture serving as the color buffer, a depth/stencil
35 buffer and a render target binding these together is maintained implicitly
36 as well.
37
38 The QRhi for the widget's top-level window is configured to use a
39 platform-specific backend and graphics API by default: Metal on macOS and
40 iOS, Direct 3D 11 on Windows, OpenGL otherwise. Call setApi() to override
41 this.
42
43 \note A single widget window can only use one QRhi backend, and so one
44 single 3D graphics API. If two QRhiWidget or QQuickWidget widgets in the
45 window's widget hierarchy request different APIs, only one of them will
46 function correctly.
47
48 \note While QRhiWidget is a public Qt API, the QRhi family of classes in
49 the Qt Gui module, including QRhi, QShader and QShaderDescription, offer
50 limited compatibility guarantees. There are no source or binary
51 compatibility guarantees for these classes, meaning the API is only
52 guaranteed to work with the Qt version the application was developed
53 against. Source incompatible changes are however aimed to be kept at a
54 minimum and will only be made in minor releases (6.7, 6.8, and so on).
55 \c{qrhiwidget.h} does not directly include any QRhi-related headers. To use
56 those classes when implementing a QRhiWidget subclass, link to
57 \c{Qt::GuiPrivate} (if using CMake), and include the appropriate headers
58 with the \c rhi prefix, for example \c{#include <rhi/qrhi.h>}.
59
60 An example of a simple QRhiWidget subclass rendering a triangle is the
61 following:
62
63 \snippet qrhiwidget/rhiwidgetintro.cpp 0
64
65 This is a widget that continuously requests updates, throttled by the
66 presentation rate (vsync, depending on the screen refresh rate). If
67 rendering continuously is not desired, the update() call in render() should
68 be removed, and rather issued only when updating the rendered content is
69 necessary. For example, if the rotation of the cube should be tied to the
70 value of a QSlider, then connecting the slider's value change signal to a
71 slot or lambda that forwards the new value and calls update() is
72 sufficient.
73
74 The vertex and fragment shaders are provided as Vulkan-style GLSL and must
75 be processed first by the Qt shader infrastructure first. This is achieved
76 either by running the \c qsb command-line tool manually, or by using the
77 \l{qt_add_shaders}{qt_add_shaders()} function in
78 CMake. The QRhiWidget implementation loads these pre-processed \c{.qsb}
79 files that are shipped with the application. See \l{Qt Shader Tools} for
80 more information about Qt's shader translation infrastructure.
81
82 The source code for these shaders could be the following:
83
84 \c{color.vert}
85
86 \snippet qrhiwidget/rhiwidgetintro.vert 0
87
88 \c{color.frag}
89
90 \snippet qrhiwidget/rhiwidgetintro.frag 0
91
92 The result is a widget that shows the following:
93
94 \image qrhiwidget-intro.jpg {Multicolored triangle on a green background}
95
96 For a complete, minimal, introductory example check out the \l{Simple RHI
97 Widget Example}.
98
99 For an example with more functionality and demonstration of further
100 concepts, see the \l{Cube RHI Widget Example}.
101
102 QRhiWidget always involves rendering into a backing texture, not
103 directly to the window (the surface or layer provided by the windowing
104 system for the native window). This allows properly compositing the content
105 with the rest of the widget-based UI, and offering a simple and compact
106 API, making it easy to get started. All this comes at the expense of
107 additional resources and a potential effect on performance. This is often
108 perfectly acceptable in practice, but advanced users should keep in mind
109 the pros and cons of the different approaches. Refer to the \l{RHI Window
110 Example} and compare it with the \l{Simple RHI Widget Example} for details
111 about the two approaches.
112
113 Reparenting a QRhiWidget into a widget hierarchy that belongs to a
114 different window (top-level widget), or making the QRhiWidget itself a
115 top-level (by setting the parent to \nullptr), involves changing the
116 associated QRhi (and potentially destroying the old one) while the
117 QRhiWidget continues to stay alive and well. To support this, robust
118 QRhiWidget implementations are expected to reimplement the
119 releaseResources() virtual function as well, and drop their QRhi resources
120 just as they do in the destructor. The \l{Cube RHI Widget Example}
121 demonstrates this in practice.
122
123 While not a primary use case, QRhiWidget also allows incorporating
124 rendering code that directly uses a 3D graphics API such as Vulkan, Metal,
125 Direct 3D, or OpenGL. See \l QRhiCommandBuffer::beginExternal() for details
126 on recording native commands within a QRhi render pass, as well as
127 \l QRhiTexture::createFrom() for a way to wrap an existing native texture and
128 then use it with QRhi in a subsequent render pass. Note however that the
129 configurability of the underlying graphics API (its device or context
130 features, layers, extensions, etc.) is going to be limited since
131 QRhiWidget's primary goal is to provide an environment suitable for
132 QRhi-based rendering code, not to enable arbitrary, potentially complex,
133 foreign rendering engines.
134
135 \since 6.7
136
137 \sa QRhi, QShader, QOpenGLWidget, {Simple RHI Widget Example}, {Cube RHI Widget Example}
138 */
139
140/*!
141 \enum QRhiWidget::Api
142 Specifies the 3D API and QRhi backend to use
143
144 \value Null
145 \value OpenGL
146 \value Metal
147 \value Vulkan
148 \value Direct3D11
149 \value Direct3D12
150
151 \sa QRhi
152 */
153
154/*!
155 \enum QRhiWidget::TextureFormat
156 Specifies the format of the texture to which the QRhiWidget renders.
157
158 \value RGBA8 See QRhiTexture::RGBA8.
159 \value RGBA16F See QRhiTexture::RGBA16F.
160 \value RGBA32F See QRhiTexture::RGBA32F.
161 \value RGB10A2 See QRhiTexture::RGB10A2.
162
163 \sa QRhiTexture
164 */
165
166/*!
167 Constructs a widget which is a child of \a parent, with widget flags set to \a f.
168 */
169QRhiWidget::QRhiWidget(QWidget *parent, Qt::WindowFlags f)
170 : QWidget(*(new QRhiWidgetPrivate), parent, f)
171{
172 Q_D(QRhiWidget);
173 d->init();
174}
175
176/*!
177 * \internal
178 */
179QRhiWidget::QRhiWidget(QRhiWidgetPrivate &dd, QWidget *parent, Qt::WindowFlags f)
180 : QWidget(dd, parent, f)
181{
182 Q_D(QRhiWidget);
183 d->init();
184}
185
186/*!
187 * \internal
188 */
189void QRhiWidgetPrivate::init()
190{
191 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::RhiBasedRendering)))
192 qWarning("QRhiWidget: QRhi is not supported on this platform.");
193 else
194 setRenderToTexture();
195
196 config.setEnabled(true);
197#if defined(Q_OS_DARWIN)
198 config.setApi(QPlatformBackingStoreRhiConfig::Metal);
199#elif defined(Q_OS_WIN)
200 config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
201#else
202 config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
203#endif
204}
205
206/*!
207 Destructor.
208 */
209QRhiWidget::~QRhiWidget()
210{
211 Q_D(QRhiWidget);
212
213 if (d->rhi) {
214 d->rhi->removeCleanupCallback(this);
215 // rhi resources must be destroyed here, due to how QWidget teardown works;
216 // it should not be left to the private object's destruction.
217 d->resetRenderTargetObjects();
218 d->resetColorBufferObjects();
219 qDeleteAll(d->pendingDeletes);
220 }
221
222 d->offscreenRenderer.reset();
223}
224
225/*!
226 Handles resize events that are passed in the \a e event parameter. Calls
227 the virtual function initialize().
228
229 \note Avoid overriding this function in derived classes. If that is not
230 feasible, make sure that QRhiWidget's implementation is invoked too.
231 Otherwise the underlying texture object and related resources will not get
232 resized properly and will lead to incorrect rendering.
233 */
234void QRhiWidget::resizeEvent(QResizeEvent *e)
235{
236 Q_D(QRhiWidget);
237
238 if (e->size().isEmpty()) {
239 d->noSize = true;
240 return;
241 }
242 d->noSize = false;
243
244 d->sendPaintEvent(QRect(QPoint(0, 0), size()));
245}
246
247/*!
248 Handles paint events.
249
250 Calling QWidget::update() will lead to sending a paint event \a e, and thus
251 invoking this function. The sending of the event is asynchronous and will
252 happen at some point after returning from update(). This function will
253 then, after some preparation, call the virtual render() to update the
254 contents of the QRhiWidget's associated texture. The widget's top-level
255 window will then composite the texture with the rest of the window.
256 */
257void QRhiWidget::paintEvent(QPaintEvent *)
258{
259 Q_D(QRhiWidget);
260 if (!updatesEnabled() || d->noSize)
261 return;
262
263 d->ensureRhi();
264 if (!d->rhi) {
265 qWarning("QRhiWidget: No QRhi");
266 emit renderFailed();
267 return;
268 }
269
270 QRhiCommandBuffer *cb = nullptr;
271 if (d->rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
272 return;
273
274 bool needsInit = false;
275 d->ensureTexture(&needsInit);
276 if (d->colorTexture || d->msaaColorBuffer) {
277 bool canRender = true;
278 if (needsInit)
279 canRender = d->invokeInitialize(cb);
280 if (canRender)
281 render(cb);
282 }
283
284 d->rhi->endOffscreenFrame();
285}
286
287/*!
288 \reimp
289*/
290bool QRhiWidget::event(QEvent *e)
291{
292 Q_D(QRhiWidget);
293 switch (e->type()) {
294 case QEvent::WindowAboutToChangeInternal:
295 // The QRhi will almost certainly change, prevent texture() from
296 // returning the existing QRhiTexture in the meantime.
297 d->textureInvalid = true;
298
299 if (d->rhi && d->rhi != d->offscreenRenderer.rhi()) {
300 // Drop the cleanup callback registered to the toplevel's rhi and
301 // do the early-release, there may not be another chance to do
302 // this, and the QRhi we have currently set may be destroyed by the
303 // time we get to ensureRhi() again.
304 d->rhi->removeCleanupCallback(this);
305 releaseResources(); // notify the user code about the early-release
306 d->releaseResources();
307 // The old QRhi may be destroyed before the next paint, reparent,
308 // or widget destruction. ensureRhi() will register a new callback
309 // even if we end up using the same QRhi again.
310 d->rhi = nullptr;
311 }
312
313 break;
314
315 case QEvent::Show:
316 if (isVisible())
317 d->sendPaintEvent(QRect(QPoint(0, 0), size()));
318 break;
319 default:
320 break;
321 }
322 return QWidget::event(e);
323}
324
325QWidgetPrivate::TextureData QRhiWidgetPrivate::texture() const
326{
327 // This is the only safe place to clear pendingDeletes, due to the
328 // possibility of the texture returned in the previous invocation of this
329 // function having been added to pendingDeletes, meaning the object then
330 // needs to be valid until the next (this) invocation of this function.
331 // (the exact object lifetime requirements depend on the
332 // QWidget/RepaintManager internal implementation; for now avoid relying on
333 // such details by clearing pendingDeletes only here, not in endCompose())
334 qDeleteAll(pendingDeletes);
335 pendingDeletes.clear();
336
337 TextureData td;
338 if (!textureInvalid)
339 td.textureLeft = resolveTexture ? resolveTexture : colorTexture;
340 return td;
341}
342
343QPlatformTextureList::Flags QRhiWidgetPrivate::textureListFlags()
344{
345 QPlatformTextureList::Flags flags = QWidgetPrivate::textureListFlags();
346 if (mirrorVertically)
347 flags |= QPlatformTextureList::MirrorVertically;
348 return flags;
349}
350
351QPlatformBackingStoreRhiConfig QRhiWidgetPrivate::rhiConfig() const
352{
353 return config;
354}
355
356void QRhiWidgetPrivate::endCompose()
357{
358 // This function is called by QWidgetRepaintManager right after the
359 // backingstore's QRhi-based flush returns. In practice that means after
360 // the begin-endFrame() on the top-level window's swapchain.
361
362 if (rhi) {
363 Q_Q(QRhiWidget);
364 emit q->frameSubmitted();
365 }
366}
367
368// This is reimplemented to enable calling QWidget::grab() on the widget or an
369// ancestor of it. At the same time, QRhiWidget provides its own
370// grabFramebuffer() as well, mirroring QQuickWidget and QOpenGLWidget for
371// consistency. In both types of grabs we end up in here.
372QImage QRhiWidgetPrivate::grabFramebuffer()
373{
374 Q_Q(QRhiWidget);
375 if (noSize)
376 return QImage();
377
378 ensureRhi();
379 if (!rhi) {
380 // The widget (and its parent chain, if any) may not be shown at
381 // all, yet one may still want to use it for grabs. This is
382 // ridiculous of course because the rendering infrastructure is
383 // tied to the top-level widget that initializes upon expose, but
384 // it has to be supported.
385 // ensureRhi() does not keep our own QRhi, so use the one created by
386 // an earlier grab again. The color buffer and the resources the
387 // subclass created in initialize() belong to it.
388 if (!offscreenRenderer.rhi()) {
389 offscreenRenderer.setConfig(config);
390 // no window passed in, so no swapchain, but we get a functional QRhi which we own
391 offscreenRenderer.create();
392 }
393 rhi = offscreenRenderer.rhi();
394 if (!rhi) {
395 qWarning("QRhiWidget: Failed to create dedicated QRhi for grabbing");
396 emit q->renderFailed();
397 return QImage();
398 }
399 }
400
401 QRhiCommandBuffer *cb = nullptr;
402 if (rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
403 return QImage();
404
405 QRhiReadbackResult readResult;
406 bool readCompleted = false;
407 bool needsInit = false;
408 ensureTexture(&needsInit);
409
410 if (colorTexture || msaaColorBuffer) {
411 bool canRender = true;
412 if (needsInit)
413 canRender = invokeInitialize(cb);
414 if (canRender)
415 q->render(cb);
416
417 QRhiResourceUpdateBatch *readbackBatch = rhi->nextResourceUpdateBatch();
418 readResult.completed = [&readCompleted] { readCompleted = true; };
419 readbackBatch->readBackTexture(resolveTexture ? resolveTexture : colorTexture, &readResult);
420 cb->resourceUpdate(readbackBatch);
421 }
422
423 rhi->endOffscreenFrame();
424
425 if (readCompleted) {
426 QImage::Format imageFormat = QImage::Format_RGBA8888;
427 switch (widgetTextureFormat) {
428 case QRhiWidget::TextureFormat::RGBA8:
429 break;
430 case QRhiWidget::TextureFormat::RGBA16F:
431 imageFormat = QImage::Format_RGBA16FPx4;
432 break;
433 case QRhiWidget::TextureFormat::RGBA32F:
434 imageFormat = QImage::Format_RGBA32FPx4;
435 break;
436 case QRhiWidget::TextureFormat::RGB10A2:
437 imageFormat = QImage::Format_BGR30;
438 break;
439 }
440 QImage wrapperImage(reinterpret_cast<const uchar *>(readResult.data.constData()),
441 readResult.pixelSize.width(), readResult.pixelSize.height(),
442 imageFormat);
443 QImage result;
444 if (rhi->isYUpInFramebuffer())
445 result = wrapperImage.flipped();
446 else
447 result = wrapperImage.copy();
448 result.setDevicePixelRatio(q->devicePixelRatio());
449 return result;
450 } else {
451 Q_UNREACHABLE();
452 }
453
454 return QImage();
455}
456
457void QRhiWidgetPrivate::resetColorBufferObjects()
458{
459 if (colorTexture) {
460 pendingDeletes.append(colorTexture);
461 colorTexture = nullptr;
462 }
463 if (msaaColorBuffer) {
464 pendingDeletes.append(msaaColorBuffer);
465 msaaColorBuffer = nullptr;
466 }
467 if (resolveTexture) {
468 pendingDeletes.append(resolveTexture);
469 resolveTexture = nullptr;
470 }
471}
472
473void QRhiWidgetPrivate::resetRenderTargetObjects()
474{
475 if (renderTarget) {
476 renderTarget->deleteLater();
477 renderTarget = nullptr;
478 }
479 if (renderPassDescriptor) {
480 renderPassDescriptor->deleteLater();
481 renderPassDescriptor = nullptr;
482 }
483 if (depthStencilBuffer) {
484 depthStencilBuffer->deleteLater();
485 depthStencilBuffer = nullptr;
486 }
487}
488
489void QRhiWidgetPrivate::releaseResources()
490{
491 resetRenderTargetObjects();
492 resetColorBufferObjects();
493 qDeleteAll(pendingDeletes);
494 pendingDeletes.clear();
495}
496
497void QRhiWidgetPrivate::ensureRhi()
498{
499 Q_Q(QRhiWidget);
500 QRhi *currentRhi = QWidgetPrivate::rhi();
501 if (currentRhi && currentRhi->backend() != QBackingStoreRhiSupport::apiToRhiBackend(config.api())) {
502 qWarning("The top-level window is already using another graphics API for composition, "
503 "'%s' is not compatible with this widget",
504 currentRhi->backendName());
505 return;
506 }
507
508 if (currentRhi && rhi != currentRhi) {
509 if (rhi) {
510 rhi->removeCleanupCallback(q);
511 // if previously we created our own but now get a QRhi from the
512 // top-level, then drop what we have and start using the top-level's
513 if (rhi == offscreenRenderer.rhi()) {
514 q->releaseResources(); // notify the user code about the early-release
515 releaseResources();
516 offscreenRenderer.reset();
517 } else {
518 // rhi resources created by us all belong to the old rhi, drop them;
519 // due to nulling out colorTexture this is also what ensures that
520 // initialize() is going to be called again eventually
521 resetRenderTargetObjects();
522 resetColorBufferObjects();
523 }
524 }
525
526 // Normally the widget gets destroyed before the QRhi (which is managed by
527 // the top-level's backingstore). When reparenting between top-levels is
528 // involved, that is not always the case. Therefore we use a per-widget rhi
529 // cleanup callback to get notified when the QRhi is about to be destroyed
530 // while the QRhiWidget is still around.
531 currentRhi->addCleanupCallback(q, [q, this](QRhi *regRhi) {
532 if (!QWidgetPrivate::get(q)->data.in_destructor && this->rhi == regRhi) {
533 q->releaseResources(); // notify the user code about the early-release
534 releaseResources();
535 // must null out our ref, the QRhi object is going to be invalid
536 this->rhi = nullptr;
537 }
538 });
539 }
540
541 rhi = currentRhi;
542}
543
544void QRhiWidgetPrivate::ensureTexture(bool *changed)
545{
546 Q_Q(QRhiWidget);
547
548 QSize newSize = fixedSize;
549 if (newSize.isEmpty())
550 newSize = q->size() * q->devicePixelRatio();
551
552 const int minTexSize = rhi->resourceLimit(QRhi::TextureSizeMin);
553 const int maxTexSize = rhi->resourceLimit(QRhi::TextureSizeMax);
554 newSize.setWidth(qMin(maxTexSize, qMax(minTexSize, newSize.width())));
555 newSize.setHeight(qMin(maxTexSize, qMax(minTexSize, newSize.height())));
556
557 if (colorTexture) {
558 if (colorTexture->format() != rhiTextureFormat || colorTexture->sampleCount() != samples) {
559 resetColorBufferObjects();
560 // sample count change needs new depth-stencil, possibly a new
561 // render target; format change needs new renderpassdescriptor;
562 // therefore must drop the rest too
563 resetRenderTargetObjects();
564 }
565 }
566
567 if (msaaColorBuffer) {
568 if (msaaColorBuffer->backingFormat() != rhiTextureFormat || msaaColorBuffer->sampleCount() != samples) {
569 resetColorBufferObjects();
570 // sample count change needs new depth-stencil, possibly a new
571 // render target; format change needs new renderpassdescriptor;
572 // therefore must drop the rest too
573 resetRenderTargetObjects();
574 }
575 }
576
577 if (!colorTexture && samples <= 1) {
578 if (changed)
579 *changed = true;
580 if (!rhi->isTextureFormatSupported(rhiTextureFormat)) {
581 qWarning("QRhiWidget: The requested texture format (%d) is not supported by the "
582 "underlying 3D graphics API implementation", int(rhiTextureFormat));
583 }
584 colorTexture = rhi->newTexture(rhiTextureFormat, newSize, samples, QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
585 if (!colorTexture->create()) {
586 qWarning("Failed to create backing texture for QRhiWidget");
587 delete colorTexture;
588 colorTexture = nullptr;
589 return;
590 }
591 }
592
593 if (samples > 1) {
594 if (!msaaColorBuffer) {
595 if (changed)
596 *changed = true;
597 if (!rhi->isFeatureSupported(QRhi::MultisampleRenderBuffer)) {
598 qWarning("QRhiWidget: Multisample renderbuffers are reported as unsupported; "
599 "sample count %d will not work as expected", samples);
600 }
601 if (!rhi->isTextureFormatSupported(rhiTextureFormat)) {
602 qWarning("QRhiWidget: The requested texture format (%d) is not supported by the "
603 "underlying 3D graphics API implementation", int(rhiTextureFormat));
604 }
605 msaaColorBuffer = rhi->newRenderBuffer(QRhiRenderBuffer::Color, newSize, samples, {}, rhiTextureFormat);
606 if (!msaaColorBuffer->create()) {
607 qWarning("Failed to create multisample color buffer for QRhiWidget");
608 delete msaaColorBuffer;
609 msaaColorBuffer = nullptr;
610 return;
611 }
612 }
613 if (!resolveTexture) {
614 if (changed)
615 *changed = true;
616 resolveTexture = rhi->newTexture(rhiTextureFormat, newSize, 1, QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
617 if (!resolveTexture->create()) {
618 qWarning("Failed to create resolve texture for QRhiWidget");
619 delete resolveTexture;
620 resolveTexture = nullptr;
621 return;
622 }
623 }
624 } else if (resolveTexture) {
625 resolveTexture->deleteLater();
626 resolveTexture = nullptr;
627 }
628
629 if (colorTexture && colorTexture->pixelSize() != newSize) {
630 if (changed)
631 *changed = true;
632 colorTexture->setPixelSize(newSize);
633 if (!colorTexture->create())
634 qWarning("Failed to rebuild texture for QRhiWidget after resizing");
635 }
636
637 if (msaaColorBuffer && msaaColorBuffer->pixelSize() != newSize) {
638 if (changed)
639 *changed = true;
640 msaaColorBuffer->setPixelSize(newSize);
641 if (!msaaColorBuffer->create())
642 qWarning("Failed to rebuild multisample color buffer for QRhiWidget after resizing");
643 }
644
645 if (resolveTexture && resolveTexture->pixelSize() != newSize) {
646 if (changed)
647 *changed = true;
648 resolveTexture->setPixelSize(newSize);
649 if (!resolveTexture->create())
650 qWarning("Failed to rebuild resolve texture for QRhiWidget after resizing");
651 }
652
653 textureInvalid = false;
654}
655
656bool QRhiWidgetPrivate::invokeInitialize(QRhiCommandBuffer *cb)
657{
658 Q_Q(QRhiWidget);
659 if (!colorTexture && !msaaColorBuffer)
660 return false;
661
662 if (autoRenderTarget) {
663 const QSize pixelSize = colorTexture ? colorTexture->pixelSize() : msaaColorBuffer->pixelSize();
664 if (!depthStencilBuffer) {
665 depthStencilBuffer = rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, pixelSize, samples);
666 if (!depthStencilBuffer->create()) {
667 qWarning("Failed to create depth-stencil buffer for QRhiWidget");
668 resetRenderTargetObjects();
669 return false;
670 }
671 } else if (depthStencilBuffer->pixelSize() != pixelSize) {
672 depthStencilBuffer->setPixelSize(pixelSize);
673 if (!depthStencilBuffer->create()) {
674 qWarning("Failed to rebuild depth-stencil buffer for QRhiWidget with new size");
675 return false;
676 }
677 }
678
679 if (!renderTarget) {
680 QRhiColorAttachment color0;
681 if (colorTexture)
682 color0.setTexture(colorTexture);
683 else
684 color0.setRenderBuffer(msaaColorBuffer);
685 if (samples > 1)
686 color0.setResolveTexture(resolveTexture);
687 QRhiTextureRenderTargetDescription rtDesc(color0, depthStencilBuffer);
688 renderTarget = rhi->newTextureRenderTarget(rtDesc);
689 renderPassDescriptor = renderTarget->newCompatibleRenderPassDescriptor();
690 renderTarget->setRenderPassDescriptor(renderPassDescriptor);
691 if (!renderTarget->create()) {
692 qWarning("Failed to create render target for QRhiWidget");
693 resetRenderTargetObjects();
694 return false;
695 }
696 }
697 } else {
698 resetRenderTargetObjects();
699 }
700
701 q->initialize(cb);
702
703 return true;
704}
705
706/*!
707 \return the currently set graphics API (QRhi backend).
708
709 \sa setApi()
710 */
711QRhiWidget::Api QRhiWidget::api() const
712{
713 Q_D(const QRhiWidget);
714 switch (d->config.api()) {
715 case QPlatformBackingStoreRhiConfig::OpenGL:
716 return Api::OpenGL;
717 case QPlatformBackingStoreRhiConfig::Metal:
718 return Api::Metal;
719 case QPlatformBackingStoreRhiConfig::Vulkan:
720 return Api::Vulkan;
721 case QPlatformBackingStoreRhiConfig::D3D11:
722 return Api::Direct3D11;
723 case QPlatformBackingStoreRhiConfig::D3D12:
724 return Api::Direct3D12;
725 case QPlatformBackingStoreRhiConfig::Null:
726 return Api::Null;
727 }
728 Q_UNREACHABLE_RETURN(Api::Null);
729}
730
731/*!
732 Sets the graphics API and QRhi backend to use to \a api.
733
734 \warning This function must be called early enough, before the widget is
735 added to a widget hierarchy and displayed on screen. For example, aim to
736 call the function for the subclass constructor. If called too late, the
737 function will have no effect.
738
739 The default value depends on the platform: Metal on macOS and iOS, Direct
740 3D 11 on Windows, OpenGL otherwise.
741
742 The \a api can only be set once for the widget and its top-level window,
743 once it is done and takes effect, the window can only use that API and QRhi
744 backend to render. Attempting to set another value, or to add another
745 QRhiWidget with a different \a api will not function as expected.
746
747 \sa setColorBufferFormat(), setDebugLayerEnabled(), api()
748 */
749void QRhiWidget::setApi(Api api)
750{
751 Q_D(QRhiWidget);
752 switch (api) {
753 case Api::OpenGL:
754 d->config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
755 break;
756 case Api::Metal:
757 d->config.setApi(QPlatformBackingStoreRhiConfig::Metal);
758 break;
759 case Api::Vulkan:
760 d->config.setApi(QPlatformBackingStoreRhiConfig::Vulkan);
761 break;
762 case Api::Direct3D11:
763 d->config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
764 break;
765 case Api::Direct3D12:
766 d->config.setApi(QPlatformBackingStoreRhiConfig::D3D12);
767 break;
768 case Api::Null:
769 d->config.setApi(QPlatformBackingStoreRhiConfig::Null);
770 break;
771 }
772}
773
774/*!
775 \return true if a debug or validation layer will be requested if applicable
776 to the graphics API in use.
777
778 \sa setDebugLayerEnabled()
779 */
780bool QRhiWidget::isDebugLayerEnabled() const
781{
782 Q_D(const QRhiWidget);
783 return d->config.isDebugLayerEnabled();
784}
785
786/*!
787 Requests the debug or validation layer of the underlying graphics API
788 when \a enable is true.
789
790 \warning This function must be called early enough, before the widget is added
791 to a widget hierarchy and displayed on screen. For example, aim to call the
792 function for the subclass constructor. If called too late, the function
793 will have no effect.
794
795 Applicable for Vulkan and Direct 3D.
796
797 By default this is disabled.
798
799 \sa setApi(), isDebugLayerEnabled()
800 */
801void QRhiWidget::setDebugLayerEnabled(bool enable)
802{
803 Q_D(QRhiWidget);
804 d->config.setDebugLayer(enable);
805}
806
807/*!
808 \property QRhiWidget::colorBufferFormat
809
810 This property controls the texture format of the texture (or renderbuffer)
811 used as the color buffer. The default value is TextureFormat::RGBA8.
812 QRhiWidget supports rendering to a subset of the formats supported by \l
813 QRhiTexture. Only formats that are reported as supported from \l
814 QRhi::isTextureFormatSupported() should be specified, rendering will not be
815 functional otherwise.
816
817 \note Setting a new format when the widget is already initialized and has
818 rendered implies that all QRhiGraphicsPipeline objects created by the
819 renderer may become unusable, if the associated QRhiRenderPassDescriptor is
820 now incompatible due to the different texture format. Similarly to changing
821 \l sampleCount dynamically, this means that initialize() or render()
822 implementations must then take care of releasing the existing pipelines and
823 creating new ones.
824 */
825
826QRhiWidget::TextureFormat QRhiWidget::colorBufferFormat() const
827{
828 Q_D(const QRhiWidget);
829 return d->widgetTextureFormat;
830}
831
832void QRhiWidget::setColorBufferFormat(TextureFormat format)
833{
834 Q_D(QRhiWidget);
835 if (d->widgetTextureFormat != format) {
836 d->widgetTextureFormat = format;
837 switch (format) {
838 case TextureFormat::RGBA8:
839 d->rhiTextureFormat = QRhiTexture::RGBA8;
840 break;
841 case TextureFormat::RGBA16F:
842 d->rhiTextureFormat = QRhiTexture::RGBA16F;
843 break;
844 case TextureFormat::RGBA32F:
845 d->rhiTextureFormat = QRhiTexture::RGBA32F;
846 break;
847 case TextureFormat::RGB10A2:
848 d->rhiTextureFormat = QRhiTexture::RGB10A2;
849 break;
850 }
851 emit colorBufferFormatChanged(format);
852 update();
853 }
854}
855
856/*!
857 \property QRhiWidget::sampleCount
858
859 This property controls for sample count for multisample antialiasing.
860 By default the value is \c 1 which means MSAA is disabled.
861
862 Valid values are 1, 4, 8, and sometimes 16 and 32.
863 \l QRhi::supportedSampleCounts() can be used to query the supported sample
864 counts at run time, but typically applications should request 1 (no MSAA),
865 4x (normal MSAA) or 8x (high MSAA).
866
867 \note Setting a new value implies that all QRhiGraphicsPipeline objects
868 created by the renderer must use the same sample count from then on.
869 Existing QRhiGraphicsPipeline objects created with a different sample count
870 must not be used anymore. When the value changes, all color and
871 depth-stencil buffers are destroyed and recreated automatically, and
872 initialize() is invoked again. However, when
873 \l autoRenderTarget is \c false, it will be up to the application to
874 manage this with regards to the depth-stencil buffer or additional color
875 buffers.
876
877 Changing the sample count from the default 1 to a higher value implies that
878 colorTexture() becomes \nullptr and msaaColorBuffer() starts returning a
879 valid object. Switching back to 1 (or 0), implies the opposite: in the next
880 call to initialize() msaaColorBuffer() is going to return \nullptr, whereas
881 colorTexture() becomes once again valid. In addition, resolveTexture()
882 returns a valid (non-multisample) QRhiTexture whenever the sample count is
883 greater than 1 (i.e., MSAA is in use).
884
885 \sa msaaColorBuffer(), resolveTexture()
886 */
887
888int QRhiWidget::sampleCount() const
889{
890 Q_D(const QRhiWidget);
891 return d->samples;
892}
893
894void QRhiWidget::setSampleCount(int samples)
895{
896 Q_D(QRhiWidget);
897 if (d->samples != samples) {
898 d->samples = samples;
899 emit sampleCountChanged(samples);
900 update();
901 }
902}
903
904/*!
905 \property QRhiWidget::fixedColorBufferSize
906
907 The fixed size, in pixels, of the QRhiWidget's associated texture. Relevant
908 when a fixed texture size is desired that does not depend on the widget's
909 size. This size has no effect on the geometry of the widget (its size and
910 placement within the top-level window), which means the texture's content
911 will appear stretched (scaled up) or scaled down onto the widget's area.
912
913 For example, setting a size that is exactly twice the widget's (pixel) size
914 effectively performs 2x supersampling (rendering at twice the resolution and
915 then implicitly scaling down when texturing the quad corresponding to the
916 widget in the window). On the other hand, setting a size that is half of the
917 widget's effectively achieves rendering at half resolution and then
918 upscaling the results.
919
920 By default the value is a null QSize. A null or empty QSize means that the
921 texture's size follows the QRhiWidget's size. (\c{texture size} = \c{widget
922 size} * \c{device pixel ratio}).
923
924 \note The device pixel ratio (the system compositor's scale factor) can have
925 a big impact on performance, since a scale factor of 2 (200%) means
926 rendering at twice the resolution, so twice of what the developer and UI
927 designer perceives as the widget's size, and then effectively downscaling
928 the content, similarly to what happens when setting this property to twice
929 the widget's pixel size on a system where the device pixel ratio is 1.
930 Therefore, this property is expected to be rarely used with sizes bigger
931 than the widget's pixel size, since many modern desktop systems have neither
932 have the need nor the performance budget for it, when a larger than 1 device
933 pixel ratio is used anyway by the system. Instead, the main use case for
934 this property is to set a smaller size, in order to render at a reasonable
935 smaller resolution instead of blindly following the window geometry, however
936 big that may be.
937 */
938
939QSize QRhiWidget::fixedColorBufferSize() const
940{
941 Q_D(const QRhiWidget);
942 return d->fixedSize;
943}
944
945void QRhiWidget::setFixedColorBufferSize(QSize pixelSize)
946{
947 Q_D(QRhiWidget);
948 if (d->fixedSize != pixelSize) {
949 d->fixedSize = pixelSize;
950 emit fixedColorBufferSizeChanged(pixelSize);
951 update();
952 }
953}
954
955/*!
956 \property QRhiWidget::mirrorVertically
957
958 When enabled, flips the image around the X axis when compositing the
959 QRhiWidget's backing texture with the rest of the widget content in the
960 top-level window.
961
962 The default value is \c false.
963 */
964
965bool QRhiWidget::isMirrorVerticallyEnabled() const
966{
967 Q_D(const QRhiWidget);
968 return d->mirrorVertically;
969}
970
971void QRhiWidget::setMirrorVertically(bool enabled)
972{
973 Q_D(QRhiWidget);
974 if (d->mirrorVertically != enabled) {
975 d->mirrorVertically = enabled;
976 emit mirrorVerticallyChanged(enabled);
977 update();
978 }
979}
980
981/*!
982 \property QRhiWidget::autoRenderTarget
983
984 The current setting for automatic depth-stencil buffer and render
985 target maintenance.
986
987 By default the value is \c true.
988 */
989bool QRhiWidget::isAutoRenderTargetEnabled() const
990{
991 Q_D(const QRhiWidget);
992 return d->autoRenderTarget;
993}
994
995/*!
996 Controls if a depth-stencil QRhiRenderBuffer and a QRhiTextureRenderTarget
997 is created and maintained automatically by the widget. The default value is
998 \c true.
999
1000 In automatic mode, the size and sample count of the depth-stencil buffer
1001 follows the color buffer texture's settings. In non-automatic mode,
1002 renderTarget() and depthStencilBuffer() always return \nullptr and it is
1003 then up to the application's implementation of initialize() to take care of
1004 setting up and managing these objects.
1005
1006 Call this function with \a enabled set to \c false early on, for example in
1007 the derived class' constructor, to disable the automatic mode.
1008 */
1009void QRhiWidget::setAutoRenderTarget(bool enabled)
1010{
1011 Q_D(QRhiWidget);
1012 if (d->autoRenderTarget != enabled) {
1013 d->autoRenderTarget = enabled;
1014 update();
1015 }
1016}
1017
1018/*!
1019 Renders a new frame, reads the contents of the texture back, and returns it
1020 as a QImage.
1021
1022 When an error occurs, a null QImage is returned.
1023
1024 The returned QImage will have a format of QImage::Format_RGBA8888,
1025 QImage::Format_RGBA16FPx4, QImage::Format_RGBA32FPx4, or
1026 QImage::Format_BGR30, depending on colorBufferFormat().
1027
1028 QRhiWidget does not know the renderer's approach to blending and
1029 composition, and therefore cannot know if the output has alpha
1030 premultiplied in the RGB color values. Thus \c{_Premultiplied} QImage
1031 formats are never used for the returned QImage, even when it would be
1032 appropriate. It is up to the caller to reinterpret the resulting data as it
1033 sees fit.
1034
1035 \note This function can also be called when the QRhiWidget is not added to
1036 a widget hierarchy belonging to an on-screen top-level window. This allows
1037 generating an image from a 3D rendering off-screen.
1038
1039 The function is named grabFramebuffer() for consistency with QOpenGLWidget
1040 and QQuickWidget. It is not the only way to get CPU-side image data out of
1041 the QRhiWidget's content: calling \l QWidget::grab() on a QRhiWidget, or an
1042 ancestor of it, is functional as well (returning a QPixmap). Besides
1043 working directly with QImage, another advantage of grabFramebuffer() is
1044 that it may be slightly more performant, simply because it does not have to
1045 go through the rest of QWidget infrastructure but can right away trigger
1046 rendering a new frame and then do the readback.
1047
1048 \sa setColorBufferFormat()
1049 */
1050QImage QRhiWidget::grabFramebuffer() const
1051{
1052 return const_cast<QRhiWidgetPrivate *>(d_func())->grabFramebuffer();
1053}
1054
1055/*!
1056 Called when the widget is initialized for the first time, when the
1057 associated texture's size, format, or sample count changes, or when the
1058 QRhi and texture change for any reason. The function is expected to
1059 maintain (create if not yet created, adjust and rebuild if the size has
1060 changed) the graphics resources used by the rendering code in render().
1061
1062 To query the QRhi, QRhiTexture, and other related objects, call rhi(),
1063 colorTexture(), depthStencilBuffer(), and renderTarget().
1064
1065 When the widget size changes, the QRhi object, the color buffer texture,
1066 and the depth stencil buffer objects are all the same instances (so the
1067 getters return the same pointers) as before, but the color and
1068 depth/stencil buffers will likely have been rebuilt, meaning the
1069 \l{QRhiTexture::pixelSize()}{size} and the underlying native texture
1070 resource may be different than in the last invocation.
1071
1072 Reimplementations should also be prepared that the QRhi object and the
1073 color buffer texture may change between invocations of this function. One
1074 special case where the objects will be different is when performing a
1075 grabFramebuffer() with a widget that is not yet shown, and then making the
1076 widget visible on-screen within a top-level widget. There the grab will
1077 happen with a dedicated QRhi that is then replaced with the top-level
1078 window's associated QRhi in subsequent initialize() and render()
1079 invocations. Another, more common case is when the widget is reparented so
1080 that it belongs to a new top-level window. In this case the QRhi and all
1081 related resources managed by the QRhiWidget will be different instances
1082 than before in the subsequent call to this function. Is is then important
1083 that all existing QRhi resources previously created by the subclass are
1084 destroyed because they belong to the previous QRhi that should not be used
1085 by the widget anymore.
1086
1087 When \l autoRenderTarget is \c true, which is the default, a
1088 depth-stencil QRhiRenderBuffer and a QRhiTextureRenderTarget associated
1089 with colorTexture() (or msaaColorBuffer()) and the depth-stencil buffer are
1090 created and managed automatically. Reimplementations of initialize() and
1091 render() can query those objects via depthStencilBuffer() and
1092 renderTarget(). When \l autoRenderTarget is set to \c false, these
1093 objects are no longer created and managed automatically. Rather, it will be
1094 up the the initialize() implementation to create buffers and set up the
1095 render target as it sees fit. When manually managing additional color or
1096 depth-stencil attachments for the render target, their size and sample
1097 count must always follow the size and sample count of colorTexture() /
1098 msaaColorBuffer(), otherwise rendering or 3D API validation errors may
1099 occur.
1100
1101 The subclass-created graphics resources are expected to be released in the
1102 destructor implementation of the subclass.
1103
1104 \a cb is the QRhiCommandBuffer for the current frame of the widget. The
1105 function is called with a frame being recorded, but without an active
1106 render pass. The command buffer is provided primarily to allow enqueuing
1107 \l{QRhiCommandBuffer::resourceUpdate()}{resource updates} without deferring
1108 to render().
1109
1110 \sa render()
1111 */
1112void QRhiWidget::initialize(QRhiCommandBuffer *cb)
1113{
1114 Q_UNUSED(cb);
1115}
1116
1117/*!
1118 Called when the widget contents (i.e. the contents of the texture) need
1119 updating.
1120
1121 There is always at least one call to initialize() before this function is
1122 called.
1123
1124 To request updates, call QWidget::update(). Calling update() from within
1125 render() will lead to updating continuously, throttled by vsync.
1126
1127 \a cb is the QRhiCommandBuffer for the current frame of the widget. The
1128 function is called with a frame being recorded, but without an active
1129 render pass.
1130
1131 \sa initialize()
1132 */
1133void QRhiWidget::render(QRhiCommandBuffer *cb)
1134{
1135 Q_UNUSED(cb);
1136}
1137
1138/*!
1139 Called when the need to early-release the graphics resources arises.
1140
1141 This normally does not happen for a QRhiWidget that is added to a top-level
1142 widget's child hierarchy and it then stays there for the rest of its and
1143 the top-level's lifetime. Thus in many cases there is no need to
1144 reimplement this function, e.g. because the application only ever has a
1145 single top-level widget (native window). However, when reparenting of the
1146 widget (or an ancestor of it) is involved, reimplementing this function
1147 will become necessary in robust, well-written QRhiWidget subclasses.
1148
1149 When this function is called, the implementation is expected to destroy all
1150 QRhi resources (QRhiBuffer, QRhiTexture, etc. objects), similarly to how it
1151 is expected to do this in the destructor. Nulling out, using a smart
1152 pointer, or setting a \c{resources-invalid} flag is going to be required as
1153 well, because initialize() will eventually get called afterwards. Note
1154 however that deferring the releasing of resources to the subsequent
1155 initialize() is wrong. If this function is called, the resource must be
1156 dropped before returning. Also note that implementing this function does
1157 not replace the class destructor (or smart pointers): the graphics
1158 resources must still be released in both.
1159
1160 See the \l{Cube RHI Widget Example} for an example of this in action. There
1161 the button that toggles the QRhiWidget between being a child widget (due to
1162 having a parent widget) and being a top-level widget (due to having no
1163 parent widget), will trigger invoking this function since the associated
1164 top-level widget, native window, and QRhi all change during the lifetime of
1165 the QRhiWidget, with the previously used QRhi getting destroyed which
1166 implies an early-release of the associated resources managed by the
1167 still-alive QRhiWidget.
1168
1169 Another case when this function is called is when grabFramebuffer() is used
1170 with a QRhiWidget that is not added to a visible window, i.e. the rendering
1171 is performed offscreen. If later on this QRhiWidget is made visible, or
1172 added to a visible widget hierarchy, the associated QRhi will change from
1173 the temporary one used for offscreen rendering to the window's dedicated
1174 one, thus triggering this function as well.
1175
1176 \sa initialize()
1177 */
1178void QRhiWidget::releaseResources()
1179{
1180}
1181
1182/*!
1183 \return the current QRhi object.
1184
1185 Must only be called from initialize() and render().
1186 */
1187QRhi *QRhiWidget::rhi() const
1188{
1189 Q_D(const QRhiWidget);
1190 return d->rhi;
1191}
1192
1193/*!
1194 \return the texture serving as the color buffer for the widget.
1195
1196 Must only be called from initialize() and render().
1197
1198 Unlike the depth-stencil buffer and the QRhiRenderTarget, this texture is
1199 always available and is managed by the QRhiWidget, independent of the value
1200 of \l autoRenderTarget.
1201
1202 \note When \l sampleCount is larger than 1, and so multisample antialiasing
1203 is enabled, the return value is \nullptr. Instead, query the
1204 \l QRhiRenderBuffer by calling msaaColorBuffer().
1205
1206 \note The backing texture size and sample count can also be queried via the
1207 QRhiRenderTarget returned from renderTarget(). This can be more convenient
1208 and compact than querying from the QRhiTexture or QRhiRenderBuffer, because
1209 it works regardless of multisampling is in use or not.
1210
1211 \sa msaaColorBuffer(), depthStencilBuffer(), renderTarget(), resolveTexture()
1212 */
1213QRhiTexture *QRhiWidget::colorTexture() const
1214{
1215 Q_D(const QRhiWidget);
1216 return d->colorTexture;
1217}
1218
1219/*!
1220 \return the renderbuffer serving as the multisample color buffer for the widget.
1221
1222 Must only be called from initialize() and render().
1223
1224 When \l sampleCount is larger than 1, and so multisample antialising is
1225 enabled, the returned QRhiRenderBuffer has a matching sample count and
1226 serves as the color buffer. Graphics pipelines used to render into this
1227 buffer must be created with the same sample count, and the depth-stencil
1228 buffer's sample count must match as well. The multisample content is
1229 expected to be resolved into the texture returned from resolveTexture().
1230 When \l autoRenderTarget is
1231 \c true, renderTarget() is set up automatically to do this, by setting up
1232 msaaColorBuffer() as the \l{QRhiColorAttachment::renderBuffer()}{renderbuffer} of
1233 color attachment 0 and resolveTexture() as its
1234 \l{QRhiColorAttachment::resolveTexture()}{resolveTexture}.
1235
1236 When MSAA is not in use, the return value is \nullptr. Use colorTexture()
1237 instead then.
1238
1239 Depending on the underlying 3D graphics API, there may be no practical
1240 difference between multisample textures and color renderbuffers with a
1241 sample count larger than 1 (QRhi may just map both to the same native
1242 resource type). Some older APIs however may differentiate between textures
1243 and renderbuffers. In order to support OpenGL ES 3.0, where multisample
1244 renderbuffers are available, but multisample textures are not, QRhiWidget
1245 always performs MSAA by using a multisample QRhiRenderBuffer as the color
1246 attachment (and never a multisample QRhiTexture).
1247
1248 \note The backing texture size and sample count can also be queried via the
1249 QRhiRenderTarget returned from renderTarget(). This can be more convenient
1250 and compact than querying from the QRhiTexture or QRhiRenderBuffer, because
1251 it works regardless of multisampling is in use or not.
1252
1253 \sa colorTexture(), depthStencilBuffer(), renderTarget(), resolveTexture()
1254 */
1255QRhiRenderBuffer *QRhiWidget::msaaColorBuffer() const
1256{
1257 Q_D(const QRhiWidget);
1258 return d->msaaColorBuffer;
1259}
1260
1261/*!
1262 \return the non-multisample texture to which the multisample content is resolved.
1263
1264 The result is \nullptr when multisample antialiasing is not enabled.
1265
1266 Must only be called from initialize() and render().
1267
1268 With MSAA enabled, this is the texture that gets composited with the rest
1269 of the QWidget content on-screen. However, the QRhiWidget's rendering must
1270 target the (multisample) QRhiRenderBuffer returned from
1271 msaaColorBuffer(). When
1272 \l autoRenderTarget is \c true, this is taken care of by the
1273 QRhiRenderTarget returned from renderTarget(). Otherwise, it is up to the
1274 subclass code to correctly configure a render target object with both the
1275 color buffer and resolve textures.
1276
1277 \sa colorTexture()
1278 */
1279QRhiTexture *QRhiWidget::resolveTexture() const
1280{
1281 Q_D(const QRhiWidget);
1282 return d->resolveTexture;
1283}
1284
1285/*!
1286 \return the depth-stencil buffer used by the widget's rendering.
1287
1288 Must only be called from initialize() and render().
1289
1290 Available only when \l autoRenderTarget is \c true. Otherwise the
1291 returned value is \nullptr and it is up the reimplementation of
1292 initialize() to create and manage a depth-stencil buffer and a
1293 QRhiTextureRenderTarget.
1294
1295 \sa colorTexture(), renderTarget()
1296 */
1297QRhiRenderBuffer *QRhiWidget::depthStencilBuffer() const
1298{
1299 Q_D(const QRhiWidget);
1300 return d->depthStencilBuffer;
1301}
1302
1303/*!
1304 \return the render target object that must be used with
1305 \l QRhiCommandBuffer::beginPass() in reimplementations of render().
1306
1307 Must only be called from initialize() and render().
1308
1309 Available only when \l autoRenderTarget is \c true. Otherwise the
1310 returned value is \nullptr and it is up the reimplementation of
1311 initialize() to create and manage a depth-stencil buffer and a
1312 QRhiTextureRenderTarget.
1313
1314 When creating \l{QRhiGraphicsPipeline}{graphics pipelines}, a
1315 QRhiRenderPassDescriptor is needed. This can be queried from the returned
1316 QRhiTextureRenderTarget by calling
1317 \l{QRhiTextureRenderTarget::renderPassDescriptor()}{renderPassDescriptor()}.
1318
1319 \sa colorTexture(), depthStencilBuffer()
1320 */
1321QRhiRenderTarget *QRhiWidget::renderTarget() const
1322{
1323 Q_D(const QRhiWidget);
1324 return d->renderTarget;
1325}
1326
1327/*!
1328 \fn void QRhiWidget::frameSubmitted()
1329
1330 This signal is emitted after the widget's top-level window has finished
1331 composition and has \l{QRhi::endFrame()}{submitted a frame}.
1332*/
1333
1334/*!
1335 \fn void QRhiWidget::renderFailed()
1336
1337 This signal is emitted whenever the widget is supposed to render to its
1338 backing texture (either due to a \l{QWidget::update()}{widget update} or
1339 due to a call to grabFramebuffer()), but there is no \l QRhi for the widget to
1340 use, likely due to issues related to graphics configuration.
1341
1342 This signal may be emitted multiple times when a problem arises. Do not
1343 assume it is emitted only once. Connect with Qt::SingleShotConnection if
1344 the error handling code is to be notified only once.
1345*/
1346
1347QT_END_NAMESPACE
Combined button and popup list for selecting options.