mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-16 22:43:24 +09:00
[Merge] (P11): dev@c14283ee - scattered sub-image writes upload exactly their texels, Magma submits a frame's recording every 16384 draws with backpressure, and Magma grows descriptor pools geometrically and trims a quiet slot whole; the handle arm notes driver-side writes by handle and the wire arm splits recordings and keeps the trim's epoch peak across descriptor rewinds
This commit is contained in:
@@ -591,6 +591,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
m_samplerResolveMemoHighWater = 0;
|
||||
m_setsPerFrame = setsPerFrame;
|
||||
m_peakDescriptorSetsObserved = 0;
|
||||
m_trimQuietEpochs = MG_Config::Features.MagmaDescriptorTrimFrames;
|
||||
// The renderer records its first frame into slot 0 before any BeginFrame runs.
|
||||
m_epochSlot = 0;
|
||||
m_epochOpen = true;
|
||||
m_descriptorSlotTrims = 0;
|
||||
m_textureManager = textureManager;
|
||||
m_samplerManager = samplerManager;
|
||||
|
||||
@@ -600,6 +605,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
frame.activeDescriptorPoolIndex = 0;
|
||||
frame.allocatedSetsThisFrame = 0;
|
||||
frame.peakAllocatedSetsThisFrame = 0;
|
||||
frame.capacitySets = m_setsPerFrame;
|
||||
frame.quietEpochs = 0;
|
||||
frame.descriptorPools.clear();
|
||||
|
||||
VkDescriptorPool initialPool = VK_NULL_HANDLE;
|
||||
@@ -654,6 +661,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
frame.activeDescriptorPoolIndex = 0;
|
||||
frame.allocatedSetsThisFrame = 0;
|
||||
frame.peakAllocatedSetsThisFrame = 0;
|
||||
frame.capacitySets = 0;
|
||||
frame.quietEpochs = 0;
|
||||
}
|
||||
m_frames.clear();
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED
|
||||
@@ -662,6 +671,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
m_wirePrivateStoragePlaceholders = 0;
|
||||
m_wireInvalidStorageImagesBindNull = false;
|
||||
#endif
|
||||
m_epochSlot = 0;
|
||||
m_epochOpen = false;
|
||||
|
||||
m_bufferManager = nullptr;
|
||||
m_programFactory = nullptr;
|
||||
@@ -701,6 +712,20 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
"UniformDescriptorBinder: new descriptor set peak observed=%u (base setsPerFrame=%u, frame=%u, pools=%zu)",
|
||||
m_peakDescriptorSetsObserved, m_setsPerFrame, frameIndex, frame.descriptorPools.size());
|
||||
}
|
||||
// The epoch that just ended is the one of the slot begun last - this slot again on the
|
||||
// drain path, the previous frame's slot on the Present path. Its peak is still unreset.
|
||||
if (m_epochOpen && m_epochSlot < m_frames.size()) {
|
||||
NoteEndedEpochForTrim(m_frames[m_epochSlot].peakAllocatedSetsThisFrame);
|
||||
}
|
||||
m_epochSlot = frameIndex;
|
||||
m_epochOpen = true;
|
||||
// No pending command buffer references this slot's sets here (see TrimFrameDescriptorPools
|
||||
// for why that holds at both of BeginFrame's call sites), so a quiet grown slot can be
|
||||
// handed back whole.
|
||||
if (m_trimQuietEpochs != 0 && frame.descriptorPools.size() > 1 &&
|
||||
frame.capacitySets >= kDescriptorTrimMinCapacity && frame.quietEpochs >= m_trimQuietEpochs) {
|
||||
TrimFrameDescriptorPools(frame, frameIndex);
|
||||
}
|
||||
frame.activeDescriptorPoolIndex = 0;
|
||||
frame.allocatedSetsThisFrame = 0;
|
||||
frame.peakAllocatedSetsThisFrame = 0;
|
||||
@@ -709,11 +734,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
}
|
||||
// The frame's descriptor sets are recycled above, so last frame's reuse targets
|
||||
// are gone: start the per-draw descriptor-reuse cache fresh this frame.
|
||||
for (auto& entry : m_descriptorReuseMemo) {
|
||||
entry.valid = false;
|
||||
}
|
||||
m_fastRebindMemo.valid = false;
|
||||
m_lastBindValid = false;
|
||||
InvalidateDescriptorSetMemos();
|
||||
// Re-fingerprint the bound sampler set fresh this frame so any GL object address
|
||||
// reuse cannot outlive a single frame (see SamplerResolveMemo). Only the entries a
|
||||
// resolve has actually written can be valid, so the high-water mark bounds the
|
||||
@@ -747,10 +768,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
entry.second.cursor = 0;
|
||||
}
|
||||
frame.allocatedSetsThisFrame = 0;
|
||||
frame.peakAllocatedSetsThisFrame = 0;
|
||||
for (auto& entry : m_descriptorReuseMemo) entry.valid = false;
|
||||
m_fastRebindMemo.valid = false;
|
||||
m_lastBindValid = false;
|
||||
// The PEAK is not reset: a rewind ends a window of the epoch, not the epoch. The trim scores
|
||||
// an epoch by the most sets the slot held at once in it (NoteEndedEpochForTrim at the next
|
||||
// BeginFrame), and on this arm that is the largest window - resetting here would score a
|
||||
// burst frame by its last window alone and trim a slot that re-grows every frame.
|
||||
InvalidateDescriptorSetMemos();
|
||||
const Uint32 touchedBindings =
|
||||
std::min<Uint32>(m_samplerResolveMemoHighWater, static_cast<Uint32>(m_samplerResolveMemo.size()));
|
||||
for (Uint32 binding = 0; binding < touchedBindings; ++binding) {
|
||||
@@ -761,6 +783,68 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
}
|
||||
#endif
|
||||
|
||||
void UniformManager::InvalidateDescriptorSetMemos() {
|
||||
// Every VkDescriptorSet handle held outside the per-layout caches - in this backend they all
|
||||
// live in this class. A trim can hand a destroyed set's handle value to a fresh allocation,
|
||||
// so the bind-dedup shadow must drop too, not only the two reuse memos.
|
||||
for (auto& entry : m_descriptorReuseMemo) {
|
||||
entry.valid = false;
|
||||
}
|
||||
m_fastRebindMemo.valid = false;
|
||||
m_lastBindValid = false;
|
||||
}
|
||||
|
||||
void UniformManager::NoteEndedEpochForTrim(Uint32 epochPeakSets) {
|
||||
for (auto& slot : m_frames) {
|
||||
if (static_cast<Uint64>(epochPeakSets) * kDescriptorTrimHeadroom > slot.capacitySets) {
|
||||
slot.quietEpochs = 0;
|
||||
} else if (slot.quietEpochs < std::numeric_limits<Uint32>::max()) {
|
||||
++slot.quietEpochs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UniformManager::TrimFrameDescriptorPools(FrameResources& frame, Uint32 frameIndex) {
|
||||
const SizeT poolsBefore = frame.descriptorPools.size();
|
||||
const Uint64 capacityBefore = frame.capacitySets;
|
||||
// Holders first: the per-layout cache and every memo that may name one of these sets.
|
||||
frame.descriptorSetCacheByLayout.clear();
|
||||
InvalidateDescriptorSetMemos();
|
||||
for (SizeT i = 1; i < frame.descriptorPools.size(); ++i) {
|
||||
if (frame.descriptorPools[i].handle != VK_NULL_HANDLE) {
|
||||
vkDestroyDescriptorPool(m_device, frame.descriptorPools[i].handle, nullptr);
|
||||
}
|
||||
}
|
||||
frame.descriptorPools.resize(1);
|
||||
auto& base = frame.descriptorPools[0];
|
||||
// Frees every set allocated from it; the spec defines no failure for vkResetDescriptorPool.
|
||||
vkResetDescriptorPool(m_device, base.handle, 0);
|
||||
base.allocatedSets = 0;
|
||||
frame.activeDescriptorPoolIndex = 0;
|
||||
frame.capacitySets = base.maxSets;
|
||||
frame.quietEpochs = 0;
|
||||
++m_descriptorSlotTrims;
|
||||
MGLOG_D("UniformDescriptorBinder: frame %u trimmed its descriptor pools (%zu pools / %llu sets -> 1 / %u)",
|
||||
frameIndex, poolsBefore, static_cast<unsigned long long>(capacityBefore), base.maxSets);
|
||||
}
|
||||
|
||||
DescriptorPoolCensus UniformManager::GetDescriptorPoolCensus() const {
|
||||
DescriptorPoolCensus census;
|
||||
census.available = true;
|
||||
census.frameSlots = static_cast<uint32_t>(m_frames.size());
|
||||
for (const auto& frame : m_frames) {
|
||||
const auto poolCount = static_cast<uint32_t>(frame.descriptorPools.size());
|
||||
census.pools += poolCount;
|
||||
census.maxPoolsInOneSlot = std::max(census.maxPoolsInOneSlot, poolCount);
|
||||
census.capacitySets += frame.capacitySets;
|
||||
for (const auto& cacheEntryPair : frame.descriptorSetCacheByLayout) {
|
||||
census.cachedSets += cacheEntryPair.second.sets.size();
|
||||
}
|
||||
}
|
||||
census.slotTrims = m_descriptorSlotTrims;
|
||||
return census;
|
||||
}
|
||||
|
||||
void UniformManager::OnDescriptorSetLayoutDestroyed(VkDescriptorSetLayout descriptorSetLayout) {
|
||||
SizeT purgedSets = 0;
|
||||
for (auto& frame : m_frames) {
|
||||
@@ -2831,32 +2915,49 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
return true;
|
||||
}
|
||||
|
||||
Uint32 UniformManager::NextDescriptorPoolMaxSets(const Vector<DescriptorPoolBucket>& pools, Bool updateAfterBind,
|
||||
Uint32 initialMaxSets) {
|
||||
const Uint32 minSets = std::max<Uint32>(1, initialMaxSets);
|
||||
const Uint32 maxSets = std::max(minSets, kMaxDescriptorPoolSets);
|
||||
Uint32 largest = 0;
|
||||
for (const auto& pool : pools) {
|
||||
if (pool.updateAfterBind == updateAfterBind) {
|
||||
largest = std::max(largest, pool.maxSets);
|
||||
}
|
||||
}
|
||||
if (largest == 0) {
|
||||
return minSets;
|
||||
}
|
||||
const Uint64 doubled = static_cast<Uint64>(largest) * 2;
|
||||
return static_cast<Uint32>(std::clamp<Uint64>(doubled, minSets, maxSets));
|
||||
}
|
||||
|
||||
Bool UniformManager::GrowFrameDescriptorPool(FrameResources& frame, Uint32 frameIndex, Bool updateAfterBind) {
|
||||
if (frame.descriptorPools.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto matchingBucket = std::find_if(
|
||||
frame.descriptorPools.begin(), frame.descriptorPools.end(),
|
||||
[updateAfterBind](const DescriptorPoolBucket& candidate) { return candidate.updateAfterBind == updateAfterBind; });
|
||||
const Uint32 currentMaxSets = matchingBucket != frame.descriptorPools.end()
|
||||
? std::max<Uint32>(1, matchingBucket->maxSets)
|
||||
: m_setsPerFrame;
|
||||
const Uint32 grownMaxSets = currentMaxSets <= (std::numeric_limits<Uint32>::max() / 2) ? (currentMaxSets * 2)
|
||||
: currentMaxSets;
|
||||
|
||||
Uint32 grownMaxSets = NextDescriptorPoolMaxSets(frame.descriptorPools, updateAfterBind, m_setsPerFrame);
|
||||
VkDescriptorPool grownPool = VK_NULL_HANDLE;
|
||||
if (!CreateDescriptorPool(grownMaxSets, updateAfterBind, grownPool)) {
|
||||
MGLOG_E_ONCE("UniformDescriptorBinder::GrowFrameDescriptorPool failed: cannot create grown pool (%u -> %u sets)",
|
||||
currentMaxSets, grownMaxSets);
|
||||
return false;
|
||||
// A large pool is a large driver allocation; if the device refuses one, settle for less
|
||||
// rather than failing the draw - halving down to the base size, where the old policy lived.
|
||||
while (!CreateDescriptorPool(grownMaxSets, updateAfterBind, grownPool)) {
|
||||
if (grownMaxSets <= m_setsPerFrame) {
|
||||
MGLOG_E_ONCE("UniformDescriptorBinder::GrowFrameDescriptorPool failed: cannot create grown pool (%u sets)",
|
||||
grownMaxSets);
|
||||
return false;
|
||||
}
|
||||
grownMaxSets = std::max(m_setsPerFrame, grownMaxSets / 2);
|
||||
}
|
||||
|
||||
frame.descriptorPools.push_back({grownPool, grownMaxSets, 0, updateAfterBind});
|
||||
frame.activeDescriptorPoolIndex = static_cast<Uint32>(frame.descriptorPools.size() - 1);
|
||||
MGLOG_D(
|
||||
"UniformDescriptorBinder: frame %u descriptor pool exhausted, grew pool (%u -> %u sets), poolCount=%zu",
|
||||
frameIndex, currentMaxSets, grownMaxSets, frame.descriptorPools.size());
|
||||
frame.capacitySets += grownMaxSets;
|
||||
// Growing is the opposite of quiet: a trim must wait a full quiet run from here.
|
||||
frame.quietEpochs = 0;
|
||||
MGLOG_D("UniformDescriptorBinder: frame %u descriptor pool exhausted, grew a %u-set pool (updateAfterBind=%d), "
|
||||
"poolCount=%zu",
|
||||
frameIndex, grownMaxSets, updateAfterBind ? 1 : 0, frame.descriptorPools.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user