mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 11:09:56 -04:00
Keep released textures by how long they went unused instead of by a small size bound.
A released texture now goes once nothing has used it for three minutes (six for content only the GPU made), measured in wall seconds from a clock of the presents. In two players' sessions the game took released textures back within a minute of the release or never, so the hold drops nothing it would ask for again. The size bound stays as the safety net for a burst of area changes, and young textures may pass it, up to twice it, while the card and the system's commit have room. vram_use defaults to low, which now means this hold; high turns it off and keeps the released textures up to the bound as before, and balanced reads as low. The old low's values stay available as their own keys. The reuse log line counts drops past the hold and splits its ages around it. tests/tex_release_check checks the rules and the clock, and simulates sessions shaped by the logs against the old and new defaults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -73,7 +73,7 @@ static const char *const known[] = {
|
||||
"SVC_SLOW_MS",
|
||||
"SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_IDLE", "SPLIT_IN_PASS", "SPLIT_US", "SPV_MARK_TEST", "SPV_OVERRIDE", "STICK_DEADZONE", "STICK_SENS",
|
||||
"SWKBD_ANSWER",
|
||||
"SET_AT", "SWKBD_TEST", "TAA", "DBG_TEST", "DEBUG_MENU", "MENU_TEST_DEBUG", "FSR4", "FSR4_FG", "FG4_VERSION", "FG4_SWAPCHAIN", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "DEPTH_INIT_INLINE", "DEPTH_ALIAS_LOG", "TEX_DISUSE", "TEX_DISUSE_EVICT", "TEX_GEN_UNDO", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_RELEASED_MAX", "TEX_REUSE", "TEX_SPLIT_FILL", "TEX_COMMIT_FREE_MB", "TEX_PROC_COMMIT_MB", "TEX_EVICT", "TEX_COMMIT_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "EVO_TRACE", "EVENT_AT_SYNC", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NAN_PRESERVE", "VK_NOOPT", "VK_NO_FLOAT64",
|
||||
"SET_AT", "SWKBD_TEST", "TAA", "DBG_TEST", "DEBUG_MENU", "MENU_TEST_DEBUG", "FSR4", "FSR4_FG", "FG4_VERSION", "FG4_SWAPCHAIN", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "DEPTH_INIT_INLINE", "DEPTH_ALIAS_LOG", "TEX_DISUSE", "TEX_DISUSE_EVICT", "TEX_GEN_UNDO", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_RELEASED_MAX", "TEX_RELEASED_HOLD", "TEX_REUSE", "TEX_SPLIT_FILL", "TEX_COMMIT_FREE_MB", "TEX_PROC_COMMIT_MB", "TEX_EVICT", "TEX_COMMIT_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "EVO_TRACE", "EVENT_AT_SYNC", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NAN_PRESERVE", "VK_NOOPT", "VK_NO_FLOAT64",
|
||||
"VK_PRESENT", "VK_RECONVERGE", "VK_NAN_BITS", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", "WATER_PROJECTIVE",
|
||||
"WAIT_WATCH_S", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WB_PROBE", "WB_SPLIT", "WB_LAZY", "WB_LAZY_CHUNK", "WB_LAZY_TOUCH", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS",
|
||||
"UPLOAD_BATCH_MB", "VRAM_HEAP_LIMIT", "VRAM_RESERVE_MB", "TEX_CLOBBER", "LOW_END_GPU",
|
||||
@@ -149,7 +149,7 @@ static const struct { const char *key, *section; } sections[] = {
|
||||
{"PIPE_SEED_BOOT", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"POOL_DATA_FIRST", "performance"}, {"TEX_REUSE_NAMED", "performance"}, {"TEX_OVERWRITTEN_USED", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"GPL_MESH", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"CLEAR_SMALL_CPU", "performance"}, {"PATCH_OCCLUSION_OFF", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"},
|
||||
{"COMPUTE_KEEP", "performance"},
|
||||
{"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"},
|
||||
{"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"},
|
||||
{"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_RELEASED_HOLD", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"},
|
||||
{"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"PIPELINE_CACHE_PLAY", "performance"}, {"PIPELINE_CACHE_PLAY_MB", "performance"}, {"FFX_PIPE_CACHE", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"},
|
||||
{"TEX_CLOBBER", "performance"}, {"LOW_END_GPU", "performance"},
|
||||
{"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_IDLE", "performance"}, {"SPLIT_IN_PASS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"},
|
||||
@@ -208,10 +208,10 @@ static const CfgMeta metas[] = {
|
||||
{"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE},
|
||||
{"ANISO", "16", "game|2|4|8|16", LIVE}, // vk_texture.c sampler_features, vk_sampler_aniso_live; 16: the game asks 1x or 2x
|
||||
{"TEXTURE_DETAIL", "auto", "auto|full|reduced|low", RESTART}, // vk_texture.c "texture detail", read at the first texture
|
||||
// video memory use: low sets the VRAMLOW defaults below (vk_texture.c released_cap_bytes and sweep_pressure,
|
||||
// vk_pipeline.c seed_boot); high keeps what features switched off hold (cfg_vram_keep: vk_dlaa.c idle_release,
|
||||
// vk_fsr.c idle_note, vk_aa.c work_idle)
|
||||
{"VRAM_USE", "balanced", "low|balanced|high", RESTART},
|
||||
// video memory use: low, the default, keeps released textures for the hold and frees what features switched off
|
||||
// hold; high sets the VRAMHIGH defaults below and keeps those (cfg_vram_keep: vk_dlaa.c idle_release, vk_fsr.c
|
||||
// idle_note, vk_aa.c work_idle). balanced, the default before 0.10, reads as low.
|
||||
{"VRAM_USE", "low", "low|high", RESTART},
|
||||
// colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses
|
||||
{"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE},
|
||||
{"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE},
|
||||
@@ -358,7 +358,9 @@ static const CfgMeta metas[] = {
|
||||
{"PASS_TIMES", "0", "0|1", RESTART}, {"DUMP_KEY", "", NULL, RESTART}, // vk_crumbs.c, vk_dump.c
|
||||
// textures and memory
|
||||
{"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART},
|
||||
{"TEX_ALIAS", "1", "0|1", RESTART}, {"DEPTH_INIT_INLINE", "1", "0|1", RESTART}, {"DEPTH_ALIAS_LOG", "0", "0|1", RESTART}, {"TEX_DISUSE", "1", "0|1|2", RESTART}, {"TEX_DISUSE_EVICT", "0", "0|1", RESTART}, {"TEX_GEN_UNDO", "1", "0|1", RESTART}, {"TEX_REUSE", "1", "0|1", RESTART}, {"TEX_SPLIT_FILL", "1", "0|1|2", RESTART}, {"TEX_VRAM_PRESSURE", "80", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART},
|
||||
{"TEX_ALIAS", "1", "0|1", RESTART}, {"DEPTH_INIT_INLINE", "1", "0|1", RESTART}, {"DEPTH_ALIAS_LOG", "0", "0|1", RESTART}, {"TEX_DISUSE", "1", "0|1|2", RESTART}, {"TEX_DISUSE_EVICT", "0", "0|1", RESTART}, {"TEX_GEN_UNDO", "1", "0|1", RESTART}, {"TEX_REUSE", "1", "0|1", RESTART}, {"TEX_SPLIT_FILL", "1", "0|1|2", RESTART}, {"TEX_VRAM_PRESSURE", "80", "0..100", RESTART},
|
||||
// vk_texture.c drop_aged: seconds a released texture is kept unused (vk_tex_release.h); vram_use = high sets 0
|
||||
{"TEX_RELEASED_HOLD", "180", "0..900", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART},
|
||||
{"TEX_CLOBBER", "0", "0|1", RESTART},
|
||||
{"LOW_END_GPU", "0", "0|1", RESTART}, // LOWEND below
|
||||
{"STG_ARENA", "1", "0|1", RESTART}, {"ASTC_GPU_DESWIZZLE", "1", "0|1", RESTART},
|
||||
@@ -646,26 +648,33 @@ static const char *lowend_value(const char *key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// vram_use = low (NX2NAVI_VRAM_USE, the Screen tab's Video memory use row): defaults for the keys the player does not
|
||||
// set, after the device profile and the low-end mode. Each trades memory for work the game then does again:
|
||||
// - TEX_RELEASED_MB 1024: released textures keep at most 1 GiB (by default a quarter of the video memory budget, 4 GiB
|
||||
// on a 32 GB card, 1.9 GiB on an 8 GB one). One the game takes back after its drop is made and uploaded or decoded
|
||||
// again, and its first frame shows the stand-in (vk_texture.c released_cap_bytes).
|
||||
// - TEX_VRAM_PRESSURE 70: textures that come back intact give way from 70% of the budget on, not 80% (sweep_pressure).
|
||||
// vram_use (NX2NAVI_VRAM_USE, the Screen tab's Video memory use row): low, the default, or high. low keeps a released
|
||||
// texture until it has gone unused for the hold (vk_texture.c drop_aged, TEX_RELEASED_HOLD 180 s; vk_tex_release.h has
|
||||
// the take-back figures behind it: in play the game took released textures back within a minute or not at all) and
|
||||
// frees what a switched-off upscaler or SMAA holds.
|
||||
// high sets the VRAMHIGH defaults for the keys the player does not set, after the device profile and the low-end mode:
|
||||
// TEX_RELEASED_HOLD 0, released textures stay up to the size bound (a quarter of the video memory budget) for the whole
|
||||
// session, as before 0.10; and it keeps what a switched-off feature holds (cfg_vram_keep). balanced, the default before
|
||||
// 0.10, and anything else read as low.
|
||||
// What the low of the 0.10 previews set and did cost something stays to be set on its own:
|
||||
// - TEX_RELEASED_MB 1024: released textures keep at most 1 GiB; those the game takes back after a drop are made and
|
||||
// uploaded or decoded again and show their stand-in for a frame (81 to 218 in each 3600 presents of a session on an
|
||||
// RTX 5090, vk_texture.c released_cap_bytes).
|
||||
// - TEX_VRAM_PRESSURE 70: textures that come back intact give way from 70% of the budget on, not 80% (sweep_pressure),
|
||||
// and are made again when next used.
|
||||
// - PIPE_SEED_BOOT 0: the shared list's pipelines this install never drew are made at their first draw, not at the
|
||||
// start, which leaves out 0.3-0.6 GiB of video memory and 2-4 GB of commit; a first visit to an area can miss a few
|
||||
// objects for a frame or two while they are made (vk_pipeline.c seed_boot).
|
||||
// Unsampled live textures are not dropped in any of the three (tex_disuse_evict stays 0): that eviction left the
|
||||
// ground flat and see-through for good (vk_texture.c disuse_evict_on). high keeps what a switched-off feature holds
|
||||
// (cfg_vram_keep), as before balanced, the default, frees it.
|
||||
static const struct { const char *key, *value; } VRAMLOW[] = {
|
||||
{"TEX_RELEASED_MB", "1024"}, {"TEX_VRAM_PRESSURE", "70"}, {"PIPE_SEED_BOOT", "0"},
|
||||
// start, which leaves out 0.3-0.6 GiB of video memory and 2-4 GB of commit; a session so set missed 1,091 pipelines
|
||||
// at a draw, 115 of them for 8 to 34 ms, and their draws were skipped (vk_pipeline.c seed_boot).
|
||||
// Unsampled live textures are not dropped in either (tex_disuse_evict stays 0): that eviction left the ground flat and
|
||||
// see-through for good (vk_texture.c disuse_evict_on).
|
||||
static const struct { const char *key, *value; } VRAMHIGH[] = {
|
||||
{"TEX_RELEASED_HOLD", "0"},
|
||||
};
|
||||
static const char *vram_use_value(const char *key) {
|
||||
const char *set = raw_get(PREFIX "VRAM_USE");
|
||||
if (!set || strcmp(set, "low")) return NULL;
|
||||
for (size_t i = 0; i < sizeof VRAMLOW / sizeof *VRAMLOW; i++)
|
||||
if (!strcasecmp(VRAMLOW[i].key, key)) return VRAMLOW[i].value;
|
||||
if (!set || strcmp(set, "high")) return NULL;
|
||||
for (size_t i = 0; i < sizeof VRAMHIGH / sizeof *VRAMHIGH; i++)
|
||||
if (!strcasecmp(VRAMHIGH[i].key, key)) return VRAMHIGH[i].value;
|
||||
return NULL;
|
||||
}
|
||||
int cfg_vram_keep(void) {
|
||||
|
||||
95
runtime/vk_tex_release.h
Normal file
95
runtime/vk_tex_release.h
Normal file
@@ -0,0 +1,95 @@
|
||||
// Released textures by age, the part without a GPU: how long a released texture is kept, how many bytes the released
|
||||
// ones may hold before the size bound drops young ones, and the wall clock that turns a present count into seconds.
|
||||
// vk_texture.c ("released textures by age", drop_aged and sweep_pressure) does what these say;
|
||||
// tests/tex_release_check.py runs this file alone.
|
||||
//
|
||||
// The game releases a texture when what uses it leaves the view and takes it back when that returns. The cache keeps a
|
||||
// released texture's image so that the take-back finds it; a texture taken back after its image was dropped is made
|
||||
// again (created and uploaded, or decoded on the GPU), and the frame that asks for it samples its stand-in. How long
|
||||
// after the release the take-backs come is what decides how long an image is worth keeping. Two sessions on an RTX
|
||||
// 5090 (2026-11-12, one at the old Balanced, one at the old Low; vk_texture.c vk_texture_reuse_log) took back, per
|
||||
// line of 3600 presents: under 2 s 22,000 to 121,000; 2 to 10 s 1 to 126; 10 to 60 s 0 to 156; 1 to 5 min and longer
|
||||
// none. Balanced kept every release up to its bound of 4 GiB, so its zero past a minute is not a dropped texture's
|
||||
// absence: nothing released for longer was asked for again. A released texture not taken back within a minute was
|
||||
// never taken back.
|
||||
//
|
||||
// So a released texture goes once it has gone unused for the hold (TRL_HOLD_S by default, NX2NAVI_TEX_RELEASED_HOLD),
|
||||
// three times the longest gap seen, whether or not the bound is reached: the cache then holds what the last few
|
||||
// minutes released, not what the bound allows, and it empties within minutes of the last area change. Unused counts
|
||||
// from the release or from the last time a shader sampled the released image (a finalized slot may show it), whichever
|
||||
// is later. The hold is the same for a texture uploaded from the game's memory and for an ASTC one the GPU decodes:
|
||||
// either made again shows its stand-in for a frame, the decode only costs more of the render thread. A texture whose
|
||||
// content only the GPU made (a render target) cannot be made again at all, it comes back empty: it waits twice the
|
||||
// hold.
|
||||
//
|
||||
// The size bound (released_cap_bytes, a quarter of the video memory budget by default) stays as the safety net it was:
|
||||
// past it the oldest released textures go even when young. With the hold on, young ones past it are kept while the card
|
||||
// and the system have room (trl_room), up to TRL_ROOM_TIMES the bound: the old Balanced dropped 31 young ones in a
|
||||
// burst of area changes at its bound of 4 GiB on a card with 20 GiB free, and each was made again within a minute. The
|
||||
// hold empties that excess within minutes, so the room it takes is temporary; without the hold (vram_use = high) the
|
||||
// bound acts as before.
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
enum { TRL_STORAGE, TRL_GPU }; // content made again from the game's memory; content only the GPU made
|
||||
|
||||
#define TRL_HOLD_S 180 // the default hold, in seconds
|
||||
#define TRL_HOLD_MAX_S 900 // the longest hold the setting takes: twice it fits the clock below
|
||||
#define TRL_ROOM_TIMES 2 // with room, the released textures may hold this many times the bound
|
||||
#define TRL_ROOM_MARGIN 10 // room: the card at least this many points of its budget under the VRAM pressure
|
||||
|
||||
// The hold of a class, in seconds; 0: no hold.
|
||||
static inline uint32_t trl_hold(uint32_t hold_s, int cls) { return cls == TRL_GPU ? hold_s * 2 : hold_s; }
|
||||
|
||||
// Whether a released texture unused for unused_s seconds has passed its hold.
|
||||
static inline int trl_aged(uint32_t hold_s, int cls, uint32_t unused_s) {
|
||||
return hold_s && unused_s >= trl_hold(hold_s, cls);
|
||||
}
|
||||
|
||||
// Whether the card and the system have room for young released textures past the bound: the device-local memory in
|
||||
// use (the driver's figure, holes counted) is TRL_ROOM_MARGIN points or more under the VRAM pressure's share of the
|
||||
// budget, and the system's commit has at least twice the free space under which it counts as low (commit_free_mb
|
||||
// below 0: the commit is not tracked here, which counts as no room).
|
||||
static inline int trl_room(uint64_t used, uint64_t budget, int pressure_pct, double commit_free_mb,
|
||||
double commit_low_mb) {
|
||||
if (!budget || pressure_pct <= TRL_ROOM_MARGIN || commit_free_mb < 0) return 0;
|
||||
return used <= budget / 100 * (uint64_t)(pressure_pct - TRL_ROOM_MARGIN) && commit_free_mb >= 2 * commit_low_mb;
|
||||
}
|
||||
|
||||
// The bytes the released textures may hold before the bound drops young ones (drop_stale's keep): the bound, or
|
||||
// TRL_ROOM_TIMES it while the hold is on and there is room. cap UINT64_MAX: no bound.
|
||||
static inline uint64_t trl_keep(uint32_t hold_s, uint64_t cap, int room) {
|
||||
if (cap == UINT64_MAX || !hold_s || !room) return cap;
|
||||
return cap > UINT64_MAX / TRL_ROOM_TIMES ? UINT64_MAX : cap * TRL_ROOM_TIMES;
|
||||
}
|
||||
|
||||
// The wall clock of the presents. The cache stamps its textures with present counts (gfx_counters.frames), and the
|
||||
// presents per second change with the scene and the menus: a hold counted in presents would be shorter in wall time
|
||||
// after a fast stretch. The clock notes the present count once a second or so (tick) and answers how many seconds ago
|
||||
// a present was, rounded down to the tick after it: a texture is taken as at most that old, never older.
|
||||
#define TRL_CLOCK_N 2048 // ticks kept: at least 2048 s, more than twice TRL_HOLD_MAX_S
|
||||
typedef struct {
|
||||
uint64_t frame[TRL_CLOCK_N]; // the present count at each tick
|
||||
uint32_t sec[TRL_CLOCK_N]; // the tick's second of the run
|
||||
uint32_t n; // ticks so far
|
||||
} TrlClock;
|
||||
|
||||
static inline void trl_clock_tick(TrlClock *c, uint64_t frame, uint32_t sec) {
|
||||
c->frame[c->n % TRL_CLOCK_N] = frame, c->sec[c->n % TRL_CLOCK_N] = sec;
|
||||
c->n++;
|
||||
}
|
||||
|
||||
// Seconds from present `frame` to now_sec: from the first tick at or after it. A present after the last tick is 0 s
|
||||
// old; one before the oldest tick kept is as old as that tick, at least TRL_CLOCK_N - 1 seconds.
|
||||
static inline uint32_t trl_clock_age(const TrlClock *c, uint64_t frame, uint32_t now_sec) {
|
||||
uint32_t kept = c->n < TRL_CLOCK_N ? c->n : TRL_CLOCK_N, first = c->n - kept;
|
||||
if (!kept || c->frame[(c->n - 1) % TRL_CLOCK_N] < frame) return 0;
|
||||
uint32_t lo = first, hi = c->n - 1; // the first tick with frame >= frame lies in [lo, hi]
|
||||
while (lo < hi) {
|
||||
uint32_t mid = lo + (hi - lo) / 2;
|
||||
if (c->frame[mid % TRL_CLOCK_N] >= frame) hi = mid;
|
||||
else lo = mid + 1;
|
||||
}
|
||||
uint32_t s = c->sec[lo % TRL_CLOCK_N];
|
||||
return now_sec > s ? now_sec - s : 0;
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "vk_dlaa.h"
|
||||
#include "vk_exec.h"
|
||||
#include "vk_tex_compact.h"
|
||||
#include "vk_tex_release.h"
|
||||
#include "prof.h"
|
||||
#include "platform/platform.h"
|
||||
|
||||
@@ -182,12 +183,23 @@ static uint32_t submit_epoch = 1; // bumped per queue submit: draw bindings ch
|
||||
static uint32_t tex_rate = 256;
|
||||
#define PRESENTS(n) ((uint64_t)(n) * tex_rate >> 8)
|
||||
|
||||
// The wall clock of the presents (vk_tex_release.h TrlClock): rate_tick notes the present count about once a second,
|
||||
// so that the released textures' hold (drop_aged) and the take-back ages (vk_texture_reuse_log) count seconds, not
|
||||
// presents at whatever rate the game runs. tex_lock.
|
||||
static TrlClock pclock;
|
||||
static uint64_t pclock_t0; // kern_now_ns at the first tick
|
||||
|
||||
static uint32_t now_sec(void) { return pclock_t0 ? (uint32_t)((kern_now_ns() - pclock_t0) / 1000000000ull) : 0; }
|
||||
|
||||
static uint32_t secs_since(uint64_t frame) { return trl_clock_age(&pclock, frame, now_sec()); }
|
||||
|
||||
static void rate_tick(void) { // each submission, tex_lock held
|
||||
static uint64_t t0, f0;
|
||||
static uint32_t ring[RATE_N], at;
|
||||
uint64_t t = kern_now_ns(), f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
if (!t0) t0 = t, f0 = f;
|
||||
if (!t0) t0 = t, f0 = f, pclock_t0 = t, trl_clock_tick(&pclock, f, 0);
|
||||
if (t - t0 < 1000000000ull) return;
|
||||
trl_clock_tick(&pclock, f, (uint32_t)((t - pclock_t0) / 1000000000ull));
|
||||
uint64_t r = (f - f0) * 256ull * 1000000000ull / ((t - t0) * 60);
|
||||
ring[at++ % RATE_N] = r < 256 ? 256 : r > 2048 ? 2048 : (uint32_t)r;
|
||||
t0 = t, f0 = f;
|
||||
@@ -723,11 +735,12 @@ static int slot_names(const Tex *e) {
|
||||
|
||||
// Why an entry was destroyed (drop_tex), in the order of drop_names.
|
||||
enum { DROP_NONE, DROP_MAX, DROP_CAP, DROP_COMMIT, DROP_VRAM, DROP_IDLE_VRAM, DROP_IDLE_COMMIT, DROP_DISUSE,
|
||||
DROP_OVERWRITTEN, DROP_REPLACED, DROP_REUSED, DROP_RANGE, DROP_EXACT, DROP_SHUTDOWN, DROP_COMPACT, DROP_N };
|
||||
DROP_OVERWRITTEN, DROP_REPLACED, DROP_REUSED, DROP_RANGE, DROP_EXACT, DROP_SHUTDOWN, DROP_COMPACT,
|
||||
DROP_AGE, DROP_N };
|
||||
static void drop_tex(Tex *e, int why);
|
||||
static void reuse_dropped(const Tex *e, int why); // "reuse of released textures", after vk_texture_census
|
||||
static void reuse_made(const Tex *e);
|
||||
static void reuse_taken_back(const Tex *e, uint64_t gap);
|
||||
static void reuse_taken_back(const Tex *e);
|
||||
static int compact_on(void); // NX2NAVI_TEX_COMPACT ("compaction", near the end of the file)
|
||||
static void compact_step(void);
|
||||
static void compact_forget(Tex *e);
|
||||
@@ -1572,8 +1585,10 @@ static void rel_append(Tex *e) {
|
||||
}
|
||||
|
||||
static Tex *sweep_at; // where sweep_released goes on
|
||||
static Tex *aged_at; // where drop_aged goes on
|
||||
static void rel_unlink(Tex *e) {
|
||||
if (sweep_at == e) sweep_at = e->rel_next;
|
||||
if (aged_at == e) aged_at = e->rel_next;
|
||||
ru_unlink(e);
|
||||
rel_bytes -= e->bytes;
|
||||
if (e->rel_prev) e->rel_prev->rel_next = e->rel_next;
|
||||
@@ -1888,7 +1903,8 @@ static const char *const drop_names[DROP_N] = {
|
||||
"", "past RELEASED_MAX released entries", "past the released entries' size cap", "under commit pressure",
|
||||
"under VRAM pressure", "idle, under VRAM pressure", "idle, under commit pressure", "unsampled (disuse)",
|
||||
"overwritten at another address", "replaced by another descriptor at its address", "its image went to another texture",
|
||||
"its storage went", "made whole again (texture detail)", "shutdown", "released, in a block a compaction empties"};
|
||||
"its storage went", "made whole again (texture detail)", "shutdown", "released, in a block a compaction empties",
|
||||
"released and unused for longer than the hold"};
|
||||
static const char *const start_names[START_N] = {
|
||||
"started on its own", "filled from storage over a live entry", "content copied from a live entry",
|
||||
"content copied from a released entry", "started empty over a live entry", "started empty over a released entry",
|
||||
@@ -2365,12 +2381,15 @@ void vk_texture_pool_ctx(int on) { pool_ctx = on; }
|
||||
static int idle(const Tex *e, uint64_t now);
|
||||
|
||||
// NX2NAVI_TEX_RELEASED_MB (default a thirty-second of the installed memory, 512 MiB to 2 GiB, or more by the card's
|
||||
// budget, released_cap_bytes; 0 = no limit) bounds the
|
||||
// own memory of the released entries: RELEASED_MAX alone kept 5-7 GiB of released render targets and decoded textures
|
||||
// after an hour of play at a 2160p game resolution, device memory the driver also charges to the system's commit (the
|
||||
// process's private bytes reached 31 GiB). A sixteenth (1 to 4 GiB) still let them hold 1.3 GiB on a 32 GB PC whose
|
||||
// commit then ran out at an area load (device lost); only entries released STALE_FRAMES presents ago go, so the
|
||||
// transient ones the game takes back within frames stay. NX2NAVI_TEX_COMMIT_FREE_MB (default the larger of 4 GiB and a tenth of the commit
|
||||
// budget, released_cap_bytes; 0 = no limit) bounds the own memory of the released entries. With the hold on (drop_aged,
|
||||
// the default) what they hold is what the last minutes released, and the bound is the safety net for a burst of area
|
||||
// changes within the hold; sweep_pressure lets young entries past it while the card and the system have room
|
||||
// (vk_tex_release.h trl_keep). Before the hold the bound was all that limited them: RELEASED_MAX alone kept 5-7 GiB of
|
||||
// released render targets and decoded textures after an hour of play at a 2160p game resolution, device memory the
|
||||
// driver also charges to the system's commit (the process's private bytes reached 31 GiB). A sixteenth (1 to 4 GiB)
|
||||
// still let them hold 1.3 GiB on a 32 GB PC whose commit then ran out at an area load (device lost); only entries
|
||||
// released STALE_FRAMES presents ago go, so the transient ones the game takes back within frames stay.
|
||||
// NX2NAVI_TEX_COMMIT_FREE_MB (default the larger of 4 GiB and a tenth of the commit
|
||||
// limit; 0 = off) lowers that bound to COMMIT_LOW_KEEP while less of the system's commit is free: an allocation that
|
||||
// fails there ends the game. On Linux nothing fails at the limit: the OOM killer ends the game when its cgroup or the
|
||||
// machine runs out of memory, and plat_commit_info reports the tighter of the two. Both drop only stale entries
|
||||
@@ -2404,6 +2423,9 @@ static long long proc_commit_mb; // this process's bound in MB; 0 = none
|
||||
static int commit_low, commit_logs; // commit_low: either of the two below at the last check
|
||||
static int sys_low, proc_high; // the system's commit is low; this process's is above its bound
|
||||
static uint64_t n_cap_drops, n_commit_drops, n_commit_past;
|
||||
// The system's commit at the last check, for the room young released entries past the bound may take (trl_room): MB
|
||||
// free and the free space under which it counts as low; -1 free: not tracked.
|
||||
static double commit_free_now = -1, commit_low_mark;
|
||||
|
||||
static uint64_t released_cap_bytes(void) {
|
||||
if (!released_cap) {
|
||||
@@ -2460,6 +2482,7 @@ static int commit_is_low(void) {
|
||||
"textures give way only when the system's commit is low",
|
||||
plat_commit_counts_vram() ? ", less the video memory in use on a discrete card," : "", proc_commit_mb);
|
||||
}
|
||||
commit_free_now = -1;
|
||||
if (!commit_free_mb && !proc_commit_mb) return 0;
|
||||
double proc, used, limit;
|
||||
plat_commit_info(&proc, &used, &limit);
|
||||
@@ -2468,6 +2491,7 @@ static int commit_is_low(void) {
|
||||
if (commit_free_mb && used >= 0 && limit > 0) {
|
||||
double want = commit_free_mb > 0 ? (double)commit_free_mb : limit / 10 > 4096 ? limit / 10 : 4096;
|
||||
low = limit - used < want;
|
||||
commit_free_now = limit - used, commit_low_mark = want;
|
||||
commit_critical = limit - used < want / 2; // the pause for drops that come back ends here
|
||||
}
|
||||
double dev = 0; // device-local memory in use, MB: in the commit (plat_commit_counts_vram), not in RAM (see above)
|
||||
@@ -2620,6 +2644,62 @@ static void drop_overwritten(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- released textures by age (vk_tex_release.h has the take-back figures behind the hold)
|
||||
// NX2NAVI_TEX_RELEASED_HOLD (seconds, default TRL_HOLD_S, at most TRL_HOLD_MAX_S; 0 = no hold; vram_use = high sets 0):
|
||||
// a released entry that owns memory goes once it has gone unused that long, twice that for content only the GPU made,
|
||||
// whatever the size bound says. Unused counts from the release or the last sample of the released image, whichever is
|
||||
// later, in seconds of the wall clock (secs_since). The rules drop_stale keeps hold here too: not before stale_after
|
||||
// (an entry the game once took back after a long gap waits GAP_TIMES that gap) and not while it is not idle (a lookup
|
||||
// other than the pool's reached it, or a shader sampled it, lately). Nor an entry the GPU drew into since its last
|
||||
// write-back: its drop would first copy it back to the game's memory and wait for the GPU (drop_tex, write_back_n),
|
||||
// a stall the sweep is not to make in play; such an entry stays to the size bound, as before the hold. Each check
|
||||
// looks at up to AGED_LOOK entries from where the last one stopped (aged_at), in release order, and starts from the
|
||||
// oldest again at the first one released less than the hold ago: the rest are younger.
|
||||
#define AGED_LOOK 256
|
||||
static int rel_hold = -1; // seconds; -1: not read yet
|
||||
static uint64_t n_age_drops, n_age_bytes; // since the last vk_texture_reuse_log line
|
||||
|
||||
static uint32_t released_hold(void) {
|
||||
if (rel_hold < 0) {
|
||||
const char *s = cfg_get("NX2NAVI_TEX_RELEASED_HOLD");
|
||||
long long v = s && *s ? atoll(s) : TRL_HOLD_S;
|
||||
rel_hold = v < 0 ? 0 : v > TRL_HOLD_MAX_S ? TRL_HOLD_MAX_S : (int)v;
|
||||
if (rel_hold)
|
||||
rt_log("vk: released textures go once unused for %d s, %d s for content only the GPU made "
|
||||
"(NX2NAVI_TEX_RELEASED_HOLD); young ones past the size bound stay while the card and the system "
|
||||
"have room, up to %d times the bound", rel_hold, (int)trl_hold((uint32_t)rel_hold, TRL_GPU),
|
||||
TRL_ROOM_TIMES);
|
||||
else
|
||||
rt_log("vk: released textures stay until the size bound or a pressure takes them "
|
||||
"(NX2NAVI_TEX_RELEASED_HOLD=0)");
|
||||
}
|
||||
return (uint32_t)rel_hold;
|
||||
}
|
||||
|
||||
static void drop_aged(void) { // tex_lock held
|
||||
uint32_t hold = released_hold();
|
||||
if (!hold) return;
|
||||
uint64_t now = frames_now();
|
||||
Tex *e = aged_at ? aged_at : rel_head;
|
||||
int dropped = 0;
|
||||
for (int n = 0; n < AGED_LOOK && dropped < PRESSURE_STEP; n++) {
|
||||
if (!e || secs_since(e->rel_frame) < hold) { // the rest is younger: from the oldest next time
|
||||
e = NULL;
|
||||
break;
|
||||
}
|
||||
Tex *next = e->rel_next;
|
||||
int cls = comes_back_intact(e) || e->overwritten ? TRL_STORAGE : TRL_GPU;
|
||||
uint64_t last = e->samp_frame > e->rel_frame ? e->samp_frame : e->rel_frame;
|
||||
if (e->bytes && !e->gpu_dirty && trl_aged(hold, cls, secs_since(last)) &&
|
||||
now - e->rel_frame >= stale_after(e) && idle(e, now)) {
|
||||
n_age_drops++, n_age_bytes += e->bytes, dropped++;
|
||||
drop_tex(e, DROP_AGE);
|
||||
}
|
||||
e = next;
|
||||
}
|
||||
aged_at = e;
|
||||
}
|
||||
|
||||
// NX2NAVI_TEX_EVICT (default on; 0 = only released entries go under VRAM pressure, as before): above the VRAM pressure
|
||||
// threshold, once the released entries that come back intact are gone, live entries go too, least recently looked up
|
||||
// first: those that come back intact and that no lookup reached for IDLE_ROUNDS pool walks and STALE_FRAMES presents
|
||||
@@ -3179,9 +3259,17 @@ static void sweep_pressure(void) { // tex_lock held
|
||||
int held = low && commit_drops_held(); // the commit drops came back (commit_back_note)
|
||||
if (rel_head) {
|
||||
drop_overwritten();
|
||||
uint64_t cap = released_cap_bytes();
|
||||
if (rel_bytes > cap) drop_stale(cap, &n_cap_drops, DROP_CAP);
|
||||
if (cap != UINT64_MAX && rel_bytes > cap + cap / 4) rel_census(cap); // well over it still: what holds the rest
|
||||
drop_aged();
|
||||
// the bound, or more of it while the hold empties the released entries within minutes and the card and the
|
||||
// system have room (vk_tex_release.h trl_keep): a young entry dropped there is one the game may still take back
|
||||
uint64_t cap = released_cap_bytes(), keep = cap, vused, vbudget;
|
||||
int vexact;
|
||||
if (rel_bytes > cap && released_hold() && vk_mem_vram(&vused, &vbudget, &vexact))
|
||||
keep = trl_keep(released_hold(), cap,
|
||||
trl_room(vused, vbudget, vram_pressure ? vram_pressure : VRAM_PRESSURE_DEFAULT,
|
||||
commit_free_now, commit_low_mark));
|
||||
if (rel_bytes > keep) drop_stale(keep, &n_cap_drops, DROP_CAP);
|
||||
if (keep != UINT64_MAX && rel_bytes > keep + keep / 4) rel_census(keep); // well over it: what holds the rest
|
||||
if (rel_bytes > COMMIT_LOW_KEEP && low && !held)
|
||||
more = drop_stale(COMMIT_LOW_KEEP, &n_commit_drops, DROP_COMMIT) == PRESSURE_STEP;
|
||||
}
|
||||
@@ -4268,7 +4356,7 @@ static Tex *entry_of(const tex_t *t) {
|
||||
e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); // in use again, as a new one
|
||||
uint64_t gap = e->samp_frame - e->rel_frame;
|
||||
if (gap > e->back_gap) e->back_gap = gap > UINT32_MAX ? UINT32_MAX : (uint32_t)gap;
|
||||
reuse_taken_back(e, gap);
|
||||
reuse_taken_back(e);
|
||||
e->released = 0, nreleased--, rel_unlink(e), n_site[2]++, tex_gen_flip(e, e->superseded || e->overwritten);
|
||||
if (e->pidx) pent[e->pidx - 1].rel = 0; // the pool's check looks at it again
|
||||
e->overwritten = 0;
|
||||
@@ -6358,27 +6446,27 @@ void vk_texture_census(VkTexCensus *s) {
|
||||
// The two tell what the bounds on the released entries cost: an entry taken back after minutes is one a smaller bound
|
||||
// may have dropped, and one made again soon after its drop is one the bound did drop. Counted since the last line; the
|
||||
// dropped keys are remembered in a table of DROPPED_N slots indexed by the key's hash, a newer drop taking the slot.
|
||||
enum { AGE_YOUNG, AGE_10S, AGE_1M, AGE_5M, AGE_OLD, AGE_N };
|
||||
static const char *const age_names[AGE_N] = {"under 2 s", "2-10 s", "10-60 s", "1-5 min", "longer"};
|
||||
// Ages are wall seconds (secs_since); 1-3 min and 3-5 min lie either side of the default hold (TRL_HOLD_S).
|
||||
enum { AGE_YOUNG, AGE_10S, AGE_1M, AGE_3M, AGE_5M, AGE_OLD, AGE_N };
|
||||
static const char *const age_names[AGE_N] = {"under 2 s", "2-10 s", "10-60 s", "1-3 min", "3-5 min", "longer"};
|
||||
static uint64_t rb_n[AGE_N], rb_bytes[AGE_N]; // take-backs by the time released (tex_lock)
|
||||
static uint64_t ra_n[AGE_N], ra_bytes, ra_why[DROP_N]; // made again by the time since the drop, and by its reason
|
||||
#define DROPPED_N 8192
|
||||
static struct { uint64_t kh, frame; uint8_t why; } dropped[DROPPED_N]; // tex_lock
|
||||
|
||||
static int age_of(uint64_t gap) { // presents, at the presents per second of now (PRESENTS)
|
||||
return gap < STALE_FRAMES ? AGE_YOUNG : gap < PRESENTS(600) ? AGE_10S : gap < PRESENTS(3600) ? AGE_1M
|
||||
: gap < PRESENTS(18000) ? AGE_5M : AGE_OLD;
|
||||
static int age_of(uint32_t s) { // seconds
|
||||
return s < 2 ? AGE_YOUNG : s < 10 ? AGE_10S : s < 60 ? AGE_1M : s < 180 ? AGE_3M : s < 300 ? AGE_5M : AGE_OLD;
|
||||
}
|
||||
|
||||
static void reuse_taken_back(const Tex *e, uint64_t gap) { // tex_lock held; e was released for gap presents
|
||||
int a = age_of(gap);
|
||||
static void reuse_taken_back(const Tex *e) { // tex_lock held; e is being taken back, rel_frame still its release
|
||||
int a = age_of(secs_since(e->rel_frame));
|
||||
rb_n[a]++, rb_bytes[a] += e->alloc ? e->bytes : 0;
|
||||
}
|
||||
|
||||
// Drops of an entry the game may still ask for: past a bound, or under a pressure, or unsampled.
|
||||
static int drop_costs(int why) {
|
||||
return why == DROP_MAX || why == DROP_CAP || why == DROP_COMMIT || why == DROP_VRAM || why == DROP_IDLE_VRAM ||
|
||||
why == DROP_IDLE_COMMIT || why == DROP_DISUSE;
|
||||
why == DROP_IDLE_COMMIT || why == DROP_DISUSE || why == DROP_AGE;
|
||||
}
|
||||
|
||||
static void reuse_dropped(const Tex *e, int why) { // tex_lock held
|
||||
@@ -6390,13 +6478,13 @@ static void reuse_dropped(const Tex *e, int why) { // tex_lock held
|
||||
static void reuse_made(const Tex *e) { // tex_lock held; e was just made
|
||||
uint64_t kh = key_hash(&e->key);
|
||||
if (dropped[kh % DROPPED_N].kh != kh || !dropped[kh % DROPPED_N].why) return;
|
||||
int a = age_of(frames_now() - dropped[kh % DROPPED_N].frame);
|
||||
int a = age_of(secs_since(dropped[kh % DROPPED_N].frame));
|
||||
ra_n[a]++, ra_bytes += e->alloc ? e->bytes : 0, ra_why[dropped[kh % DROPPED_N].why]++;
|
||||
dropped[kh % DROPPED_N].why = 0;
|
||||
}
|
||||
|
||||
void vk_texture_reuse_log(uint64_t frame) {
|
||||
char line[1024];
|
||||
char line[1536];
|
||||
const double M = 1048576.0;
|
||||
lock();
|
||||
int o = snprintf(line, sizeof line, "vk: released textures frame %llu, since the last line: taken back by the time "
|
||||
@@ -6410,15 +6498,18 @@ void vk_texture_reuse_log(uint64_t frame) {
|
||||
(unsigned long long)again, ra_bytes / M);
|
||||
for (int a = 0; a < AGE_N; a++)
|
||||
o += snprintf(line + o, sizeof line - (size_t)o, "%s %s %llu", a ? "," : "", age_names[a], (unsigned long long)ra_n[a]);
|
||||
snprintf(line + o, sizeof line - (size_t)o, "; by the drop: released past the size bound %llu, past the count %llu, "
|
||||
"VRAM pressure %llu released and %llu idle, commit %llu released and %llu idle, unsampled %llu); now %d "
|
||||
"released, %.0f MiB of a bound of %.0f MiB", (unsigned long long)ra_why[DROP_CAP],
|
||||
snprintf(line + o, sizeof line - (size_t)o, "; by the drop: released past the hold %llu, past the size bound "
|
||||
"%llu, past the count %llu, VRAM pressure %llu released and %llu idle, commit %llu released and %llu "
|
||||
"idle, unsampled %llu); dropped past the hold %llu (%.0f MiB); now %d released, %.0f MiB of a bound of "
|
||||
"%.0f MiB, hold %u s",
|
||||
(unsigned long long)ra_why[DROP_AGE], (unsigned long long)ra_why[DROP_CAP],
|
||||
(unsigned long long)ra_why[DROP_MAX], (unsigned long long)ra_why[DROP_VRAM],
|
||||
(unsigned long long)ra_why[DROP_IDLE_VRAM], (unsigned long long)ra_why[DROP_COMMIT],
|
||||
(unsigned long long)ra_why[DROP_IDLE_COMMIT], (unsigned long long)ra_why[DROP_DISUSE], nreleased,
|
||||
rel_bytes / M, released_cap_bytes() == UINT64_MAX ? 0.0 : released_cap_bytes() / M);
|
||||
(unsigned long long)ra_why[DROP_IDLE_COMMIT], (unsigned long long)ra_why[DROP_DISUSE],
|
||||
(unsigned long long)n_age_drops, n_age_bytes / M, nreleased, rel_bytes / M,
|
||||
released_cap_bytes() == UINT64_MAX ? 0.0 : released_cap_bytes() / M, released_hold());
|
||||
memset(rb_n, 0, sizeof rb_n), memset(rb_bytes, 0, sizeof rb_bytes), memset(ra_n, 0, sizeof ra_n);
|
||||
memset(ra_why, 0, sizeof ra_why), ra_bytes = 0;
|
||||
memset(ra_why, 0, sizeof ra_why), ra_bytes = 0, n_age_drops = n_age_bytes = 0;
|
||||
unlock();
|
||||
rt_log("%s", line);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user