rhi: Add explicit subclass for RTs from swapchains

We want to enable gaining access to the underlying resource(s) by
inspecting a QRhiRenderTarget. This is not currently possible for
swapchains since there is nothing that references the actual
QRhiSwapChain. To clean this up, make an explicit, new
QRhiSwapChainRenderTarget subclass. Thus the logic already used in a
couple of places to examine the resources attached to a
QRhiTextureRenderTarget can now work with swapchain render targets too,
by branching based on the resourceType().

This eliminates the somewhat odd setup where a "RenderTarget" resource
is QRhiRenderTarget corresponding (but not exposing!) a swapchain,
whereas a "TextureRenderTarget" is a QRhiTextureRenderTarget which
is a subclass of QRhiRenderTarget. Now we correctly have an (abstract)
base and two subclasses, one for each type of render targets.

Besides, it allows us to clean up the oddly named
Q...ReferenceRenderTarget classes in the backends, which initially tried
to indicate that this "render target" merely references (or, in
practice, is) a swapchain. We can now have a nice and symmetrical
Q...SwapChainRenderTarget and Q...TextureRenderTarget naming scheme.

Change-Id: Ib07e9be99a316eec67b94de0860e08f5f4638959
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
This commit is contained in:
Laszlo Agocs 2022-04-01 15:01:45 +02:00
parent d72f2ca9ea
commit a5db072dc2
13 changed files with 130 additions and 84 deletions

View File

