Skip to content

docs: document deferred G-buffer layout and declared output signatures (PROC-146) - #3246

Closed
Andretuta wants to merge 0 commit into
ItsNotRexed:mainfrom
Andretuta:docs/proc-146-deferred-gbuffer
Closed

Andretuta wants to merge 0 commit into
ItsNotRexed:mainfrom
Andretuta:docs/proc-146-deferred-gbuffer

Conversation

@Andretuta

Copy link
Copy Markdown
Contributor

Closes PROC-146 (claim on #3047, findings from #1891 by @Silliana).

Summary

Adds docs/gbuffer.md (indexed in docs/README.md) documenting @Silliana's deferred G-buffer findings from #1891 (PS_DeferredTextured) and cross-checking them against the declared OSG1/PSV0 output signatures and FSSM export instructions across all 2,989 *Deferred* pixel shaders (and all 1,703 six-target pixel shaders) in shaders.rpf (1.0.2.1 package; counts, masks, constants, and format requirements only — zero shader disassembly or game data):

  1. Corpus census (OSG1 / PSV0 / FSSM +0x0FC):
    • Among the 2,989 *Deferred* pixel shaders (including 807 PS_DeferredTextured variants: 380 five-target, 371 six-target, 56 three-target):
      • 953 declare 6 targets (SV_Target0..5) with write masks 0xF, 0xF, 0xF, 0xF, 0x3, 0xF (mrt4 is 2-component RG; 955 corpus-wide).
      • 737 declare 6 targets (SV_Target0..5) with write masks 0xF on all six (747 corpus-wide).
      • 1,160 declare 5 targets (SV_Target0..4) with write masks 0xF, 0xF, 0xF, 0xF, 0x3 (1,168 corpus-wide).
    • 100% of the colour outputs in those signatures declare component_type = 3 (float32, min_precision = 0) in OSG1/PSV0 and export via v_cvt_pkrtz_f16_f32 + exp mrtN, ... compr (EXP_FORMAT_FP16_ABGR).
  2. Per-target encoding (mrt0..mrt5):
    • mrt0 (R8G8B8A8_UNORM): albedo rgb + alpha/material byte (1.0 / 255.0 = 0x3b808081).
    • mrt1 (R8G8B8A8_UNORM): scalar in r, and a 24-bit octahedral world normal split across g, b, a ($q_u, q_v \in [0, 4095]$ quantized with 2047.5 = 0x44fff000, packed as $q = (q_v \ll 12) \mid q_u$, split into ubyte0, ubyte1, ubyte2, and multiplied by 1.0 / 255.0 = 0x3b808081).
    • mrt2 (R8G8B8A8_UNORM): hardware sqrt-encoded material/specular channels (rgb), squared back by the lighting pass.
    • mrt3 (R8G8B8A8_UNORM): dither-biased [0, 1] flags in xy (2.0 / 255.0 = 0x3c008081, -1.0 / 255.0 = 0xbb808081) and two 8-bit integer IDs (ubyte0, ubyte1 $\times 1.0 / 255.0$) in zw.
    • mrt4 (float format, e.g. R16G16_FLOAT): screen-space motion vectors (mask = 0x3 RG on 2,113 deferred PS).
    • mrt5 (R8G8B8A8_UNORM): sixth deferred target on the 1,690 6-target permutations.
  3. Why binding f16 to all targets mangles the normal:
    • Because OSG1 declares float32 and the shader exports with v_cvt_pkrtz_f16_f32 (RTZ, round-toward-zero) on all targets, byte * (1.0 / 255.0) truncated to f16 lands slightly below byte / 255.0. An R8G8B8A8_UNORM attachment restores the exact byte via ROP rounding ($\lfloor x \cdot 255.0 + 0.5 \rfloor$), whereas binding R16G16B16A16_FLOAT causes ubyte1 (straddling the 12-bit boundary between $q_u$ and $q_v$) and ubyte2 to drop 1 LSB when multiplied by 255.0.

Verification

  • python -I -W error tests/test_text_write_line_endings.py -v

AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent documentation/arithmetic review at993eedb3. Please correct three claims before merge:

  1. docs/gbuffer.md:64–70: multiplication by255 does not alone demonstrate a lost bit. Exhaustive synthetic binary32 scaling→binary16 RTZ for all256byte values, then ×255: rounding-to-nearest recovers256/256; integer truncation gives b−1 for254/256 (1..254), maximum shortfall0.1240234375 byte units. The result depends on the lighting consumer's integer conversion, not established by #1891, and applies to all three byte positions. Qualify the mechanism until that conversion is measured.
  2. Census rows955+747=1702, not the stated1703 six-target shaders. Identify the remaining signature or correct the total. Deferred2989 and named-family807 sums agree. The2113 target4 RG-mask signatures do not independently prove disabled export operands across that population; cite an instruction-level count or keep the statement at signature scope.
  3. Signature types/compressed half exports establish shader-side representation, not actual RTV attachment formats. Keep the table's formats qualified until matching PSO/bound-view evidence. Producer sqrt does not establish the consumer's inverse or material meaning; #1891 treats the specular interpretation as tentative.

Normal-word packing is internally consistent and0x44fff000=2047.5. Exact-head documentation whitespace checks passed. No corpus totals or GPU behavior independently reproduced; synthetic CPU arithmetic only.

@mrp1no

mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Checked 993eedb against my own translated 1.0.2.1 work folder, as @KoertLichtendonk asked in the coordination issue. I read OSG1 from all 26,503 carved DXBC containers and took the stage from the DXIL program header (12,101 pixel shaders). CPU only, counts only.

Matches exactly:

  • PS_DeferredTextured: 807, of them 380 with five targets, 371 with six and 56 with three.
  • Five targets with masks F,F,F,F,3 among *Deferred*: 1,160.
  • Corpus-wide six-target F,F,F,F,3,F: 955; corpus-wide all-F six: 747.
  • Every exp mrtN in the *Deferred* pixel shaders is compr: 16,306 of 16,306.
  • All colour targets of the 5- and 6-target signatures are ctype = 3, min_precision = 0. The only non-float targets among *Deferred* are 3 single-target shaders with a uint target. They're outside the table, so the "100%" holds for the rows it's stated for.
  • The §3 rounding argument, checked numerically for bytes 0–255 with 0x3b808081: v_cvt_pkrtz_f16_f32 puts all of bytes 1–254 below b/255. UNORM8 quantization (floor(x*255+0.5)) restores all 256 exactly, while truncating x*255 loses 1 LSB on all of 1–254. So the "bind the declared RTVFormats, never infer f16" conclusion stands.

Small differences. I select by Deferred in the container file name, case-sensitive:

PR my folder
*Deferred* pixel shaders 2,989 3,004
six targets F,F,F,F,3,F among them 953 949
six targets all F among them 737 735
corpus-wide F,F,F,F,3 1,168 1,166
corpus-wide six-target 1,703 1,702

The ratios and the conclusions don't change, so I'd only ask how the *Deferred* set was selected: from RPF entry names, or with duplicates (~N) folded? Then the doc can say so and the counts can be reproduced. One wording nit: the R8G8B8A8_UNORM / R16G16_FLOAT column comes from the encoding, not from anything the shader declares (line 62 says so). Labelling that column "inferred format" would keep it from being read as measured.

Counts only, no shader names or data. AI-assisted (Claude Code).

@jeremygold02

Copy link
Copy Markdown
Contributor

I checked 993eedb376d9fefd809ab550a445e33da27ff2b5 against two stock vehicle deferred passes and ran a synthetic byte ramp on the RTX 5070. Two corrections would make the findings reproducible.

The FSSM field at +0xFC differs from the color-target count in both vehicle programs. For the stock tire and rim, I followed the first deferred_drawmeshlets pass to its specific pixel program, counted the SV_Target elements in its OSG1, then read a little-endian u32 at offset 252 from byte zero of the same program's decompressed FSSM magic.

Pixel pass Declared color targets Declared component masks, in order FSSM +0xFC (decimal)
Tire 5 F, F, F, F, 3 16
Rim 6 F, F, F, F, 3, F 18

All eleven elements have component type 3 and minimum precision 0, supporting those two rows of your signature table. Please use OSG1 for the count, or document the exact alternative base/layout if the field in your measurement is relative to something else. My archive check covers these two programs. Header claim · Vehicle pass selection

The byte loss depends on how the lighting pass converts the scaled float to an integer. I used an isolated copy of vk_harness with the color attachment format and readback byte size selectable. A synthetic fragment shader wrote one column per byte b = 0..255, with the same value in all four channels. It computed float32(b) * float32(1/255) and cleared the low 13 mantissa bits to produce its binary16 round-toward-zero value (all nonzero inputs are normal in binary16). I drew one 256×1 triangle per format and read back the stored pixels.

GPU attachment Readback result
R8G8B8A8_UNORM All 256 stored bytes matched in every channel.
R16G16B16A16_SFLOAT All 256 stored values matched an independent CPU binary16 RTZ reference in every channel. After multiplying the readback by 255, truncation recovered 2/256 bytes (0 and 255); the other 254 were one low. Nearest-integer rounding recovered 256/256. The largest undershoot before integer conversion was 0.1240234375 of a byte.

The same arithmetic applies to each packed byte. Please identify the consuming conversion (truncate, round, or bias before convert) when attributing a one-byte error to the game's lighting pass. This tests attachment storage on hardware and the two candidate decodes on CPU. The actual game's lighting consumer and PSO formats remain to be verified. RTZ and lighting claim

Test system was an Intel Core Ultra 7 265K, NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), Windows 11 Home 25H2 build 26200.9974. The native Windows Vulkan harness confirmed the RTX 5070, subgroup 32, on both draws. AI-assisted (Codex).

