189void QRhiWidgetPrivate::init()
191 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::RhiBasedRendering)))
192 qWarning(
"QRhiWidget: QRhi is not supported on this platform.");
194 setRenderToTexture();
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);
202 config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
209QRhiWidget::~QRhiWidget()
214 d->rhi->removeCleanupCallback(
this);
217 d->resetRenderTargetObjects();
218 d->resetColorBufferObjects();
219 qDeleteAll(d->pendingDeletes);
222 d->offscreenRenderer.reset();
257void QRhiWidget::paintEvent(QPaintEvent *)
260 if (!updatesEnabled() || d->noSize)
265 qWarning(
"QRhiWidget: No QRhi");
270 QRhiCommandBuffer *cb =
nullptr;
271 if (d->rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
274 bool needsInit =
false;
275 d->ensureTexture(&needsInit);
276 if (d->colorTexture || d->msaaColorBuffer) {
277 bool canRender =
true;
279 canRender = d->invokeInitialize(cb);
284 d->rhi->endOffscreenFrame();
290bool QRhiWidget::event(QEvent *e)
294 case QEvent::WindowAboutToChangeInternal:
297 d->textureInvalid =
true;
299 if (d->rhi && d->rhi != d->offscreenRenderer.rhi()) {
304 d->rhi->removeCleanupCallback(
this);
306 d->releaseResources();
317 d->sendPaintEvent(QRect(QPoint(0, 0), size()));
322 return QWidget::event(e);
372QImage QRhiWidgetPrivate::grabFramebuffer()
388 if (!offscreenRenderer.rhi()) {
389 offscreenRenderer.setConfig(config);
391 offscreenRenderer.create();
393 rhi = offscreenRenderer.rhi();
395 qWarning(
"QRhiWidget: Failed to create dedicated QRhi for grabbing");
396 emit q->renderFailed();
401 QRhiCommandBuffer *cb =
nullptr;
402 if (rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
405 QRhiReadbackResult readResult;
406 bool readCompleted =
false;
407 bool needsInit =
false;
408 ensureTexture(&needsInit);
410 if (colorTexture || msaaColorBuffer) {
411 bool canRender =
true;
413 canRender = invokeInitialize(cb);
417 QRhiResourceUpdateBatch *readbackBatch = rhi->nextResourceUpdateBatch();
418 readResult.completed = [&readCompleted] { readCompleted =
true; };
419 readbackBatch->readBackTexture(resolveTexture ? resolveTexture : colorTexture, &readResult);
420 cb->resourceUpdate(readbackBatch);
423 rhi->endOffscreenFrame();
426 QImage::Format imageFormat = QImage::Format_RGBA8888;
427 switch (widgetTextureFormat) {
428 case QRhiWidget::TextureFormat::RGBA8:
430 case QRhiWidget::TextureFormat::RGBA16F:
431 imageFormat = QImage::Format_RGBA16FPx4;
433 case QRhiWidget::TextureFormat::RGBA32F:
434 imageFormat = QImage::Format_RGBA32FPx4;
436 case QRhiWidget::TextureFormat::RGB10A2:
437 imageFormat = QImage::Format_BGR30;
440 QImage wrapperImage(
reinterpret_cast<
const uchar *>(readResult.data.constData()),
441 readResult.pixelSize.width(), readResult.pixelSize.height(),
444 if (rhi->isYUpInFramebuffer())
445 result = wrapperImage.flipped();
447 result = wrapperImage.copy();
448 result.setDevicePixelRatio(q->devicePixelRatio());
497void QRhiWidgetPrivate::ensureRhi()
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());
508 if (currentRhi && rhi != currentRhi) {
510 rhi->removeCleanupCallback(q);
513 if (rhi == offscreenRenderer.rhi()) {
514 q->releaseResources();
516 offscreenRenderer.reset();
521 resetRenderTargetObjects();
522 resetColorBufferObjects();
531 currentRhi->addCleanupCallback(q, [q,
this](QRhi *regRhi) {
532 if (!QWidgetPrivate::get(q)->data.in_destructor &&
this->rhi == regRhi) {
533 q->releaseResources();
544void QRhiWidgetPrivate::ensureTexture(
bool *changed)
548 QSize newSize = fixedSize;
549 if (newSize.isEmpty())
550 newSize = q->size() * q->devicePixelRatio();
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())));
558 if (colorTexture->format() != rhiTextureFormat || colorTexture->sampleCount() != samples) {
559 resetColorBufferObjects();
563 resetRenderTargetObjects();
567 if (msaaColorBuffer) {
568 if (msaaColorBuffer->backingFormat() != rhiTextureFormat || msaaColorBuffer->sampleCount() != samples) {
569 resetColorBufferObjects();
573 resetRenderTargetObjects();
577 if (!colorTexture && samples <= 1) {
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));
584 colorTexture = rhi->newTexture(rhiTextureFormat, newSize, samples, QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
585 if (!colorTexture->create()) {
586 qWarning(
"Failed to create backing texture for QRhiWidget");
588 colorTexture =
nullptr;
594 if (!msaaColorBuffer) {
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);
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));
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;
613 if (!resolveTexture) {
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;
624 }
else if (resolveTexture) {
625 resolveTexture->deleteLater();
626 resolveTexture =
nullptr;
629 if (colorTexture && colorTexture->pixelSize() != newSize) {
632 colorTexture->setPixelSize(newSize);
633 if (!colorTexture->create())
634 qWarning(
"Failed to rebuild texture for QRhiWidget after resizing");
637 if (msaaColorBuffer && msaaColorBuffer->pixelSize() != newSize) {
640 msaaColorBuffer->setPixelSize(newSize);
641 if (!msaaColorBuffer->create())
642 qWarning(
"Failed to rebuild multisample color buffer for QRhiWidget after resizing");
645 if (resolveTexture && resolveTexture->pixelSize() != newSize) {
648 resolveTexture->setPixelSize(newSize);
649 if (!resolveTexture->create())
650 qWarning(
"Failed to rebuild resolve texture for QRhiWidget after resizing");
653 textureInvalid =
false;
656bool QRhiWidgetPrivate::invokeInitialize(QRhiCommandBuffer *cb)
659 if (!colorTexture && !msaaColorBuffer)
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();
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");
680 QRhiColorAttachment color0;
682 color0.setTexture(colorTexture);
684 color0.setRenderBuffer(msaaColorBuffer);
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();
698 resetRenderTargetObjects();
711QRhiWidget::Api QRhiWidget::api()
const
713 Q_D(
const QRhiWidget);
714 switch (d->config.api()) {
715 case QPlatformBackingStoreRhiConfig::OpenGL:
717 case QPlatformBackingStoreRhiConfig::Metal:
719 case QPlatformBackingStoreRhiConfig::Vulkan:
721 case QPlatformBackingStoreRhiConfig::D3D11:
722 return Api::Direct3D11;
723 case QPlatformBackingStoreRhiConfig::D3D12:
724 return Api::Direct3D12;
725 case QPlatformBackingStoreRhiConfig::Null:
728 Q_UNREACHABLE_RETURN(Api::Null);
749void QRhiWidget::setApi(Api api)
754 d->config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
757 d->config.setApi(QPlatformBackingStoreRhiConfig::Metal);
760 d->config.setApi(QPlatformBackingStoreRhiConfig::Vulkan);
762 case Api::Direct3D11:
763 d->config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
765 case Api::Direct3D12:
766 d->config.setApi(QPlatformBackingStoreRhiConfig::D3D12);
769 d->config.setApi(QPlatformBackingStoreRhiConfig::Null);
832void QRhiWidget::setColorBufferFormat(TextureFormat format)
835 if (d->widgetTextureFormat != format) {
836 d->widgetTextureFormat = format;
838 case TextureFormat::RGBA8:
839 d->rhiTextureFormat = QRhiTexture::RGBA8;
841 case TextureFormat::RGBA16F:
842 d->rhiTextureFormat = QRhiTexture::RGBA16F;
844 case TextureFormat::RGBA32F:
845 d->rhiTextureFormat = QRhiTexture::RGBA32F;
847 case TextureFormat::RGB10A2:
848 d->rhiTextureFormat = QRhiTexture::RGB10A2;
851 emit colorBufferFormatChanged(format);