diff options
Diffstat (limited to 'docs/embedded_shaders.md')
| -rw-r--r-- | docs/embedded_shaders.md | 220 |
1 files changed, 194 insertions, 26 deletions
diff --git a/docs/embedded_shaders.md b/docs/embedded_shaders.md index 24f0b9971..ed3a254d7 100644 --- a/docs/embedded_shaders.md +++ b/docs/embedded_shaders.md | |||
| @@ -7,8 +7,9 @@ hold counts, offsets, chunk names and module identifiers. | |||
| 7 | 7 | ||
| 8 | ## 1. Why they matter | 8 | ## 1. Why they matter |
| 9 | 9 | ||
| 10 | `make_game.py`'s `shaders` stage carves `shaders.rpf` only (`make_game.py`:655). Two DLLs in the same package | 10 | `tools/shader_carve.py` carves `shaders.rpf` only. Two DLLs in the same package carry compute programs of their |
| 11 | carry compute programs of their own: | 11 | own, and `make_game.py`'s `shaders` stage translates both sets after the game's shaders (section 7 for the |
| 12 | builder, [fspx.md](fspx.md) for FidelityFX): | ||
| 12 | 13 | ||
| 13 | * `XboxBvhBuilder.dll`, Microsoft's XDXR acceleration-structure builder. Its constructor creates compute PSOs | 14 | * `XboxBvhBuilder.dll`, Microsoft's XDXR acceleration-structure builder. Its constructor creates compute PSOs |
| 14 | through device slot 62, and the builder is on the critical path for ray tracing. | 15 | through device slot 62, and the builder is on the critical path for ray tracing. |
| @@ -25,18 +26,34 @@ python -I tools/pe_shader_containers.py <dll> --json <work>/containers.json --ex | |||
| 25 | 26 | ||
| 26 | ## 2. Inventory | 27 | ## 2. Inventory |
| 27 | 28 | ||
| 28 | The 16 DLLs and the package's root files were scanned. All the containers are in two DLLs: | 29 | The 16 DLLs and the package's root files were scanned first (`XboxBvhBuilder.dll` and `amd_fidelityfx_upscaler_xbox.dll`); `game_scarlett_master.exe` (RT-5) adds 10 more console containers: |
| 29 | 30 | ||
| 30 | | image | containers | bytes | min / median / max | chunks | | 31 | | image | containers | bytes | min / median / max | chunks | |
| 31 | |---|---:|---:|---:|---| | 32 | |---|---:|---:|---:|---| |
| 32 | | `XboxBvhBuilder.dll` | 52 | 674,404 | 4,072 / 7,454 / 74,508 | `SFI0 ISG1 OSG1 PSV0 RTS0 STAT ILDN HASH DXIL` | | 33 | | `XboxBvhBuilder.dll` | 52 | 674,404 | 4,072 / 7,454 / 74,508 | `SFI0 ISG1 OSG1 PSV0 RTS0 STAT ILDN HASH DXIL` | |
| 33 | | `amd_fidelityfx_upscaler_xbox.dll` | 29 | 289,656 | 1,764 / 5,176 / 42,616 | `SFI0 ISG1 OSG1 PSV0 RTS0 HASH ACCC` | | 34 | | `amd_fidelityfx_upscaler_xbox.dll` | 29 | 289,656 | 1,764 / 5,176 / 42,616 | `SFI0 ISG1 OSG1 PSV0 RTS0 HASH ACCC` | |
| 34 | | other 14 DLLs | 0 | 0 | — | — | | 35 | | other 14 DLLs | 0 | 0 | — | — | |
| 36 | | `game_scarlett_master.exe` | 10 | 9,520 | 924 / 936 / 992 | `SFI0 ISG1 OSG1 PSV0 HASH DXIL PRIV` | | ||
| 35 | 37 | ||
| 36 | The 29 `ACCC` containers hold `FSPX` payloads ([fspx.md](fspx.md)); the 52 `DXIL` containers are the subject of | 38 | The 29 `ACCC` containers hold `FSPX` payloads ([fspx.md](fspx.md)); the 52 `DXIL` containers are the subject of |
| 37 | this note. The FSR DLL has one rejected `DXBC` hit: the bytes are an immediate in its own magic check, and the | 39 | this note. The FSR DLL has one rejected `DXBC` hit: the bytes are an immediate in its own magic check, and the |
| 38 | validator rejects it (the scan reports both counts). | 40 | validator rejects it (the scan reports both counts). |
| 39 | 41 | ||
| 42 | ### Retail executable (`game_scarlett_master.exe`, RT-5) | ||
| 43 | |||
| 44 | Scanning the retail executable (120,181,760 bytes, SHA-256 `e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993`) | ||
| 45 | with `tools/pe_shader_containers.py` finds **10** valid DXBC containers (9,520 bytes total, file offsets | ||
| 46 | `0x688cd3c`..`0x688fdf6`, sizes 924–992 bytes) and 1 rejected `DXBC` hit: | ||
| 47 | |||
| 48 | * **Stages (`PSV0`):** **1** vertex shader (`0x688cd3c`, 992 bytes) and **9** pixel shaders (924–980 bytes). Unlike the | ||
| 49 | DLL containers above, none carries `ILDN`, `STAT`, `RTS0`, or LLVM bitcode (`has_bitcode = false`). All 10 carry | ||
| 50 | console-only `XBCH` (`XDAT` tag `0x0d`, 307–368 bytes) and `NOBC` (tag `0x07`, 28 bytes), plus a 12-byte `PRIV` chunk. | ||
| 51 | * **Translation and GPU check:** `tools/pe_shader_containers.py --extract` previously hardcoded `"stage": "cs"` in | ||
| 52 | `index.json`, causing `rdna2spirv.py` to reject all 10 with `CS group size`. With `stage` read from `PSV0`, all 10 | ||
| 53 | unpack via `tools/fssm_unpack.py` (10/10 `FSSM`, 1,245–4,176 bytes), disassemble cleanly via `tools/shader_disasm.py --backend python` | ||
| 54 | (10/10 clean), translate via `tools/rdna2spirv.py` (10/10 translated, all wave64 tier A), and pass `vk_pipeline_check` | ||
| 55 | on an NVIDIA GeForce RTX 5060 Laptop GPU (driver `617.14`, Windows 11): **10/10 modules and 10/10 pipelines OK** (1 VS, 9 PS, 0 failed, 0 skipped). | ||
| 56 | |||
| 40 | **Every one of the 52 containers carries two representations of its program:** | 57 | **Every one of the 52 containers carries two representations of its program:** |
| 41 | 58 | ||
| 42 | * an LLVM bitcode blob in the `DXIL` chunk (`BC C0 DE`, 1,820 to 70,276 bytes), and | 59 | * an LLVM bitcode blob in the `DXIL` chunk (`BC C0 DE`, 1,820 to 70,276 bytes), and |
| @@ -66,14 +83,20 @@ All spans fit inside their enclosing parts, and all 104 `STAT` and outer `DXIL` | |||
| 66 | The bitcode is what a PC D3D12 compiler consumes; the runtime path is the `XBCH` record. The constructor passes | 83 | The bitcode is what a PC D3D12 compiler consumes; the runtime path is the `XBCH` record. The constructor passes |
| 67 | the **whole container** to the compute-PSO call: at `0x180021330` it loads `DAT_180025700` with size `0x1134` | 84 | the **whole container** to the compute-PSO call: at `0x180021330` it loads `DAT_180025700` with size `0x1134` |
| 68 | (4,404 bytes, the size of container 0, `CopyAndCompactBVH`) and calls `FUN_18000c470`, whose vtable call at | 85 | (4,404 bytes, the size of container 0, `CopyAndCompactBVH`) and calls `FUN_18000c470`, whose vtable call at |
| 69 | `+0x1f0` (slot 62) receives that pointer, its size and two zero words. `FUN_18001d400` adds two more PSOs, | 86 | `+0x1f0` (slot 62) receives that pointer, its size and two zero words. `FUN_18001d400` creates the |
| 70 | `XDXR PostBuildInfoQuery` (0x14a4 bytes) and `XDXR GetBVHCompactedSize`. `bvh_builder_test` measures **53** PSO | 87 | `XDXR GetBVHCompactedSize` PSO (0x14a4 bytes; measured below). `bvh_builder_test` measures **53** PSO |
| 71 | creations during construction over the **52** containers ([d3d12_vulkan.md](d3d12_vulkan.md) A.2), so one | 88 | creations during construction over the **52** containers ([d3d12_vulkan.md](d3d12_vulkan.md) A.2), so one |
| 72 | container is created twice. It is **`GetBVHCompactedSize`** (5,284 bytes = 0x14a4): creations **#47 and #53** pass | 89 | container is created twice. It is **`GetBVHCompactedSize`** (5,284 bytes = 0x14a4): creations **#47 and #53** pass |
| 73 | byte-identical containers (RT-182, `LEONIDA_BVH_RECORD62=1`, which now prints each creation's module from its `ILDN` | 90 | byte-identical containers (RT-182, `LEONIDA_BVH_RECORD62=1`, which now prints each creation's module from its `ILDN` |
| 74 | part and says when a container repeats). 0x14a4 is also the size this section gives for `XDXR PostBuildInfoQuery`, | 91 | part and says when a container repeats). The test now also prints each creation's call site and the name the builder |
| 75 | which fits `FUN_18001d400`'s two XDXR PSOs being this one container created twice, once under each name; the test | 92 | gives the returned PSO (RT-266, WARP, default constructor flag): the builder names every one of the 53 PSOs with |
| 76 | does not see the PSO names, so that last step is a hypothesis. The creation order, by module (it is not the table | 93 | `SetName`, `XDXR <module>` (for example `XDXR InitPartitionedHeaders`; some modules are shortened, as |
| 94 | `XDXR CopyAndCompact`, or numbered, as `XDXR CalculateMortonCodes 0`/`1`). **#47 and #53 both get `XDXR | ||
| 95 | GetBVHCompactedSize`**, from the same call site in `FUN_18001d400` (return address `+0x1d503`, called through | ||
| 96 | `FUN_18000c470` at `+0xc4e4`); `FUN_18001d400` is called twice, once from `FUN_18000d850` (#47) and once from | ||
| 97 | `FUN_180021330` directly (#53). So the repeat is one PSO created twice under one name, not one container under two | ||
| 98 | names, and no creation is named `XDXR PostBuildInfoQuery` in this run (default flag, WARP; the flag-true | ||
| 99 | constructor was not run). The creation order, by module (it is not the table | ||
| 77 | order of section 3; `CopyAndCompactBVH` is #52, not the first): | 100 | order of section 3; `CopyAndCompactBVH` is #52, not the first): |
| 78 | 101 | ||
| 79 | | creations | modules, in the order of the `CreateComputePipelineState` calls | | 102 | | creations | modules, in the order of the `CreateComputePipelineState` calls | |
| @@ -82,12 +105,12 @@ order of section 3; `CopyAndCompactBVH` is #52, not the first): | |||
| 82 | | #14-#29 | `BitonicPreSortCS`, `BitonicOuterSortCS`, `BitonicInnerSortCS`, `BitonicPreSort64CS`, `BitonicOuterSort64CS`, `BitonicInnerSort64CS`, `RadixReset`, `RadixCount`, `PrefixSum64`, `PrefixSum128`, `PrefixSum256`, `PrefixSum512`, `PrefixSum1024`, `PrefixSum2048`, `PrefixSum4096`, `RadixAlloc` | | 105 | | #14-#29 | `BitonicPreSortCS`, `BitonicOuterSortCS`, `BitonicInnerSortCS`, `BitonicPreSort64CS`, `BitonicOuterSort64CS`, `BitonicInnerSort64CS`, `RadixReset`, `RadixCount`, `PrefixSum64`, `PrefixSum128`, `PrefixSum256`, `PrefixSum512`, `PrefixSum1024`, `PrefixSum2048`, `PrefixSum4096`, `RadixAlloc` | |
| 83 | | #30-#46 | `RearrangeTriangles`, `RearrangeBVHs`, `LoadInstances`, `SplitInstances`, `LoadTriangles`, `LoadProceduralGeometry`, `ReloadTriangles`, `ReloadProcedurals`, `PT_ConstructBVH4`, `ConstructBVH4`, `RefitBVH4`, `BuildBVHSplits`, `BuildBVHSplits64`, `FindTreelets`, `ResetKDOP`, `BuildKDOP`, `BuildApexPointMap` | | 106 | | #30-#46 | `RearrangeTriangles`, `RearrangeBVHs`, `LoadInstances`, `SplitInstances`, `LoadTriangles`, `LoadProceduralGeometry`, `ReloadTriangles`, `ReloadProcedurals`, `PT_ConstructBVH4`, `ConstructBVH4`, `RefitBVH4`, `BuildBVHSplits`, `BuildBVHSplits64`, `FindTreelets`, `ResetKDOP`, `BuildKDOP`, `BuildApexPointMap` | |
| 84 | | #47-#53 | `GetBVHCompactedSize`, `DecompressHeader`, `LeafRehydration`, `Box16Rehydration`, `Box32Rehydration`, `CopyAndCompactBVH`, `GetBVHCompactedSize` (again) | | 107 | | #47-#53 | `GetBVHCompactedSize`, `DecompressHeader`, `LeafRehydration`, `Box16Rehydration`, `Box32Rehydration`, `CopyAndCompactBVH`, `GetBVHCompactedSize` (again) | |
| 85 | container is created twice: **`GetBVHCompactedSize`** (RT-182). With `LEONIDA_BVH_RECORD62=1` the test prints each | 108 | |
| 86 | creation's container with its `ILDN` module name and the first bytes of its digest: creations 1 to 51 are 51 | 109 | The test also prints each creation's container with its `ILDN` module name and the first bytes of its digest |
| 87 | distinct containers, `GetBVHCompactedSize` (5,284 bytes, 0x14a4) among them at 47; creation 52 is | 110 | (RT-182): creations 1 to 51 are 51 distinct containers, `GetBVHCompactedSize` (5,284 bytes, 0x14a4) among them at 47; |
| 88 | `CopyAndCompactBVH` (4,404 bytes, 0x1134); creation 53 is `GetBVHCompactedSize` again, with the same digest and | 111 | creation 52 is `CopyAndCompactBVH` (4,404 bytes, 0x1134); creation 53 is `GetBVHCompactedSize` again, with the |
| 89 | size. So the 0x14a4-byte container `FUN_18001d400` passes for `XDXR PostBuildInfoQuery` is the | 112 | same digest and size. So the 0x14a4-byte container `FUN_18001d400` passes is the `GetBVHCompactedSize` module, and |
| 90 | `GetBVHCompactedSize` module, and the PSO for it is created twice. Measured on 1.0.2.1 with an RTX 5080 (driver | 113 | the PSO for it is created twice (under one name, `XDXR GetBVHCompactedSize`, per the SetName record above). Measured on 1.0.2.1 with an RTX 5080 (driver |
| 91 | 621.01): all 53 creations return `S_OK`. | 114 | 621.01): all 53 creations return `S_OK`. |
| 92 | 115 | ||
| 93 | ## 3. The 52 modules | 116 | ## 3. The 52 modules |
| @@ -147,17 +170,6 @@ The single remaining untranslated program stops at one rule: | |||
| 147 | |---|---| | 170 | |---|---| |
| 148 | | `Box16Rehydration` | `buffer_store_short` (#1864) | | 171 | | `Box16Rehydration` | `buffer_store_short` (#1864) | |
| 149 | 172 | ||
| 150 | | module(s) | blocker then | now | | ||
| 151 | |---|---|---| | ||
| 152 | | `BuildKDOP`, `LoadInstances`, `ReloadProcedurals`, `LoadProceduralGeometry`, `LoadTriangles` | `global_atomic_fmax` | translate | | ||
| 153 | | `PT_LoadInstances` | `global_atomic_fmin` | translates | | ||
| 154 | | `Box32Rehydration`, `ReloadTriangles` | `buffer_load_ushort` | translate | | ||
| 155 | | `LeafRehydration` | `tbuffer_load_format_x GFX10_FORMAT_8_UINT` | translates | | ||
| 156 | | `Box16Rehydration` | `buffer_load_ushort` | stops at `buffer_store_short` (#1864) | | ||
| 157 | |||
| 158 | The disassembly count is unchanged: the same 42 programs have no invalid word, and 10 have invalid words in the | ||
| 159 | code. All 10 of those translate; `Box16Rehydration` is one of the 42 clean ones. | ||
| 160 | |||
| 161 | Reproduction, after `--extract` above: | 173 | Reproduction, after `--extract` above: |
| 162 | 174 | ||
| 163 | ```sh | 175 | ```sh |
| @@ -166,6 +178,8 @@ python -I tools/shader_disasm.py <work>/embedded/dxbc/fssm --out <work>/embedded | |||
| 166 | python -I tools/rdna2spirv.py <work>/embedded/dxbc/disasm --out <work>/embedded/spirv | 178 | python -I tools/rdna2spirv.py <work>/embedded/dxbc/disasm --out <work>/embedded/spirv |
| 167 | ``` | 179 | ``` |
| 168 | 180 | ||
| 181 | `make_game.py` runs the same four tools as a build step (section 7). | ||
| 182 | |||
| 169 | ## 5. What this changes | 183 | ## 5. What this changes |
| 170 | 184 | ||
| 171 | * **The RT builder does not need a DXIL path for the console code.** Its programs are RDNA2 `FSSM`, and 51 of | 185 | * **The RT builder does not need a DXIL path for the console code.** Its programs are RDNA2 `FSSM`, and 51 of |
| @@ -201,7 +215,8 @@ python -I tools/rdna2spirv.py <work>/embedded/dxbc/disasm --out <work>/embedded/ | |||
| 201 | * the wave64 bitonic sorts (`BitonicPreSort64CS`, `BitonicInnerSort64CS`) declare 32,780 bytes of workgroup memory | 215 | * the wave64 bitonic sorts (`BitonicPreSort64CS`, `BitonicInnerSort64CS`) declare 32,780 bytes of workgroup memory |
| 202 | with the translator's default cap, 12 over the 32 KiB AMD's Windows driver allows | 216 | with the translator's default cap, 12 over the 32 KiB AMD's Windows driver allows |
| 203 | (`VUID-RuntimeSpirv-Workgroup-06530`). Their highest LDS offset is 8192, and `--max-shared-memory 32768` shrinks | 217 | (`VUID-RuntimeSpirv-Workgroup-06530`). Their highest LDS offset is 8192, and `--max-shared-memory 32768` shrinks |
| 204 | the array to what they use; | 218 | the array to what they use. That was before SHADER-579 moved the barrier-round counts into the LDS array: today |
| 219 | both declare 32,768 bytes with the default cap, and section 7 has what the cap still changes; | ||
| 205 | * with every table word 0 instead, the descriptors point at the table itself, and `RadixCount` then writes into the | 220 | * with every table word 0 instead, the descriptors point at the table itself, and `RadixCount` then writes into the |
| 206 | table that other waves are reading, so its output differs between runs. That is the test input, not the | 221 | table that other waves are reading, so its output differs between runs. That is the test input, not the |
| 207 | translator; the tool's default avoids it. | 222 | translator; the tool's default avoids it. |
| @@ -225,3 +240,156 @@ container only when the chunk table is sorted, 4-byte aligned, entirely inside t | |||
| 225 | chunk ends exactly at that total; that is what rejects the FSR DLL's `DXBC` immediate. The synthetic tests in | 240 | chunk ends exactly at that total; that is what rejects the FSR DLL's `DXBC` immediate. The synthetic tests in |
| 226 | `tests/test_pe_shader_containers.py` build containers by hand (no game data) and cover the accepted layout, a | 241 | `tests/test_pe_shader_containers.py` build containers by hand (no game data) and cover the accepted layout, a |
| 227 | bare magic, a truncated chunk, an `ACCC`/`FSPX` container, the `ILDN` name and the extraction index. | 242 | bare magic, a truncated chunk, an `ACCC`/`FSPX` container, the `ILDN` name and the extraction index. |
| 243 | |||
| 244 | ## 7. Build step and run-time lookup | ||
| 245 | |||
| 246 | **Build (D3D-557).** `make_game.py`'s `shaders` stage, after the game's own shaders and the FidelityFX step, runs | ||
| 247 | `bvh_shaders()` on the package's `XboxBvhBuilder.dll`. It is the recipe of section 4, with the extraction index | ||
| 248 | passed along so that each tool reads only what the one before it wrote: | ||
| 249 | |||
| 250 | ```sh | ||
| 251 | python -I tools/pe_shader_containers.py <game>/XboxBvhBuilder.dll --extract <work>/shaders/bvh | ||
| 252 | python -I tools/fssm_unpack.py <work>/shaders/bvh --carve-index <work>/shaders/bvh/index.json | ||
| 253 | python -I tools/shader_disasm.py <work>/shaders/bvh/fssm --unpack-index <work>/shaders/bvh/fssm_index.json --out <work>/shaders/bvh/disasm --mcpu gfx1013 | ||
| 254 | python -I tools/rdna2spirv.py <work>/shaders/bvh/disasm --out <work>/shaders/spirv/bvh --no-asm --max-shared-memory 32768 | ||
| 255 | ``` | ||
| 256 | |||
| 257 | The last line also gets `--wave64-emulate` and the fallback flags of the build, like the game's shaders. | ||
| 258 | |||
| 259 | * **The key is the game shaders' key.** The programs are `FSSM` (section 4), so each index entry gets | ||
| 260 | `fssm_sha256`, the SHA-256 of the blob `fssm_unpack.py` inflates from the container's `XBCH` record. That is what | ||
| 261 | the runtime computes from the container the DLL passes to slot 62, as it does for a shader of `shaders.rpf`. No | ||
| 262 | second key or name table was added. The 52 blobs are 52 different programs, and none of them is in a translated | ||
| 263 | 1.0.2.1 corpus of `shaders.rpf` (0 of 52 hashes among its 26,501 index entries), so the two indexes do not meet; | ||
| 264 | if a program were in both, both entries would be candidates, the game's first. | ||
| 265 | * **One folder, redone as a whole.** The step is reused only when both indexes, every module the translation index | ||
| 266 | names and a matching `bvh_build.json` (written last) are there. It runs again when the DLL, one of the three | ||
| 267 | tools before the translator, the translator, `llvm-mc`, the wave64 mode, the flags or the memory cap change, and | ||
| 268 | it never makes the game's shaders translate again. Before it runs, it removes the containers, blobs, listings and modules of the run before: | ||
| 269 | `pe_shader_containers.py` never overwrites a container, so a second extraction into the same folder would write | ||
| 270 | `<module>_NN.dxbc` beside the first and those would be translated as more programs. A package without the DLL | ||
| 271 | skips the step. | ||
| 272 | * **A program without a rule is reported, not skipped.** There is no skip list. After the translation the step | ||
| 273 | compares the extraction index with the translation index and logs the counts and the name of every program the | ||
| 274 | runtime cannot find (no entry with an `fssm_sha256`, which includes an entry whose metadata failed). It logs them | ||
| 275 | on every run, also when the step is reused. Today that is one line: 51 of 52, `Box16Rehydration` | ||
| 276 | (`buffer_store_short`, #1864). The other 51 are published. When no program translates the step fails and keeps | ||
| 277 | no index. | ||
| 278 | * **`--max-shared-memory 32768`.** The constructor creates a pipeline for every program and throws at the first | ||
| 279 | failure, and the runtime refuses a module over the device's `maxComputeSharedMemorySize` (`E_NOTIMPL`, | ||
| 280 | [design.md](design.md), "Workgroup memory limit"). 32,768 is what the AMD Windows and Intel drivers report. | ||
| 281 | Measured on `main` `3faac557`, by `lds_bytes` and by comparing the modules byte for byte: | ||
| 282 | |||
| 283 | | translation | default cap (65,536) | `--max-shared-memory 32768` | | ||
| 284 | |---|---|---| | ||
| 285 | | native | largest 32,768 B (`BitonicPreSort64CS`, `BitonicInnerSort64CS`, `RadixAlloc`), none over | all 51 modules identical | | ||
| 286 | | `--wave64-emulate` | `RadixAlloc` 37,084 B, the others at most 32,768 B | `RadixAlloc` 22,236 B, the other 50 modules identical | | ||
| 287 | |||
| 288 | So the cap changes one module, and only in an emulated build; the same 51 programs translate with and without | ||
| 289 | it. Without it that build stops on a 32,768-byte device at creation #29: measured on the Radeon below with the | ||
| 290 | emulated modules made at the default cap, `RadixAlloc` is refused (`declares 37084 bytes of workgroup memory`, | ||
| 291 | `0x80004001`) after 28 creations. | ||
| 292 | |||
| 293 | **Run time.** `ShaderLibrary::load(dir)` (`runtime/src/shader_library.h`) reads `<dir>/index.json`, then | ||
| 294 | `<dir>/fspx/index.json` and `<dir>/bvh/index.json` when they are there. The builder's entries are named | ||
| 295 | `bvh/<name>`, the path of their module below `<dir>`, and go into the table of the game's shaders, so | ||
| 296 | `create_compute` finds them through the lookup it already had (`compute_program()` gives an `FSSM` program, looked up | ||
| 297 | by `(cs, fssm_sha256)`). A missing or unreadable `bvh/index.json` leaves the other indexes loaded, and the main | ||
| 298 | index is still required. `bvh_size()` counts the entries, which `shader_library_corpus_test` leaves out of the | ||
| 299 | entries a carved container must reach, like the FSPX ones. | ||
| 300 | |||
| 301 | **Measured: `LEONIDA_BVH_DEVICE=vulkan bvh_builder_test`** (package 1.0.2.1, `LEONIDA_BVH_RECORD62=1`, | ||
| 302 | `LEONIDA_SHADERS` on a folder holding this step's output and an empty main index, since only this step was run). | ||
| 303 | Before the step, on `main` `3faac557`, creation #1 failed on both GPUs: `0x80004005` with no library, `0x80070057` | ||
| 304 | with a translated corpus of 26,521 entries, none of them the builder's. | ||
| 305 | |||
| 306 | | GPU | translation | result | | ||
| 307 | |---|---|---| | ||
| 308 | | RTX 3070, NVIDIA 610.74 (subgroups 32..32, limit 49,152) | `--wave64-emulate` | 49 of 50 creations `S_OK`; #50 `Box16Rehydration` has no module, `E_INVALIDARG`, and the constructor throws `0x80070057` | | ||
| 309 | | same | native | the same 49 of 50; the runtime warns that this device cannot require 64-wide subgroups | | ||
| 310 | | Radeon integrated (Ryzen 7 9700X), AMD 32.0.21042.62 (subgroups 32..64, limit 32,768) | `--wave64-emulate` | 49 of 50 `S_OK`, stops at #50 the same way | | ||
| 311 | | same | native | 49 of 50 `S_OK`, stops at #50 the same way. That is the translator of `main` `561b65a8`; with the one of `3faac557`, 48 creations returned `S_OK` and #49 `LeafRehydration` did not come back from the driver's pipeline compile in 16 minutes (below) | | ||
| 312 | |||
| 313 | The rows above were measured before #1864 (`buffer_store_short`) was on `main`. With it (`main` `8575d36e`) the | ||
| 314 | step translates 52 of 52, and on the Radeon with the emulated modules all 53 creations return `S_OK` and | ||
| 315 | `bvh_builder_test` passes on the Vulkan device; the RTX 3070 and the native modules were not run again. | ||
| 316 | |||
| 317 | Before that, with #1864's translator change applied to a local copy of the tools (not part of this step), 52 of 52 translate | ||
| 318 | and pass `spirv-val`, and with the emulated modules all 53 creations return `S_OK` on both GPUs: the constructor | ||
| 319 | and the destructor return and the test passes. So with the emulated modules the one thing between this step and a | ||
| 320 | constructed builder on the Vulkan device is `buffer_store_short`. The PC-device run of the test (WARP) is | ||
| 321 | unchanged: 53 of 53. | ||
| 322 | |||
| 323 | **`LeafRehydration` on the AMD driver (SHADER-995).** The step was first measured with the translator of `main` | ||
| 324 | `3faac557`. `vk_pipeline_check` then created the same pipelines without the D3D12 layer: all 51 on the RTX 3070 | ||
| 325 | (native and emulated) and on the Radeon (emulated), and on the Radeon with the native modules 50 of 51, while | ||
| 326 | `LeafRehydration`, a 1.66 MB wave64 module, was still compiling when the check was stopped after 15 minutes. Its | ||
| 327 | emulated module was created in 12 s on that driver, and its native module in 11 s on the RTX 3070. | ||
| 328 | |||
| 329 | The stop was that driver's compile time for the control-flow form of one module, not the lookup, native wave64 or | ||
| 330 | the module's size. The translator of `3faac557` emitted a structured tree deeper than 16 in the dispatch-loop form | ||
| 331 | (SHADER-299: one loop around an `OpSwitch` with a case per basic block), and this program's tree nests 18 deep | ||
| 332 | (`ReloadTriangles`' 17; found by lowering `--max-structured-depth` until the form changes). Since SHADER-427 the | ||
| 333 | bound is 64, so with `main` `561b65a8` all 51 native modules are structured (47) or barrier rounds (4), the native | ||
| 334 | row of the table above is that translation, and `vk_pipeline_check` creates all 51 native pipelines on the Radeon | ||
| 335 | (18.4 s on one thread, `LoadInstances` 8.6 s the slowest; the 51 emulated ones 26.9 s, `LeafRehydration` 11.6 s the | ||
| 336 | slowest). No build option is involved, and the step needed no change. @Jadiac5 measured the same translation on an | ||
| 337 | RX 9070 XT (RDNA4, AMD 32.0.32029.2010): 51 of 51 native and 51 of 51 emulated, the slowest module in 19 to 35 s | ||
| 338 | ([their comment](https://github.com/ItsNotRexed/Leonida/pull/5680#issuecomment-7101091229)). Those are the | ||
| 339 | structured modules too, so the dispatch-loop module below has only been tried on the gfx1036 and the RTX 3070. | ||
| 340 | |||
| 341 | Measured on the Radeon (gfx1036, AMD 32.0.21042.62) with `vk_pipeline_check --jobs 1`, one module in the driver at | ||
| 342 | a time beside the one long run. That driver keeps compiled shaders on disk and gives back a module it has seen in | ||
| 343 | 0.2 s, so every time is the first compile of a module, or of a copy with another generator word in its header: | ||
| 344 | |||
| 345 | | `LeafRehydration` module | size | shape | pipeline creation | | ||
| 346 | |---|---|---|---| | ||
| 347 | | dispatch loop, translator of `3faac557` | 1,658,968 B | 1,532 blocks: one loop around an `OpSwitch` with 581 cases | created after 2,039.5 s (34 minutes): 2,014 s of CPU time, one busy thread, the process at 1.2 GB after 5 minutes and 1.7 GB after 21. Slow, not hung | | ||
| 348 | | the same module, pipeline requiring 32-wide subgroups, or no size | | | not within 150 s either way | | ||
| 349 | | dispatch loop, current translator (`--dispatch-loop` or `--max-structured-depth 16`, the same bytes) | 1,192,148 B | 2,954 blocks, the same loop and switch | XXnot created within 10 minutes; the run was stopped there | | ||
| 350 | | structured, the default now | 1,238,360 B | 4,108 blocks, 1,763 selections, no loop, no switch | 2.9 s (3.6 s and 3.2 s for two more copies) | | ||
| 351 | | `--wave64-emulate`, translator of `3faac557` | 1,703,748 B | emulated wave64 structured | 12.6 s | | ||
| 352 | | `--wave64-emulate`, current translator | 1,358,436 B | emulated wave64 structured | 12.4 s | | ||
| 353 | |||
| 354 | What the time depends on, tried on the current translator's dispatch-loop module with a limit of 45 to 150 s per | ||
| 355 | compile: | ||
| 356 | |||
| 357 | * **Not the subgroup operations or the subgroup size.** All three native forms hold the same 590 | ||
| 358 | `OpGroupNonUniformAny` and no other subgroup operation. A copy with each of them replaced by an `OpLogicalOr` (a | ||
| 359 | compile-time probe, not an equivalent module) did not finish in 120 s, and neither did the first module with | ||
| 360 | 32-wide or no required subgroup size. | ||
| 361 | * **Not the size of the switch by itself.** The other programs compile in the dispatch-loop form on that driver, | ||
| 362 | 2.5 to 5 times slower than structured: `LoadInstances` (628 cases, 5.35 MB) in 40.3 s against 8.6 s, | ||
| 363 | `ReloadTriangles` (311) 3.6 s against 0.7 s, `ConstructBVH4` (206) 2.2 s against 0.6 s, `PT_ConstructBVH4` (186) | ||
| 364 | 1.9 s, `LoadTriangles` (185) 2.1 s, `SplitInstances` (153, 1.53 MB) 4.0 s against 1.2 s, `RefitBVH4` (125) 1.3 s, | ||
| 365 | `FindTreelets` (89) 0.9 s, and the wave64 `Box32Rehydration` (47) 0.5 s and `RadixAlloc` (17) 0.4 s. | ||
| 366 | * **It is how much of this program the loop reaches.** With only the first K of the 581 cases left in the | ||
| 367 | `OpSwitch` (the other blocks unreachable; a probe again): K = 64 0.8 s, 128 1.5 s, 256 3.6 s, 272 5.4 s, 280 | ||
| 368 | 23.1 s, 284 24.5 s, 286 43.5 s, 287 42.9 s, and 288, 320, 384 and 512 not within 60 to 90 s. So past case 272 a | ||
| 369 | few more blocks multiply the time. That looks like growth with the paths through the program rather than with | ||
| 370 | its size, which these numbers suggest and do not show. | ||
| 371 | * **Nothing done to the module from outside changed that.** `spirv-opt -O` (1,350,016 B) and the local SSA | ||
| 372 | rewrite with dead-code elimination (`--ssa-rewrite` after the two local eliminations and before | ||
| 373 | `--eliminate-dead-code-aggressive`, 1,120,512 B) validate and did not finish in 120 s; | ||
| 374 | `--eliminate-dead-branches --merge-blocks --cfg-cleanup` returns the module unchanged. Marking every load and | ||
| 375 | store of the dispatch variables `%pc` and `%done` `Volatile`, which keeps the module equivalent, did not finish in | ||
| 376 | 150 s either (whether that driver honours the flag was not checked). `-Os`, `--merge-return` and | ||
| 377 | `--scalar-replacement` were not run. | ||
| 378 | |||
| 379 | So the AMD compiler's time on this module comes from the dispatch-loop form of this one program: of the eleven | ||
| 380 | tried it is the only one that is both large and without a loop of its own (its structured module has no | ||
| 381 | `OpLoopMerge`), which is an observation, not an explanation, and which of the driver's passes takes the time was | ||
| 382 | not looked into. The default translation does not emit that form for any of these 51 programs, so the step keeps | ||
| 383 | what `wave64_settings` chooses for this driver (native, #857) and makes no exception for the builder. A module can | ||
| 384 | still come out in the dispatch-loop form elsewhere (a graph `cfg_structure` cannot make reducible, a tree deeper | ||
| 385 | than the bound, `--dispatch-loop`); [testing.md](testing.md), "Known problems", has the row for that. | ||
| 386 | |||
| 387 | Still open: nothing has dispatched one of these pipelines through the D3D12 layer, so this shows that the | ||
| 388 | constructor's pipelines are created, not that a build produces the right tree (section 5). | ||
| 389 | |||
| 390 | `tests/test_bvh_shaders_stage.py` runs the step on a synthetic DLL (hand-built containers around a made-up `FSSM` | ||
| 391 | blob, no game data): the key the extracted layout gives the translator, the tools and arguments, reuse and each | ||
| 392 | reason to run again, a second run that leaves no duplicate containers, the report of an untranslated program on | ||
| 393 | every run and the failure when nothing translates. `shader_library_test` covers the `bvh/` index (found by the | ||
| 394 | container's program hash, counted apart, a broken or missing index beside the others) and `d3d12_compute_test` | ||
| 395 | creates a pipeline through slot 62 whose only translation is in `<shaders>/bvh/index.json`. | ||