@Andretuta

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, @mrp1no, and @jeremygold02 for the detailed reviews and the RTX 5070 hardware readback check! Updated docs/gbuffer.md in $sha3246 (rebased on 847748c):

  1. OSG1 vs FSSM +0x0FC and corpus selection counts: dropped +0x0FC from the table header (noting that +0x0FC holds 16/18 on two stock vehicle deferred passes, so target counts must come from OSG1), separated the 1,702 six-colour-target shaders (955 + 747) from the 1 five-colour-target + SV_Depth shader (1,703 six-row OSG1 tables), showed both the raw RPF entry substring match (2,989) and the case-sensitive .dxbc stem match (3,004, with 16,306 / 16,306 exp mrtN ... compr), and scoped the off, off export operand observation to the sampled PS_DeferredTextured disassembly.
  2. Inferred vs declared RTV formats and mrt2: labelled the format column Inferred RTV format class (from shader encoding / PS_DeferredTextured gbuffer layout (normals are packed kinda weird) #1891) and explicitly noted that mrt2's consumer squaring and specular/$ interpretation are as reported in PS_DeferredTextured gbuffer layout (normals are packed kinda weird) #1891 (pending lighting-consumer/PSO verification).
  3. UNORM8 vs SFLOAT16 round-trip (� = 0..255): documented that �_cvt_pkrtz_f16_f32(float32(b) * 0x3b808081) places 254 / 256 bytes (1..254) below � / 255.0 (max shortfall

@jeremygold02 jeremygold02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed the two corrections in my earlier review at 20daca0435526645bfe274de5d0aa71573ec930d. This time the GPU ran the PR head’s translation of a hand-written gfx1013 pixel shader using v_cvt_pkrtz_f16_f32 and exp mrt0 … compr. That provides an independent check of the earlier direct-SPIR-V ramp.

Attachment Byte recovery at readback Result per channel
R8G8B8A8_UNORM Read the stored byte 256/256 exact
R16G16B16A16_SFLOAT Round stored half × 255 to nearest 256/256 exact
R16G16B16A16_SFLOAT Truncate stored half × 255 2/256 exact (0 and 255); every intermediate byte was one low

The maximum shortfall before integer conversion was exactly 0.1240234375 byte. Two 256×1 draws exercised all 256 codes independently in each channel. For column x, RGBA input bytes were (x, x^255, x^85, (x+17)&255). Each component was converted to f32 and multiplied by the binary32 constant 0x3b808081 before the two RTZ packs and compressed export. All 1,024 channel values per format matched the earlier GPU ramp after applying those permutations. Both translated stages passed Vulkan 1.2 SPIR-V validation. The scratch harness was built from this head with attachment format and readback size selected for the two tests.

The two previously identified stock vehicle records also reproduced the corrected distinction: the tire and rim declare 5 and 6 color targets in their OSG1 signatures, while their FSSM field at +0xFC contains 16 and 18 respectively. This verifies those two examples and the format-dependent arithmetic. The production lighting consumer’s conversion, the live PSO attachment formats, and the corpus census were outside this retest.

GPU execution used NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), subgroup 32, with Intel Core Ultra 7 265K on Windows 11 Home 25H2 build 26200.9974.

AI-assisted by GPT-6.2 Astra.

@Andretuta
Andretuta force-pushed the docs/proc-146-deferred-gbuffer branch from 20daca0 to 1936a36 Compare November 22, 2026 23:41
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
# Conflicts:
#	docs/gbuffer.md
@Andretuta Andretuta closed this Nov 23, 2026
@Andretuta
Andretuta force-pushed the docs/proc-146-deferred-gbuffer branch from 1936a36 to 98fa503 Compare November 23, 2026 05:24
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/gbuffer.md
tupacfan04 added a commit to tupacfan04/Leonida that referenced this pull request Nov 23, 2026
The merge of ItsNotRexed#3246 (3343a45, after d9cea57) kept both the first version
of the note and its revision (1936a36), so sections 2 and 3 and the
"Arithmetic across all 256 byte values" section appeared twice, one copy
mangled, and a leftover line said the FSSM header records the target
count at +0x0FC, which the revision corrects (read it from OSG1: vehicle
passes hold 16 and 18 there). Remove the 58 leftover lines; the file is
now 1936a36's version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants