feat(ktx): add initial ktx input/output support.#5185
Draft
walcht wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
Draft
feat(ktx): add initial ktx input/output support.#5185walcht wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
walcht wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
Conversation
Add limited support for input and output for the KTX2 format. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2329.
Add initial KTX2 format support (closest similar plugin/format in OIIO is DDS).
(Note: 2nd attempt at opening this PR because the first one was signed off with
my other email).
Important
This is a very early draft PR because I have been trying since some days to fix
CI issues (compiles and passes tests fine on my Ubuntu machine but fails on
almost all CI jobs...). Any help with these CI issues is appreciated.
Consequently, there are a lot of TODO comments, and code snippets that haven't
been properly tested yet. All of these will be addressed after fixing CI issues.
This KTX plugin support obviously nulifies the benefits of using KTX in the
first place. That being said, this plugin is still useful so that end users
don't have to convert back and forth between KTX <-> supported format (e.g., PNG).
It is also useful to convert to and from KTX2 format using OIIO.
An example usecase would be Blender and its glTf import/export plugin.
(see KhronosGroup/glTF-Blender-IO#1896).
Ideally, at some point in the future, OIIO may introduce a new API to accomodate
texture formats that are mainly used for fast texture uploads to GPUs.
For details on KTX2 and what this PR supports and what it doesn't (alongside a
description of current limitations), see
src/ktx.imageio/README.md(copied partsof it at the end of this).
Tests:
Added initial testsuite but only for the input (i.e., info_command) part of the plugin.
Ideally, after fixing CI issues, the tests will be adjusted for ktxouput.
Checklist:
=> Haven't used any AI coding assistant tools in any capacity whatsoever.
behavior.
=> Not yet. But will add docstrings alongside custom "ktx:" ImageSpec
attribute descriptions.
=> Partially. As stated above, I mainly opened the PR now in the hope of speeding
up fixes for the CI issues.
PR, by pushing the changes to my fork and seeing that the automated CI
passed there.
=> Only 2 jobs pass (clang-format, ABI check).
fixed any problems reported by the clang-format CI test.
corresponding Python bindings. If altering ImageBufAlgo functions, I also
exposed the new functionality as oiiotool options.
=> No new API is introduced. This is just a plugin.
CI Issues:
Tried adjusting the
build_cmakescript but still CMake 3.18.2 is used in somerunners.
some reason). This is probably due to me missing some crucial setup step (I thought
LINK_LIBRARIES KTX::ktxis sufficient for linking to libktx to work - which, again, is onmy machine).
KtxConfig.cmakeon MacOS => Absolutely no idea why (passes in otherWindows/Linux jobs).
some libktx build instruction details that I haven't looked at yet).
Feel free to edit this directly.
The sections below are copied from
src/ktx.imageio/README.md. These will be updatedas this PR progresses.
Supported Encoders/Decoders
Supported/Tested texture kinds:
SINGLE_TEXTURE_1D(TODO)SINGLE_TEXTURE_2DSINGLE_TEXTURE_3D(TODO)CUBEMAP_TEXTURE(TODO)ARRAY_TEXTURE_1D(TODO)ARRAY_TEXTURE_2D(TODO)ARRAY_TEXTURE_3D(not planned)ARRAY_TEXTURE_CUBEMAP(not planned)Supported/Tested VkFormats:
VK_FORMAT_R8_UNORMVK_FORMAT_R8G8_SRGBVK_FORMAT_R8G8B8_SRGBVK_FORMAT_R8G8B8A8_SRGBVK_FORMAT_ETC2_R8G8B8_SRGB_BLOCKVK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCKVK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCKVK_FORMAT_BC1_RGB_SRGB_BLOCKVK_FORMAT_BC3_SRGB_BLOCKVK_FORMAT_BC4_UNORM_BLOCKVK_FORMAT_BC5_UNORM_BLOCKVK_FORMAT_BC7_SRGB_BLOCKVK_FORMAT_ASTC_4x4_SRGB_BLOCKBCn GPU block-compressed formats:
ETC2 GPU block-compressed formats:
ETC2_RGB(aka ETC1) decoderETC2_RGB_A1decoderETC2_RGBAdecoderASTC GPU block-compressed formats:
ASTCdecoder (using libktx'sktxTexture2_DecodeAstc)ASTCencoder (using libktx'sktxTexture2_CompressAstc)Basis Universal schemes:
UASTCencoder/decoderETC1Sencoder/decoderSupercompression schemes:
ZLIBdecompressor/compressor (using libktx'sktxTexture2_DeflateZLIB)ZSTDdecompressor/compressor (using libktx'sktxTexture2_DeflateZstd)Limitations
If original KTX2 format contained generated mip maps, there is simply no way
to know which filter and its parameters that were used to regenerate these
mipmaps. To avoid any issues, we simply early quit (return false) in
open()if
get_int_attribute("ktx:miplevels") > 1.TODO: mipmap generation using sane defaults (using OIIO).
KTX2 supports many GPU-block-compression encoders and each one may have many
different parameters that change the encoding quality (as usual, quality-speed
tradeoff). There is simply no way to regenerate the exact same input texture
without knowing these parameters and nor the KTX2 specs nor libktx nor
KTX-Software tooling stores any (or sufficient) information about these params
in the metadata.
As stated in the comments in
ktxinput.cpp, if given ktx texture issupercompressed then it has to be all decompressed (i.e., NOT the decompression
of the underlying GPU texture format but rather the supercompression). This
means that if you just need a particular subimage/miplevel, you pay the memory
price of loading the whole KTX texture (which might be very large for 3D
textures and texture arrays).
KTX2 writer writes the whole texture (i.e., all subimages/mipmaps) in the
close()function (i.e., when the ImageOutput object is destroyed or requestedto close). libktx does not provide a way to append or write subimages (is this
problematic or contrary to the way OIIO expects us to write files?).
KTX1 format is not yet supported. Adding support for it after finishing KTX2
should be relatively straightforward (Note: KTX1 is officially deprecated and
KTX-Software provides tools to convert from KTX1 to KTX2).
Only LDR formats (to be more precise, only TypeDesc::UINT8). Adding support
for HDR is straightforward (conversions for large number of enum values from
VkFormat have to be written).
bc7enc_rdo dependency for encoding/decoding BC1-7 formats does not (
contrary to what the repo description suggests) support BC6HS/BC6HU HDR formats.
See below on what we can use for BC6HU/BC6HS encoding/decoding.
Dependencies
libktx: for general KTX@ format support (loading of KTX2 files, transcoding
support, supercompression decompression support, etc.).
OpenImageIO/src/cmake/build_Ktx.cmakelib/etcdec.cxx's license is not opensource but libktx exposes a function to decode ETC formats, do we use it?
bc7enc_rdo: for BC1, BC2, BC3, BC4, BC5 and BC7 decoding/encoding.
ktxTexture2_DecodeBcnfunctionto KTX-Software (libktx) so that we no longer have to include this dependency here.
etcdec: ETC2/EAC decoding.
remove this dependency?
Note: for BC6HS/BC6HU and ETC encoding support, we can use
ConvectionKernels. It is better to have this dependency built separately (i.e., not copied into
source
ktx.imageiodirectory).Personally, for ETC encoding, I would prefer to use ETCPACK since libktx already
uses its decoder and is the more standard choice (i.e., seems more official).
For BC6HU/BC6HS decoding, we can use the same dependency used by the DDS format
(bcdec.h). For encoding, we can extract the needed function from DirectX Texture
Library (MIT license).
Resources