diff options
| author | abilliontokens <39147239+abilliontokens@users.noreply.github.com> | 2026-11-23 10:45:04 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 12:45:04 -0600 |
| commit | 4cd82fe3fab31ed913bcb779dba42416ffbb7288 (patch) | |
| tree | 3d79beed6c7369837435e593f9859ee26a435aac /scripts | |
| parent | 9504332f292e54f45c7d3295f73873d955a7c9fc (diff) | |
vs_layout_census: confirm legacy non-NGG VS per PAL hardware stage; measure the 195 ES variants (#6558)
Squash-merged after diff review of PR #6558
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/vs_layout_census.py | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/scripts/vs_layout_census.py b/scripts/vs_layout_census.py new file mode 100644 index 000000000..249a9c74e --- /dev/null +++ b/scripts/vs_layout_census.py | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | """SHADER-33 follow-up: which PAL hardware stage every FSSM payload runs on, and whether the VS input layout holds. | ||
| 2 | |||
| 3 | python -I scripts/vs_layout_census.py <work>/shaders/dxbc [--json OUT] [--limit N] | ||
| 4 | |||
| 5 | Reads <dxbc>/index.json, every tag-0x0D payload of each container (tools/fssm_unpack.unpack_all) and its PAL | ||
| 6 | stage descriptors (tools/pal_stages.stage_descriptors), then the code: the .s listing for the container's last | ||
| 7 | payload (fssm_unpack's default) and, for any earlier payload, a fresh llvm-mc disassembly. Prints counts only: | ||
| 8 | |||
| 9 | - payloads by (API id, PAL hardware id) and the carve index's stage for the same stem | ||
| 10 | - NGG markers per hardware id: `exp prim`, any `s_sendmsg`, `s_sendmsg MSG_GS_ALLOC_REQ` (the NGG | ||
| 11 | allocation), `s_sendmsg` with `GS_OP_*` (legacy geometry-stream output) and `v_mbcnt` | ||
| 12 | - for API-VS payloads (hardware id 4 = the hardware VS; id 2 = the ES variant of a two-payload container): | ||
| 13 | the fetch call, the entry VGPRs read before written (scripts/vs_input_survey.read_before_write) and the | ||
| 14 | v0/v3 families of scripts/vs_input_survey.survey | ||
| 15 | |||
| 16 | The hardware-stage names come from tools/pal_stages.py (an outside project's id order); this census is what | ||
| 17 | checks them against the code: every hardware-id-7 payload carries the NGG markers and no other stage does. | ||
| 18 | Standard library only; llvm-mc only for payloads without a listing. Nothing from the game is printed or | ||
| 19 | written beyond counts. | ||
| 20 | """ | ||
| 21 | import argparse | ||
| 22 | import json | ||
| 23 | import os | ||
| 24 | import re | ||
| 25 | import sys | ||
| 26 | from collections import Counter, defaultdict | ||
| 27 | |||
| 28 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 29 | sys.path.insert(0, os.path.join(ROOT, "tools")) | ||
| 30 | sys.path.insert(0, os.path.join(ROOT, "scripts")) | ||
| 31 | import fssm_unpack # noqa: E402 | ||
| 32 | import pal_stages # noqa: E402 | ||
| 33 | import shader_disasm # noqa: E402 | ||
| 34 | import shader_ir # noqa: E402 | ||
| 35 | from vs_input_survey import (FETCH_CALL, MBCNT, PARAM_EXPORT, POS0_EXPORT, PRIM_EXPORT, SENDMSG, # noqa: E402 | ||
| 36 | read_before_write) | ||
| 37 | |||
| 38 | ALLOC_REQ = re.compile(r"s_sendmsg\s+sendmsg\(MSG_GS_ALLOC_REQ\)") | ||
| 39 | GS_OP = re.compile(r"s_sendmsg\s+sendmsg\(MSG_GS,") | ||
| 40 | |||
| 41 | |||
| 42 | def markers(text): | ||
| 43 | """The code markers that separate the NGG path, legacy GS stream output and the plain VS.""" | ||
| 44 | return { | ||
| 45 | "exp prim": bool(PRIM_EXPORT.search(text)), | ||
| 46 | "s_sendmsg": bool(SENDMSG.search(text)), | ||
| 47 | "s_sendmsg GS_ALLOC_REQ": bool(ALLOC_REQ.search(text)), | ||
| 48 | "s_sendmsg GS_OP": bool(GS_OP.search(text)), | ||
| 49 | "v_mbcnt": bool(MBCNT.search(text)), | ||
| 50 | "exp pos0": bool(POS0_EXPORT.search(text)), | ||
| 51 | "exp param": bool(PARAM_EXPORT.search(text)), | ||
| 52 | "fetch call": bool(FETCH_CALL.search(text)), | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | def insns_from_text(text): | ||
| 57 | """shader_ir instructions from listing text (no file, no pc tracking: read_before_write needs neither).""" | ||
| 58 | insns = [] | ||
| 59 | for line in text.splitlines(): | ||
| 60 | if not line.startswith("\t"): | ||
| 61 | continue | ||
| 62 | ins = shader_ir.parse_line(line) | ||
| 63 | if ins: | ||
| 64 | insns.append(ins) | ||
| 65 | return insns | ||
| 66 | |||
| 67 | |||
| 68 | def default_disasm(blob): | ||
| 69 | """llvm-mc disassembly of one FSSM payload's code range (the same mcpu make_game uses).""" | ||
| 70 | start, end, _how = shader_disasm.code_range(blob) | ||
| 71 | if end is None: | ||
| 72 | return "" | ||
| 73 | asm, _bad, _err = shader_disasm.disasm(blob[start:end], "gfx1013") | ||
| 74 | return asm | ||
| 75 | |||
| 76 | |||
| 77 | def payloads_of(dxbc_path): | ||
| 78 | """(stem, payload index, blob, listed) for every tag-0x0D payload of every container, in index order.""" | ||
| 79 | with open(dxbc_path, "rb") as f: | ||
| 80 | data = f.read() | ||
| 81 | try: | ||
| 82 | blobs = fssm_unpack.unpack_all(data) | ||
| 83 | except (ValueError, OSError): | ||
| 84 | return [] | ||
| 85 | listed = len(blobs) - 1 | ||
| 86 | return [(os.path.splitext(os.path.basename(dxbc_path))[0], i, blob, i == listed) | ||
| 87 | for i, blob in enumerate(blobs)] | ||
| 88 | |||
| 89 | |||
| 90 | def survey(dxbc_dir, disasm_fn=None, limit=None): | ||
| 91 | """The census over <dxbc>: counts by stage, hardware id, markers and VS entry VGPRs.""" | ||
| 92 | index_path = os.path.join(dxbc_dir, "index.json") | ||
| 93 | with open(index_path, encoding="utf-8") as f: | ||
| 94 | index = json.load(f) | ||
| 95 | if not isinstance(index, list) or not all(isinstance(e, dict) for e in index): | ||
| 96 | raise ValueError("index.json is not a list of shader entries") | ||
| 97 | stages = {} | ||
| 98 | names = [] | ||
| 99 | for e in index: | ||
| 100 | if isinstance(e.get("file"), str): | ||
| 101 | stem = os.path.splitext(e["file"])[0] | ||
| 102 | if stem not in stages: | ||
| 103 | names.append(stem) | ||
| 104 | stages[stem] = e.get("stage") | ||
| 105 | if limit is not None: | ||
| 106 | names = names[:limit] | ||
| 107 | |||
| 108 | by_stage_hw = Counter() | ||
| 109 | markers_by_hw = defaultdict(Counter) | ||
| 110 | descriptors_per_blob = Counter() | ||
| 111 | api_mismatch = 0 | ||
| 112 | no_record = 0 | ||
| 113 | vs_rows = [] | ||
| 114 | disassembled = 0 | ||
| 115 | for name in names: | ||
| 116 | dxbc_path = os.path.join(dxbc_dir, name + ".dxbc") | ||
| 117 | if not os.path.isfile(dxbc_path): | ||
| 118 | continue | ||
| 119 | rows = payloads_of(dxbc_path) | ||
| 120 | for _stem, payload_index, blob, listed in rows: | ||
| 121 | records = pal_stages.stage_descriptors(blob) | ||
| 122 | if not records: | ||
| 123 | no_record += 1 | ||
| 124 | continue | ||
| 125 | first = records[0] | ||
| 126 | hw, api = first["hardware_id"], first["api_id"] | ||
| 127 | by_stage_hw[(stages.get(name), api, hw)] += 1 | ||
| 128 | descriptors_per_blob[len(records)] += 1 | ||
| 129 | if stages.get(name) and api is not None and stages[name] != {0: "vs", 1: "ps", 2: "gs", 3: "cs", | ||
| 130 | 6: "ms"}.get(api): | ||
| 131 | api_mismatch += 1 | ||
| 132 | listing = os.path.join(dxbc_dir, "disasm", name + ".s") | ||
| 133 | if listed and os.path.isfile(listing): | ||
| 134 | with open(listing, encoding="utf-8", errors="replace") as f: | ||
| 135 | text = f.read() | ||
| 136 | elif disasm_fn is not None: | ||
| 137 | text = disasm_fn(blob) | ||
| 138 | disassembled += 1 | ||
| 139 | else: | ||
| 140 | text = None | ||
| 141 | if text is not None: | ||
| 142 | for key, value in markers(text).items(): | ||
| 143 | if value: | ||
| 144 | markers_by_hw[hw][key] += 1 | ||
| 145 | if api == 0: | ||
| 146 | entry = {"hw": hw, "payload": payload_index, "listed": listed} | ||
| 147 | if text is not None: | ||
| 148 | _, read_v = read_before_write(insns_from_text(text)) | ||
| 149 | low = sorted(r for r in read_v if r < 16) | ||
| 150 | entry.update({ | ||
| 151 | "fetch": bool(FETCH_CALL.search(text)), | ||
| 152 | "reads_v0": 0 in read_v, | ||
| 153 | "reads_v3": 3 in read_v, | ||
| 154 | "reads_v1_v2": any(r in (1, 2) for r in read_v), | ||
| 155 | "low_vgprs": low, | ||
| 156 | }) | ||
| 157 | vs_rows.append(entry) | ||
| 158 | |||
| 159 | return { | ||
| 160 | "containers": len(names), | ||
| 161 | "payloads": sum(by_stage_hw.values()), | ||
| 162 | "payloads_without_record": no_record, | ||
| 163 | "descriptors_per_blob": dict(sorted(descriptors_per_blob.items())), | ||
| 164 | "api_stage_mismatches": api_mismatch, | ||
| 165 | "disassembled_unlisted": disassembled, | ||
| 166 | "by_stage_hw": {"|".join(str(x) for x in k): v for k, v in sorted(by_stage_hw.items(), | ||
| 167 | key=lambda kv: (str(kv[0][0]), kv[0][1] or -1, kv[0][2] or -1))}, | ||
| 168 | "markers_by_hw": {str(hw): dict(sorted(c.items())) for hw, c in sorted(markers_by_hw.items())}, | ||
| 169 | "vs_payloads": len(vs_rows), | ||
| 170 | "vs_by_hw": dict(sorted(Counter(r["hw"] for r in vs_rows).items())), | ||
| 171 | "vs_fetch": sum(1 for r in vs_rows if r.get("fetch")), | ||
| 172 | "vs_reads_v0": sum(1 for r in vs_rows if r.get("reads_v0")), | ||
| 173 | "vs_reads_v3": sum(1 for r in vs_rows if r.get("reads_v3")), | ||
| 174 | "vs_reads_v1_v2": sum(1 for r in vs_rows if r.get("reads_v1_v2")), | ||
| 175 | "vs_low_vgprs": {str(hw): dict(sorted(Counter(",".join(map(str, r.get("low_vgprs", ()))) | ||
| 176 | for r in vs_rows if r["hw"] == hw).items(), | ||
| 177 | key=lambda kv: -kv[1])[:12]) | ||
| 178 | for hw in sorted({r["hw"] for r in vs_rows})}, | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | def main(argv=None): | ||
| 183 | ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) | ||
| 184 | ap.add_argument("dxbc", help="<work>/shaders/dxbc from make_game.py") | ||
| 185 | ap.add_argument("--json", help="also write the counts as JSON") | ||
| 186 | ap.add_argument("--limit", type=int, help="stop after this many containers (index order)") | ||
| 187 | a = ap.parse_args(argv) | ||
| 188 | index_path = os.path.join(a.dxbc, "index.json") | ||
| 189 | if a.json and os.path.exists(a.json) and os.path.exists(index_path) and os.path.samefile(a.json, index_path): | ||
| 190 | print(f"vs_layout_census: error: --json would overwrite the input {index_path}", file=sys.stderr) | ||
| 191 | return 2 | ||
| 192 | try: | ||
| 193 | r = survey(a.dxbc, disasm_fn=default_disasm, limit=a.limit) | ||
| 194 | except (OSError, ValueError) as e: | ||
| 195 | print(f"vs_layout_census: error: cannot read {index_path}: {e}", file=sys.stderr) | ||
| 196 | return 2 | ||
| 197 | print(f"{r['containers']} containers, {r['payloads']} payloads " | ||
| 198 | f"({r['payloads_without_record']} without a PAL record), descriptors per blob {r['descriptors_per_blob']}") | ||
| 199 | print("stage/hw (carve stage|API|PAL hw):", r["by_stage_hw"]) | ||
| 200 | print("markers by PAL hw:", r["markers_by_hw"]) | ||
| 201 | print(f"API-vs payloads {r['vs_payloads']} by PAL hw {r['vs_by_hw']}: fetch {r['vs_fetch']}, " | ||
| 202 | f"reads v0 {r['vs_reads_v0']}, reads v3 {r['vs_reads_v3']}, reads v1/v2 {r['vs_reads_v1_v2']}") | ||
| 203 | print("VS/ES entry VGPRs (low 16, top 12):", r["vs_low_vgprs"]) | ||
| 204 | print(f"API stage mismatches {r['api_stage_mismatches']}, " | ||
| 205 | f"unlisted payloads disassembled {r['disassembled_unlisted']}") | ||
| 206 | if a.json: | ||
| 207 | try: | ||
| 208 | with open(a.json, "w", encoding="utf-8", newline="\n") as f: | ||
| 209 | json.dump(r, f, indent=1) | ||
| 210 | except OSError as e: | ||
| 211 | print(f"vs_layout_census: error: cannot write {a.json}: {e.strerror or e}", file=sys.stderr) | ||
| 212 | return 2 | ||
| 213 | return 0 | ||
| 214 | |||
| 215 | |||
| 216 | if __name__ == "__main__": | ||
| 217 | try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode | ||
| 218 | sys.stdout.reconfigure(errors="replace") | ||
| 219 | except (AttributeError, ValueError, OSError): | ||
| 220 | pass | ||
| 221 | sys.exit(main()) | ||