KhronosGroup / KTX-Software

KTX (Khronos Texture) Library and Tools
Other
888 stars 233 forks source link

Improve color space handling with supercompression & transcoding #98

Open lexaknyazev opened 5 years ago

lexaknyazev commented 5 years ago

Here's a full description of how color space information traverses KTX pipeline with supercompression from the source image to the GPU texture.

Source PNG

As per the PNG spec, each PNG image has a color space defined by zero or more color space information chunks (sRGB, gAMA, cHRM, and iCCP):

KTX software follows these rules and infers the source image color space with additional limitations:

When the source image actually contains linear values (e.g., it's a normal map) but the PNG file doesn't signal that by using the appropriate combination of chunks, users can override KTX software behavior by providing --linear parameter to the command-line tool.

The resulting KTX2 file always has the color space information in its DFD section.

Basis Supercompression

Basis compressor has an artifact-reducing optimization, enabled by settingm_perceptual in the compressor parameters, which handles the original pixel values as perceptual colors, i.e. sRGB. By default they are treated as linear which is suitable for arbitrary vectors as well as linear colors. The KTX software sets this parameter when transcoding textures with the sRGB bit set in the DFD section. It does not expose the parameter as a user setting.

Runtime Texture Transcoding

At runtime, a GPU texture object of appropriate format needs to be created to receive transcoded block-compressed data. Its format may or may not imply hardware sRGB sampling and filtering, so depending on the platform, applications may need to plan for some fallback behavior.

When the GPU texture is created by the KTX library, it may need to know that hardware sRGB is not available and the conversion will happen in shaders.

Here is how platform support for sRGB filtering of block-compressed formats looks like.

ASTC

On platforms that support ASTC, each combination of block dimensions exists in both linear and sRGB options.

BC1, BC2, BC3

Vulkan, Metal, and Direct3D have native support for BC1/2/3. Direct3D and Metal support only alpha-"punchthrough" flavor of BC1.

BC7

Vulkan, Metal, and Direct3D have native support for both variants of BC7.

ETC RGB/RGBA

Originally, this format supported only linear sampling and filtering of RGB without alpha. Given that ETC1 RGB format is a strict subset of ETC2 RGB format, and the latter supports hardware sRGB sampling and filtering, it became possible to use hardware sRGB sampling and filtering with existing ETC1 data on capable hardware.

PVRTC1/2

BC4/5, EAC R11/RG11

These formats support only linear sampling and filtering.

MarkCallow commented 4 years ago

There is nothing to be done in the current KTX-Software as far as I can see. This useful material needs to go in a document somewhere. Where would be appropriate?

I've updated the section about the "colors" optimization in the above. Please check it.