Files
filament/tools/matedit
Mathias Agopian 167a91efaa Optimize Shader LineDictionary (#9814)
* Optimize Shader LineDictionary with Variable-Length 3-Streams

This commit completely reorganizes the string dictionary compression
pipeline used by compiled Material Text Chunks and improves matinfo
dictionary output.

1. Multi-Base Variable-Length Scaling:
We replaced the static 16-bit indices overhead with a
bounded payload. Now, indices scale dynamically:
- 0 to 239 evaluate in 1 byte.
- 240 to 3584 leverage 0xF0-0xFD escapes evaluating in 2 bytes.
- 3584+ are locked behind a 0xFF marker to 3 bytes.
This natively eradicated the massive monolithic lengths and zero-padding
issues previously dominating shader packages.

2. Variable-Length 3-Stream Decoding:
To solve the Zstandard/Zlib entropy fragmentation that conventionally
plagues interleaved variable byte lengths (which previously inflated
our `filament.aar` boundary constraint by +2KB), we segregated the encoded
payloads.

By grouping high-entropy string boundaries into a `Base Stream`
and isolating offset digits inside an `Extension Stream`,
predictive LZ77 ZIP sliding-windows perfectly map
over both arrays independently without disruption.

3. Optimize Numeric Stream using LEB128
Prior to this change, numerical suffixes split from shader
variables (e.g., `param_1024` -> `param_` + `1024`) were fed back
into the localized String Dictionary. Because high-frequency numbers were
assigned disjointed localized IDs per shader variant, LZ77
failed to cross-reference their repetitive structures across
shipped `.aar` archives, fracturing compression sequences.

This patch implements a unified 3-Stream topology. It
extracts numerical primitives (< 32768) away from the baseline
String Dictionary, writing them into an isolated, contiguous
LEB128 array.

By using a dedicated `[254]` Escape Token within the primary stream, numerical
variables maintain exact 1-byte (`< 128`) or 2-byte (`>= 128`)
geometric layouts across all permutations.

The resulting deterministic alignment guarantees that Zlib sliding windows
can deduplicate highly repetitive variables across the
entire application binary block.

4. We use the ShaderStage information to create distinct index ranges, which
further help use 1-byte indices.

Verification Metrics:
 `filament-android.aar`: -7,938 B
 `gltfio-android.aar`: -290,939 B
 `libfilament.a`: -18,464 B

* Optimize shader dictionary by decoding '_' for numeric literals

Most numbers extracted from the shader text are preceded by an 
underscore (e.g., from `_`, `hp_copy_`), which previously caused 
standalone `_` strings to heavily pollute the LineDictionary.

This change removes the standalone `_` from the dictionary index:
- `MaterialChunk` rehydrates the `_` prefix when decoding these numeric
literals.

This frees up dictionary indices, yielding massive byte savings across 
uncompressed binaries (e.g., -28.4 KB for volume_masked.filamat).

* Optimize ShaderMinifier to strip explicit spacing

Spirv-cross outputs GLSL with explicit spacing around generic
operators (e.g., ` = `, `, `, ` ) * `). This padding consumes a 
significant amount of uncompressed bytes across large ubershaders. 

By applying targeted string replacements at the end of the `ShaderMinifier` 
pass, we strip this extraneous padding down to its raw tokens 
(e.g., `a=b`, `a,b`, `a*b`). 

This optimization preserves isolating spaces where valuable, ensuring 
line-dictionary tokens (such as raw `=` or `,`)  remain deduplicated 
instead of fusing into unpredictable variables. 

Impact:
This saves roughly ~9.1 KB in `libfilament.a` and ~3.2 KB in 
`volume_masked.filamat` uncompressed, with proportional gains across 
the downstream LZ4 compressed archives.
2026-03-26 22:41:36 -07:00
..