Improve SGD5 shader group parser #29

Manually merged
github merged 2 commits from gh-pr/29/head into gh-pr/29/base 2026-11-27 02:31:56 +00:00
Owner

@⁠Xyzstk on GitHub · 2026-11-22 00:01 UTC · original
Xyzstk:sgd5-fssm-rdna2 → main · +1130 −155 · 4 files · 2 commits · merged 2026-11-22 02:23 UTC

Summary

This adds a structural SGD5 shader-group parser, raw archive stream discovery, DXBC/FSSM extraction, and unified RDNA2 inspection in the Shaders page. It also resolves readable resource names through effect reflection string pools and applies them to disassembly and the experimental HLSL/SPIR-V views.

The format details are intentionally kept out of the repository and documented here instead.

SGD5 container layout

All values are little-endian. Size fields describe the bytes that follow them and do not include the size field itself.

The container starts with:

u32 magic;                  // "SGD5"
u64 group_hash;             // opaque
u32 descriptor_count;

struct Descriptor {
    u32 size;               // excludes this u32; 24 + entry_count * 8
    u64 hash;               // opaque
    u16 entry_count;
    u8  unknown[14];
    struct Entry {
        u32 name_hash;      // lowercased Jenkins one-at-a-time hash
        u8  kind0;
        u8  kind1;
        u16 index;
    } entries[entry_count];
} descriptors[descriptor_count];

A shared reflection block follows. It contains referenced_count + extra_count 24-byte records:

u16 referenced_count;
u16 extra_count;
u16 reserved;
u16 size;                   // 8 + record_count * 24 + 1
ReflectionRecord records[record_count];
u8  terminator;

Each reflection record is:

u16 resource_type;
u16 slot;
u16 flags;
u16 reserved;
u32 name_offset;
u32 unknown;
u32 name_hash;
u32 name_hash_duplicate;

Six shader arrays then follow in VS, PS, GS, DS, HS, CS order.

Shader array and DXBC/FSSM payload

Each shader is:

u8  name_length;
u8  name[name_length];
u8  wave_size;
u32 compressed_size;        // includes the 12-byte codec header
u32 codec;                  // 0x000DDCE7
u32 compressed_size_copy;
u32 uncompressed_size;
u8  deflate_data[compressed_size - 12];
u64 sga_hash;               // opaque
u8  root_signature_or_metadata[144];
ShaderReflection reflection;

The payload is DEFLATE-compressed and expands to a DXBC container. The DXBC private data contains a zlib-compressed FSSM payload, and FSSM exposes raw GFX10.3/RDNA2 code through its code offset and size fields. That code is fed into the existing RDNA2 decoder and HLSL/SPIR-V lifters.

This means the SGD5 path and the independent runtime-cache path share the same machine-code decoder and viewer infrastructure.

Effect blobs

The effect array begins with a u32 effect_count, followed by variable-sized effect blobs:

u8  name_length;            // includes trailing NUL
u8  name[name_length - 1];
u8  nul;
u32 data_buffer_size;
struct StageIndices {
    u32 count;
    u32 indices[count];
} stages[6];               // VS, PS, GS, DS, HS, CS

u8  metadata_base[0x5c];    // extension count is u32 at +0x50
struct Extension {
    u32 zero;               // observed 0
    u32 marker;             // observed 0x00000101
    u8  payload[8];
} extensions[extension_count];

// Repeated while the current tail word has nonzero upper 16 bits:
u8  continuation[0x18];

// Counted by the low byte when the tail selects this form:
u8  state_chunks[count][0x44];

u32 part1_count;
u8  part1[part1_count * 0x24];
u32 part2_count;
u8  part2[part2_count * 4];
u32 part3_count;
u8  part3[part3_count * 0x18];
u32 part4_dword_count;
u32 part4[part4_dword_count];
u32 reflection_sentinel;    // 0x80000000

This replaces earlier reflection-table scanning with a deterministic grammar. In the current sample, 872 effects have no extension, 56 have one extension, and four have two extensions. One effect combines an extension, continuation, and state chunks.

Reflection and property tables

The main effect reflection table contains resource records, cbuffer-member records, texture entries, a string pool, and float defaults:

u32 size;
u16 resource_count;
u16 cbuffer_member_count;
u16 texture_count;
u16 size_copy;              // equals size
ReflectionRecord resources[resource_count];
ReflectionRecord cbuffer_members[cbuffer_member_count];
struct TextureEntry {
    u32 name_hash;
    u16 resource_type;
    u16 index;
} textures[texture_count];
u8  string_pool[...];
f32 defaults[texture_count];

A secondary reflection table follows, then the property section:

u16 property_count;
u16 property_flags;

struct Property {
    u32 size;

