Skip to content

Commit 487f4d0

Browse files
committed
Prevent circular buffer overflow during UboManager reallocation (#9714)
When UboManager::reallocate() is triggered, a large number of material instances may be invalidated simultaneously. This leads to a massive spike in descriptor set updates and command generation, which can overflow the circular buffer. To prevent this, we now flush commands in batches, we trigger a flush whenever the command buffer usage exceeds half of its capacity. (Like what RenderPass::Executor::execute does) BUGS = [474264976, 479079631]
1 parent e00be09 commit 487f4d0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

filament/src/details/Engine.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,21 @@ void FEngine::prepare(DriverApi& driver) {
735735
}
736736

737737
UboManager* uboManager = mUboManager;
738+
size_t const capacity = getMinCommandBufferSize();
738739
for (auto& materialInstanceList: mMaterialInstances) {
739-
materialInstanceList.second.forEach([&driver, uboManager](FMaterialInstance const* item) {
740-
// post-process materials instances must be commited explicitly because their
741-
// parameters are typically not set at this point in time.
742-
if (item->getMaterial()->getMaterialDomain() == MaterialDomain::SURFACE) {
743-
item->commit(driver, uboManager);
744-
}
745-
});
740+
materialInstanceList.second.forEach(
741+
[this, &driver, uboManager, capacity](FMaterialInstance const* item) {
742+
// post-process materials instances must be commited explicitly because their
743+
// parameters are typically not set at this point in time.
744+
if (item->getMaterial()->getMaterialDomain() == MaterialDomain::SURFACE) {
745+
// If the remaining space is less than half the capacity, we flush right
746+
// away to allow some headroom for commands that might come later.
747+
if (UTILS_UNLIKELY(driver.getCircularBuffer().getUsed() > capacity / 2)) {
748+
flush();
749+
}
750+
item->commit(driver, uboManager);
751+
}
752+
});
746753
}
747754

748755
if (useUboBatching) {

0 commit comments

Comments
 (0)