mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 09:19:58 -04:00
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:
@@ -4190,6 +4190,41 @@ void vk_upload_end(void) { plat_rmutex_unlock(&cmd_lock); }
|
||||
static VmaPool stg_pool;
|
||||
static int stg_pool_tried;
|
||||
|
||||
// A staging buffer the driver refused (vk_staging): the buffers kept for reuse go back to the driver before one more
|
||||
// try, with the headroom's spare blocks (vk_headroom_release): the staging cache, the arena's free chunks and the
|
||||
// write-back snapshot pool, up to some hundred MiB. On an integrated GPU 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). The replay's transient buffers and descriptor pools do the
|
||||
// same (vk_exec_draw.c scratch, alloc_set). Returns the bytes given back.
|
||||
uint64_t vk_mem_give_back(void) {
|
||||
VkCtx *c = vk_ctx();
|
||||
uint64_t freed = 0;
|
||||
plat_rmutex_lock(&cmd_lock);
|
||||
for (int i = 0; i < stg_ncache; i++) {
|
||||
freed += stg_cache[i].size;
|
||||
vmaDestroyBuffer(c->vma, stg_cache[i].buf, stg_cache[i].alloc);
|
||||
}
|
||||
stg_ncache = 0, stg_cache_bytes = 0;
|
||||
for (int i = 0; i < narena_free; i++) vmaDestroyBuffer(c->vma, arena_free[i].buf, arena_free[i].alloc);
|
||||
freed += (uint64_t)narena_free * ARENA_CHUNK;
|
||||
narena_free = 0;
|
||||
plat_rmutex_unlock(&cmd_lock);
|
||||
plat_mutex_lock(&snap_pool_lock);
|
||||
for (int i = 0; i < nsnap_pool; i++) vmaDestroyBuffer(c->vma, snap_pool[i].buf, snap_pool[i].alloc);
|
||||
freed += snap_pool_bytes;
|
||||
nsnap_pool = 0, snap_pool_bytes = 0;
|
||||
plat_mutex_unlock(&snap_pool_lock);
|
||||
return freed + vk_headroom_release();
|
||||
}
|
||||
|
||||
// NX2NAVI_STAGING_FAIL_AT=n (test): the n-th staging buffer the driver makes is taken as refused for memory once, so
|
||||
// the give-back runs (vk_mem_give_back).
|
||||
static int staging_fail_now(void) {
|
||||
static int64_t at = -1, n;
|
||||
if (at < 0) at = cfg_int("NX2NAVI_STAGING_FAIL_AT");
|
||||
return at > 0 && plat_inc64(&n) == at;
|
||||
}
|
||||
|
||||
void *vk_staging(VkDeviceSize size, VkBuffer *buf, VmaAllocation *alloc) {
|
||||
VkCtx *c = vk_ctx();
|
||||
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size ? size : 16,
|
||||
@@ -4233,6 +4268,21 @@ void *vk_staging(VkDeviceSize size, VkBuffer *buf, VmaAllocation *alloc) {
|
||||
aci.pool = NULL;
|
||||
r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, &info);
|
||||
}
|
||||
if (r == VK_SUCCESS && staging_fail_now()) {
|
||||
vmaDestroyBuffer(c->vma, b, a);
|
||||
r = VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
}
|
||||
if (r == VK_ERROR_OUT_OF_DEVICE_MEMORY || r == VK_ERROR_OUT_OF_HOST_MEMORY) {
|
||||
VkResult first = r;
|
||||
uint64_t freed = vk_mem_give_back();
|
||||
aci.pool = NULL;
|
||||
r = vmaCreateBuffer(c->vma, &bi, &aci, &b, &a, &info);
|
||||
static volatile int32_t told;
|
||||
if (plat_inc32(&told) <= 16)
|
||||
rt_log("vk: staging buffer of %llu bytes refused (%d): %.1f MiB of kept buffers and spare blocks given back, "
|
||||
"%s", (unsigned long long)size, (int)first, freed / 1048576.0,
|
||||
r == VK_SUCCESS ? "made at the second try" : "refused again");
|
||||
}
|
||||
if (r != VK_SUCCESS) rt_fatal("vk: staging buffer of %llu bytes failed (%d)", (unsigned long long)size, (int)r);
|
||||
vk_texstat_add(VK_TS_STG_NEW, 1, bi.size);
|
||||
*buf = b;
|
||||
|
||||
Reference in New Issue
Block a user