mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 11:29:58 -04:00
Read shaders from the disk cache off the render thread past a small budget, and pack the cache on carried updates.
A profiled traversal session had 9 of its 16 worst hitches (100 to 500 ms) inside the render thread's shader lookup, reading <key>.res.json and <key>.spv of shaders met for the first time: opening a loose file costs about 3 ms the first time on Windows. get_async_one now reads in place while those reads stay within 1/8 of the wall time (2 ms at once); past that three loader threads read them and the draws wait as for a translation. The perf line counts both. The launcher's updates carry the shaders over and never packed the cache, which held 44,000 loose translations in the test install. An update now packs it once it holds 1,000 loose ones, and the pack tool reads the loose files on 16 threads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1363,6 +1363,8 @@ def step_shaders(out, update=False):
|
||||
break
|
||||
if update and shader_carry(out):
|
||||
say(' the shaders prepared before this update stay in use; the game translates them again in the background')
|
||||
if loose_shaders(out) >= PACK_LOOSE_MIN:
|
||||
pack_shader_cache(out)
|
||||
return
|
||||
cmd = [sys.executable, '-u', str(KIT / 'tools' / 'prepare_shaders.py'), str(out), '--romfs', str(out / 'romfs'),
|
||||
'--exefs', str(out / 'exefs'), '--no-list', '--exe', EXE_NAME]
|
||||
@@ -1415,6 +1417,21 @@ def step_shaders(out, update=False):
|
||||
pack_shader_cache(out)
|
||||
|
||||
|
||||
# An update that carries the shaders over packs the cache when it has this many loose translations: the game writes
|
||||
# each translation it makes in play, and those of its background pass after a translator change (about 42,000), as loose
|
||||
# files, and reading one the first time cost 5.6 ms on Windows against a fraction of a millisecond from the pack.
|
||||
PACK_LOOSE_MIN = 1000
|
||||
|
||||
|
||||
def loose_shaders(out):
|
||||
"""The loose translations (<key>.spv) in the game's shader cache."""
|
||||
try:
|
||||
with os.scandir(out / 'shader-cache') as it:
|
||||
return sum(1 for e in it if e.name.endswith('.spv'))
|
||||
except OSError:
|
||||
return 0
|
||||
|
||||
|
||||
def pack_shader_cache(out):
|
||||
"""The prepared shader cache folded into shader-cache/shader-cache.pack (tools/pack_shader_cache.py): the
|
||||
preparation leaves four files per shader, about 175,000 for the whole game, which the game looks up one by one at
|
||||
|
||||
@@ -19,6 +19,8 @@ import os
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
from collections import deque
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
MAGIC = b'TKSPACK1'
|
||||
@@ -26,6 +28,11 @@ PACK = 'shader-cache.pack'
|
||||
KINDS = {'.spv': 1, '.res.json': 2, '.use': 3, '.in': 4}
|
||||
NAME = re.compile(r'([0-9a-f]{64})(\.spv|\.res\.json|\.use|\.in)')
|
||||
USE_SIZE = 20 # gfx_shader.c use_store: magic, version, three masks
|
||||
# Loose files are read on READERS threads, at most WINDOW ahead of the one being written. On Windows the time goes into
|
||||
# opening each file (about 3 ms the first time, against 0.04 ms once the system has seen it), which threads overlap:
|
||||
# read one at a time, the 175,000 files of a launcher's cache took minutes.
|
||||
READERS = 16
|
||||
WINDOW = 1024
|
||||
|
||||
|
||||
def loose_files(cache):
|
||||
@@ -74,6 +81,13 @@ def valid(kind, data):
|
||||
return len(data) == USE_SIZE if kind == 3 else len(data) > 0
|
||||
|
||||
|
||||
def read_loose(path):
|
||||
try:
|
||||
return path.read_bytes()
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
|
||||
def pack(cache, keep=False):
|
||||
cache = Path(cache)
|
||||
out = cache / PACK
|
||||
@@ -89,15 +103,23 @@ def pack(cache, keep=False):
|
||||
index, written, skipped, offset = [], [], 0, 16 + 48 * len(keys)
|
||||
old = open(out, 'rb') if packed else None
|
||||
try:
|
||||
with open(tmp, 'wb') as f:
|
||||
with open(tmp, 'wb') as f, ThreadPoolExecutor(READERS) as pool:
|
||||
f.write(bytes(offset)) # header and index, written once the offsets are known
|
||||
for key in keys:
|
||||
ahead, todo = deque(), iter(keys)
|
||||
|
||||
def submit():
|
||||
key = next(todo, None)
|
||||
if key is not None:
|
||||
ahead.append((key, pool.submit(read_loose, loose[key]) if key in loose else None))
|
||||
|
||||
for _ in range(WINDOW):
|
||||
submit()
|
||||
while ahead:
|
||||
key, reading = ahead.popleft()
|
||||
submit()
|
||||
data = None
|
||||
if key in loose: # a valid loose file, else the packed copy, else nothing
|
||||
try:
|
||||
data = loose[key].read_bytes()
|
||||
except OSError:
|
||||
pass
|
||||
if reading: # a valid loose file, else the packed copy, else nothing
|
||||
data = reading.result()
|
||||
if data is not None and valid(key[0], data):
|
||||
written.append(loose[key])
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user