Skip to content

Add smh GPU strand groom decoder, RNG/interpolation, GLB exporter and docs - #4162

Open
timelery wants to merge 2 commits into
ItsNotRexed:mainfrom
timelery:asset-smh-strand-grooms
Open

timelery wants to merge 2 commits into
ItsNotRexed:mainfrom
timelery:asset-smh-strand-grooms

Conversation

@timelery

@timelery timelery commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Clean-room, standard-library-only (python -I, no numpy/Pillow) decoder, Class 0 RNG & barycentric strand generator, corpus census tool, and glTF 2.0 (.glb) exporter for RAGE smh GPU strand grooms (hair, beards, brows, lashes, body hair, and animal fur).

  • Claim: Coordinating who works on what (21) #4084 (comment)
  • AI-assisted: written and verified with coding-agent assistance.
  • No game data: zero game archives, carved files, shader disassembly, textures, or asset-name dumps are included. All unit tests build synthetic binary groom buffers in memory/tempdir.

Deliverables

  1. tools/smh_groom.py (pure Python stdlib, reuses tools/glb_writer.py framing & atomic write):
    • Binary parser for smh groom LOD resources (native 16-zero-prefixed zlib streams, raw zlib streams, standalone raw GPU asset buffers, and RPF9 archive scanning), including dedicated parse_system_page() decoding TYPE_ID = 0x7d8f3db3, primary AABB (+0x30/+0x40), unset secondary bounds sentinel detection (+0x48..+0x70 / +0x50..+0x6f +FLT_MAX/-FLT_MAX on child rigs and non-scalp grooms), buffer descriptor, skinning bone hashes (+0x98/+0xa0), region records (+0xa8/+0xb0), the +0xb8 metadata hash table (slots 0..3 bone hashes, slot 4 +0xc8 style preset hash, slot 5 +0xcc part class hash mapped via PART_CLASS_NAMES), strand totals (+0xd0), and +0xe8..+0x10f IEEE 754 float16 style parameters (strand width, longest guide, braid parameter, and two 6-half-float curl/frizz blocks), plus the 0xA0-byte GPU buffer offset table.
    • Bit-exact pure-Python port of smh_gen_1_CS_InterpolateStrands Class 0 RNG (4-attempt triangle selection with 256-bit region mask and IEEE 754 float32 density thresholds, v_mad_u32_u24 barycentric cascade, and PCG length variation) plus rest-pose barycentric guide blending and segment trimming.
    • LOD chain grouping (including split-chain rejoining) and aggregate corpus census (including partClassHistogram, distinct style preset counts, and unset rig-float counts).
    • Binary glTF 2.0 (.glb) exporter supporting tubes (3-sided tapered tubes), ribbons (flat tapered strips), curves (LINES, mode 1), and line-strip (LINE_STRIP, mode 3), with RAGE Z-up to glTF Y-up conversion.
    • CLI: inspect, census, export-glb.
  2. tests/test_smh_groom.py:
    • 18 pure-synthetic unit tests verifying system-page validation, +0xb8 metadata hash slots & PART_CLASS_NAMES, +0x48..+0x70 unset child-rig float sentinel detection, +0xe8..+0x110 half-float style parameters (including NaN and short-page handling), GPU header/array/tail parsing, multi-chunk native resource scanning, contiguous & split LOD chain grouping, bit-exact mul24/pcg/scatter RNG against an independent scalar reference, region-0 non-culling, barycentric interpolation & tip-repeating length trimming, GLB validity across all 4 geometry modes via glb_writer.read_glb_summary(), error handling on truncated/corrupt inputs, and end-to-end python -I CLI execution.
  3. docs/strand_grooms.md:
    • Clean-room format & shader-verified compute pipeline specification (smh_pose_*, smh_sim_*, smh_gen_*, smh_draw_64/smh_draw_128, VS_HairStrandGeometry/PS_HairStrandVBuffer), clarifying class 0 vs. class 1 placement semantics, the 0x150-byte per-instance t71 record, the smh_gen_N variant bitmask, system-page +0xb8 metadata hashes, +0x50..+0x6f unset float sentinels, +0xe8 half-float style parameters, standing-height / head-sphere calibration, bone-based face/scalp discrimination, split-chain archive structure, Class 0 RNG algorithm, exact vs. modelled/open matrix, CLI reference, and aggregate corpus census.

Corpus Verification (Read-Only, Local Package)

Verified read-only with python -I tools/smh_groom.py census on local packs.rpf and pack/core_restofworld/core_restofworld.rpf:

  • 20,046 / 20,046 groom LOD resources decoded cleanly (0 errors): 20,010 in packs.rpf (5,741 assets) and 36 in core_restofworld.rpf (10 assets), totaling 5,751 logical groom assets and 902,456,856 bytes of GPU asset buffers.
  • Stored strand totals: 77,126,413 class 0 strands (tri_count[:, 0] sum matches system +0xd0 in 5,716 / 5,751 LOD 0 assets) and 35,365,208 class 1 strands (tri_count[:, 1] sum matches +0xd4 in 3,158 / 5,751 LOD 0 assets).
  • System page metadata verified over all 5,741 packs.rpf assets: 425 distinct +0xc8 style preset hashes, 30 distinct +0xcc part class hashes (11 mapped across 9 named classes + 19 unmapped), and 2,457 assets with unset +0x50..+0x6f secondary bounds floats.