    if (size == 0x80000000) {
        u32 sentinel_hash;
    } else {
        u64 entry_hash;     // opaque
        u32 count_word;     // low byte = binding_count
        u32 flags;
        u8  reserved[16];
        struct Binding {
            u32 name_hash;
            u32 tag;
        } bindings[count_word & 0xff];
    }
} properties[property_count];

u32 string_pool_size;
u8  string_pool[string_pool_size];

Resource names use the lowercased Jenkins one-at-a-time hash. Both plain and _-prefixed sampler forms are indexed, matching the behavior of the older version’s parser. Resolved names are attached to matching shader reflection records and shown in the RDNA2 views.

GUI and CLI integration

  • Opening the raw shader archive loads the runtime cache and every SGD5 stream through one source-switching flow.
  • The unified Shaders page supports GFX10.3 disassembly plus experimental HLSL and SPIR-V lifting for both sources.
  • SGD5 shader rows show effect associations, and selected shaders show their effect names.
  • Extracted DXBC/FSSM information and resolved resource names are displayed alongside the RDNA2 code.
  • The extraction CLI continues to emit DXBC files and manifests.

The final stream trailer is ten bytes: a u32 flags value followed by six zero bytes. Unrelated archive data can follow the last SGD5 stream, so trailer validation no longer requires EOF or another SGD5 magic.

Validation

  • cargo test -q: 42 tests; 39 passed, 3 ignored.
  • Raw archive smoke test: 2 SGD5 groups, 26,503 shaders, and 932 effects parsed.
  • Limited extraction from the raw archive completed successfully.
  • git diff --check is clean.
  • Rustfmt was not applied because the repository has substantial pre-existing formatting drift; applying it would create unrelated changes.

Remaining unknowns

  • group_hash, descriptor hashes, property hashes, and shader sga_hash remain opaque 64-bit values.
  • Some descriptor metadata, the 144-byte shader metadata area, effect metadata fields, secondary reflection data, and state-chunk fields remain unidentified.
  • The lifters remain experimental and do not fully reconstruct all control flow or resource semantics.
**@⁠Xyzstk** on GitHub · 2026-11-22 00:01 UTC · [original](https://github.com/rpatty-dev/PercyStar/pull/29) `Xyzstk:sgd5-fssm-rdna2` → `main` · +1130 −155 · 4 files · 2 commits · merged 2026-11-22 02:23 UTC ## Summary This adds a structural `SGD5` shader-group parser, raw archive stream discovery, `DXBC`/`FSSM` extraction, and unified RDNA2 inspection in the Shaders page. It also resolves readable resource names through effect reflection string pools and applies them to disassembly and the experimental HLSL/SPIR-V views. The format details are intentionally kept out of the repository and documented here instead. ## SGD5 container layout All values are little-endian. Size fields describe the bytes that follow them and do not include the size field itself. The container starts with: ```c u32 magic; // "SGD5" u64 group_hash; // opaque u32 descriptor_count; struct Descriptor { u32 size; // excludes this u32; 24 + entry_count * 8 u64 hash; // opaque u16 entry_count; u8 unknown[14]; struct Entry { u32 name_hash; // lowercased Jenkins one-at-a-time hash u8 kind0; u8 kind1; u16 index; } entries[entry_count]; } descriptors[descriptor_count]; ``` A shared reflection block follows. It contains `referenced_count + extra_count` 24-byte records: ```c u16 referenced_count; u16 extra_count; u16 reserved; u16 size; // 8 + record_count * 24 + 1 ReflectionRecord records[record_count]; u8 terminator; ``` Each reflection record is: ```c u16 resource_type; u16 slot; u16 flags; u16 reserved; u32 name_offset; u32 unknown; u32 name_hash; u32 name_hash_duplicate; ``` Six shader arrays then follow in `VS, PS, GS, DS, HS, CS` order. ## Shader array and DXBC/FSSM payload Each shader is: ```c u8 name_length; u8 name[name_length]; u8 wave_size; u32 compressed_size; // includes the 12-byte codec header u32 codec; // 0x000DDCE7 u32 compressed_size_copy; u32 uncompressed_size; u8 deflate_data[compressed_size - 12]; u64 sga_hash; // opaque u8 root_signature_or_metadata[144]; ShaderReflection reflection; ``` The payload is DEFLATE-compressed and expands to a `DXBC` container. The `DXBC` private data contains a zlib-compressed `FSSM` payload, and `FSSM` exposes raw GFX10.3/RDNA2 code through its code offset and size fields. That code is fed into the existing RDNA2 decoder and HLSL/SPIR-V lifters. This means the `SGD5` path and the independent runtime-cache path share the same machine-code decoder and viewer infrastructure. ## Effect blobs The effect array begins with a `u32 effect_count`, followed by variable-sized effect blobs: ```c u8 name_length; // includes trailing NUL u8 name[name_length - 1]; u8 nul; u32 data_buffer_size; struct StageIndices { u32 count; u32 indices[count]; } stages[6]; // VS, PS, GS, DS, HS, CS u8 metadata_base[0x5c]; // extension count is u32 at +0x50 struct Extension { u32 zero; // observed 0 u32 marker; // observed 0x00000101 u8 payload[8]; } extensions[extension_count]; // Repeated while the current tail word has nonzero upper 16 bits: u8 continuation[0x18]; // Counted by the low byte when the tail selects this form: u8 state_chunks[count][0x44]; u32 part1_count; u8 part1[part1_count * 0x24]; u32 part2_count; u8 part2[part2_count * 4]; u32 part3_count; u8 part3[part3_count * 0x18]; u32 part4_dword_count; u32 part4[part4_dword_count]; u32 reflection_sentinel; // 0x80000000 ``` This replaces earlier reflection-table scanning with a deterministic grammar. In the current sample, 872 effects have no extension, 56 have one extension, and four have two extensions. One effect combines an extension, continuation, and state chunks. ## Reflection and property tables The main effect reflection table contains resource records, cbuffer-member records, texture entries, a string pool, and float defaults: ```c u32 size; u16 resource_count; u16 cbuffer_member_count; u16 texture_count; u16 size_copy; // equals size ReflectionRecord resources[resource_count]; ReflectionRecord cbuffer_members[cbuffer_member_count]; struct TextureEntry { u32 name_hash; u16 resource_type; u16 index; } textures[texture_count]; u8 string_pool[...]; f32 defaults[texture_count]; ``` A secondary reflection table follows, then the property section: ```c u16 property_count; u16 property_flags; struct Property { u32 size; if (size == 0x80000000) { u32 sentinel_hash; } else { u64 entry_hash; // opaque u32 count_word; // low byte = binding_count u32 flags; u8 reserved[16]; struct Binding { u32 name_hash; u32 tag; } bindings[count_word & 0xff]; } } properties[property_count]; u32 string_pool_size; u8 string_pool[string_pool_size]; ``` Resource names use the lowercased Jenkins one-at-a-time hash. Both plain and `_`-prefixed sampler forms are indexed, matching the behavior of the older version’s parser. Resolved names are attached to matching shader reflection records and shown in the RDNA2 views. ## GUI and CLI integration - Opening the raw shader archive loads the runtime cache and every `SGD5` stream through one source-switching flow. - The unified Shaders page supports GFX10.3 disassembly plus experimental HLSL and SPIR-V lifting for both sources. - `SGD5` shader rows show effect associations, and selected shaders show their effect names. - Extracted `DXBC`/`FSSM` information and resolved resource names are displayed alongside the RDNA2 code. - The extraction CLI continues to emit `DXBC` files and manifests. The final stream trailer is ten bytes: a `u32 flags` value followed by six zero bytes. Unrelated archive data can follow the last `SGD5` stream, so trailer validation no longer requires EOF or another `SGD5` magic. ## Validation - `cargo test -q`: 42 tests; 39 passed, 3 ignored. - Raw archive smoke test: 2 `SGD5` groups, 26,503 shaders, and 932 effects parsed. - Limited extraction from the raw archive completed successfully. - `git diff --check` is clean. - Rustfmt was not applied because the repository has substantial pre-existing formatting drift; applying it would create unrelated changes. ## Remaining unknowns - `group_hash`, descriptor hashes, property hashes, and shader `sga_hash` remain opaque 64-bit values. - Some descriptor metadata, the 144-byte shader metadata area, effect metadata fields, secondary reflection data, and state-chunk fields remain unidentified. - The lifters remain experimental and do not fully reconstruct all control flow or resource semantics.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 02:31:54 +00:00
github manually merged commit 208f3390c3 into gh-pr/29/base 2026-11-27 02:31:56 +00:00
Author
Owner

@⁠rpatty-dev on GitHub · 2026-11-22 02:23 UTC · original

Merged onto main and pre-sized resolve_hashed_names HashMap capacity in commit 6687bcd to avoid repeated rehashing on large shader string pools (52/52 tests + release build passing). Awesome SGD5/FSSM parser!

**@⁠rpatty-dev** on GitHub · 2026-11-22 02:23 UTC · [original](https://github.com/rpatty-dev/PercyStar/pull/29#issuecomment-7072880165) Merged onto main and pre-sized resolve_hashed_names HashMap capacity in commit 6687bcd to avoid repeated rehashing on large shader string pools (52/52 tests + release build passing). Awesome SGD5/FSSM parser!
This discussion has been locked. Commenting is limited to contributors.
No description provided.