diff options
| author | DadaCap <167002618+DadaCap@users.noreply.github.com> | 2026-11-28 12:41:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 12:41:59 +0100 |
| commit | feaaa13a66528a6525ff6bc4f0d8a8bef6442e25 (patch) | |
| tree | 78245010e0f7bfb99dcef9b27f9ad021d2321cea /tools | |
| parent | 23ccf1486e16dcabe35a80d4cfe7346d3d1624a2 (diff) | |
| parent | 7c2de8a936897b2322caf574c991746dfe732b01 (diff) | |
Merge pull request #1423 from GTAVIB4GTAVI/feat/library-code-base-pairs
pso_code_base_pairs: count PSOs by code-base stage (SHADER-CODEBASE-PAIRS)
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/feature_scan.py | 4 | ||||
| -rw-r--r-- | tools/pso_code_base_pairs.py | 189 | ||||
| -rw-r--r-- | tools/shader_library.py | 3 |
3 files changed, 193 insertions, 3 deletions
diff --git a/tools/feature_scan.py b/tools/feature_scan.py index 8045c9a9e..b1ef0d4a8 100644 --- a/tools/feature_scan.py +++ b/tools/feature_scan.py | |||
| @@ -14,8 +14,8 @@ Reads what the shaders stage of make_game.py leaves in <work>/shaders, and print | |||
| 14 | accesses (#16): the MTBUF instruction formats, and for MUBUF where V# word 3 (the format) comes from, each checked | 14 | accesses (#16): the MTBUF instruction formats, and for MUBUF where V# word 3 (the format) comes from, each checked |
| 15 | against the translator's typed-buffer table (rdna2spirv.Gen.FORMATS). | 15 | against the translator's typed-buffer table (rdna2spirv.Gen.FORMATS). |
| 16 | - spirv/index.json (rdna2spirv.py): uses_code_base per stage. A graphics PSO can only need two code bases | 16 | - spirv/index.json (rdna2spirv.py): uses_code_base per stage. A graphics PSO can only need two code bases |
| 17 | (§5.2) if two of its stages use one; this gives the per-stage counts. Which stages are paired in one PSO is in | 17 | (§5.2) if two of its stages use one; this gives the per-stage counts. Pairing programs to the stages of a PSO is |
| 18 | the executable, not in shaders.rpf. | 18 | the pipeline library's job: tools/pso_code_base_pairs.py joins the two and counts whole PSOs. |
| 19 | 19 | ||
| 20 | InnerCoverage metadata is cross-tabulated by stage: SFI0 set/clear/unknown versus ISG1 | 20 | InnerCoverage metadata is cross-tabulated by stage: SFI0 set/clear/unknown versus ISG1 |
| 21 | present/absent/unknown. Unknown includes missing or truncated parts; unreadable containers | 21 | present/absent/unknown. Unknown includes missing or truncated parts; unreadable containers |
diff --git a/tools/pso_code_base_pairs.py b/tools/pso_code_base_pairs.py new file mode 100644 index 000000000..2f9ddf8bd --- /dev/null +++ b/tools/pso_code_base_pairs.py | |||
| @@ -0,0 +1,189 @@ | |||
| 1 | """Count how many real graphics PSOs put a code base in two stages (docs/d3d12_vulkan.md §5.2). | ||
| 2 | |||
| 3 | python -I tools/pso_code_base_pairs.py <work>/shaders --library <shaders.rpf> [--out pairs.json] | ||
| 4 | |||
| 5 | §5.2 leaves the push-constant shape open -- one 168-byte block per PSO or one buffer per stage -- and says to decide | ||
| 6 | after measuring how many real PSOs have two code-base stages. `docs/boot.md` row 14 asks the same thing. The | ||
| 7 | translation index gives `uses_code_base` per program (`tools/feature_scan.py` prints the per-stage counts), and its | ||
| 8 | header says the pairing is not in `shaders.rpf`. It is: the pipeline library in the archive's zlib part records, for | ||
| 9 | every graphics PSO, the program in each stage slot (`tools/shader_library.py`), so a stage of a PSO joins to that | ||
| 10 | program's own `uses_code_base`. | ||
| 11 | |||
| 12 | The join needs one step in between. A library program is not the translation index's key: the two tables name the | ||
| 13 | same code differently. The library names a program by slot and index (`lib_ps_00042`), the translation index is keyed | ||
| 14 | by the container it was translated from. What they have in common is the digest: a stage's resource-layout blob ends | ||
| 15 | with the 16-byte HASH digest of the container its code came from (`shader_library.stage_digest`), and `shader_carve.py` | ||
| 16 | leaves that same HASH part in every container it carved into `<work>/shaders/dxbc`. So | ||
| 17 | |||
| 18 | PSO -> stage slot -> program -> layout digest -> carved container -> translation-index row | ||
| 19 | |||
| 20 | and the container's file stem is the translation index's key. Where a container was carved twice under two stems the | ||
| 21 | stage counts as code-base if any of its rows does. | ||
| 22 | |||
| 23 | Counts only are printed and written: no shader name, listing, code or data leaves the work folder. Standard library | ||
| 24 | only; needs the retail package (for `shaders.rpf`) and the `shaders` stage of `make_game.py`. | ||
| 25 | """ | ||
| 26 | import argparse | ||
| 27 | import json | ||
| 28 | import mmap | ||
| 29 | import os | ||
| 30 | import sys | ||
| 31 | from collections import Counter | ||
| 32 | |||
| 33 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | ||
| 34 | import shader_library # noqa: E402 | ||
| 35 | from report_paths import same_report_path # noqa: E402 | ||
| 36 | |||
| 37 | |||
| 38 | def container_stem(path: str) -> str: | ||
| 39 | """The translation index's key for a carved container: its file name without the folder and extension.""" | ||
| 40 | return os.path.splitext(os.path.basename(path))[0] | ||
| 41 | |||
| 42 | |||
| 43 | def read_spirv(path: str) -> dict: | ||
| 44 | """{container stem: True/False} for every translated program in a `spirv/index.json`. | ||
| 45 | |||
| 46 | An object maps the container stem to its row; a list of rows is the other shape `rdna2spirv.py` writes, where | ||
| 47 | the stem comes from the row's own file or name. Rows without a stem are skipped: they cannot be joined.""" | ||
| 48 | with open(path, encoding="utf-8-sig") as f: | ||
| 49 | idx = json.load(f) | ||
| 50 | if isinstance(idx, dict): | ||
| 51 | pairs = idx.items() | ||
| 52 | elif isinstance(idx, list): | ||
| 53 | pairs = ((row.get("file") or row.get("name") or "", row) for row in idx if isinstance(row, dict)) | ||
| 54 | else: | ||
| 55 | pairs = () | ||
| 56 | out = {} | ||
| 57 | for key, row in pairs: | ||
| 58 | if not isinstance(row, dict) or not key: | ||
| 59 | continue | ||
| 60 | out[container_stem(str(key))] = row.get("uses_code_base") is True | ||
| 61 | return out | ||
| 62 | |||
| 63 | |||
| 64 | def code_base_flags(lib: dict, digests: dict, code_base: dict) -> dict: | ||
| 65 | """{library program index: True / False / None} -- None when no carved container of that program is in the | ||
| 66 | translation index, so the program's code base is unknown rather than absent.""" | ||
| 67 | flags = {} | ||
| 68 | for i in range(lib["header"]["programs"]): | ||
| 69 | stems = {container_stem(path) for d in lib["digest"].get(i, ()) for _, path in digests.get(d, ())} | ||
| 70 | known = {code_base[s] for s in stems if s in code_base} | ||
| 71 | flags[i] = None if not known else True in known | ||
| 72 | return flags | ||
| 73 | |||
| 74 | |||
| 75 | def count_records(lib: dict, flags: dict) -> dict: | ||
| 76 | """The report: how the graphics PSOs and compute records split by the code-base flag of their stages. | ||
| 77 | |||
| 78 | `stages` keys are stage names joined with '+', sorted, so 'ps+vs' is one pixel and one vertex stage.""" | ||
| 79 | slots, by_number, pairs, unresolved = Counter(), Counter(), Counter(), Counter() | ||
| 80 | by_stage = Counter() | ||
| 81 | for rec in lib["graphics"]: | ||
| 82 | flag = {slot: flags.get(ref["program"]) for slot, ref in rec["stages"].items()} | ||
| 83 | slots["+".join(sorted(flag))] += 1 | ||
| 84 | code_base = sorted(slot for slot, known in flag.items() if known is True) | ||
| 85 | by_number[len(code_base)] += 1 | ||
| 86 | by_stage.update(code_base) | ||
| 87 | if len(code_base) >= 2: | ||
| 88 | pairs["+".join(code_base)] += 1 | ||
| 89 | missing = sorted(slot for slot, known in flag.items() if known is None) | ||
| 90 | if missing: | ||
| 91 | unresolved["+".join(missing)] += 1 | ||
| 92 | compute = Counter(str(flags.get(rec["program"])) for rec in lib["compute"] if rec["program"] is not None) | ||
| 93 | programs, program_code_base = Counter(), Counter() | ||
| 94 | for i, stages in lib["stage"].items(): | ||
| 95 | programs["+".join(sorted(stages))] += 1 | ||
| 96 | if flags.get(i) is True: | ||
| 97 | program_code_base["+".join(sorted(stages))] += 1 | ||
| 98 | return { | ||
| 99 | "library": lib["header"], | ||
| 100 | "stage_slots": dict(slots), | ||
| 101 | "psos_by_code_base_stages": {str(n): c for n, c in by_number.items()}, | ||
| 102 | "psos_with_code_base_by_stage": dict(by_stage), | ||
| 103 | "psos_with_two_code_base_stages": dict(pairs), | ||
| 104 | "psos_unresolved": dict(unresolved), | ||
| 105 | "compute_records_by_code_base": dict(compute), | ||
| 106 | "programs": dict(programs), | ||
| 107 | "programs_with_code_base": dict(program_code_base), | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | def print_report(report: dict, carve_errors: int) -> None: | ||
| 112 | h = report["library"] | ||
| 113 | print(f"library {h['name']} {h['version']} hash {h['hash']}: {h['graphics']} graphics + {h['compute']} " | ||
| 114 | f"compute records, {h['programs']} programs") | ||
| 115 | print("\nstage slots per graphics PSO:") | ||
| 116 | for name, n in sorted(report["stage_slots"].items(), key=lambda x: (-x[1], x[0])): | ||
| 117 | print(f" {n:7d} {name}") | ||
| 118 | print("\ngraphics PSOs by the number of stages that use a code base:") | ||
| 119 | for n in sorted(report["psos_by_code_base_stages"], key=int): | ||
| 120 | print(f" {report['psos_by_code_base_stages'][n]:7d} {n}") | ||
| 121 | print("\ngraphics PSOs with a code-base stage, counted per stage:") | ||
| 122 | for name, n in sorted(report["psos_with_code_base_by_stage"].items(), key=lambda x: (-x[1], x[0])): | ||
| 123 | print(f" {n:7d} {name}") | ||
| 124 | two = report["psos_with_two_code_base_stages"] | ||
| 125 | print("\ngraphics PSOs with two or more code-base stages, by stage:") | ||
| 126 | for name, n in sorted(two.items(), key=lambda x: (-x[1], x[0])): | ||
| 127 | print(f" {n:7d} {name}") | ||
| 128 | if not two: | ||
| 129 | print(" 0 (none)") | ||
| 130 | print("\nlibrary programs that use a code base, by stage (compare tools/feature_scan.py):") | ||
| 131 | for name in sorted(report["programs_with_code_base"]): | ||
| 132 | print(f" {report['programs_with_code_base'][name]:7d} of {report['programs'][name]:7d} {name}") | ||
| 133 | unknown = report["psos_unresolved"] | ||
| 134 | print(f"\ngraphics PSOs with a stage whose code base is unknown: {sum(unknown.values())}") | ||
| 135 | for name, n in sorted(unknown.items(), key=lambda x: (-x[1], x[0])): | ||
| 136 | print(f" {n:7d} {name}") | ||
| 137 | if carve_errors: | ||
| 138 | print(f"\n{carve_errors} carved containers were unreadable, above.") | ||
| 139 | |||
| 140 | |||
| 141 | def main() -> int: | ||
| 142 | ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) | ||
| 143 | ap.add_argument("shaders", help="<work>/shaders from make_game.py") | ||
| 144 | ap.add_argument("--library", required=True, | ||
| 145 | help="shaders.rpf, or a pipeline library saved by tools/shader_library.py") | ||
| 146 | ap.add_argument("--out", help="also write the counts as JSON") | ||
| 147 | a = ap.parse_args() | ||
| 148 | |||
| 149 | spirv_index = os.path.join(a.shaders, "spirv", "index.json") | ||
| 150 | dxbc_dir = os.path.join(a.shaders, "dxbc") | ||
| 151 | for path, what in ((a.shaders, "folder"), (spirv_index, "file"), (dxbc_dir, "folder")): | ||
| 152 | if not os.path.exists(path): | ||
| 153 | print(f"ERROR: {path} {what} does not exist. Give the <work>/shaders folder make_game.py writes.", | ||
| 154 | file=sys.stderr) | ||
| 155 | return 2 | ||
| 156 | for input_path in (spirv_index, os.path.join(dxbc_dir, "index.json")): | ||
| 157 | if a.out and same_report_path(a.out, input_path): | ||
| 158 | print(f"ERROR: --out {a.out} is an input ({input_path}).", file=sys.stderr) | ||
| 159 | return 2 | ||
| 160 | |||
| 161 | with open(a.library, "rb") as f: | ||
| 162 | head = f.read(4) | ||
| 163 | f.seek(0) | ||
| 164 | if head == shader_library.MAGIC: # a library saved by tools/shader_library.py | ||
| 165 | raw = f.read() | ||
| 166 | else: # an archive: reassemble it from its zlib blocks | ||
| 167 | m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) | ||
| 168 | try: | ||
| 169 | raw = shader_library.find_library(m) | ||
| 170 | finally: | ||
| 171 | m.close() | ||
| 172 | lib = shader_library.parse(raw) | ||
| 173 | |||
| 174 | code_base = read_spirv(spirv_index) | ||
| 175 | errors = [] | ||
| 176 | digests = shader_library.carve_digests(dxbc_dir, errors=errors) | ||
| 177 | report = count_records(lib, code_base_flags(lib, digests, code_base)) | ||
| 178 | print_report(report, len(errors)) | ||
| 179 | if a.out: | ||
| 180 | with open(a.out, "w", encoding="utf-8", newline="\n") as f: | ||
| 181 | json.dump(report, f, indent=1, sort_keys=True) | ||
| 182 | f.write("\n") | ||
| 183 | print(f"\nwrote {a.out}") | ||
| 184 | return 0 | ||
| 185 | |||
| 186 | |||
| 187 | if __name__ == "__main__": | ||
| 188 | sys.stdout.reconfigure(errors="replace") | ||
| 189 | sys.exit(main()) | ||
diff --git a/tools/shader_library.py b/tools/shader_library.py index 943bdd8bb..905f7195c 100644 --- a/tools/shader_library.py +++ b/tools/shader_library.py | |||
| @@ -14,7 +14,8 @@ stage, a table of state blobs and a table of native RDNA2 programs. Layout (docs | |||
| 14 | P x 8 (offset, size) into the code section, 256-byte aligned | 14 | P x 8 (offset, size) into the code section, 256-byte aligned |
| 15 | blob section, then code section; the file ends exactly there | 15 | blob section, then code section; the file ends exactly there |
| 16 | 16 | ||
| 17 | A missing stage is program 0xFFFFFFFF. Stage slots: 0 pixel, 1 vertex, 2 mesh (6 is used once and not identified). | 17 | A missing stage is program 0xFFFFFFFF. Stage slots: 0 pixel, 1 vertex, 2 mesh (slot 6 appears in the 2 records that |
| 18 | have pixel, vertex and slot 6, with no stage blob, and is not identified). | ||
| 18 | A compute state blob has the 28-byte layout of an FSPX kind-0 entry (tools/fspx_unpack.py), and every program starts | 19 | A compute state blob has the 28-byte layout of an FSPX kind-0 entry (tools/fspx_unpack.py), and every program starts |
| 19 | with the same 36-byte hash prologue as an FSPX program, so the programs are written in the layout fspx_unpack.py uses | 20 | with the same 36-byte hash prologue as an FSPX program, so the programs are written in the layout fspx_unpack.py uses |
| 20 | and tools/rdna2spirv.py reads (fspx/<name>.code, fspx/<name>.json, index.json). A compute program also gets its | 21 | and tools/rdna2spirv.py reads (fspx/<name>.code, fspx/<name>.json, index.json). A compute program also gets its |