KhronosGroup / KTX-Software

KTX (Khronos Texture) Library and Tools
Other
854 stars 226 forks source link

Feature request: Tool to create combined image by gathering channels from input images #839

Open tksuoran opened 7 months ago

tksuoran commented 7 months ago

Use cases:

In theory also these could be possible:

MarkCallow commented 7 months ago

@tksuoran are you the same person I worked with in the OpenGL ES (and M3G?) working groups a lifetime ago?

The use cases say what you want to do but not why. I'm curious why you would want to, e.g, put two normal maps in a single texture object. Please provide further explanation.

donmccurdy commented 7 months ago

@MarkCallow I've had a few people request this feature from glTF Transform, and can share the use cases behind that. The glTF format and its material-related extensions define a significant number of texture inputs to the PBR shading model. Many of these require ≤3 channels, and so can be combined. The most common example would be occlusion/roughness/metalness, in red/green/blue channels.

Current texture inputs, and their assigned channels in glTF:

gltf texture channels

Screenshot regex'd from glTF Transform codebase.

I realize that combining uncorrelated data channels into a single texture with Basis Universal compression would require some care, so I'm unsure how practical this is, or how great the memory savings would be when all is said and done. Nevertheless it is a fairly popular request. I may eventually implement it with a pre-process merging the textures to PNGs before processing them with KTX2, but direct APIs to do this in KTX Software would be very welcome!

tksuoran commented 7 months ago

@tksuoran are you the same person I worked with in the OpenGL ES (and M3G?) working groups a lifetime ago?

Hi Mark, yes I am, indeed it has been a while.

The use cases say what you want to do but not why. I'm curious why you would want to, e.g, put two normal maps in a single texture object. Please provide further explanation.

My initial thinking was that I would like to avoid artificial limitations, and promote generic interface, where use would have the freedom to choose inputs and outputs as they wish. One remotely plausible use case would be material/shader which uses both normal map (encoded in RG) and tangent texture (encoded in BA), while using same encoding for tangent texture and normal map.. However, I am not sure if using the normal map encoding scheme for tangent textures is the good idea or not.

MarkCallow commented 7 months ago

I may eventually implement it with a pre-process merging the textures to PNGs before processing them with KTX2, but direct APIs to do this in KTX Software would be very welcome!

The libktx API gives access to the memory where the images are stored in the ktxTexture2 object and has functions to query the offsets within that memory of each layer, level and face or slice. Using that together with the format info, from the DFD or the vkFormat field, you can write code to take an input image and write its components to the desired components of the final texture and when done use the standard libktx API functions to compress the texture and write it to disk. If you implement this way then your code could become the basis of a PR to add the feature you want. Writing it this way will will not take more time than merging the inputs to a PNG. It may even take less.

MarkCallow commented 2 months ago

Many of these require ≤3 channels, and so can be combined.

@donmccurdy does glTF require these be combined or is it optional?

donmccurdy commented 2 months ago

Optional, there is no requirement that textures be packed.