Files
NX2navi/tools/pipeline_list.py
chrg6 918d399d55 Add the shared pipeline list: mesh pipelines players met are built at the shader preparation, before the first start.
The preparation translates every shader and builds each one's stage library, so a pipeline a draw meets for the first time is linked in 2 to 6 ms. A mesh pipeline has no stage libraries: on an install that never drew it, it compiles whole in play, 0.2 to 1.1 s with its draws left out of the picture (18 of them on a first visit of Kokiri Forest). Whole pipelines were built ahead only from the folder's own pipeline-keys.bin.

tools/pipeline_list.py merge folds the pipeline-keys.bin and shader-answers.bin of game folders that were played into one list file: every key record once, with the answers record of each shader it names. A record names its shaders by input id, which depends on the game's code and the answers alone, and holds the game's pipeline state; what depends on the GPU is derived when the pipeline is built, so a record made on one machine is good on another with the same game version and runtime. Four folders on this PC give 5,432 records, 317 of them mesh pipelines (147 KB of keys).

tools/prepare_shaders.py reads the list (tools/data/pipeline_list.bin, data/pipeline_list.bin in a release folder, or --pipeline-list), adds its shader variants to the seed and writes its records as seed-pipeline-keys.bin. nx2navi.exe --prepare-shaders builds the mesh pipelines among them after the seed is translated and saves the pipeline cache before the stage libraries are built (NX2NAVI_PREPARE_PIPELINES=all builds the vertex pipelines too); --warm-shaders does it again after an update or a new driver, and --prepare-pipelines is that step alone. The pipelines go into the caches and are not kept or added to the folder's own key file, so they cost no start-up time or video memory until a draw meets them. A marker file keeps a driver that crashes on one of them from failing every preparation.

The list is derived from game data and stays out of the repository: check_content.py refuses files recorded from a game folder by their first bytes, and make_kit.py, make_launcher.py and make_release.py carry a list given with --pipeline-list. All three now also carry pipeline_list.py, which prepare_shaders.py imports.

Checked without the game: tests/pipeline_list_test.py, and prepare_shaders.py --no-run on the game files with a list of 317 mesh pipelines (all 266 shader variants found in this game version, 177 new to the seed). The build step in nx2navi.exe compiles and links and has not been run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 14:52:46 -05:00

197 lines
8.8 KiB
Python

"""The shared pipeline list: graphics pipelines players met, for an install to build before play.
A game folder records every graphics pipeline a run builds in pipeline-keys.bin (runtime/vk_pipeline.c "pipeline
keys": the pipeline state with its shaders named by input id) and every shader variant in shader-answers.bin
(runtime/gfx_shader.c "answers met in play": stage, code hash, input id, translator answers). The next start of that
folder builds its recorded pipelines ahead of play; another install, or the same one after its caches were removed,
meets each of them in play first. A mesh pipeline met that way has no stage libraries to be linked from, so its draws
are left out of the picture for its whole compile, 0.2 to 1.1 s.
This tool folds the two files of any number of game folders into one list file. tools/prepare_shaders.py reads the
list beside the shared shader list: the shader variants it names go into the seed, so the preparation translates them,
and its pipeline records go next to the game as seed-pipeline-keys.bin, which `nx2navi.exe --prepare-shaders` (and
`--prepare-pipelines` on a folder that is prepared already) builds once into the pipeline cache and the graphics
driver's own.
What a record depends on. Its shaders are named by input id, a hash of the game's shader code and the answers, which
no translator, GPU or driver changes: a record made on one machine names the same shaders on another with the same
game version. Its state is the game's (formats, blend, depth and stencil, topology, sample count) and a few choices of
the runtime's settings (a culled foliage shadow pass, the depth range); everything that depends on the GPU is derived
from it when the pipeline is built. A record whose state this install never reaches costs one pipeline in the caches,
and one whose shader this game version lacks is left out by the preparation. The record layout belongs to the runtime
that wrote it (KEYS_VERSION and the record size, in the file's header): a list is made again for a runtime that
changes either, and the runtime ignores one that does not match.
The list is derived from game data: it stays out of the repository (tools/check_content.py refuses it) and travels
with the installer archive as data/pipeline_list.bin, as the shader list does.
python tools/pipeline_list.py merge OUT.bin GAME_DIR [GAME_DIR ...] [--mesh-only]
python tools/pipeline_list.py info FILE_OR_GAME_DIR [...]
File: "NX2NPLST", version u32, answers bytes u32, keys bytes u32, then a shader-answers.bin image (header and the
records the keys name, input id kept) and a pipeline-keys.bin image (header and records).
"""
import argparse
import struct
import sys
from pathlib import Path
MAGIC = b'NX2NPLST'
VERSION = 1
ANS_MAGIC, ANS_REC = b'NX2NSANS', 4 + 32 + 32 + 147 + 1 # gfx_shader.c: stage, code hash, input id, answers, pad
KEYS_MAGIC = b'NX2NPKEY'
ID = 32
STAGE_NAMES = {0: 'vertex', 4: 'fragment', 5: 'compute', 6: 'task', 7: 'mesh'} # runtime/gfx.h GFX_STAGE_*
STAGE_MESH = 7
def read_answers(data):
"""shader-answers.bin image -> its records (bytes each); [] for another magic or record size."""
if len(data) < 16 or data[:8] != ANS_MAGIC:
return []
_, rec = struct.unpack_from('<II', data, 8)
if rec != ANS_REC:
return []
n = (len(data) - 16) // rec
return [data[16 + i * rec:16 + (i + 1) * rec] for i in range(n)]
def read_keys(data):
"""pipeline-keys.bin image -> (magic, version, record size, records); records [] for a file too short."""
if len(data) < 16:
return b'', 0, 0, []
magic = data[:8]
version, rec = struct.unpack_from('<II', data, 8)
if rec < 3 * ID + 8:
return magic, version, rec, []
n = (len(data) - 16) // rec
return magic, version, rec, [data[16 + i * rec:16 + (i + 1) * rec] for i in range(n)]
def key_ids(rec):
"""The input ids a key record names: vertex (or mesh), then fragment and task when it has them."""
has_fs, has_ts = struct.unpack_from('<II', rec, 3 * ID)
ids = [rec[:ID]]
if has_fs:
ids.append(rec[ID:2 * ID])
if has_ts:
ids.append(rec[2 * ID:3 * ID])
return ids
def read_dir(d):
"""A game folder's recorded keys and answers; a missing file gives no records."""
d = Path(d)
kp, ap = d / 'pipeline-keys.bin', d / 'shader-answers.bin'
keys = read_keys(kp.read_bytes()) if kp.is_file() else (b'', 0, 0, [])
return keys, read_answers(ap.read_bytes()) if ap.is_file() else []
def merge(dirs, mesh_only=False, log=print):
"""One list from the folders' files: each key record once, and the answers record of every shader they name."""
header, keys, seen, answers = None, [], set(), {}
for d in dirs:
(magic, version, rec, recs), ans = read_dir(d)
if not recs:
log(f'{d}: no pipeline key records')
continue
if header is None:
header = (magic, version, rec)
elif header != (magic, version, rec):
log(f'{d}: its pipeline-keys.bin is of another layout ({magic!r}, version {version}, {rec} bytes a record); '
'left out')
continue
by_id = {a[36:68]: a for a in ans}
added = lacking = 0
for r in recs:
ids = key_ids(r)
if r in seen:
continue
if any(i not in by_id and i not in answers for i in ids):
lacking += 1 # a shader the folder's answers do not name: another install could not translate it
continue
if mesh_only and by_id.get(ids[0], answers.get(ids[0]))[0:4] != struct.pack('<I', STAGE_MESH):
continue
seen.add(r)
keys.append(r)
added += 1
for i in ids:
answers.setdefault(i, by_id.get(i) or answers[i])
log(f'{d}: {len(recs)} key records, {added} new to the list, {lacking} without their shaders\' answers')
if header is None:
return None
return header, keys, list(answers.values())
def pack(header, keys, answers):
magic, version, rec = header
a = ANS_MAGIC + struct.pack('<II', 1, ANS_REC) + b''.join(answers)
k = magic + struct.pack('<II', version, rec) + b''.join(keys)
return MAGIC + struct.pack('<III', VERSION, len(a), len(k)) + a + k
def unpack(data):
"""A list file -> (answers records, pipeline-keys.bin image); None when it is not one."""
if len(data) < 20 or data[:8] != MAGIC:
return None
version, na, nk = struct.unpack_from('<III', data, 8)
if version != VERSION or len(data) != 20 + na + nk:
return None
return read_answers(data[20:20 + na]), data[20 + na:]
def describe(name, keys_image, answers):
magic, version, rec, recs = read_keys(keys_image)
stage = {a[36:68]: struct.unpack_from('<I', a)[0] for a in answers}
mesh = sum(1 for r in recs if stage.get(r[:ID]) == STAGE_MESH)
task = sum(1 for r in recs if struct.unpack_from('<I', r, 3 * ID + 4)[0])
named = {i for r in recs for i in key_ids(r)}
by = {}
for i in named:
s = STAGE_NAMES.get(stage.get(i), 'not in the answers')
by[s] = by.get(s, 0) + 1
print(f'{name}: {len(recs)} pipelines ({mesh} mesh, {task} of them with a task stage), layout {magic.decode("ascii", "replace")} '
f'version {version}, {rec} bytes a record; {len(named)} shaders named: '
+ ', '.join(f'{v} {k}' for k, v in sorted(by.items())))
return len(recs), mesh
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
sub = ap.add_subparsers(dest='cmd', required=True)
m = sub.add_parser('merge', help='fold game folders into a list file')
m.add_argument('out', type=Path)
m.add_argument('dirs', nargs='+', type=Path)
m.add_argument('--mesh-only', action='store_true', help='keep the mesh pipelines only')
i = sub.add_parser('info', help='what a list file or a game folder holds')
i.add_argument('paths', nargs='+', type=Path)
a = ap.parse_args()
if a.cmd == 'info':
for p in a.paths:
if p.is_dir():
(magic, version, rec, recs), ans = read_dir(p)
image = magic + struct.pack('<II', version, rec) + b''.join(recs)
describe(str(p), image, ans)
else:
got = unpack(p.read_bytes())
if got is None:
print(f'{p}: not a pipeline list')
return 1
describe(str(p), got[1], got[0])
return 0
got = merge(a.dirs, a.mesh_only)
if got is None:
print('no pipeline key records in any folder')
return 1
header, keys, answers = got
data = pack(header, keys, answers)
a.out.parent.mkdir(parents=True, exist_ok=True)
a.out.write_bytes(data)
describe(str(a.out), unpack(data)[1], answers)
print(f'{a.out}: {len(data)} bytes')
return 0
if __name__ == '__main__':
sys.exit(main())