Commit c47c3655 authored by Laszlo Agocs's avatar Laszlo Agocs

Add an rt flag to preserve depth-stencil

parent f349d345
......@@ -606,7 +606,8 @@ class Q_RHI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
{
public:
enum Flag {
PreserveColorContents = 1 << 0
PreserveColorContents = 1 << 0,
PreserveDepthStencilContents = 1 << 1
};
Q_DECLARE_FLAGS(Flags, Flag)
......
......@@ -978,10 +978,12 @@ void QRhiD3D11::beginPass(QRhiCommandBuffer *cb,
QD3D11CommandBuffer *cbD = QRHI_RES(QD3D11CommandBuffer, cb);
bool needsColorClear = true;
bool needsDsClear = true;
QD3D11RenderTargetData *rtD = rtData(rt);
if (rt->type() == QRhiRenderTarget::RtTexture) {
QD3D11TextureRenderTarget *rtTex = QRHI_RES(QD3D11TextureRenderTarget, rt);
needsColorClear = !rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveColorContents);
needsDsClear = !rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveDepthStencilContents);
}
cbD->currentTarget = rt;
......@@ -995,12 +997,10 @@ void QRhiD3D11::beginPass(QRhiCommandBuffer *cb,
clearCmd.cmd = QD3D11CommandBuffer::Command::Clear;
clearCmd.args.clear.rt = rt;
clearCmd.args.clear.mask = 0;
if (!rtD->colorAttCount)
needsColorClear = false;
if (rtD->dsAttCount)
clearCmd.args.clear.mask |= QD3D11CommandBuffer::Command::Depth | QD3D11CommandBuffer::Command::Stencil;
if (needsColorClear)
if (rtD->colorAttCount && needsColorClear)
clearCmd.args.clear.mask |= QD3D11CommandBuffer::Command::Color;
if (rtD->dsAttCount && needsDsClear)
clearCmd.args.clear.mask |= QD3D11CommandBuffer::Command::Depth | QD3D11CommandBuffer::Command::Stencil;
memcpy(clearCmd.args.clear.c, &colorClearValue.rgba, sizeof(float) * 4);
clearCmd.args.clear.d = depthStencilClearValue.d;
......
......@@ -1377,6 +1377,7 @@ void QRhiGles2::beginPass(QRhiCommandBuffer *cb,
QGles2CommandBuffer *cbD = QRHI_RES(QGles2CommandBuffer, cb);
bool needsColorClear = true;
bool needsDsClear = true;
QGles2BasicRenderTargetData *rtD = nullptr;
QGles2CommandBuffer::Command fbCmd;
fbCmd.cmd = QGles2CommandBuffer::Command::BindFramebuffer;
......@@ -1390,6 +1391,7 @@ void QRhiGles2::beginPass(QRhiCommandBuffer *cb,
QGles2TextureRenderTarget *rtTex = QRHI_RES(QGles2TextureRenderTarget, rt);
rtD = &rtTex->d;
needsColorClear = !rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveColorContents);
needsDsClear = !rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveDepthStencilContents);
fbCmd.args.bindFramebuffer.rt = rtTex;
}
break;
......@@ -1405,10 +1407,10 @@ void QRhiGles2::beginPass(QRhiCommandBuffer *cb,
QGles2CommandBuffer::Command clearCmd;
clearCmd.cmd = QGles2CommandBuffer::Command::Clear;
clearCmd.args.clear.mask = 0;
if (rtD->attCount > 1)
clearCmd.args.clear.mask |= GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
if (needsColorClear)
if (rtD->attCount > 0 && needsColorClear)
clearCmd.args.clear.mask |= GL_COLOR_BUFFER_BIT;
if (rtD->attCount > 1 && needsDsClear)
clearCmd.args.clear.mask |= GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
memcpy(clearCmd.args.clear.c, &colorClearValue.rgba, sizeof(float) * 4);
clearCmd.args.clear.d = depthStencilClearValue.d;
......
......@@ -1046,17 +1046,21 @@ void QRhiMetal::beginPass(QRhiCommandBuffer *cb,
switch (rt->type()) {
case QRhiRenderTarget::RtRef:
rtD = QRHI_RES(QMetalReferenceRenderTarget, rt)->d;
cbD->d->currentPassRpDesc = d->createDefaultRenderPass(false, colorClearValue, depthStencilClearValue);
cbD->d->currentPassRpDesc = d->createDefaultRenderPass(rtD->dsAttCount, colorClearValue, depthStencilClearValue);
break;
case QRhiRenderTarget::RtTexture:
{
QMetalTextureRenderTarget *rtTex = QRHI_RES(QMetalTextureRenderTarget, rt);
rtD = rtTex->d;
cbD->d->currentPassRpDesc = d->createDefaultRenderPass(false, colorClearValue, depthStencilClearValue);
cbD->d->currentPassRpDesc = d->createDefaultRenderPass(rtD->dsAttCount, colorClearValue, depthStencilClearValue);
if (rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveColorContents)) {
for (int i = 0; i < rtD->colorAttCount; ++i)
cbD->d->currentPassRpDesc.colorAttachments[i].loadAction = MTLLoadActionLoad;
}
if (rtD->dsAttCount && rtTex->m_flags.testFlag(QRhiTextureRenderTarget::PreserveDepthStencilContents)) {
cbD->d->currentPassRpDesc.depthAttachment.loadAction = MTLLoadActionLoad;
cbD->d->currentPassRpDesc.stencilAttachment.loadAction = MTLLoadActionLoad;
}
}
break;
default:
......
......@@ -795,6 +795,7 @@ bool QRhiVulkan::createDefaultRenderPass(VkRenderPass *rp, bool hasDepthStencil,
bool QRhiVulkan::createOffscreenRenderPass(VkRenderPass *rp,
const QVector<QRhiTextureRenderTargetDescription::ColorAttachment> &colorAttachments,
bool preserveColor,
bool preserveDs,
QRhiRenderBuffer *depthStencilBuffer,
QRhiTexture *depthTexture)
{
......@@ -834,14 +835,16 @@ bool QRhiVulkan::createOffscreenRenderPass(VkRenderPass *rp,
: QRHI_RES(QVkRenderBuffer, depthStencilBuffer)->vkformat;
const VkSampleCountFlagBits samples = depthTexture ? QRHI_RES(QVkTexture, depthTexture)->samples
: QRHI_RES(QVkRenderBuffer, depthStencilBuffer)->samples;
const VkAttachmentLoadOp loadOp = preserveDs ? VK_ATTACHMENT_LOAD_OP_LOAD : VK_ATTACHMENT_LOAD_OP_CLEAR;
const VkAttachmentStoreOp storeOp = depthTexture ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE;
VkAttachmentDescription attDesc;
memset(&attDesc, 0, sizeof(attDesc));
attDesc.format = dsFormat;
attDesc.samples = samples;
attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attDesc.storeOp = depthTexture ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE;
attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attDesc.stencilStoreOp = depthTexture ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE;
attDesc.loadOp = loadOp;
attDesc.storeOp = storeOp;
attDesc.stencilLoadOp = loadOp;
attDesc.stencilStoreOp = storeOp;
attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attDesc.finalLayout = depthTexture ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attDescs.append(attDesc);
......@@ -3625,6 +3628,7 @@ QRhiRenderPassDescriptor *QVkTextureRenderTarget::newCompatibleRenderPassDescrip
if (!rhiD->createOffscreenRenderPass(&rp->rp,
m_desc.colorAttachments,
m_flags.testFlag(QRhiTextureRenderTarget::PreserveColorContents),
m_flags.testFlag(QRhiTextureRenderTarget::PreserveDepthStencilContents),
m_desc.depthStencilBuffer,
m_desc.depthTexture))
{
......
......@@ -406,6 +406,7 @@ public:
bool createOffscreenRenderPass(VkRenderPass *rp,
const QVector<QRhiTextureRenderTargetDescription::ColorAttachment> &colorAttachments,
bool preserveColor,
bool preserveDs,
QRhiRenderBuffer *depthStencilBuffer,
QRhiTexture *depthTexture);
bool ensurePipelineCache();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment