Give back the kept staging and snapshot buffers and the spare blocks when an allocation is refused, then try once more.

A staging buffer, a transient buffer of the replay or a descriptor pool that the driver refuses for memory used to end
the game at once. Now the buffers kept for reuse go back to the driver first: the staging cache, the staging arena's
free chunks, the write-back snapshot pool and the headroom's spare blocks (no new spare block for 30 s after that), and
the allocation is tried once more; only a second refusal ends the game. On integrated GPUs the device-local heap is the
machine's RAM and fills before the commit checks see anything: staging buffers of 11 KiB to 16 MiB refused there ended
the game in Hyrule Field and on the first scene of play (#465, #567).

NX2NAVI_STAGING_FAIL_AT=n (diagnostics) takes the n-th staging buffer as refused once, to test the path.

Check: a Kakariko run with NX2NAVI_STAGING_FAIL_AT=300 logged "staging buffer of 524288 bytes refused (-2): 348.2 MiB of kept buffers and spare
blocks given back, made at the second try" and played on to the end of its script. The transient buffer and descriptor
pool paths call the same give-back and were not forced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 19:14:58 -05:00
parent 14fbc6a0ea
commit 74b7449518
5 changed files with 92 additions and 4 deletions

View File

@@ -305,6 +305,15 @@ static uint8_t *scratch(VkExec *ex, uint64_t size, VkBuffer *buf, VkDeviceSize *
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT};
VmaAllocationInfo info;
VkResult res = vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &c->buf, &c->alloc, &info);
if (res == VK_ERROR_OUT_OF_DEVICE_MEMORY || res == VK_ERROR_OUT_OF_HOST_MEMORY) { // once more after a give-back
uint64_t freed = vk_mem_give_back();
VkResult again = vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &c->buf, &c->alloc, &info);
static volatile int32_t told;
if (plat_inc32(&told) <= 16)
rt_log("vk: transient buffer of %llu bytes refused (%d): %.1f MiB given back, %s", (unsigned long long)n,
(int)res, freed / 1048576.0, again == VK_SUCCESS ? "made at the second try" : "refused again");
res = again;
}
if (res != VK_SUCCESS) rt_fatal("vk: transient buffer of %llu bytes failed (%d)", (unsigned long long)n, (int)res);
c->map = info.pMappedData, c->size = n, c->used = 0;
r->nchunks++;
@@ -2814,7 +2823,16 @@ static VkDescriptorSet alloc_set(VkExec *ex, VkDescriptorSetLayout layout) {
plat_mutex_lock(&spare_lock);
if (nspare_pool) pool = spare_pool[--nspare_pool];
plat_mutex_unlock(&spare_lock);
if (!pool && vkCreateDescriptorPool(c->device, &pi, NULL, &pool) != VK_SUCCESS) rt_fatal("vk: descriptor pool failed");
if (!pool) {
VkResult res = vkCreateDescriptorPool(c->device, &pi, NULL, &pool);
if (res == VK_ERROR_OUT_OF_DEVICE_MEMORY || res == VK_ERROR_OUT_OF_HOST_MEMORY) { // once more, as scratch
uint64_t freed = vk_mem_give_back();
res = vkCreateDescriptorPool(c->device, &pi, NULL, &pool);
rt_log("vk: descriptor pool refused: %.1f MiB given back, %s", freed / 1048576.0,
res == VK_SUCCESS ? "made at the second try" : "refused again");
}
if (res != VK_SUCCESS) rt_fatal("vk: descriptor pool failed (%d)", (int)res);
}
r->pools[r->npools++] = pool;
}
rt_fatal("vk: descriptor set allocation failed twice");