@ -2943,6 +2943,8 @@ const QRhiNativeHandles *QRhiRenderPassDescriptor::nativeHandles()
\internal
\inmodule QtGui
\brief Represents an onscreen (swapchain) or offscreen (texture) render target.
\sa QRhiSwapChainRenderTarget, QRhiTextureRenderTarget
*/
/*!
@ -2953,14 +2955,6 @@ QRhiRenderTarget::QRhiRenderTarget(QRhiImplementation *rhi)
{
}
/*!
\return the resource type.
*/
QRhiResource::Type QRhiRenderTarget::resourceType() const
{
return RenderTarget;
}
/*!
\fn QSize QRhiRenderTarget::pixelSize() const
@ -2988,6 +2982,42 @@ QRhiResource::Type QRhiRenderTarget::resourceType() const
QWindow.
*/
/*!
\internal
*/
QRhiSwapChainRenderTarget::QRhiSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain_)
: QRhiRenderTarget(rhi),
m_swapchain(swapchain_)
{
}
/*!
\class QRhiSwapChainRenderTarget
\internal
\inmodule QtGui
\brief Swapchain render target resource.
When targeting the color buffers of a swapchain, active render target is a
QRhiSwapChainRenderTarget. This is what
QRhiSwapChain::currentFrameRenderTarget() returns.
\sa QRhiSwapChain
*/
/*!
\return the resource type.
*/
QRhiResource::Type QRhiSwapChainRenderTarget::resourceType() const
{
return SwapChainRenderTarget;
}
/*!
\fn QRhiSwapChain *QRhiSwapChainRenderTarget::swapChain() const
\return the swapchain object.
*/
/*!
\class QRhiTextureRenderTarget
\internal
@ -2997,6 +3027,10 @@ QRhiResource::Type QRhiRenderTarget::resourceType() const
A texture render target allows rendering into one or more textures,
optionally with a depth texture or depth/stencil renderbuffer.
For multisample rendering the common approach is to use a renderbuffer as
the color attachment and set the non-multisample destination texture as the
\c{resolve texture}.
\note Textures used in combination with QRhiTextureRenderTarget must be
created with the QRhiTexture::RenderTarget flag.
@ -4911,8 +4945,8 @@ static const char *resourceTypeStr(QRhiResource *res)
return "RenderBuffer";
case QRhiResource::RenderPassDescriptor:
return "RenderPassDescriptor";
case QRhiResource::RenderTarget:
return "RenderTarget";
case QRhiResource::SwapChainRenderTarget:
return "SwapChainRenderTarget";
case QRhiResource::TextureRenderTarget:
return "TextureRenderTarget";
case QRhiResource::ShaderResourceBindings:

View File

@ -74,6 +74,7 @@ class QRhiSampler;
class QRhiCommandBuffer;
class QRhiResourceUpdateBatch;
class QRhiResourceUpdateBatchPrivate;
class QRhiSwapChain;
class Q_GUI_EXPORT QRhiDepthStencilClearValue
{
@ -697,7 +698,7 @@ public:
Sampler,
RenderBuffer,
RenderPassDescriptor,
RenderTarget,
SwapChainRenderTarget,
TextureRenderTarget,
ShaderResourceBindings,
GraphicsPipeline,
@ -1031,8 +1032,6 @@ protected:
class Q_GUI_EXPORT QRhiRenderTarget : public QRhiResource
{
public:
QRhiResource::Type resourceType() const override;
virtual QSize pixelSize() const = 0;
virtual float devicePixelRatio() const = 0;
virtual int sampleCount() const = 0;
@ -1045,6 +1044,17 @@ protected:
QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
};
class Q_GUI_EXPORT QRhiSwapChainRenderTarget : public QRhiRenderTarget
{
public:
QRhiResource::Type resourceType() const override;
QRhiSwapChain *swapChain() const { return m_swapchain; }
protected:
QRhiSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain_);
QRhiSwapChain *m_swapchain;
};
class Q_GUI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
{
public:

View File

@ -1759,8 +1759,8 @@ void QRhiD3D11::finishActiveReadbacks()
static inline QD3D11RenderTargetData *rtData(QRhiRenderTarget *rt)
{
switch (rt->resourceType()) {
case QRhiResource::RenderTarget:
return &QRHI_RES(QD3D11ReferenceRenderTarget, rt)->d;
case QRhiResource::SwapChainRenderTarget:
return &QRHI_RES(QD3D11SwapChainRenderTarget, rt)->d;
case QRhiResource::TextureRenderTarget:
return &QRHI_RES(QD3D11TextureRenderTarget, rt)->d;
default:
@ -3453,33 +3453,33 @@ QVector<quint32> QD3D11RenderPassDescriptor::serializedFormat() const
return {};
}
QD3D11ReferenceRenderTarget::QD3D11ReferenceRenderTarget(QRhiImplementation *rhi)
: QRhiRenderTarget(rhi),
QD3D11SwapChainRenderTarget::QD3D11SwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain)
: QRhiSwapChainRenderTarget(rhi, swapchain),
d(rhi)
{
}
QD3D11ReferenceRenderTarget::~QD3D11ReferenceRenderTarget()
QD3D11SwapChainRenderTarget::~QD3D11SwapChainRenderTarget()
{
destroy();
}
void QD3D11ReferenceRenderTarget::destroy()
void QD3D11SwapChainRenderTarget::destroy()
{
// nothing to do here
}
QSize QD3D11ReferenceRenderTarget::pixelSize() const
QSize QD3D11SwapChainRenderTarget::pixelSize() const
{
return d.pixelSize;
}
float QD3D11ReferenceRenderTarget::devicePixelRatio() const
float QD3D11SwapChainRenderTarget::devicePixelRatio() const
{
return d.dpr;
}
int QD3D11ReferenceRenderTarget::sampleCount() const
int QD3D11SwapChainRenderTarget::sampleCount() const
{
return d.sampleCount;
}
@ -4380,7 +4380,7 @@ void QD3D11CommandBuffer::destroy()
QD3D11SwapChain::QD3D11SwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rt(rhi),
rt(rhi, this),
cb(rhi)
{
backBufferTex = nullptr;
@ -4825,7 +4825,7 @@ bool QD3D11SwapChain::createOrResize()
frameCount = 0;
ds = m_depthStencil ? QRHI_RES(QD3D11RenderBuffer, m_depthStencil) : nullptr;
QD3D11ReferenceRenderTarget *rtD = QRHI_RES(QD3D11ReferenceRenderTarget, &rt);
QD3D11SwapChainRenderTarget *rtD = QRHI_RES(QD3D11SwapChainRenderTarget, &rt);
rtD->d.rp = QRHI_RES(QD3D11RenderPassDescriptor, m_renderPassDesc);
rtD->d.pixelSize = pixelSize;
rtD->d.dpr = float(window->devicePixelRatio());

View File

@ -177,10 +177,10 @@ struct QD3D11RenderTargetData
QRhiRenderTargetAttachmentTracker::ResIdList currentResIdList;
};
struct QD3D11ReferenceRenderTarget : public QRhiRenderTarget
struct QD3D11SwapChainRenderTarget : public QRhiSwapChainRenderTarget
{
QD3D11ReferenceRenderTarget(QRhiImplementation *rhi);
~QD3D11ReferenceRenderTarget();
QD3D11SwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
~QD3D11SwapChainRenderTarget();
void destroy() override;
QSize pixelSize() const override;
@ -571,7 +571,7 @@ struct QD3D11SwapChain : public QRhiSwapChain
QWindow *window = nullptr;
QSize pixelSize;
QD3D11ReferenceRenderTarget rt;
QD3D11SwapChainRenderTarget rt;
QD3D11CommandBuffer cb;
DXGI_FORMAT colorFormat;
DXGI_FORMAT srgbAdjustedColorFormat;

View File

@ -3953,8 +3953,8 @@ QGles2RenderTargetData *QRhiGles2::enqueueBindFramebuffer(QRhiRenderTarget *rt,
static const bool doClearColorBuffer = qEnvironmentVariableIntValue("QT_GL_NO_CLEAR_COLOR_BUFFER") == 0;
switch (rt->resourceType()) {
case QRhiResource::RenderTarget:
rtD = &QRHI_RES(QGles2ReferenceRenderTarget, rt)->d;
case QRhiResource::SwapChainRenderTarget:
rtD = &QRHI_RES(QGles2SwapChainRenderTarget, rt)->d;
if (wantsColorClear)
*wantsColorClear = doClearBuffers && doClearColorBuffer;
if (wantsDsClear)
@ -5196,33 +5196,33 @@ QVector<quint32> QGles2RenderPassDescriptor::serializedFormat() const
return {};
}
QGles2ReferenceRenderTarget::QGles2ReferenceRenderTarget(QRhiImplementation *rhi)
: QRhiRenderTarget(rhi),
QGles2SwapChainRenderTarget::QGles2SwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain)
: QRhiSwapChainRenderTarget(rhi, swapchain),
d(rhi)
{
}
QGles2ReferenceRenderTarget::~QGles2ReferenceRenderTarget()
QGles2SwapChainRenderTarget::~QGles2SwapChainRenderTarget()
{
destroy();
}
void QGles2ReferenceRenderTarget::destroy()
void QGles2SwapChainRenderTarget::destroy()
{
// nothing to do here
}
QSize QGles2ReferenceRenderTarget::pixelSize() const
QSize QGles2SwapChainRenderTarget::pixelSize() const
{
return d.pixelSize;
}
float QGles2ReferenceRenderTarget::devicePixelRatio() const
float QGles2SwapChainRenderTarget::devicePixelRatio() const
{
return d.dpr;
}
int QGles2ReferenceRenderTarget::sampleCount() const
int QGles2SwapChainRenderTarget::sampleCount() const
{
return d.sampleCount;
}
@ -5731,7 +5731,7 @@ void QGles2CommandBuffer::destroy()
QGles2SwapChain::QGles2SwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rt(rhi),
rt(rhi, this),
cb(rhi)
{
}

View File

@ -219,10 +219,10 @@ struct QGles2RenderTargetData
QRhiRenderTargetAttachmentTracker::ResIdList currentResIdList;
};
struct QGles2ReferenceRenderTarget : public QRhiRenderTarget
struct QGles2SwapChainRenderTarget : public QRhiSwapChainRenderTarget
{
QGles2ReferenceRenderTarget(QRhiImplementation *rhi);
~QGles2ReferenceRenderTarget();
QGles2SwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
~QGles2SwapChainRenderTarget();
void destroy() override;
QSize pixelSize() const override;
@ -735,7 +735,7 @@ struct QGles2SwapChain : public QRhiSwapChain
QSurface *surface = nullptr;
QSize pixelSize;
QGles2ReferenceRenderTarget rt;
QGles2SwapChainRenderTarget rt;
QGles2CommandBuffer cb;
int frameCount = 0;
};

View File

@ -2032,8 +2032,8 @@ void QRhiMetal::beginPass(QRhiCommandBuffer *cb,
QMetalRenderTargetData *rtD = nullptr;
switch (rt->resourceType()) {
case QRhiResource::RenderTarget:
rtD = QRHI_RES(QMetalReferenceRenderTarget, rt)->d;
case QRhiResource::SwapChainRenderTarget:
rtD = QRHI_RES(QMetalSwapChainRenderTarget, rt)->d;
cbD->d->currentPassRpDesc = d->createDefaultRenderPass(rtD->dsAttCount, colorClearValue, depthStencilClearValue, rtD->colorAttCount);
if (rtD->colorAttCount) {
QMetalRenderTargetData::ColorAtt &color0(rtD->fb.colorAtt[0]);
@ -3053,34 +3053,34 @@ QVector<quint32> QMetalRenderPassDescriptor::serializedFormat() const
return serializedFormatData;
}
QMetalReferenceRenderTarget::QMetalReferenceRenderTarget(QRhiImplementation *rhi)
: QRhiRenderTarget(rhi),
QMetalSwapChainRenderTarget::QMetalSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain)
: QRhiSwapChainRenderTarget(rhi, swapchain),
d(new QMetalRenderTargetData)
{
}
QMetalReferenceRenderTarget::~QMetalReferenceRenderTarget()
QMetalSwapChainRenderTarget::~QMetalSwapChainRenderTarget()
{
destroy();
delete d;
}
void QMetalReferenceRenderTarget::destroy()
void QMetalSwapChainRenderTarget::destroy()
{
// nothing to do here
}
QSize QMetalReferenceRenderTarget::pixelSize() const
QSize QMetalSwapChainRenderTarget::pixelSize() const
{
return d->pixelSize;
}
float QMetalReferenceRenderTarget::devicePixelRatio() const
float QMetalSwapChainRenderTarget::devicePixelRatio() const
{
return d->dpr;
}
int QMetalReferenceRenderTarget::sampleCount() const
int QMetalSwapChainRenderTarget::sampleCount() const
{
return d->sampleCount;
}
@ -3947,7 +3947,7 @@ void QMetalCommandBuffer::resetPerPassCachedState()
QMetalSwapChain::QMetalSwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rtWrapper(rhi),
rtWrapper(rhi, this),
cbWrapper(rhi),
d(new QMetalSwapChainData)
{

View File

@ -163,10 +163,10 @@ struct QMetalRenderPassDescriptor : public QRhiRenderPassDescriptor
struct QMetalRenderTargetData;
struct QMetalReferenceRenderTarget : public QRhiRenderTarget
struct QMetalSwapChainRenderTarget : public QRhiSwapChainRenderTarget
{
QMetalReferenceRenderTarget(QRhiImplementation *rhi);
~QMetalReferenceRenderTarget();
QMetalSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
~QMetalSwapChainRenderTarget();
void destroy() override;
QSize pixelSize() const override;
@ -337,7 +337,7 @@ struct QMetalSwapChain : public QRhiSwapChain
int currentFrameSlot = 0; // 0..QMTL_FRAMES_IN_FLIGHT-1
int frameCount = 0;
int samples = 1;
QMetalReferenceRenderTarget rtWrapper;
QMetalSwapChainRenderTarget rtWrapper;
QMetalCommandBuffer cbWrapper;
QMetalRenderBuffer *ds = nullptr;
QMetalSwapChainData *d = nullptr;

View File

@ -788,32 +788,32 @@ QVector<quint32> QNullRenderPassDescriptor::serializedFormat() const
return {};
}
QNullReferenceRenderTarget::QNullReferenceRenderTarget(QRhiImplementation *rhi)
: QRhiRenderTarget(rhi),
QNullSwapChainRenderTarget::QNullSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain)
: QRhiSwapChainRenderTarget(rhi, swapchain),
d(rhi)
{
}
QNullReferenceRenderTarget::~QNullReferenceRenderTarget()
QNullSwapChainRenderTarget::~QNullSwapChainRenderTarget()
{
destroy();
}
void QNullReferenceRenderTarget::destroy()
void QNullSwapChainRenderTarget::destroy()
{
}
QSize QNullReferenceRenderTarget::pixelSize() const
QSize QNullSwapChainRenderTarget::pixelSize() const
{
return d.pixelSize;
}
float QNullReferenceRenderTarget::devicePixelRatio() const
float QNullSwapChainRenderTarget::devicePixelRatio() const
{
return d.dpr;
}
int QNullReferenceRenderTarget::sampleCount() const
int QNullSwapChainRenderTarget::sampleCount() const
{
return 1;
}
@ -965,7 +965,7 @@ void QNullCommandBuffer::destroy()
QNullSwapChain::QNullSwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rt(rhi),
rt(rhi, this),
cb(rhi)
{
}

View File

@ -124,10 +124,10 @@ struct QNullRenderTargetData
QRhiRenderTargetAttachmentTracker::ResIdList currentResIdList;
};
struct QNullReferenceRenderTarget : public QRhiRenderTarget
struct QNullSwapChainRenderTarget : public QRhiSwapChainRenderTarget
{
QNullReferenceRenderTarget(QRhiImplementation *rhi);
~QNullReferenceRenderTarget();
QNullSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
~QNullSwapChainRenderTarget();
void destroy() override;
QSize pixelSize() const override;
@ -201,7 +201,7 @@ struct QNullSwapChain : public QRhiSwapChain
bool createOrResize() override;
QWindow *window = nullptr;
QNullReferenceRenderTarget rt;
QNullSwapChainRenderTarget rt;
QNullCommandBuffer cb;
int frameCount = 0;
};

View File

@ -2322,8 +2322,8 @@ void QRhiVulkan::beginPass(QRhiCommandBuffer *cb,
QVkRenderTargetData *rtD = nullptr;
switch (rt->resourceType()) {
case QRhiResource::RenderTarget:
rtD = &QRHI_RES(QVkReferenceRenderTarget, rt)->d;
case QRhiResource::SwapChainRenderTarget:
rtD = &QRHI_RES(QVkSwapChainRenderTarget, rt)->d;
rtD->rp->lastActiveFrameSlot = currentFrameSlot;
Q_ASSERT(currentSwapChain);
currentSwapChain->imageRes[currentSwapChain->currentImageIndex].lastUse =
@ -5122,8 +5122,8 @@ static inline QVkRenderTargetData *maybeRenderTargetData(QVkCommandBuffer *cbD)
QVkRenderTargetData *rtD = nullptr;
if (cbD->recordingPass == QVkCommandBuffer::RenderPass) {
switch (cbD->currentTarget->resourceType()) {
case QRhiResource::RenderTarget:
rtD = &QRHI_RES(QVkReferenceRenderTarget, cbD->currentTarget)->d;
case QRhiResource::SwapChainRenderTarget:
rtD = &QRHI_RES(QVkSwapChainRenderTarget, cbD->currentTarget)->d;
break;
case QRhiResource::TextureRenderTarget:
rtD = &QRHI_RES(QVkTextureRenderTarget, cbD->currentTarget)->d;
@ -6432,32 +6432,32 @@ const QRhiNativeHandles *QVkRenderPassDescriptor::nativeHandles()
return &nativeHandlesStruct;
}
QVkReferenceRenderTarget::QVkReferenceRenderTarget(QRhiImplementation *rhi)
: QRhiRenderTarget(rhi)
QVkSwapChainRenderTarget::QVkSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain)
: QRhiSwapChainRenderTarget(rhi, swapchain)
{
}
QVkReferenceRenderTarget::~QVkReferenceRenderTarget()
QVkSwapChainRenderTarget::~QVkSwapChainRenderTarget()
{
destroy();
}
void QVkReferenceRenderTarget::destroy()
void QVkSwapChainRenderTarget::destroy()
{
// nothing to do here
}
QSize QVkReferenceRenderTarget::pixelSize() const
QSize QVkSwapChainRenderTarget::pixelSize() const
{
return d.pixelSize;
}
float QVkReferenceRenderTarget::devicePixelRatio() const
float QVkSwapChainRenderTarget::devicePixelRatio() const
{
return d.dpr;
}
int QVkReferenceRenderTarget::sampleCount() const
int QVkSwapChainRenderTarget::sampleCount() const
{
return d.sampleCount;
}
@ -7250,7 +7250,7 @@ const QRhiNativeHandles *QVkCommandBuffer::nativeHandles()
QVkSwapChain::QVkSwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rtWrapper(rhi),
rtWrapper(rhi, this),
cbWrapper(rhi)
{
}

View File

@ -219,10 +219,10 @@ struct QVkRenderTargetData
static const int MAX_COLOR_ATTACHMENTS = 8;
};
struct QVkReferenceRenderTarget : public QRhiRenderTarget
struct QVkSwapChainRenderTarget : public QRhiSwapChainRenderTarget
{
QVkReferenceRenderTarget(QRhiImplementation *rhi);
~QVkReferenceRenderTarget();
QVkSwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
~QVkSwapChainRenderTarget();
void destroy() override;
QSize pixelSize() const override;
@ -625,7 +625,7 @@ struct QVkSwapChain : public QRhiSwapChain
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;
QVarLengthArray<VkPresentModeKHR, 8> supportedPresentationModes;
VkDeviceMemory msaaImageMem = VK_NULL_HANDLE;
QVkReferenceRenderTarget rtWrapper;
QVkSwapChainRenderTarget rtWrapper;
QVkCommandBuffer cbWrapper;
struct ImageResources {

View File

@ -3454,6 +3454,8 @@ void tst_QRhi::renderToWindowSimple()
QVERIFY(rhi->beginFrame(swapChain.data()) == QRhi::FrameOpSuccess);
QRhiCommandBuffer *cb = swapChain->currentFrameCommandBuffer();
QRhiRenderTarget *rt = swapChain->currentFrameRenderTarget();
QCOMPARE(rt->resourceType(), QRhiResource::SwapChainRenderTarget);
QCOMPARE(static_cast<QRhiSwapChainRenderTarget *>(rt)->swapChain(), swapChain.data());
const QSize outputSize = swapChain->currentPixelSize();
QCOMPARE(rt->pixelSize(), outputSize);
QRhiViewport viewport(0, 0, float(outputSize.width()), float(outputSize.height()));