mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-18 12:08:38 +00:00
Merge the freeze fixes: full-screen draws skip while their pipeline builds, staging buffers come from a pool of their own, released textures go oldest first instead of thousands in one frame, and input scripts count game time before the world clock
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
67f6c973ff
9 files changed
+115
-34
No files matched your search
@@ -76,7 +76,7 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app
|
||||
- Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default because no timing has shown a gain yet.
|
||||
- Driver data. The NVN driver keeps a per-stage constant bank (c0) that shaders read for things like point size, dispatch size and base instance. The back end fills it the way the driver does.
|
||||
- Shaders: the Ryujinx translator 1.3.2 with MacNativeGames' patches and ours (`tools/xlate/patches/`), built with .NET 10 NativeAOT as `totk_xlate.exe` and run as a separate process. Its SPIR-V goes to Vulkan directly. Translated shaders are cached on disk.
|
||||
- Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it. Shader stages are compiled ahead into libraries; a draw that meets a new state links them at once (fast link), and an optimised pipeline replaces the linked one when a background thread finishes it. Pipeline keys seen in play are saved and built at boot. A draw whose pipeline is not ready is skipped when one of its pass's targets was rendered in the previous frame, and waits otherwise, so one-shot passes are never lost (`TOTK_PIPE_SKIP`); the stage libraries a waiting draw needs are built on idle workers in parallel (`TOTK_PIPE_WAIT`). Without the extension, a missing pipeline is built on a worker and the draw waits or is skipped by the same rule.
|
||||
- Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it. Shader stages are compiled ahead into libraries; a draw that meets a new state links them at once (fast link), and an optimised pipeline replaces the linked one when a background thread finishes it. Pipeline keys seen in play are saved and built at boot. A draw whose pipeline is not ready is skipped when one of its pass's targets was rendered in the previous frame, and waits otherwise, so one-shot passes are never lost; a full-screen draw is skipped as well rather than waiting (`TOTK_PIPE_SKIP`); the stage libraries a waiting draw needs are built on idle workers in parallel (`TOTK_PIPE_WAIT`). Without the extension, a missing pipeline is built on a worker and the draw waits or is skipped by the same rule.
|
||||
- Occlusion queries never block the CPU; a result that is not ready counts as visible.
|
||||
- ASTC textures are uploaded as they are where the device reports `textureCompressionASTC_LDR`, and decoded by a compute shader everywhere else (`vk_astc.c`, `TOTK_ASTC`). The CPU decoder (`tex_astc*.c`) is the fallback and the test oracle.
|
||||
- Draw state. A draw whose program, bound state and textures repeat reuses the previous draw's shaders and skips rebuilding its descriptor sets; descriptor sets are written through update templates (`vk_exec_draw.c`, `vk_pipeline.c`).
|
||||
|
||||
+2
-2
@@ -60,9 +60,9 @@ The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sti
|
||||
@<ms> LSTICK|RSTICK <x> <y> [hold_ms]
|
||||
```
|
||||
|
||||
`<frame>` counts presented frames. `@<ms>` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. Each event logs once (`input: script frame N: A`). `tools/input/load_save.txt` loads the first save from the title screen.
|
||||
`<frame>` counts 60 Hz ticks of game time since the first frame (presented frames at 60 fps), so the title menus get the presses at the same game time at any frame limit. `@<ms>` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. In a script with timed lines the untimed ones stop when the world clock starts, as the load ends at another tick in each run. Each event logs once (`input: script frame N tick T: A`). `tools/input/load_save.txt` loads the first save from the title screen.
|
||||
|
||||
`TOTK_INPUT_RECORD=<file>` (`input_record` in `totk.ini`, or Input recording in the menu) writes the merged input in the same format, one line per button press and per stick segment: events before the world clock starts by frame, the rest timed. The file replays as a script, which makes it the repro to attach to a bug report.
|
||||
`TOTK_INPUT_RECORD=<file>` (`input_record` in `totk.ini`, or Input recording in the menu) writes the merged input in the same format, one line per button press and per stick segment: events before the world clock starts by tick, the rest timed; a button or stick held when the world clock starts is cut there and goes on as a timed event. The file replays as a script, which makes it the repro to attach to a bug report.
|
||||
|
||||
## RenderDoc capture (blocked)
|
||||
|
||||
|
||||
+1
-1
@@ -100,7 +100,7 @@ static const CfgMeta metas[] = {
|
||||
{"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART},
|
||||
{"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART},
|
||||
// shaders and pipelines
|
||||
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART},
|
||||
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART},
|
||||
{"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART},
|
||||
{"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART},
|
||||
// textures and memory
|
||||
|
||||
@@ -49,12 +49,14 @@
|
||||
#define STEP_FIRST_S (1.0 / 60.0)
|
||||
|
||||
static _Atomic uint64_t *step_word; // patch page +0: ratio (low float), seconds (high float)
|
||||
static double step_now = STEP_FIRST_S; // the step the patch page holds: what the game steps each frame
|
||||
static _Atomic uint32_t *step_interval; // patch page +8: seconds * 60 (patches-v143.txt realtime)
|
||||
static uint64_t last_ns;
|
||||
static double samples[STEP_SAMPLES];
|
||||
static unsigned sample_count, sample_next;
|
||||
|
||||
static void step_store(float seconds) {
|
||||
step_now = seconds;
|
||||
float ratio = seconds * 30.0f, interval = seconds * 60.0f;
|
||||
uint32_t lo, hi, iv;
|
||||
memcpy(&lo, &ratio, 4);
|
||||
@@ -217,9 +219,11 @@ static void speed_report(uint64_t now, double step, int interval) {
|
||||
}
|
||||
|
||||
static _Atomic int64_t world_us = -1;
|
||||
static _Atomic uint64_t game_us; // game time stepped since the first present (frame_step_ticks)
|
||||
static uint64_t presents;
|
||||
|
||||
int64_t frame_step_world_us(void) { return atomic_load_explicit(&world_us, memory_order_relaxed); }
|
||||
uint64_t frame_step_ticks(void) { return atomic_load_explicit(&game_us, memory_order_relaxed) * 60 / 1000000; }
|
||||
|
||||
static void world_clock(double dt, double step) {
|
||||
presents++;
|
||||
@@ -253,6 +257,11 @@ void frame_step_present(uint64_t display_ns, int interval) {
|
||||
}
|
||||
}
|
||||
last_ns = display_ns;
|
||||
// The game clock of untimed script lines: what the game stepped this frame, the patch page's step with the
|
||||
// real-time step (kept through a reset), whole vsyncs without it.
|
||||
double game = step_word ? step_now : (interval > 0 ? interval : 1) / 60.0;
|
||||
atomic_store_explicit(&game_us, atomic_load_explicit(&game_us, memory_order_relaxed) + (uint64_t)(game * 1e6 + 0.5),
|
||||
memory_order_relaxed);
|
||||
world_clock(dt, step);
|
||||
if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval);
|
||||
}
|
||||
@@ -249,6 +249,7 @@ uint64_t frame_step_cap_ns(void); // with the real-time
|
||||
double frame_step_limit_hz(void); // the frame rate the limit gives (overlay); 0 unknown
|
||||
int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before
|
||||
// (TOTK_FPS_LIMIT, live)
|
||||
uint64_t frame_step_ticks(void); // game time since the first present in 60 Hz ticks (input scripts)
|
||||
void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and
|
||||
// present interval (60 Hz ticks)
|
||||
// ---- boot (boot.c, main.c)
|
||||
|
||||
+37
-15
@@ -41,10 +41,13 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
|
||||
};
|
||||
|
||||
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]" or "<frame> LSTICK|RSTICK <x> <y> [hold]", ORed into the real input.
|
||||
// A line "@<ms> ..." is timed instead: <ms> and its hold are milliseconds of the world clock (frame_step_world_us, game
|
||||
// time since the loaded save's first frame), so it lands at the same game time at any frame rate and load length. A
|
||||
// timed event shorter than a frame is applied once.
|
||||
// at, hold: presents, or microseconds of the world clock when timed.
|
||||
// <frame> and its hold are 60 Hz ticks of game time since the first present (frame_step_ticks: presents at 60 fps), so
|
||||
// the title menus get the presses at the same game time at any frame limit. A line "@<ms> ..." is timed instead: <ms>
|
||||
// and its hold are milliseconds of the world clock (frame_step_world_us, game time since the loaded save's first
|
||||
// frame), so it lands at the same game time at any frame rate and load length. A timed event shorter than a frame is
|
||||
// applied once. In a script with timed lines (a recording) the untimed ones belong before the world clock: they stop
|
||||
// when it starts, as the load ends at another tick in each run.
|
||||
// at, hold: ticks, or microseconds of the world clock when timed.
|
||||
typedef struct { uint64_t at, hold; int bit, stick, timed; int32_t x, y; int logged; char text[48]; } ScriptEv;
|
||||
static const struct { const char *name; int bit; } script_buttons[] = {
|
||||
{"A", B_A}, {"B", B_B}, {"X", B_X}, {"Y", B_Y}, {"LCLICK", B_STICK_L}, {"RCLICK", B_STICK_R}, {"L", B_L}, {"R", B_R},
|
||||
@@ -52,6 +55,7 @@ static const struct { const char *name; int bit; } script_buttons[] = {
|
||||
};
|
||||
static ScriptEv *script_ev;
|
||||
static int script_n, script_state; // 0 not loaded, 1 loaded
|
||||
static int script_mixed; // the script has timed lines: its untimed ones end where the world clock starts
|
||||
|
||||
static void script_load(void) {
|
||||
script_state = 1;
|
||||
@@ -93,6 +97,7 @@ static void script_load(void) {
|
||||
if (!script_ev) { script_n = 0; fclose(f); return; }
|
||||
}
|
||||
script_ev[script_n++] = e;
|
||||
script_mixed |= timed;
|
||||
}
|
||||
fclose(f);
|
||||
rt_log("input: script %s, %d events", path, script_n);
|
||||
@@ -101,17 +106,17 @@ static void script_load(void) {
|
||||
static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
if (!script_state) script_load();
|
||||
if (!script_n) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), tick = frame_step_ticks();
|
||||
int64_t w = frame_step_world_us();
|
||||
for (int i = 0; i < script_n; i++) {
|
||||
ScriptEv *e = &script_ev[i];
|
||||
if (e->timed && w < 0) continue;
|
||||
uint64_t now = e->timed ? (uint64_t)w : fr;
|
||||
if (e->timed ? w < 0 : script_mixed && w >= 0) continue;
|
||||
uint64_t now = e->timed ? (uint64_t)w : tick;
|
||||
if (now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
|
||||
if (!e->logged) {
|
||||
e->logged = 1;
|
||||
if (e->timed) rt_log("input: script frame %llu at %.1f ms: %s", (unsigned long long)fr, w / 1000.0, e->text);
|
||||
else rt_log("input: script frame %llu: %s", (unsigned long long)fr, e->text);
|
||||
else rt_log("input: script frame %llu tick %llu: %s", (unsigned long long)fr, (unsigned long long)tick, e->text);
|
||||
}
|
||||
if (e->stick) { int32_t *s = e->stick == 1 ? sl : sr; s[0] = e->x; s[1] = e->y; }
|
||||
else *b |= 1ull << e->bit;
|
||||
@@ -120,17 +125,19 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
|
||||
// TOTK_INPUT_RECORD=<file>: writes the merged input as TOTK_INPUT_SCRIPT lines, one per button press and per stick
|
||||
// segment, so a played session replays as a script. A stick segment ends when an axis moves by more than REC_STICK_STEP.
|
||||
// An event that begins on the world clock is written timed ("@<ms>", hold in ms), one before it in presents.
|
||||
// An event that begins on the world clock is written timed ("@<ms>", hold in ms), one before it in ticks
|
||||
// (frame_step_ticks). When the world clock starts, held buttons and sticks are cut there and go on as timed events.
|
||||
enum { REC_STICK_STEP = 2048 };
|
||||
static FILE *rec;
|
||||
static int rec_state; // 0 not opened, 1 recording, -1 off
|
||||
static uint64_t rec_down[16]; // frame + 1 at which each button went down, 0 while up
|
||||
static uint64_t rec_down[16]; // tick + 1 at which each button went down, 0 while up
|
||||
static int64_t rec_down_w[16]; // the world clock then, -1 before it started
|
||||
static uint64_t rec_stick_from[2]; // frame + 1 at which the current non-zero stick segment began
|
||||
static uint64_t rec_stick_from[2]; // tick + 1 at which the current non-zero stick segment began
|
||||
static int64_t rec_stick_w[2];
|
||||
static int32_t rec_stick[2][2];
|
||||
static int64_t rec_last_w = -1; // the world clock at the previous apply
|
||||
|
||||
// "<frame> <what> <hold>" or "@<ms> <what> <hold ms>": from frame from and world clock from_w to now.
|
||||
// "<tick> <what> <hold>" or "@<ms> <what> <hold ms>": from tick from and world clock from_w to now.
|
||||
static void rec_line(uint64_t from, int64_t from_w, uint64_t fr, int64_t w, const char *what) {
|
||||
if (from_w >= 0 && w >= from_w)
|
||||
fprintf(rec, "@%.1f %s %.1f\n", from_w / 1000.0, what, (w > from_w ? w - from_w : 1000) / 1000.0);
|
||||
@@ -148,7 +155,7 @@ static void rec_close_stick(int s, uint64_t fr, int64_t w) {
|
||||
|
||||
static void rec_flush_all(void) {
|
||||
if (rec_state != 1) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1;
|
||||
uint64_t fr = frame_step_ticks() + 1;
|
||||
int64_t w = frame_step_world_us();
|
||||
for (int i = 0; i < 16; i++)
|
||||
if (rec_down[i]) { rec_line(rec_down[i] - 1, rec_down_w[i], fr, w, script_buttons[i].name); rec_down[i] = 0; }
|
||||
@@ -163,13 +170,28 @@ static void rec_apply(uint64_t b, const int32_t sl[2], const int32_t sr[2]) {
|
||||
rec = path && *path ? fopen(path, "w") : NULL;
|
||||
rec_state = rec ? 1 : -1;
|
||||
if (path && *path && !rec) rt_log("input: cannot open TOTK_INPUT_RECORD %s", path);
|
||||
if (rec) { fprintf(rec, "# Recorded input (TOTK_INPUT_RECORD); frame = presents, @ms = world clock.\n"); atexit(rec_flush_all);
|
||||
if (rec) { fprintf(rec, "# Recorded input (TOTK_INPUT_RECORD); frame = 60 Hz ticks of game time, @ms = world clock.\n"); atexit(rec_flush_all);
|
||||
rt_log("input: recording to %s", path); }
|
||||
}
|
||||
if (rec_state != 1) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
uint64_t fr = frame_step_ticks();
|
||||
int64_t w = frame_step_world_us();
|
||||
int wrote = 0;
|
||||
if (w >= 0 && rec_last_w < 0) { // the world clock starts: what is held goes on as timed events from @0
|
||||
for (int i = 0; i < 16; i++)
|
||||
if (rec_down[i]) {
|
||||
rec_line(rec_down[i] - 1, -1, fr, -1, script_buttons[i].name);
|
||||
rec_down[i] = fr + 1, rec_down_w[i] = w;
|
||||
wrote = 1;
|
||||
}
|
||||
for (int s = 0; s < 2; s++)
|
||||
if (rec_stick_from[s]) {
|
||||
rec_close_stick(s, fr, -1);
|
||||
rec_stick_from[s] = fr + 1, rec_stick_w[s] = w;
|
||||
wrote = 1;
|
||||
}
|
||||
}
|
||||
rec_last_w = w;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int bit = script_buttons[i].bit, down = (b >> bit & 1) != 0;
|
||||
if (down && !rec_down[i]) rec_down[i] = fr + 1, rec_down_w[i] = w;
|
||||
|
||||
@@ -1741,10 +1741,13 @@ static void bind_vertex_buffers(VkExec *ex) {
|
||||
// TOTK_PIPE_SKIP: which draws are skipped, rather than waited for, while their shaders or pipeline build.
|
||||
// strict (0): only draws whose pass targets were all rendered in the previous presented frame; a pass with a target
|
||||
// that was not counts as one-shot and waits.
|
||||
// any (1, default): also draws in a pass with at least one target rendered in the previous frame (a per-frame depth
|
||||
// any (1): also draws in a pass with at least one target rendered in the previous frame (a per-frame depth
|
||||
// buffer or g-buffer with a colour target the frame before did not use: half-resolution effect buffers in fights).
|
||||
// Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait.
|
||||
// fullscreen (2): as any, and full-screen draws into such passes are skipped instead of waiting from their budget.
|
||||
// fullscreen (2, default): as any, and full-screen draws into such passes are skipped instead of waiting from their
|
||||
// budget. A full-screen pass whose stage libraries are cold in the driver cache took 86 ms on average (up to 194 ms)
|
||||
// in a fight on an RX 9070: three frames in a row each spent the 50 ms budget, and some of the draws were skipped
|
||||
// after all. Skipping costs the pass for the frames until the pipeline is ready.
|
||||
static int pipe_skip_mode(void) {
|
||||
static int mode = -1;
|
||||
if (mode < 0) {
|
||||
|
||||
@@ -1672,12 +1672,39 @@ VkCommandBuffer vk_upload_begin(void) {
|
||||
|
||||
void vk_upload_end(void) { LeaveCriticalSection(&cmd_lock); }
|
||||
|
||||
// Staging memory comes from a pool of its own that keeps STG_MIN_BLOCKS blocks from its first use (at boot). In the
|
||||
// default pools VMA creates a block under the lock of its memory type (VmaBlockVector::Allocate), so a block made by
|
||||
// the headroom thread (vk_headroom.c) or a burst of uploads held every staging allocation of the render thread: one
|
||||
// took 47-50 ms, for a few bytes, in the first frame after a save load. Larger buffers than a block go to the default
|
||||
// pool.
|
||||
#define STG_BLOCK (64ull << 20)
|
||||
#define STG_MIN_BLOCKS 2
|
||||
static VmaPool stg_pool;
|
||||
static int stg_pool_tried;
|
||||
|
||||
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,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT};
|
||||
VmaAllocationCreateInfo aci = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT};
|
||||
if (!__atomic_load_n(&stg_pool_tried, __ATOMIC_ACQUIRE)) {
|
||||
InitOnceExecuteOnce(&cmd_once, cmd_init, NULL, NULL);
|
||||
EnterCriticalSection(&cmd_lock);
|
||||
if (!stg_pool_tried) {
|
||||
uint32_t type;
|
||||
VmaPoolCreateInfo pi = {.blockSize = STG_BLOCK, .minBlockCount = STG_MIN_BLOCKS};
|
||||
if (vmaFindMemoryTypeIndexForBufferInfo(c->vma, &bi, &aci, &type) == VK_SUCCESS) {
|
||||
pi.memoryTypeIndex = type;
|
||||
if (vmaCreatePool(c->vma, &pi, &stg_pool) != VK_SUCCESS) stg_pool = NULL;
|
||||
}
|
||||
rt_log("vk: staging pool %s (%d blocks of %llu MiB kept)", stg_pool ? "created" : "unavailable: default pool",
|
||||
STG_MIN_BLOCKS, (unsigned long long)(STG_BLOCK >> 20));
|
||||
__atomic_store_n(&stg_pool_tried, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
LeaveCriticalSection(&cmd_lock);
|
||||
}
|
||||
if (stg_pool && bi.size <= STG_BLOCK / 2) aci.pool = stg_pool;
|
||||
VmaAllocationInfo info;
|
||||
VkBuffer b;
|
||||
VmaAllocation a;
|
||||
@@ -1797,6 +1824,9 @@ void vk_mem_shutdown(void) {
|
||||
nspare = 0;
|
||||
if (cmd_pool) vkDestroyCommandPool(c->device, cmd_pool, NULL);
|
||||
cmd_pool = VK_NULL_HANDLE;
|
||||
if (stg_pool) vmaDestroyPool(c->vma, stg_pool); // after retire: no staging buffer is left in it
|
||||
stg_pool = NULL;
|
||||
stg_pool_tried = 0;
|
||||
LeaveCriticalSection(&cmd_lock);
|
||||
AcquireSRWLockExclusive(&pool_lock);
|
||||
for (int i = 0; i < pool_index.n; i++) destroy_rec(c, pool_index.e[i].buf);
|
||||
|
||||
+29
-13
@@ -56,6 +56,7 @@ typedef struct Tex {
|
||||
// storage binding); ReadTexels waits for it instead of draining the replay queue
|
||||
uint8_t listed; // in owned[]
|
||||
uint32_t wt_n; // WriteTexels into the storage since the last upload
|
||||
struct Tex *rel_prev, *rel_next; // while released: the list of released entries, oldest first
|
||||
} Tex;
|
||||
|
||||
// A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the
|
||||
@@ -84,6 +85,7 @@ static SampSlot *sslots;
|
||||
static int nsslots;
|
||||
static nvn_hmap samplers = {.klen = sizeof(samp_par)};
|
||||
static int nreleased;
|
||||
static Tex *rel_head, *rel_tail; // released entries in release order (sweep_released drops from the head)
|
||||
static uint64_t n_uploads, n_reuploads;
|
||||
// Re-uploads since the last perf line (tex_lock): all, and those of textures a WriteTexels wrote since their last upload.
|
||||
static uint64_t ru_n, ru_bytes, ru_ns, ru_wt_n, ru_wt_bytes, ru_wt_ns;
|
||||
@@ -429,6 +431,22 @@ static void own_remove(Tex *e) {
|
||||
ReleaseSRWLockExclusive(&owned_lock);
|
||||
}
|
||||
|
||||
static void rel_append(Tex *e) {
|
||||
e->rel_next = NULL;
|
||||
e->rel_prev = rel_tail;
|
||||
if (rel_tail) rel_tail->rel_next = e;
|
||||
else rel_head = e;
|
||||
rel_tail = e;
|
||||
}
|
||||
|
||||
static void rel_unlink(Tex *e) {
|
||||
if (e->rel_prev) e->rel_prev->rel_next = e->rel_next;
|
||||
else rel_head = e->rel_next;
|
||||
if (e->rel_next) e->rel_next->rel_prev = e->rel_prev;
|
||||
else rel_tail = e->rel_prev;
|
||||
e->rel_prev = e->rel_next = NULL;
|
||||
}
|
||||
|
||||
static void destroy_tex(Tex *e) {
|
||||
own_remove(e);
|
||||
tex_gen_bump(e->key.addr);
|
||||
@@ -441,24 +459,20 @@ static void destroy_tex(Tex *e) {
|
||||
for (Tex *p = head; p; p = p->next_at)
|
||||
if (p->next_at == e) { p->next_at = e->next_at; break; }
|
||||
}
|
||||
if (e->released) nreleased--;
|
||||
if (e->released) nreleased--, rel_unlink(e);
|
||||
vk_exec_image_gone(e->pub.image);
|
||||
vk_defer_image(e->pub.image, e->alloc);
|
||||
free(e);
|
||||
}
|
||||
|
||||
// Released entries with another descriptor at the same address are replaced; too many released entries in all are
|
||||
// dropped (they come back on demand, mirrored ones re-uploaded from their storage).
|
||||
// Released entries with another descriptor at the same address are replaced; past RELEASED_MAX released entries the
|
||||
// oldest one goes at each release (it comes back on demand, a mirrored one re-uploaded from its storage). TotK
|
||||
// finalizes and initializes transient textures every frame, so a released entry is mostly taken back within frames.
|
||||
// Dropping all of them at once when the count passed 4096 destroyed thousands of images in one frame: 333-422 ms
|
||||
// frames every 40-50 s on a GTX 1050 Ti, then the textures still in use were created and uploaded again.
|
||||
#define RELEASED_MAX 2048
|
||||
static void sweep_released(void) {
|
||||
if (nreleased <= 4096) return;
|
||||
Tex **list = malloc(sizeof *list * (size_t)by_key.n);
|
||||
int n = 0;
|
||||
for (uint32_t i = 0; i < by_key.cap; i++) {
|
||||
Tex *e = by_key.vals[i];
|
||||
if (e && e->released) list[n++] = e;
|
||||
}
|
||||
for (int i = 0; i < n; i++) destroy_tex(list[i]);
|
||||
free(list);
|
||||
while (nreleased > RELEASED_MAX && rel_head) destroy_tex(rel_head);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- uploads
|
||||
@@ -800,7 +814,7 @@ static Tex *entry_of(const tex_t *t) {
|
||||
nvn_hm_put(&by_key, &k, e);
|
||||
tex_gen_bump(k.addr);
|
||||
}
|
||||
if (e->released) e->released = 0, nreleased--, tex_gen_bump(k.addr);
|
||||
if (e->released) e->released = 0, nreleased--, rel_unlink(e), tex_gen_bump(k.addr);
|
||||
e->t = *t;
|
||||
return e;
|
||||
}
|
||||
@@ -862,6 +876,7 @@ void vk_texture_release(const tex_t *t) {
|
||||
if (e && !e->released) {
|
||||
e->released = 1;
|
||||
nreleased++;
|
||||
rel_append(e);
|
||||
tex_gen_bump(k.addr);
|
||||
sweep_released();
|
||||
}
|
||||
@@ -1612,6 +1627,7 @@ void vk_texture_shutdown(void) {
|
||||
samplers = (nvn_hmap){.klen = sizeof(samp_par)};
|
||||
free(tslots), free(sslots);
|
||||
tslots = NULL, sslots = NULL, ntslots = nsslots = 0, nreleased = 0;
|
||||
rel_head = rel_tail = NULL;
|
||||
if (sinfo) memset(sinfo, 0, sizeof *sinfo << 20); // no slot is registered: tokens 0
|
||||
unlock();
|
||||
}
|
||||
Reference in new issue
Block a user