upleb.uk

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

summaryrefslogtreecommitdiff
path: root/docs/gbuffer.md
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-22 18:23:56 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-22 18:23:58 -0300
commit20daca0435526645bfe274de5d0aa71573ec930d (patch)
tree1332ff15d70d1e5fbb8eb0a087177050abd2eb90 /docs/gbuffer.md
parent5122533e59dd1e416d7a6da9ae51d140db095827 (diff)
docs/gbuffer: clarify OSG1 vs FSSM +0xFC, 1702/1703 counts, and RTZ truncation vs rounding
Diffstat (limited to 'docs/gbuffer.md')
-rw-r--r--docs/gbuffer.md88
1 files changed, 49 insertions, 39 deletions
diff --git a/docs/gbuffer.md b/docs/gbuffer.md
index e8a3347df..640245c9d 100644
--- a/docs/gbuffer.md
+++ b/docs/gbuffer.md
@@ -1,46 +1,51 @@
1# Deferred G-buffer targets and declared output signatures (`PROC-146`) 1# Deferred G-buffer targets and declared output signatures (`PROC-146`)
2 2
3Reported by @Silliana in [#1891](https://github.com/ItsNotRexed/Leonida/issues/1891) for `PS_DeferredTextured` and 3Reported by @Silliana in [#1891](https://github.com/ItsNotRexed/Leonida/issues/1891) for `PS_DeferredTextured` and
4checked here across all **2,989 `*Deferred*` pixel shaders** (and all **1,703 six-target pixel shaders**) in the 4checked here across the `*Deferred*` pixel shaders and six-output pixel shaders in the 1.0.2.1 `shaders.rpf` corpus.
51.0.2.1 `shaders.rpf` corpus. Counts, target masks, numeric constants and format requirements only; no shader text or 5Counts, target masks, numeric constants and format requirements only; no shader text or disassembly.
6disassembly.
7 6
8## 1. Corpus census of deferred pixel-shader output signatures (`OSG1` / `PSV0`) 7## 1. Corpus census of deferred pixel-shader output signatures (`OSG1` / `PSV0`)
9 8
10Every DXBC container in `shaders.rpf` carries an `OSG1` (and `PSV0`) output signature table with `{register, 9Every DXBC container in `shaders.rpf` carries an `OSG1` (and `PSV0`) output signature table with `{register,
11semantic_index, component_type, mask, min_precision}` per `SV_TargetN` output, and the unpacked FSSM header records the 10semantic_index, component_type, mask, min_precision}` per declared output element. (The FSSM header dword at `+0x0FC`
12exported target count as `u32` at `+0x0FC`. 11holds `5` and `6` on the two sampled `PS_DeferredTextured` blobs below, but holds `16` and `18` on two stock vehicle
12deferred passes, so target counts must be read from `OSG1` rather than FSSM `+0x0FC`.)
13 13
14Across the **2,989** pixel shaders with `Deferred` in their container name (including **807** `PS_DeferredTextured` 14Depending on whether container names are matched case-insensitively on raw RPF entry names (`2,989` pixel shaders) or
15variants: 380 five-target, 371 six-target and 56 three-target): 15case-sensitively on `Deferred` in the carved `.dxbc` file stems (`3,004` pixel shaders, including `807`
16`PS_DeferredTextured` variants: 380 five-target, 371 six-target and 56 three-target):
16 17
17| Targets declared (`+0x0FC` / `OSG1`) | `*Deferred*` PS count | `PS_DeferredTextured` count | Corpus-wide PS count | `OSG1` component type on all colour targets | Per-target `OSG1` write masks (`SV_Target0..N-1`) | 18| `OSG1` outputs declared | `*Deferred*` PS (entry match / `.dxbc` stem match) | `PS_DeferredTextured` count | Corpus-wide PS (entry / `.dxbc` stem) | `OSG1` component type on all colour targets | Per-target `OSG1` write masks (`SV_Target0..N-1`) |
18|---:|---:|---:|---:|---|---| 19|---|---:|---:|---:|---|---|
19| 6 (`SV_Target0..5`) | 953 | 371 | 955 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0x3, 0xF` (`mrt4` is 2-component `RG`) | 20| 6 colour (`SV_Target0..5`) | 953 / 949 | 371 | 955 / 955 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0x3, 0xF` (`SV_Target4` mask is 2-component `RG`) |
20| 6 (`SV_Target0..5`) | 737 | 0 | 747 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0xF, 0xF` (all six `RGBA`) | 21| 6 colour (`SV_Target0..5`) | 737 / 735 | 0 | 747 / 747 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0xF, 0xF` (all six `RGBA`) |
21| 5 (`SV_Target0..4`) | 1,160 | 380 | 1,168 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0x3` (`mrt4` is 2-component `RG`) | 22| 5 colour + `SV_Depth` | 0 / 0 | 0 | 1 / 0 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0x3` + `SV_Depth` (`0x1`) |
22| 5 (`SV_Target0..4`) | 2 | 0 | 20 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0xF` | 23| 5 colour (`SV_Target0..4`) | 1,160 / 1,160 | 380 | 1,168 / 1,166 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0x3` (`SV_Target4` mask is 2-component `RG`) |
23| 3 (`SV_Target0..2`) | 125 | 56 | 248 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0x3` (`mrt2` is 2-component `RG`) | 24| 5 colour (`SV_Target0..4`) | 2 / 2 | 0 | 20 / 20 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0xF, 0xF, 0xF` |
24| other (`0, 1, 2, 4` or `5 + SV_Depth`) | 12 | 0 | — | `float32` (3 single-target `uint32`) | depth-only, discard, or reduced passes | 25| 3 colour (`SV_Target0..2`) | 125 / 125 | 56 | 248 / 248 | `float32` (`ctype = 3`, `prec = 0`) | `0xF, 0xF, 0x3` (`SV_Target2` mask is 2-component `RG`) |
26| other (`0..4` outputs) | 12 / 33 | 0 | — | `float32` (3 single-target `uint32`) | depth-only, discard, or reduced passes |
25 27
26In the compiled RDNA2 code of every 5-target and 6-target `PS_DeferredTextured` variant, all exported targets 28The two six-colour-target rows total **1,702** pixel shaders corpus-wide (`955 + 747`; plus 1 five-colour-target +
27(`mrt0..mrt4` or `mrt0..mrt5`) pack their outgoing register pairs with `v_cvt_pkrtz_f16_f32` and export them with 29`SV_Depth` shader with 6 `OSG1` rows = 1,703 six-row `OSG1` tables). In the compiled RDNA2 code across the `*Deferred*`
28`exp mrtN, ... compr` (`EXP_FORMAT_FP16_ABGR`). 30pixel shaders, **16,306 of 16,306 (100%)** `exp mrtN` instructions use compressed half-float export (`compr`,
31`EXP_FORMAT_FP16_ABGR`), fed by `v_cvt_pkrtz_f16_f32`.
29 32
30## 2. G-buffer target layout (`mrt0..mrt5`) 33## 2. G-buffer target layout (`mrt0..mrt5`)
31 34
32Confirming #1891 against the 5-target (`mrt0..mrt4`) and 6-target (`mrt0..mrt5`) `PS_DeferredTextured` programs: 35Comparing #1891 against the 5-target (`mrt0..mrt4`) and 6-target (`mrt0..mrt5`) `PS_DeferredTextured` programs (note
36that the RTV format column below is **inferred from the shader-side encoding and #1891**, not declared by `OSG1` or a
37captured PSO):
33 38
34| Target | `OSG1` mask | Expected RTV format class | Channel layout and encoding in `PS_DeferredTextured` | 39| Target | `OSG1` mask | Inferred RTV format class (from shader encoding / #1891) | Channel layout and shader-side encoding in `PS_DeferredTextured` |
35|---|---|---|---| 40|---|---|---|---|
36| `mrt0` (`SV_Target0`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Albedo + alpha/material byte:** `rgb` holds base colour/albedo; in 6-target variants `a` packs a byte extracted from an integer register and scaled by `1.0 / 255.0` (`0x3b808081`). | 41| `mrt0` (`SV_Target0`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Albedo + alpha/material byte:** `rgb` holds base colour/albedo; in the sampled 6-target variant `a` packs a byte extracted from an integer register (`ubyte0`) and scaled by `1.0 / 255.0` (`0x3b808081`). |
37| `mrt1` (`SV_Target1`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Scalar in `r` + 24-bit octahedral world normal split across `g`, `b`, `a`:** see section 3 below. | 42| `mrt1` (`SV_Target1`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Scalar in `r` + 24-bit octahedral world normal split across `g`, `b`, `a`:** see section 3 below. |
38| `mrt2` (`SV_Target2`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Square-root encoded material/specular channels:** `rgb` values pass through hardware square root (`sqrt(x)`, or `sqrt(1.0 - sqrt(x))` on 6-target variants) before export so the deferred lighting pass squares them back; `a` holds an unencoded `[0, 1]` scalar (`0.5` or a conditional select). | 43| `mrt2` (`SV_Target2`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Square-root encoded channels:** producer applies hardware square root (`sqrt(x)` or `sqrt(1.0 - sqrt(x))`) before `v_cvt_pkrtz_f16_f32` export; #1891 notes that the lighting consumer squares `mrt2` back (tentatively specular / $F_0$ material terms, pending lighting-consumer verification). |
39| `mrt3` (`SV_Target3`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Flags + byte IDs:** `x` and `y` carry clamped `[0, 1]` material/shading flags biased by a dither term (`scale = 2.0 / 255.0` = `0x3c008081`, `offset = -1.0 / 255.0` = `0xbb808081`); `z` and `w` carry two 8-bit integer IDs extracted with byte-to-float conversion (`ubyte0`, `ubyte1`) and multiplied by `1.0 / 255.0` (`0x3b808081`). | 44| `mrt3` (`SV_Target3`) | `0xF` (`RGBA`) | `R8G8B8A8_UNORM` | **Flags + byte IDs:** `x` and `y` carry clamped `[0, 1]` values biased by a dither term (`scale = 2.0 / 255.0` = `0x3c008081`, `offset = -1.0 / 255.0` = `0xbb808081`); `z` and `w` carry two 8-bit integer IDs extracted with byte-to-float conversion (`ubyte0`, `ubyte1`) and multiplied by `1.0 / 255.0` (`0x3b808081`). |
40| `mrt4` (`SV_Target4`) | `0x3` (`RG`, 2,113 deferred PS) or `0xF` (`RGBA`, 739 deferred PS) | Float format (e.g. `R16G16_FLOAT`) | **Motion vectors:** unscaled signed float screen-space motion offset (`xy`, with `zw` disabled as `off, off` on the 2,113 `mask = 0x3` variants). | 45| `mrt4` (`SV_Target4`) | `0x3` (`RG`, 2,113 / 2,109 deferred PS) or `0xF` (`RGBA`, 739 / 737 deferred PS) | Float format (e.g. `R16G16_FLOAT`) | **Motion vectors:** unscaled signed float screen-space motion offset (`xy`; in the sampled 5- and 6-target `PS_DeferredTextured` disassembly with `mask = 0x3`, exported as `exp mrt4, vN, vN, off, off compr`). |
41| `mrt5` (`SV_Target5`) | `0xF` (`RGBA`, 1,690 deferred PS) | `R8G8B8A8_UNORM` | **Sixth deferred target** on the 6-target permutations (`rgb` material terms + clamped alpha). | 46| `mrt5` (`SV_Target5`) | `0xF` (`RGBA`, 1,690 / 1,684 deferred PS) | `R8G8B8A8_UNORM` | **Sixth deferred target** on the 6-target permutations (`rgb` material terms + clamped alpha). |
42 47
43## 3. The 24-bit octahedral normal in `mrt1.gba` and why binding `f16` targets breaks lighting 48## 3. The 24-bit octahedral normal in `mrt1.gba` and `UNORM8` vs `FLOAT16` storage
44 49
45`PS_DeferredTextured` does not store a 3-channel `(nx, ny, nz)` normal in `mrt1`. Instead: 50`PS_DeferredTextured` does not store a 3-channel `(nx, ny, nz)` normal in `mrt1`. Instead:
46 51
@@ -55,19 +60,24 @@ Confirming #1891 against the 5-target (`mrt0..mrt4`) and 6-target (`mrt0..mrt5`)
55 multiplied by `1.0 / 255.0` (`0x3b808081`), rounded toward zero to `f16` (`v_cvt_pkrtz_f16_f32`), and exported to 60 multiplied by `1.0 / 255.0` (`0x3b808081`), rounded toward zero to `f16` (`v_cvt_pkrtz_f16_f32`), and exported to
56 `mrt1.g`, `mrt1.b`, and `mrt1.a`. 61 `mrt1.g`, `mrt1.b`, and `mrt1.a`.
57 62
58### Consequence for the runtime and test harnesses 63### Arithmetic across all 256 byte values and consequence for the runtime
59 64
60- **Declared signatures do not separate `UNORM8` from `FLOAT16`:** because `OSG1` and `PSV0` declare `float32` on all 65- **Declared signatures do not separate `UNORM8` from `FLOAT16`:** because `OSG1` and `PSV0` declare `float32` on all
61 six `SV_Target0..5` outputs and the RDNA2 code uses `exp mrtN ... compr` (`EXP_FORMAT_FP16_ABGR`) for all of them, 66 six `SV_Target0..5` outputs and the RDNA2 code uses `exp mrtN ... compr` (`EXP_FORMAT_FP16_ABGR`) for all of them,
62 neither the DXBC signature nor the FSSM export opcode indicates that `mrt0..mrt3` (and `mrt5`) are `R8G8B8A8_UNORM` 67 neither the DXBC signature nor the FSSM export opcode indicates which G-buffer targets are `R8G8B8A8_UNORM` and which
63 while `mrt4` is a float motion target. 68 target (`mrt4`) is a float motion target.
64- **Why binding `R16G16B16A16_FLOAT` to `mrt1` or `mrt3` corrupts the packed bytes:** `v_cvt_pkrtz_f16_f32` rounds 69- **`UNORM8` vs `SFLOAT16` round-trip across `b = 0..255`:** `v_cvt_pkrtz_f16_f32(float32(b) * 0x3b808081)` rounds
65 `byte * (1.0 / 255.0)` **toward zero** (`RTZ`), producing an `f16` value slightly *below* `byte / 255.0` for most byte 70 **toward zero** (`RTZ`) to binary16, placing all **254 intermediate bytes (`b = 1..254`)** slightly *below* `b / 255.0`
66 values `1..254`. When `mrt1` is bound as `R8G8B8A8_UNORM`, the render-target output stage quantizes with 71 (with a maximum shortfall of `0.1240234375` of a byte before integer conversion):
67 $\lfloor x \cdot 255.0 + 0.5 \rfloor$ and restores the exact byte `0..255`. When `mrt1` is bound as an `f16` float 72 - When the attachment is `R8G8B8A8_UNORM`, the render-target output stage quantizes with nearest-integer rounding
68 target and the lighting pass reconstructs the bytes by multiplying by `255.0`, the RTZ truncation drops 1 LSB on 73 ($\lfloor x \cdot 255.0 + 0.5 \rfloor$) and stores **256 of 256** bytes exactly in every channel.
69 `ubyte1` (bits `[15:8]`, which straddles the 12-bit split between $q_u$ and $q_v$) and `ubyte2` (bits `[23:16]`), 74 - When the attachment is `R16G16B16A16_SFLOAT`, the stored binary16 RTZ value is kept verbatim. How a downstream
70 shifting the reconstructed normal by whole octahedral steps and producing speckled lighting (#1891). 75 lighting shader recovers each byte from `x * 255.0` determines whether a 1-LSB error occurs: **nearest-integer
71- The runtime must always bind the PSO's declared `RTVFormats[]` (`R8G8B8A8_UNORM` for the byte-packed G-buffer targets 76 rounding** recovers **256 of 256** bytes, whereas **integer truncation** (`uint32(x * 255.0)`) recovers only
72 and a float format for `mrt4`) and must never infer `R16G16B16A16_FLOAT` for all G-buffer attachments from `OSG1` or 77 **2 of 256** bytes (`0` and `255`) and yields `b - 1` on all **254** intermediate values `1..254` across all three
73 `exp ... compr`. 78 byte positions (`ubyte0`, `ubyte1`, `ubyte2`). When `ubyte1` (bits `[15:8]`, straddling the 12-bit boundary between
79 $q_u$ and $q_v$) or `ubyte2` (bits `[23:16]`) drops by 1 under a truncating consumer, the reassembled 24-bit
80 octahedral normal shifts by large steps (#1891; actual lighting-consumer decode and PSO formats remain to be
81 verified against the lighting pass).
82- The runtime must therefore bind the PSO's declared `RTVFormats[]` rather than inferring `R16G16B16A16_FLOAT` for all
83 G-buffer attachments from `OSG1` or `exp ... compr`.