Tests Run (Windows, Python 3.14.7, CPU / stdlib only)

  • python -I scripts/check_no_game_data.py — PASS
  • python -I scripts/check_no_game_data.py tools/smh_groom.py tests/test_smh_groom.py docs/strand_grooms.md — PASS
  • python -m compileall -q make_game.py tools tests — PASS
  • python -I tests/test_smh_groom.py -v — 18/18 PASS
  • python -I tests/test_tool_startup.py — 2/2 PASS
  • python -I tests/test_tool_stdout_encoding.py — 4/4 PASS
  • python -I tests/test_docs_cli_flags.py — 3/3 PASS
  • python -I tests/test_docs_design_rows.py — 2/2 PASS
  • python -I tests/test_docs_links.py — 6/6 PASS
  • python -I tests/test_docs_list_items.py — 2/2 PASS
  • python -I tests/test_docs_python_deps.py — 3/3 PASS
  • python -I tests/test_docs_repo_paths.py — 2/2 PASS
  • python -I tests/test_docs_table_rows.py — 2/2 PASS

@Proudnoob4

Copy link
Copy Markdown
Contributor

Game-copy check of d8bfb820: the census reproduces exactly. Windows 10 22H2, Python 3.13.0, CPU only, my own 1.0.2.1 copy. packs.rpf and pack/core_restofworld/core_restofworld.rpf both match their SHA-256 in game/xbox-1.0.2.1.json.

  • python -I tests/test_smh_groom.py: 15 tests OK.
  • python -I tools/smh_groom.py census <packs.rpf> <core_restofworld.rpf> --json: exit 0 in 45 s, nothing on stderr. Every total in the description comes out the same:
PR here
LOD resources / decode errors 20,046 / 0 20,046 / 0
LODs: packs.rpf + core_restofworld.rpf 20,010 + 36 20,010 + 36
assets: packs.rpf + core_restofworld.rpf 5,741 + 10 5,741 + 10
GPU buffer bytes 902,456,856 902,456,856
class 0 / class 1 strands 77,126,413 / 35,365,208 77,126,413 / 35,365,208
tri-sum equals +0xd0 / +0xd4 in LOD 0 5,716 / 3,158 5,716 / 3,158

The other census figures in docs/strand_grooms.md match too:

  • header +0x2c is 0x0001ffff in 39 LODs
  • 3 to 5,185 guides per LOD, and 2 to 80 nodes per guide
  • LOD depths 4,534 / 341 / 11 / 865
  • optional arrays in 12,101 / 10,684 / 5,718 LODs, and +0x3c non-zero in 10,756
  • placement classes 1,462 / 1,371 / 1,119 / 1,076 / 723

This checks the parser and the census on a second copy, not the RNG port or the exported geometry: I did not run export-glb on game data.

Counts only; nothing from the game leaves my machine.

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested d8bfb82 on Windows, CPython 3.14.7, CPU only (no GPU, no game files). python -m pytest -q tests/test_smh_groom.py gives 15 tests passed and 4 subtests passed. The exporter is new, thus there is no merge-base control.

One non-blocking point about memory. --max-strands does not limit the memory for the strand roots. With max_strands=1, to_glb gives the full candidate count to scatter. At tools/smh_groom.py:721, generate_strands calls scatter before it applies its limit. scatter makes four lists for each candidate.

I measured the Python allocation of to_glb only, with 128 triangles, four guides, one strand class and one exported strand. The control stops scatter after the first candidate.

candidates GLB bytes peak, this head peak, control
1,024 1,832 226,739 17,956
8,192 1,832 1,718,808 18,700
32,640 1,836 6,808,576 19,428

The control gives a byte-identical GLB in each row. The peak increases linearly with the candidate count (about 210 bytes for each candidate). This is temporary memory, not a leak.

import gc, runpy, tracemalloc
from unittest.mock import patch
m = runpy.run_path("tests/test_smh_groom.py")
g = m["smh_groom"]
def measure(groom):
    gc.collect()
    tracemalloc.start()
    data, stats = g.to_glb(groom, max_strands=1, classes=(0,), scalp=False, guides=False, geometry="curves")
    peak = tracemalloc.get_traced_memory()[1]
    tracemalloc.stop()
    return data, stats["strandsShown"][0], peak
original = g.scatter
for density in (8, 64, 255):
    groom = g.parse(*m["build_synthetic_groom_buffers"](triangles=[(0, 1, 2)] * 128, tri_count=[(density, 0, 0, 0)] * 128))
    data, shown, peak = measure(groom)
    with patch.object(g, "scatter", lambda groom, count, *args: original(groom, 1, *args)):
        check, _, control = measure(groom)
    assert check == data and shown == 1
    print(128 * density, shown, len(data), peak, control)

Possible correction: make candidates in order until the number of accepted strands is reached, and continue past rejected candidates. Keep the full scatter result for callers that need all strands.

AI-assisted.

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.

3 participants