Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F

## Found on this game

- [A shader build's Python finishes one shader at a time](../../../docs/reference/shaders.md#a-shader-builds-python-finishes-one-shader-at-a-time): resolved, the whole Mac map's cold build on an M5 Max took 50 minutes with half the cores idle; 22 minutes as worker processes.
- [The 60 Hz link at 40 fps in fullscreen on a ProMotion screen](../../../docs/reference/frame-rate.md#the-60-hz-link-at-40-fps-in-fullscreen-on-a-promotion-screen): 40 fps in fullscreen on an M5 Pro MacBook Pro with the GPU at 10 ms of the 16.7, every present one 120 Hz refresh late and every other display-link tick skipped; the screen's fixed 60 Hz mode held 60.0 fps there and fullscreen now switches to it, with the 120 Hz link (59 fps) for screens with no such mode.
- [The shader package and the shader map come from different splits](../../../docs/reference/packing.md#the-shader-package-and-the-shader-map-come-from-different-splits): resolved, a black screen with sound in a build through NX2Apple ([issue #1841](https://github.com/seedds/nxnative/issues/1841)); the present pass was translated from a terrain shader's record. Records now carry their keys, and the shader build finds them by key.
- [Native mesh pipelines compiled at their first draw](../../../docs/reference/shaders.md#native-mesh-pipelines-compiled-at-their-first-draw): resolved. The foliage, shadows and enemies drawn with task and mesh shaders stalled at their first draw on a cold Metal cache (59.5 s of builds at the Kokiri Forest entry on an M5 Max, one frame of 42.9 s), as nothing recorded or kept their pipelines; `pso_manifest.tsv` now carries 123 `M` rows (Kokiri Forest, Hyrule Field, Castle Town, Hyrule Castle), and with the prepare step they are built before the first frame. Pipelines outside the list still stall once, then go into `pso_user.tsv`.
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/nvn/dmsh_chain.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "39eb2f781d07"
#define DMSH_CHAIN "a2727a3090bc"
124 changes: 99 additions & 25 deletions dm/gfx/tools/dmsh_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,16 @@ def compile_targets(): # the selected targets from the same MSL (MSL identical
keys = build_keys(want, loc, PAIRS)
if want: want = set(keys) # dependency outputs are cleaned too, so a failed pixel build cannot leave old MSL for padding
NJ = int(os.environ.get('J', '8'))
BATCH = 4 * NJ # shaders translated and finished together: the next batch is translated while this one is finished, and a shader's working files go when it is (fin).
# A build runs as several processes. Each shader needs about 40 ms of this file's own Python (the rewrites, its outputs, the cache), which one process runs one shader at a time
# whatever J is: on Ocarina of Time's cold build on an 18-core Mac that Python was busy the whole build while the cores were half idle. PROCS copies of this run (workers, DMSH_SHARD:
# their index, their count, the phase and the file of their results) each build a share of a phase's shaders with their own threads and stage the outputs in the same cache under this
# folder's name, as one process would; this run then reads their results and writes the library as before. DMSH_PROCS sets the count (1: no workers)
SHARD = os.environ.get('DMSH_SHARD')
PROCS = 1 if SHARD else max(1, int(os.environ.get('DMSH_PROCS') or -(-NJ // 3))) # one process per 3 of J: 6 for J=18 measured best (193 s → 101 s on a 1,714-shader sample)
SHARD_OUT = ''
if SHARD:
_si, _sn, _sph, SHARD_OUT = SHARD.split('/', 3)
keys = [k for k in keys if (k in PAIRS) == (_sph == '2')][int(_si)::int(_sn)]
# Minecraft Dungeons II, 15,826 shaders: at most 3,279 files under the build folder with batches of 256 removed whole, 68,230 before batches (Octopath Traveler 0's stopped build)
# A full build starts from no shader outputs, a partial one from none of the keys it builds, so what a run leaves is what it built: every output is restored from the cache anyway.
# The chain version is the same whatever the profile's tables, so a key that left the map, a shader that no longer builds or a variant no longer made would keep an old library.
Expand All @@ -1764,6 +1773,9 @@ def compile_targets(): # the selected targets from the same MSL (MSL identical
OLD_IMAGE = {}
for _label, _d in (('mac', out), ('ios', IOSOUT)):
if not _d: continue
if SHARD: # the build that started this worker cleared the folder
if want and os.path.isfile(shader_lib.image_of(_d)): OLD_IMAGE[_label] = romfs.Path(shader_lib.image_of(_d))
continue
if not want:
shutil.rmtree(_d, ignore_errors=True); shader_lib.remove_image(_d)
elif os.path.isfile(shader_lib.image_of(_d)): OLD_IMAGE[_label] = romfs.Path(shader_lib.image_of(_d))
Expand All @@ -1772,7 +1784,7 @@ def compile_targets(): # the selected targets from the same MSL (MSL identical
_m = OUT_KEY.match(_n)
if _m and _m.group(1) in want: os.unlink(os.path.join(_d, _n))
if STAMP_BEFORE != INPUTS and os.path.exists(os.path.join(_d, dmsh_chain.STAMP)): os.unlink(os.path.join(_d, dmsh_chain.STAMP))
STORE.delete_prefix(STAGE + '/') # what a stopped build of this folder staged
if not SHARD: STORE.delete_prefix(STAGE + '/') # what a stopped build of this folder staged

def sweep_work(root):
"""The working folders (dmsh_*) a build left that is no longer running: each holds the process ID of its build (pid), one from before that is taken after a day.
Expand All @@ -1794,7 +1806,7 @@ def sweep_work(root):
with open(os.path.join(w, 'pid'), 'w') as _f: _f.write(str(os.getpid()))
MTL = dmsh_mtlcache.Cache(STORE, {'mac': ('macosx', MAC_RECORD, mac_compile), **({'ios': ('iphoneos', ios_metal.RECORD, ios_metal.compile)} if IOSOUT else {})}, w)
# first take the libraries earlier chain versions left on disk: the b-*/ and b-*.img beside the output (iOS: the folder of the same name) and the roots DMSH_PREFILL lists
for _spec in [cache_root + ('=' + os.path.dirname(IOSOUT) if IOSOUT else '')] + [x for x in os.environ.get('DMSH_PREFILL', '').split(os.pathsep) if x]:
for _spec in [] if SHARD else [cache_root + ('=' + os.path.dirname(IOSOUT) if IOSOUT else '')] + [x for x in os.environ.get('DMSH_PREFILL', '').split(os.pathsep) if x]:
_mac, _, _ios = _spec.partition('=')
for _b in sorted(os.listdir(_mac)) if os.path.isdir(_mac) else []:
if _b.startswith('b-') and os.path.realpath(shader_lib.folder_of(os.path.join(_mac, _b))) != os.path.realpath(out):
Expand Down Expand Up @@ -1837,7 +1849,7 @@ def plain_digest(k): # a paired pixel shader's key, also when it is not built
return shader_key(k, *rec_of(k)) if k in loc else 'missing'
for key in keys:
if PAIRS.get(key): digests[key] = pair_keyed(digests[key], [plain_digest(p) for p in sorted(PAIRS[key])])
for i, key in enumerate(keys):
for i, key in enumerate(keys if PROCS == 1 else ()): # with workers, each looks up its own share
name = loc[key][0]
rule_fps[key] = hashlib.sha256(shader_rule_source(name) + ','.join(sorted(PAIRS.get(key, ()))).encode()).hexdigest() # the vertex shader must be rebuilt when its pairs change (vs_pad)
hit, cache_reasons[key] = cache_lookup(key, digests[key], rule_fps[key], CODE_FP)
Expand All @@ -1849,17 +1861,6 @@ def xlate_errors(r):
for ln in r.stderr.splitlines():
m = re.match(r'\[RED\] (\w+): (.*)', ln)
if m: xerr[m.group(1)] = m.group(2)[:160]
def translate(batch):
"""A batch's misses translated into a working folder of its own (removed once the batch is finished) → that folder"""
wb = tempfile.mkdtemp(prefix='batch_', dir=w)
miss = [k for k in batch if k not in cache_hits]
for k in miss: jobs[k], c1s[k] = job_for(k, loc[k][0], rec_of(k)[1], wb)
def xl(ch):
jp = os.path.join(wb, f'job_{abs(hash(ch[0]))}.json'); json.dump([jobs[k] for k in ch], open(jp, 'w'))
return subprocess.run([XLATE, jp, wb], capture_output=True, text=True)
with cf.ThreadPoolExecutor(NJ) as ex:
for r in ex.map(xl, [c for c in (miss[i::NJ] for i in range(NJ)) if c]): xlate_errors(r)
return wb
def restored(key, wb):
"""A cache hit's outputs back on disk while it is finished. When another build pruned them since the look (cache_lookup), the shader is translated now, in the batch's folder: False"""
if restore_cache(key, digests[key]): return True
Expand Down Expand Up @@ -1888,17 +1889,90 @@ def fin(key, wb):
if e.name.startswith((key, 'job1_' + key)) and e.is_file(): os.unlink(e.path)
with _prog['lock']: DONE['n'] += 1; n = DONE['n']
progress('compiling', n, len(keys), cached=len(cache_hits), misses=NMISS)
# Two phases: paired vertex shaders read their pixel shaders' MSL (vs_pad), so they go last, in batches of their own. The next batch is translated while this one is finished
batches = [b[i:i + BATCH] for b in ([k for k in keys if k not in PAIRS], [k for k in keys if k in PAIRS]) for i in range(0, len(b), BATCH)]
# Two phases: paired vertex shaders read their pixel shaders' MSL (vs_pad), so they go last, after every other shader is finished. Within a phase nothing waits for a batch:
# the misses are translated in chunks of CHUNK, largest record first, and each shader is finished as soon as its own chunk is translated. Batches finished together used to
# hold every worker until the batch's slowest shader was done (Ocarina of Time's full build left the CPU about half idle). A chunk's working folder goes when its last shader
# is finished, and at most WINDOW chunks are translated but not yet finished, which bounds the files under the build folder as the batches did
def translate_chunk(ch):
"""A chunk of misses translated by one translator run into a working folder of its own (removed once its last shader is finished) → that folder"""
wb = tempfile.mkdtemp(prefix='chunk_', dir=w)
for k in ch: jobs[k], c1s[k] = job_for(k, loc[k][0], rec_of(k)[1], wb)
jp = os.path.join(wb, 'job.json'); json.dump([jobs[k] for k in ch], open(jp, 'w'))
xlate_errors(subprocess.run([XLATE, jp, wb], capture_output=True, text=True))
return wb
CHUNK = int(os.environ.get('DMSH_CHUNK', '8'))
WINDOW = 2 * NJ
order = {k: (k in PAIRS, i) for i, k in enumerate(keys)} # results in the order the phases run, as the batches gave them
def run_phase(phase):
hits = [k for k in phase if k in cache_hits]
miss = sorted((k for k in phase if k not in cache_hits), key=lambda k: -len(rec_of(k)[1]))
chunks = [miss[i:i + CHUNK] for i in range(0, len(miss), CHUNK)]
out_res, left, lock, room = [], {}, threading.Lock(), threading.Semaphore(WINDOW)
def done_one(wb):
with lock:
left[wb] -= 1; last = not left[wb]
if last: shutil.rmtree(wb, ignore_errors=True); room.release()
with cf.ThreadPoolExecutor(NJ) as tx, cf.ThreadPoolExecutor(NJ) as ex:
fins, errs, chunk_of = [], [], {}
def fin_in(k, wb):
try: return fin(k, wb)
finally: done_one(wb)
def start(wb, ks): # a folder's shaders, handed to the finishing workers
with lock:
left[wb] = len(ks); fins.extend(ex.submit(fin_in, k, wb) for k in ks)
def translated(f): # in the translating worker, once its chunk is translated
try: start(f.result(), chunk_of[f])
except BaseException as e: errs.append(e); room.release()
if hits:
room.acquire(); start(tempfile.mkdtemp(prefix='hits_', dir=w), hits)
for ch in chunks:
room.acquire() # released when a chunk's last shader is finished
if errs: break
f = tx.submit(translate_chunk, ch)
with lock: chunk_of[f] = ch
f.add_done_callback(translated) # after chunk_of has it: a chunk translated already runs its callback here
tx.shutdown(wait=True)
if errs: raise errs[0]
with lock: todo = list(fins)
out_res = [f.result() for f in todo]
return out_res
STAT_SETS = {'pre': PRE_STATS, 'vin': VIN_STATS, 'pull': PULL_STATS, 'side': SIDE_STATS, 'bf': BF_STATS}
def run_procs():
"""The phases as PROCS workers each → the results of every shader, with the workers' counts added to this run's"""
out_res = []
for ph in ('1', '2'):
n = min(PROCS, sum(1 for k in keys if (k in PAIRS) == (ph == '2')))
if not n: continue
base, procs = len(out_res), []
for i in range(n):
rj = os.path.join(w, f'worker_{ph}_{i}.json')
env = dict(os.environ, DMSH_SHARD=f'{i}/{n}/{ph}/{rj}', J=str(-(-3 * NJ // (2 * n))), **({'DMSH_PROGRESS': rj + '.progress'} if PROGRESS else {}))
procs.append((subprocess.Popen(sys.orig_argv, env=env), rj))
while any(p.poll() is None for p, _ in procs): # the workers' progress as one
done = 0
for _, rj in procs:
try:
with open(rj + '.progress') as f: done += json.load(f).get('done', 0)
except (OSError, ValueError): pass
progress('compiling', base + done, len(keys)); time.sleep(0.5)
for p, rj in procs:
if p.returncode: sys.exit(f'[RED] shader build worker {rj} stopped ({p.returncode}); its messages are above')
with open(rj) as f: r = json.load(f)
out_res += [tuple(x) for x in r['res']]
for name, c in r['stats'].items(): STAT_SETS[name].update(c)
for what, n_ in r['mtl']: MTL.count(tuple(what) if isinstance(what, list) else what, n_)
return out_res
res = []
with cf.ThreadPoolExecutor(1) as tx, cf.ThreadPoolExecutor(NJ) as ex:
nxt = tx.submit(translate, batches[0]) if batches else None
for i, batch in enumerate(batches):
assert nxt is not None
wb = nxt.result()
nxt = tx.submit(translate, batches[i + 1]) if i + 1 < len(batches) else None
res += list(ex.map(lambda k, wb=wb: fin(k, wb), batch))
shutil.rmtree(wb, ignore_errors=True)
if PROCS > 1: res = run_procs()
else:
for phase in ([k for k in keys if k not in PAIRS], [k for k in keys if k in PAIRS]): res += run_phase(phase)
res.sort(key=lambda r: order[r[0]])
if SHARD: # a worker: its results for the run that started it, which writes the library
with open(SHARD_OUT + '.tmp', 'w') as f:
json.dump({'res': res, 'stats': {k: dict(v) for k, v in STAT_SETS.items()}, 'mtl': [[list(k) if isinstance(k, tuple) else k, n] for k, n in MTL.stats.items()]}, f, default=list)
os.replace(SHARD_OUT + '.tmp', SHARD_OUT)
shutil.rmtree(w, ignore_errors=True)
sys.exit(0)
# Passthrough geometry as mesh shaders (dmsh_mesh.py): <vertex key>_<geometry key>_m3 for each pair in the profile's geo_mesh.tsv whose two libraries exist; the host
# draws a lean triangle-list geometry draw with it when its pair has one, and through the compute path otherwise (nvn_geometry.inc geo_mesh_draw)
MESH_STATS = collections.Counter()
Expand Down
13 changes: 8 additions & 5 deletions dm/tools/tests/test_shader_toolchain_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
import tempfile
import threading
import time
import types
from typing import Any
import unittest
Expand Down Expand Up @@ -230,12 +231,14 @@ def finish_key(key):
self.ns['publish_cache'](key, key + '-digest', 'rule', 'source')
return key, 'ok', '', refl

# the batches, then the loop that translates and finishes them (the translator and fin substituted)
start = next(i for i, n in enumerate(self.tree.body) if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == 'batches' for t in n.targets))
end = next(i for i, n in enumerate(self.tree.body) if i > start and isinstance(n, ast.With)) + 1
# the phases, the chunks and the loop that translates and finishes them, in this process (the translator and fin substituted)
start = next(i for i, n in enumerate(self.tree.body) if isinstance(n, ast.Assign) and any(isinstance(t, ast.Name) and t.id == 'CHUNK' for t in n.targets))
end = next(i for i, n in enumerate(self.tree.body) if i > start and isinstance(n, ast.Expr) and 'res.sort' in ast.unparse(n)) + 1
work = Path(self.tmp.name) / 'work'
def translate(batch): work.mkdir(exist_ok=True); return str(work)
self.ns.update(cf=cf, NJ=1, BATCH=1, keys=keys, fin=lambda key, wb: finish_key(key), translate=translate)
def translate_chunk(chunk): work.mkdir(exist_ok=True); return str(work)
self.ns.update(cf=cf, NJ=1, keys=keys, fin=lambda key, wb: finish_key(key), translate_chunk=translate_chunk, tempfile=tempfile, w=self.tmp.name,
PROCS=1, cache_hits=set(), rec_of=lambda key: ('package', b''), collections=collections, PROGRESS=None, MTL=None, sys=sys, time=time,
**{n: collections.Counter() for n in ('PRE_STATS', 'VIN_STATS', 'PULL_STATS', 'SIDE_STATS', 'BF_STATS')})
exec(compile(ast.Module(body=self.tree.body[start:end], type_ignores=[]), str(TOOLS / 'dmsh_build.py'), 'exec'), self.ns)
self.assertEqual(built, [pixel, vertex])
self.assertIn('dm_pad7 [[user(locn7)]]', (out / (vertex + '_b.metal')).read_text())
Expand Down
Loading
Loading