upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/first_light.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/first_light.md')
-rw-r--r--docs/first_light.md88
1 files changed, 74 insertions, 14 deletions
diff --git a/docs/first_light.md b/docs/first_light.md
index 654665c6c..9eaff8f37 100644
--- a/docs/first_light.md
+++ b/docs/first_light.md
@@ -1,9 +1,9 @@
1# First light: a game model's UVs drawn with translated game shaders 1# Drawing model UVs and textures with First Light
2 2
3**Status: measured** on the 1.0.2.1 package and an NVIDIA RTX 2060 SUPER (driver 620.73.04, Vulkan 1.4.357, 3**Status: measured** on the 1.0.2.1 package and an NVIDIA RTX 2060 SUPER (driver 620.73.04, Vulkan 1.4.357,
4CachyOS). The fixture draws a model from the package with a vertex and pixel shader chosen to display its UVs. The image 4CachyOS). The untextured fixture draws a model from the package with a shader pair chosen to display its UVs.
5stays in the user's work folder; this note records the counts and the recipe, not the model, the shaders or a 5With `--dds`, the default pixel shader samples the supplied texture at those UVs. Section 6 gives the textured
6screenshot. 6draw recipe. The image stays in the user's work folder.
7 7
8## 1. What was drawn 8## 1. What was drawn
9 9
@@ -14,7 +14,7 @@ those counts and has SHA-256
14assembles it, `tools/first_light.py`, centres and fits X/Y to NDC and flattens Z (the chosen VS applies no 14assembles it, `tools/first_light.py`, centres and fits X/Y to NDC and flattens Z (the chosen VS applies no
15matrix), so the draw is an orthographic projection of the model's own geometry. 15matrix), so the draw is an orthographic projection of the model's own geometry.
16 16
17The shaders are two ordinary containers from `shaders.rpf`, carved and translated by the repository's own tools: 17The untextured fixture uses two containers from `shaders.rpf`, carved and translated by the repository's tools.
18 18
19| stage | FSSM SHA-256 | what it does | 19| stage | FSSM SHA-256 | what it does |
20|---|---|---| 20|---|---|---|
@@ -25,7 +25,7 @@ The model's authored shader assignment can be checked separately. In the model e
25above, `MeshShaders[0]` selects material slot 0, the only slot. Looking up its `Materials[0].Shader` in the 25above, `MeshShaders[0]` selects material slot 0, the only slot. Looking up its `Materials[0].Shader` in the
26native shader database yields one effect with one technique and four compute-only passes. The VS and PS 26native shader database yields one effect with one technique and four compute-only passes. The VS and PS
27in the table are the fixture's chosen pair. The model's use during gameplay remains unverified. 27in the table are the fixture's chosen pair. The model's use during gameplay remains unverified.
28See the [effect lookup](https://github.com/ItsNotRexed/Leonida/pull/3036) and the 28See the [effect lookup tool](../tools/sgd5_effects.py) and the
29[Windows reproduction of the UV draw](https://github.com/ItsNotRexed/Leonida/pull/2893#issuecomment-7084947193). 29[Windows reproduction of the UV draw](https://github.com/ItsNotRexed/Leonida/pull/2893#issuecomment-7084947193).
30 30
31Both disassemble with no invalid word, translate, and pass `spirv-val`; `tools/shader_link_check.py check` reports 31Both disassemble with no invalid word, translate, and pass `spirv-val`; `tools/shader_link_check.py check` reports
@@ -69,8 +69,10 @@ python -I tools/first_light.py --check --out ~/leonida-work/first_light/out
69``` 69```
70 70
71`--check` on an existing folder re-runs the harness through the same fresh path and `check_image` as the test; a 71`--check` on an existing folder re-runs the harness through the same fresh path and `check_image` as the test; a
72harness that exits 0 without writing, an all-opaque image and a swapped-U/V image all exit 1. The test has seven 72harness that exits 0 without writing, an all-opaque image and a swapped-U/V image all exit 1. The test has
73cases: the two package-gated draw/manifest checks, the `--check` command and four oracle fixtures. 73twenty-five cases: the two package-gated draw/manifest checks, the `--check` command, four oracle fixtures, the
74mesh depth fit, a preview with non-finite readback channels, the stage files of two shaders with the same name, two
75OBJ parsing checks, seven checks of the textured path (section 6) and six CPU checks of `--camera` (section 7).
74 76
75`first_light.py` carves `shaders.rpf` if the work folder does not have it, translates only the chosen pair, converts 77`first_light.py` carves `shaders.rpf` if the work folder does not have it, translates only the chosen pair, converts
76the OBJ to the harness's mesh file (`u32 vertex_count, u32 index_count`, `(x,y,z,u,v) f32` vertices, `u32` 78the OBJ to the harness's mesh file (`u32 vertex_count, u32 index_count`, `(x,y,z,u,v) f32` vertices, `u32`
@@ -82,6 +84,9 @@ Each run stages its pair in `<work>/first_light/dxbc` and translates it into `<w
82are removed at the start of a run, so they hold that run's two shaders and nothing an earlier run selected; do not 84are removed at the start of a run, so they hold that run's two shaders and nothing an earlier run selected; do not
83keep files there. The output folder (`first_light/out` or `--out`) and `<work>/shaders` are not emptied. If either 85keep files there. The output folder (`first_light/out` or `--out`) and `<work>/shaders` are not emptied. If either
84folder is a symlink or a junction, or a file in it cannot be removed, the run stops and names it. 86folder is a symlink or a junction, or a file in it cannot be removed, the run stops and names it.
87With `--keep-z` or `--no-fit`, a triangle can cross the near or far clip plane. For the
88passthrough VS, the CPU predicts a UV only where the interpolated depth is between 0 and 1,
89inclusive. If several triangles cover a probe there, the last one drawn supplies its UV.
85 90
86## 4. The harness mode 91## 4. The harness mode
87 92
@@ -135,9 +140,10 @@ The CPU oracle (`check_image`) samples the decoded `R8G8B8A8_UNORM` texture bili
135`centre_uv` and each `uv_probes` entry and requires the GPU readback to match within `0.02` (rejecting the 140`centre_uv` and each `uv_probes` entry and requires the GPU readback to match within `0.02` (rejecting the
136magenta unbound-texture fallback and wrong-colour probes). Verified on Windows 11 with an NVIDIA GeForce RTX 3060 141magenta unbound-texture fallback and wrong-colour probes). Verified on Windows 11 with an NVIDIA GeForce RTX 3060
137(driver 621.24, Vulkan 1.4.357): **12,804/16,384 (0.781)** covered. Without `LEONIDA_FIRST_LIGHT_DIR`, 142(driver 621.24, Vulkan 1.4.357): **12,804/16,384 (0.781)** covered. Without `LEONIDA_FIRST_LIGHT_DIR`,
138`python -I tests/test_first_light.py -v` runs **14 tests: 11 passed, 3 skipped** (the 3 package-gated 143`python -I tests/test_first_light.py -v` runs **46 tests: 42 passed, 4 skipped** (the 3 package-gated
139`FirstLightTests` require `LEONIDA_FIRST_LIGHT_DIR`; the 4 `OracleTests` and 7 `TexturedOracleTests` run without a 144`FirstLightTests` require `LEONIDA_FIRST_LIGHT_DIR`, and `test_harness_rejects_wrapped_tsharp_va_overflow` needs a
140package). With `LEONIDA_FIRST_LIGHT_DIR` set against a local package run, all **14/14** tests pass. 145built `vk_harness`; the 4 `OracleTests` and the other 11 `TexturedOracleTests` run without a package). With
146`LEONIDA_FIRST_LIGHT_DIR` set against a local package run (and `vk_harness` built) none of the 46 is skipped.
141 147
142```sh 148```sh
143python -I tools/first_light.py --game "<package>" --work ~/leonida-work \ 149python -I tools/first_light.py --game "<package>" --work ~/leonida-work \
@@ -146,10 +152,64 @@ python -I tools/first_light.py --game "<package>" --work ~/leonida-work \
146LEONIDA_FIRST_LIGHT_DIR=~/leonida-work/first_light/out python -I tests/test_first_light.py -v 152LEONIDA_FIRST_LIGHT_DIR=~/leonida-work/first_light/out python -I tests/test_first_light.py -v
147``` 153```
148 154
149## 7. Limits and next 155## 7. A camera (TEST-523)
150 156
151* The chosen VS applies no matrix; the tool's fit is an orthographic stand-in for the game's view/projection. A 157`first_light.py --camera` draws the same model in perspective with a game VS that applies a world matrix and a
152 real camera needs a VS that reads its constants, which is the next model/shader pair. 158view-projection matrix of its own, read from two constant buffers. The PS is the one from section 1:
159
160| stage | FSSM SHA-256 | what it does |
161|---|---|---|
162| VS | `e909a9e5a0096e00bc2b621ae3bdc96ac904f3047aed3917835774cbccb09716` | POSITION × world × view-projection to clip space (computed w); TEXCOORD to a varying |
163| PS | `ed4147a5f7514947c2eb8d79255bd80f72761ac1ba2205dd811d3e050cf1b495` | as in section 1 |
164
165The VS's scalar loads give its constant layout. The 32-bit address of a descriptor table is in s9 (the high half is
1660). The V# at table + 0x20 points at the world rows: X, Y, Z and translation, one float4 each at 0x00–0x30, of
167which the VS reads xyz. The V# at table + 0x60 points at a constant buffer whose view-projection rows sit at
1680x160–0x19f. Both matrices multiply a row vector: `world = [x y z 1] W`, `clip = [world 1] VP`. The view-projection
169block is common in the game: on 1.0.2.1, all 9 translated VS modules with exactly POSITION and TEXCOORD inputs and
170a clip w that is not the constant 1.0 read 16 dwords at 0x160 of a constant buffer, as do 123 of the 254 VS
171modules with a POSITION input and a computed w, and 1,266 of all 2,236 VS modules. That it is the view-projection
172everywhere is a hypothesis; for this VS the arithmetic shows it.
173
174The mesh keeps the model's own coordinates. The tool builds a world matrix that centres the model and scales its
175bounding sphere to radius 1. The camera sits 3 radii away, `--yaw` degrees around Z (the game's up axis) from −Y
176and `--pitch` degrees above the XY plane, with a vertical `--fov` (defaults 30, 25 and 50). The projection is
177Vulkan's, with the near and far planes just outside the sphere, so depth clips no vertex. The tool writes the two
178matrices, the two V#s (raw buffers, stride 0, sized to what the VS reads) and the table into `vk_harness`'s heap
179(`VKH_HEAP`) and the table address into s9 (`VKH_SGPRS`). The manifest's `camera` block keeps both, so `--check`
180and the test draw with the same constants. The prediction projects the same vertices with the same float32
181matrices on the CPU and interpolates UV perspective-correctly, dividing each screen-space weight by its vertex's
182clip w. The output goes to `<work>/first_light/camera`, leaving the orthographic folder for section 5.
183
184On an Apple M4 (macOS, MoltenVK 1.4.2), with the section 1 model and the default camera at 128×128:
185
186* The draw covers **6,851 of 16,384 pixels (0.418)**.
187* The centre pixel matches the predicted UV `(0.8100, 0.3096)`, and so do the four U ≠ V probes, within the
188 oracle's 2e-3. The corner keeps the clear value.
189* On a full 32×32 grid of pixel centres, the predicted coverage agrees on all 1,024. The UV agrees within 2e-3 on
190 all 428 covered ones, with the worst at 5.05e-4.
191* The same draw without the constants covers nothing and is rejected. With the camera turned 2° in yaw, the
192 centre pixel is off and the check is rejected too.
193* The pair passes `spirv-val`, and `shader_link_check.py check` reports it compatible.
194
195The model's triangles are small, so perspective-correct and affine UV differ by at most 1e-4 at the probes. The
196image therefore does not tell the two apart; `CameraTests` in `tests/test_first_light.py` checks the
197perspective-correct weights on one large triangle instead. Those CPU tests also read the constants back the way
198the VS's loads do (table, V#, num_records, rows) and compare the result with the matrices the prediction uses.
199
200```sh
201python -I tools/first_light.py --game "<package>" --work ~/leonida-work \
202 --obj ~/leonida-work/assets/data/models/<model>.obj --camera --run # coverage window 0.2-0.99
203LEONIDA_FIRST_LIGHT_DIR=~/leonida-work/first_light/camera python -I tests/test_first_light.py -v
204```
205
206## 8. Limits and next
207
208* The camera draw (section 7) runs in `vk_harness` only. Through the D3D12 layer, s9 and the table it points at
209 have to come from root arguments, which `d3d12_first_light_test` does not set up yet.
210* `--camera` and `--dds` don't combine: the camera VS's table and the textured PS's T# both take s9 of the shared
211 push constants and heap VA 0x100, so `first_light.py` refuses the pair. A textured perspective draw needs a
212 second constant layout.
153* One model and one draw. The `levels.rpf` windows have world props with POSITION/NORMAL/COLOUR/UV layouts that 213* One model and one draw. The `levels.rpf` windows have world props with POSITION/NORMAL/COLOUR/UV layouts that
154 match the `sga_dr_*`/`Rage_Transform*` families by signature; pairing one of those is the natural next image. 214 match the `sga_dr_*`/`Rage_Transform*` families by signature; pairing one of those is the natural next image.
155* The game still cannot boot: the executable is not in the package (RT-5), so no call site selects these programs. 215* The game still cannot boot: the executable is not in the package (RT-5), so no call site selects these programs.