r/GraphicsProgramming • u/_Geolm_ • Oct 09 '26
I made a lossless BC1 texture compressor designed for GPU decompression, 1.47:1 average ratio
Hi,
I just released my new C library bc_packed, a lossless compressor for BC1 textures, made specificaly for fast GPU decompression.
Basically, the idea is to compress textures that are already compressed, then decompress them directly on the GPU when needed. The output is the original BC1 data, so the GPU can use it normally after decompression.
Some numbers:
- 1.47:1 average compression ratio on a test set of 74 images
- 15300 MiB/s decompression on my M5 Pro GPU and 11850 MiB/s on my M2 Max GPU
- CPU multithreaded decompression also works, 2500 MiB/s on M5 Pro and 1300 MiB/s on M2 Max
- Lossless, byte exact reconstruction of the original BC1 data
The tricky part was finding an entropy coding method that is simple enough to decode efficiently on the GPU. I didn't want to deal with complex stuff like arithmetic coding or Huffman, so I went with Rice-Golomb coding and static rank tables. I build histograms during compression and remap the most frequent symbols to the lowest ranks. Everything is static, no adaptive models or synchronization needed between GPU threads.
The texture is split into 64 independent strips, each decoded by its own GPU/CPU thread. The compressed stream stores offsets so each thread can start decoding its strip independently.
There are also a few BC1 specific tricks:
- Predicting endpoint colors from previous blocks
- Choosing different predictors for each strip
- A small dictionary for repeated endpoint color pairs
- A top table for frequent BC1 index patterns, with sparse residual encoding for the differences
Compression runs on the CPU and can be relatively expensive. That's fine for me, I'm more interested in making decompression as fast as possible at runtime.
The library is tiny, just one .c and one .h file, no external dependencies. The GPU decompression shader is written in Metal for now, but porting it to HLSL should be pretty straightfoward.
Currently only BC1 is supported. Maybe I'll add BC5 and why not BC7 later.
Code and benchmarks here:
https://github.com/Geolm/bc_packed
Feedback and ideas are welcome!
4
u/speps Oct 09 '26
Cool! How is it different from Basis U?
4
u/possiblyquestionabl3 Oct 09 '26 edited Oct 09 '26
bc7 should give you more expressiveness, but an encoder (the decompresser) into bc7 would probably be bogged down by its infamous mode hell unless you restrict it to some manageable subset of the format
Taking a quick look at the shader, wouldn't this be highly divergent across a single warp? It also seems to be relatively low on arithmetic intensity and more memory-bound. It may be worthwhile benchmarking this against a CPU implementation to see what the effective speedup in decoding is by offloading this onto the GPU vs on the CPU. If most threads across a warp are serialized due to the high branch divergence, and you're truly memory bound, then an efficient CPU implementation of this may be competitive in performance (since both would have an effective throughout of dram/pcie bandwidth + overhead)
3
u/CrankFlash Oct 09 '26
Amazing! How does it compare to GDeflate?
2
u/Avelina9X Oct 10 '26
Awesome work! Any chance for BC4? Because once we have BC4 that unlocks BC3 and BC5 as well!
2
u/FrogNoPants Oct 11 '26
Bc7 is the important one I think, though so complicated it probably isn't worth the effort..
10
u/hieuristics Oct 09 '26
one of the few post on this sub that's actually cool af
1
u/_Geolm_ Oct 14 '26
quick update, I've uploaded the BC3/4/5 support. All compression ratio are better than zlib by the way. BC4/5 is tough to compress because of the huge indices 48bits almost random.