microsoft / DirectXTex

DirectXTex texture processing library
https://walbourn.github.io/directxtex/
MIT License
1.83k stars 450 forks source link

BC1-3 Compressor single color optimization #223

Open walbourn opened 3 years ago

walbourn commented 3 years ago

The libsquish library adds a special-case block compression for DXT when all 16 source pixels in the block are the same color.

For this case, it uses a table-based scheme to pick the optimal encoding.

NVTT adopted the same solution.

walbourn commented 3 years ago

Algorithm discussed in this blog

walbourn commented 3 years ago

Q: What do to about dithering with the single-color blocks?

Hancapo commented 3 months ago

Any news about this? I'm trying to batch convert a bunch of images using a C# wrapper of this library but I'm getting the typical green-ish artifacts in every DXT1/DXT5, there's a workaround that can be done through code or something like that?

walbourn commented 3 months ago

This issue is specifically for compressing 4x4 blocks that all contain exactly the same color. BC1-3 (a.k.a. DXT1-5) all use 5:6:5 for RGB so that sometimes can make the result be a little green tinted depending on the input values.

By default the DXT1-5 compressor is using a 'luminance perceptual' weighting for the colors. You could try TEX_COMPRESS_UNIFORM (which for texconv is -bc u switch). It could also be the result of SRGB vs. Linear color space mix ups.

Hancapo commented 3 months ago

Using TEX_COMPRESS_FLAGS.UNIFORM doesn't change anything, getting the same artifacts as always. I tried using almost every combination of WIC_FLAGS and TEX_COMPRESS_FLAGS PD: The wrapper is using the March 2024 version of the lib.

ScratchImage image = TexHelper.Instance.LoadFromWICFile("Original.png", WIC_FLAGS.NONE);
ScratchImage compressed = image.Compress(DXGI_FORMAT.BC1_UNORM, TEX_COMPRESS_FLAGS.UNIFORM, 0.5f);
compressed.SaveToDDSFile(DDS_FLAGS.NONE, "Compressed.dds");
walbourn commented 3 months ago

Can you attach the original.png?

Hancapo commented 3 months ago

Sure: Image