rhi: vulkan: Double buffer the descriptor sets backing an srb always
This is almost always the case in a typical Quick or Quick 3D rendering sequence, since there is almost always a uniform buffer, which is almost always a QRhiBuffer::Dynamic buffer, thus triggering double (or triple, whatever max_frames_in_flight is) buffering and using multiple descriptor sets. This breaks down e.g. when using a compute pipeline and a texture for imageLoad/store in the srb. If the texture is rebuilt between frame N and N+1, then in frame N+1 the srb sees that the descriptor set needs to be rewritten, but that's wrong since frame N might still be in flight, using that descriptor set. From now on, pretend that an srb always needs double buffering, regardless of what kind of resources it references. Also drop unused code paths. The updateShaderResources function's argument was never -1, for example, so no point in keeping that logic. Pick-to: 6.8 Change-Id: If8abb42fd9a2de5aad59ab6ceab3c8086aebae68 Reviewed-by: Andy Nichols <andy.nichols@qt.io> (cherry picked from commit 30dc9ed13fcc2691ed656f6f36d419133856c8cd) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
7d8b81a491
commit
ec386cd2a9
@ -3524,7 +3524,7 @@ bool QRhiVulkan::ensurePipelineCache(const void *initialData, size_t initialData
|
||||
return true;
|
||||
}
|
||||
|
||||
void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb, int descSetIdx)
|
||||
void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb)
|
||||
{
|
||||
QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, srb);
|
||||
|
||||
@ -3534,16 +3534,13 @@ void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb, i
|
||||
QVarLengthArray<VkWriteDescriptorSet, 12> writeInfos;
|
||||
QVarLengthArray<std::pair<int, int>, 12> infoIndices;
|
||||
|
||||
const bool updateAll = descSetIdx < 0;
|
||||
int frameSlot = updateAll ? 0 : descSetIdx;
|
||||
while (frameSlot < (updateAll ? QVK_FRAMES_IN_FLIGHT : descSetIdx + 1)) {
|
||||
for (int i = 0, ie = srbD->sortedBindings.size(); i != ie; ++i) {
|
||||
const QRhiShaderResourceBinding::Data *b = shaderResourceBindingData(srbD->sortedBindings.at(i));
|
||||
QVkShaderResourceBindings::BoundResourceData &bd(srbD->boundResourceData[frameSlot][i]);
|
||||
QVkShaderResourceBindings::BoundResourceData &bd(srbD->boundResourceData[currentFrameSlot][i]);
|
||||
|
||||
VkWriteDescriptorSet writeInfo = {};
|
||||
writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
writeInfo.dstSet = srbD->descSets[frameSlot];
|
||||
writeInfo.dstSet = srbD->descSets[currentFrameSlot];
|
||||
writeInfo.dstBinding = uint32_t(b->binding);
|
||||
writeInfo.descriptorCount = 1;
|
||||
|
||||
@ -3560,7 +3557,7 @@ void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb, i
|
||||
bd.ubuf.id = bufD->m_id;
|
||||
bd.ubuf.generation = bufD->generation;
|
||||
VkDescriptorBufferInfo bufInfo;
|
||||
bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[frameSlot] : bufD->buffers[0];
|
||||
bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[currentFrameSlot] : bufD->buffers[0];
|
||||
bufInfo.offset = b->u.ubuf.offset;
|
||||
bufInfo.range = b->u.ubuf.maybeSize ? b->u.ubuf.maybeSize : bufD->m_size;
|
||||
// be nice and assert when we know the vulkan device would die a horrible death due to non-aligned reads
|
||||
@ -3656,7 +3653,7 @@ void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb, i
|
||||
bd.sbuf.id = bufD->m_id;
|
||||
bd.sbuf.generation = bufD->generation;
|
||||
VkDescriptorBufferInfo bufInfo;
|
||||
bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[frameSlot] : bufD->buffers[0];
|
||||
bufInfo.buffer = bufD->m_type == QRhiBuffer::Dynamic ? bufD->buffers[currentFrameSlot] : bufD->buffers[0];
|
||||
bufInfo.offset = b->u.ubuf.offset;
|
||||
bufInfo.range = b->u.ubuf.maybeSize ? b->u.ubuf.maybeSize : bufD->m_size;
|
||||
bufferInfoIndex = bufferInfos.size();
|
||||
@ -3670,8 +3667,6 @@ void QRhiVulkan::updateShaderResourceBindings(QRhiShaderResourceBindings *srb, i
|
||||
writeInfos.append(writeInfo);
|
||||
infoIndices.append({ bufferInfoIndex, imageInfoIndex });
|
||||
}
|
||||
++frameSlot;
|
||||
}
|
||||
|
||||
for (int i = 0, writeInfoCount = writeInfos.size(); i < writeInfoCount; ++i) {
|
||||
const int bufferInfoIndex = infoIndices[i].first;
|
||||
@ -5582,8 +5577,7 @@ void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBin
|
||||
}
|
||||
|
||||
QVkShaderResourceBindings *srbD = QRHI_RES(QVkShaderResourceBindings, srb);
|
||||
const int descSetIdx = srbD->hasSlottedResource ? currentFrameSlot : 0;
|
||||
auto &descSetBd(srbD->boundResourceData[descSetIdx]);
|
||||
auto &descSetBd(srbD->boundResourceData[currentFrameSlot]);
|
||||
bool rewriteDescSet = false;
|
||||
|
||||
// Do host writes and mark referenced shader resources as in-use.
|
||||
@ -5720,11 +5714,11 @@ void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBin
|
||||
|
||||
// write descriptor sets, if needed
|
||||
if (rewriteDescSet)
|
||||
updateShaderResourceBindings(srb, descSetIdx);
|
||||
updateShaderResourceBindings(srb);
|
||||
|
||||
// make sure the descriptors for the correct slot will get bound.
|
||||
// also, dynamic offsets always need a bind.
|
||||
const bool forceRebind = (srbD->hasSlottedResource && cbD->currentDescSetSlot != descSetIdx) || srbD->hasDynamicOffset;
|
||||
const bool forceRebind = cbD->currentDescSetSlot != currentFrameSlot || srbD->hasDynamicOffset;
|
||||
|
||||
const bool srbChanged = gfxPsD ? (cbD->currentGraphicsSrb != srb) : (cbD->currentComputeSrb != srb);
|
||||
|
||||
@ -5755,7 +5749,7 @@ void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBin
|
||||
df->vkCmdBindDescriptorSets(cbD->activeSecondaryCbStack.last(),
|
||||
gfxPsD ? VK_PIPELINE_BIND_POINT_GRAPHICS : VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
gfxPsD ? gfxPsD->layout : compPsD->layout,
|
||||
0, 1, &srbD->descSets[descSetIdx],
|
||||
0, 1, &srbD->descSets[currentFrameSlot],
|
||||
uint32_t(dynOfs.size()),
|
||||
dynOfs.size() ? dynOfs.constData() : nullptr);
|
||||
} else {
|
||||
@ -5764,7 +5758,7 @@ void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBin
|
||||
cmd.args.bindDescriptorSet.bindPoint = gfxPsD ? VK_PIPELINE_BIND_POINT_GRAPHICS
|
||||
: VK_PIPELINE_BIND_POINT_COMPUTE;
|
||||
cmd.args.bindDescriptorSet.pipelineLayout = gfxPsD ? gfxPsD->layout : compPsD->layout;
|
||||
cmd.args.bindDescriptorSet.descSet = srbD->descSets[descSetIdx];
|
||||
cmd.args.bindDescriptorSet.descSet = srbD->descSets[currentFrameSlot];
|
||||
cmd.args.bindDescriptorSet.dynamicOffsetCount = dynOfs.size();
|
||||
cmd.args.bindDescriptorSet.dynamicOffsetIndex = cbD->pools.dynamicOffset.size();
|
||||
cbD->pools.dynamicOffset.append(dynOfs.constData(), dynOfs.size());
|
||||
@ -5778,7 +5772,7 @@ void QRhiVulkan::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBin
|
||||
cbD->currentComputeSrb = srb;
|
||||
}
|
||||
cbD->currentSrbGeneration = srbD->generation;
|
||||
cbD->currentDescSetSlot = descSetIdx;
|
||||
cbD->currentDescSetSlot = currentFrameSlot;
|
||||
}
|
||||
|
||||
srbD->lastActiveFrameSlot = currentFrameSlot;
|
||||
@ -7992,13 +7986,10 @@ bool QVkShaderResourceBindings::create()
|
||||
std::copy(m_bindings.cbegin(), m_bindings.cend(), std::back_inserter(sortedBindings));
|
||||
std::sort(sortedBindings.begin(), sortedBindings.end(), QRhiImplementation::sortedBindingLessThan);
|
||||
|
||||
hasSlottedResource = false;
|
||||
hasDynamicOffset = false;
|
||||
for (const QRhiShaderResourceBinding &binding : std::as_const(sortedBindings)) {
|
||||
const QRhiShaderResourceBinding::Data *b = QRhiImplementation::shaderResourceBindingData(binding);
|
||||
if (b->type == QRhiShaderResourceBinding::UniformBuffer && b->u.ubuf.buf) {
|
||||
if (QRHI_RES(QVkBuffer, b->u.ubuf.buf)->type() == QRhiBuffer::Dynamic)
|
||||
hasSlottedResource = true;
|
||||
if (b->u.ubuf.hasDynamicOffset)
|
||||
hasDynamicOffset = true;
|
||||
}
|
||||
|
@ -252,7 +252,6 @@ struct QVkShaderResourceBindings : public QRhiShaderResourceBindings
|
||||
void updateResources(UpdateFlags flags) override;
|
||||
|
||||
QVarLengthArray<QRhiShaderResourceBinding, 8> sortedBindings;
|
||||
bool hasSlottedResource = false;
|
||||
bool hasDynamicOffset = false;
|
||||
int poolIndex = -1;
|
||||
VkDescriptorSetLayout layout = VK_NULL_HANDLE;
|
||||
@ -860,7 +859,7 @@ public:
|
||||
VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage,
|
||||
int startLayer, int layerCount,
|
||||
int startLevel, int levelCount);
|
||||
void updateShaderResourceBindings(QRhiShaderResourceBindings *srb, int descSetIdx = -1);
|
||||
void updateShaderResourceBindings(QRhiShaderResourceBindings *srb);
|
||||
void ensureCommandPoolForNewFrame();
|
||||
double elapsedSecondsFromTimestamp(quint64 timestamp[2], bool *ok);
|
||||
void printExtraErrorInfo(VkResult err);
|
||||
|
Loading…
x
Reference in New Issue
Block a user