Repository navigation
Conversation
Add limited support for input and output for the KTX2 format. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Update: I'm working on integrating BCn decoders/encoders directly within libktx so that this PR gets significantly simpler (e.g., from 12_000 lines most of which are from dependencies to circa 2_000). |
*Significantly simplify KTX support by relying on libktx to decode/encode GPU-compressed formats such as BCn, ASTC, and ETC. Consequently, BCn and ETC dependencies are removed. All future required encoders/decoders should be implemented in libktx and not here to keep this is as simple, maintainable, and short as possible. *Remove ETC sub-dependency from local libktx build. *Update ktx README.md documentation to reflect that only libktx dependency is and should be used (i.e., no additional dependencies should be added). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Update2: significantly simplified the whole PR. Now only dependency is libktx (since I offloaded most work to libktx via a PR that will be probably merged soon). Can you please not approve workflows unless I explicitly request a workflow run? (to avoid filling the actions log with CIs that I know will certainly fail). |
*IntelLLVM is simply not supported by libktx. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Sure, in this case I can do so, but only because you are a a first-time contributor to this project, so admins have to approve workflow runs on the project's main account. Once you have any PR accepted and merged, the workflows will run automatically. Here is the general solution that you or others can do, in this situation and after you no longer need admin approval: Let's say you have pushed to your fork's branch "mypr" and submitted a PR to the project from that branch. (Aside: I strongly recommend working in topic branches, and never submitting a PR from your "main".) After submitting the PR, you realize you need to iterate (you want to add things, or CI is not passing and you need to make repairs, or you have review comments to address), but you don't want to do a series of many many pushes as you go, each of which will trigger a CI run on the main branch as well as sending email alerts to everybody "watching" this project, since pushing to your branch is technically a PR update. Instead, push to a different branch! This pushes your fork's "mypr" branch to your fork in a "test" branch. This will run the CI on your fork in your account (allowing you to see if CI passes for the changes you've made), but since your "test" branch is not associated with the PR, it will not send the rest of us alerts and will not trigger a CI run on the project's main repo account. Then when you finally have it right and are ready for the rest of us to see it, you can |
*Add to_native* conversion to KTX2 output similar to how JPEG and PNG OIIO outputs are written. *Apply clang-format-17 as opposed to the previously applied clang-format v18 which caused the CI to fail. *Update CMAKE_VERSION in CIs to minimum version required by libktx (i.e., 3.23). *Fix uninitialized structs passed to libktx. *Other misc clean-ups and minor refactoring. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Question about older CMake version that is failing some CIs and icpx:
All CI issues are now solved. Will push updates here when sufficient testing is done. |
…actoring *KTX does not support compilation with Intel's C++ compiler ICX hence why an override option is provided and is used in ICX CI. *Do all decompression/decoding/deflation in open() rather than in seek_subimage() Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Due to missing 'alpha' support (which obviously exists in libktx), unit testing was failing (due to unexpected number of channels). *Misc cleanup; removal of ktxTexture* member field and only using ktxTexture2*. *Remove MSVC/GNU/Clang checks on libktx dependency in externalpackages. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Previously, a buffer allocated via a malloc by libktx was freed using RAII via std::unique_ptr's delete[] which doesn't match the allocator. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*libktx > 4.3.2 requires CMake >= 3.22. The code changes required for this will also be applied accordingly (e.g., older libktx versions don't support direct BCn encoding/decoding). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Previously, libktx version was hardcoded to v5.0.0 now it is based on the CMake-set variable Ktx_VERSION. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*malloc has to be matched by a call to free not a call to delete. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Attempting to address old CMake version with 'oldest' CIs. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Point to not-yet-merged PR in libktx only for Intel-based MacOS runner. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
This is added so that CIs on older runners/compilers/cmake can pass. Newer version of libktx require CMake >= 3.22 which these runners cannot install. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
ktxBasisParams is vastly different starting from libktx >= 5.0.0. Use smart pointers to handle ktxTexture resources and clean them up. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
To be reverted once the linkage issue for ASTCENC is solved. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
* Prior to this commit, metadata KVD keys were wrongly constructed via a string_view; the `keylen` includes the NUL termination char which should not be supplied to the string_view. * KTXorientation metadata is now parsed and mapped to the corresponding OIIO Orientation value. A test case with `--reorient` is included. * Cubemaps are now properly supported. Cubemap faces are exposed via the `subimage` parameter. * clang-format using latest clang-format so that the CI shuts up. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
* Using the attribute "ktx:1d" and given a height <= 1, a 1D texture can be written. * The attribute "ktx::supercompressionscheme" is now fixed to only store the strings "zstd" or "zip" (i.e., zlib). * Misc clean-up and dead code/comments removal. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
* span-based methods (both for the reader and the writer) now hold the main implementations and the to-be-deprecated and less-safer point-based methods simply call span-based methods with inferred data buffer length. * add proper support for writing cubemap textures using write_tiles. * fix reading of cubemap textures to now use read_native_tiles instead of the previous erroneous each-slice-is-a-subimage approach. * fix reading of volume textures by 'simulatin' them as tile-based textures with tile size: WIDTHx1x1. This, however, weirdly causes slowdowns because of repetitive calls to read_native_volumetric_tiles (either OIIO poorly implements this or we are doing something wrong in this KTX2 plugin). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Hi, I'd like to revive this so that if possible, we can include it in the 3.2 release. Two problems currently standing in our way:
|
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Even if CMake version > 3.23 requirement is satisfied, libktx dependencies will just not build using an old version of gcc. Just disable it. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Dropping a note here either for now/this PR or for subsequent work after this lands. Users will need a way, through the ImageSpec attributes, to control the KTX writer so that it performs the following in some cases: Users need to do this for "data"/"non-color" textures like normal maps or similar. I don't know all the nuances around if that call is valid for all formats inside the KTX container but another implementation I'm working with at the moment is doing so at least for the |
|
interesting... thanks, Jesse -- noted. When |
* write_tile(s) used to segfault when writing cubemaps because of wrong dst offset computation. m_img initialization and extension whenever a new mipmap is added was also wrong. Writes of volume and cubemap textures are now tested (using OIIO API not oiiotool) at: https://github.com/walcht/ktx-oiio-examples * major refactoring of KTX2 documentation. Attributes now use underscore rather than the harder-to-read Pascal Case naming. Unused attributes are removed. Several spell fixes are introduced. * ktxinput now supports cubemap array textures. * disable clang-format'ing on metadata parsing function. Formatting just makes it really hard to follow (which introduced some errors which are now fixed). * major other misc updates (add noexcept when necessary, more asserts, etc.) Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Some updates:
Since I have time this week, I will focus a lot on extensive testing on my other repo of the KTX2 writer part (against 100s of libktx's CTS test files). I will also focus on colorspaces since I still only tested/cared about sRGB/linear... |
* the attribute "compression" is used by OIIO and was causing CIs to fail because it was being set to "zip" which is not supported for KTX2 formats (only supported as a super-compression scheme). * improve the get_vkformat_from_info() to support almost all widely used VkFormats. * add "ktx:normalized" bool attribute to ktxoutput to specifiy whether the target VkFormat should be normalized (UNORM or SNORM depending on type sign) if possible (e.g., if colorspace is not sRGB). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
CIs were failing because I used the attribute This is now reverted to |
No, it was correct all along. You should use "compression" like all the other plugins do. The correct behavior when receiving an unsupported/invalid compression is just to silently use a suitable default that is appropriate or typical for the file format. Do you know where in the software it is setting it to "zip"? Maybe the problem is that this caller is more opinionated than it should be? |
The problem is occurring in Before I re-revert this and use KTX2 supports two kinds of compressions: the first layer is either a GPU-block-compressed format or a latent Basis Universal coder (the current attribute for this is The question now is: what should the Last note: please do not merge this until I do extensive ktxoutput testing (1000s on libktx CTS testsuite) on my second repo. I had ktxoutput segfaults before that I couldn't capture using the testsuite in I will try to finalize all of this this week (alongside some libktx fixes). |
That test is checking that certain internal hint attributes that are not meant to be written to files are properly suppressed, by all file writers. It makes a small TIFF file, reads it, then writes to a file of each format that supports arbitrary metadata, to ensure that it really is suppressed. The TIFF file it reads has "zip" compression, so of course that's what it will contain when it writes the ktx file. This is indicative of what will happen any time you do a plain OIIO-based copy of a file, such as This is precisely the reason why I gave you the advice above. You have to ignore unknown compression formats, because they might have come from other file types with compression methods you don't support in ktx. The only reason that test is failing is that the ktx output is having a hard error when encountering the "compression" value that came from the input file. Substitute in a reasonable default in that case, without generating an error message.
If it were me, I would choose the following design:
|
|
Concerning the discussion about
|
|
Yes, the code I was reviewing is related to glTF processing. I'll loop in @julienduroure in case he wants to clarify further, but I think the initial request still stands. Users will need a way, through the ImageSpec attributes, to control the KTX writer so that it sets those primaries to unspecified when requested. |
|
While testing with Has anyone encountered this before? If so, is it a false positive? (so that I know whether to spend time debugging this). I built OIIO as such: |
* avoid applying a further super-compression on an already-supercompressed KTX texture object. * add proper support for compressing to ASTC (i.e., astcenc params attributes and tests). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
In memory, types generally need to be aligned, e.g., a 4-byte This can also happen in less obvious ways, like What you need to do is repeat the command (still with the sanitizer-compiled version) run in gdb or lldb, catch the error, and walk up the stack until you can see the line in OIIO code where it is happening and which variables are involved. |
brechtvl
left a comment
There was a problem hiding this comment.
Not a complete review by any means, but some hopefully straightforward things to fix.
| if (m_tex->numDimensions == 1) { | ||
| m_spec.attribute("textureformat", "Plain Texture"); | ||
| } | ||
| if (m_tex->numDimensions == 2) { |
There was a problem hiding this comment.
| if (m_tex->numDimensions == 2) { | |
| else if (m_tex->numDimensions == 2) { |
| create_info.numDimensions = m_is1D ? 1 : m_basedepth > 1 ? 3u : 2u; | ||
| create_info.numLevels = m_miplevel_idx + 1; | ||
| create_info.numLayers = 1; | ||
| create_info.numFaces = (m_spec.tile_width > 1 && m_basedepth <= 1) ? 6 : 1; |
There was a problem hiding this comment.
| create_info.numFaces = (m_spec.tile_width > 1 && m_basedepth <= 1) ? 6 : 1; | |
| create_info.numFaces = m_is_cubemap ? 6 : 1; |
m_spec comes from the last subimage, which could be the 1x1 mip level I think. Simpler to check m_is_cubemap which was already determined earlier.
| case TypeDesc::UINT8: { | ||
| switch (spec.nchannels) { | ||
| case 3: return srgb_colorspace ? VK_FORMAT_B8G8R8_SRGB : normalized ? VK_FORMAT_B8G8R8_UNORM : VK_FORMAT_B8G8R8_UINT; | ||
| case 4: return srgb_colorspace ? VK_FORMAT_B8G8R8A8_SRGB : normalized ? VK_FORMAT_B8G8R8_UNORM : VK_FORMAT_B8G8R8A8_UINT; |
There was a problem hiding this comment.
| case 4: return srgb_colorspace ? VK_FORMAT_B8G8R8A8_SRGB : normalized ? VK_FORMAT_B8G8R8_UNORM : VK_FORMAT_B8G8R8A8_UINT; | |
| case 4: return srgb_colorspace ? VK_FORMAT_B8G8R8A8_SRGB : normalized ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_B8G8R8A8_UINT; |
| @@ -0,0 +1,781 @@ | |||
| // Copyright Co tributors to the OpenImageIO project. | |||
There was a problem hiding this comment.
| // Copyright Co tributors to the OpenImageIO project. | |
| // Copyright Contributors to the OpenImageIO project. |
| // https://registry.khronos.org/KTX/specs/2.0/ktxspec.v2.html#_ktxorientation | ||
| OIIO_ASSERT(vallen <= 4); // at most '[rl][du][oi]\0' | ||
| auto cval = reinterpret_cast<const char*>(val); | ||
| if (vallen <= 1 || cval[vallen - 1] != '\0') { |
There was a problem hiding this comment.
| if (vallen <= 1 || cval[vallen - 1] != '\0') { | |
| if (vallen <= 1 || vallen > 4 || cval[vallen - 1] != '\0') { |
Better to also not crash in case it happens anyway, rather than just assert in debug builds.
| ENABLEVAR ENABLE_DPX | ||
| IMAGEDIR oiio-images/dpx URL "Recent checkout of OpenImageIO-images") | ||
| oiio_add_tests (ktx | ||
| ENABLEVAR ENABLE_KTX |
There was a problem hiding this comment.
| ENABLEVAR ENABLE_KTX | |
| FOUNDVAR Ktx_FOUND ENABLEVAR ENABLE_KTX |
To properly disable test if not found.
| // clang-format off | ||
| m_max_nmiplevels = m_is3D | ||
| ? (uint32_t)floor(logf(std::min(std::min(m_spec.width, m_spec.height), m_spec.depth)) / logf(2)) + 1 | ||
| : (uint32_t)floor(logf(std::min(m_spec.width, m_spec.height)) / logf(2)) + 1; |
There was a problem hiding this comment.
Don't use floating point math for computing mip levels due to rounding errors. 8192x8192 already gives the wrong number (13 instead of 14).
Should be possible to copy integer math code from elsewhere in OIIO to do this.
|
|
||
| ioproxy_retrieve_from_config(m_spec); | ||
| if (!ioproxy_use_or_open(m_filename)) | ||
| return false; |
There was a problem hiding this comment.
This ioproxy is in the middle between various validations, I think it should be near the end of this function.
| } | ||
|
|
||
| // Load the actual image data (libktx owns the data buffer) | ||
| if (auto result = ktxTexture2_LoadImageData(m_tex.get(), NULL, 0); |
There was a problem hiding this comment.
Here it's loading the image data before check_open validates the dimensions, which defeats the purpose of limits checked there.
| } | ||
|
|
||
| if (auto Q = m_spec.find_attribute("ktx:normalized", TypeDesc::INT)) { | ||
| m_normalized = static_cast<bool>(Q->get_int()); |
There was a problem hiding this comment.
m_normalized defaults to false if ktx:normalized is not specified. But when converting from most (all?) other image file formats, they are meant to be normalized to 0..1 I think.
Also, ktxinput.cpp doesn't set the ktx:normalized attribute so it doesn't roundtrip.
Seems my comment on the code you referenced is incorrect. Thanks to point it out. If I understand correctly, it should change from: to Is that OK? |
Description
Fixes #2329. Add initial KTX2 format support (closest similar plugin/format in OIIO is DDS).
This KTX plugin support obviously nullifies 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.
PR, by pushing the changes to my fork and seeing that the automated CI
passed there.
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.
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_1DSINGLE_TEXTURE_2DSINGLE_TEXTURE_3DCUBEMAP_TEXTUREARRAY_TEXTURE_1DARRAY_TEXTURE_2DARRAY_TEXTURE_CUBEMAP[ ](not planned)ARRAY_TEXTURE_3DSupported/Tested raw VkFormats (decoder + encoder):
VK_FORMAT_R8_UNORMVK_FORMAT_R8G8_[SRGB|UNORM]VK_FORMAT_R8G8B8_[SRGB|UNORM]VK_FORMAT_R8G8B8A8_[SRGB|UNORM]VK_FORMAT_R16G16B16_SFLOATVK_FORMAT_ASTC_NxM_SRGB_BLOCK(all block dimensions)VK_FORMAT_ASTC_NxM_SFLOAT_BLOCK(all block dimensions)Block-compressed formats (decoder + encoder):
[ ] BCn (waiting on libktx BCn support PR merge here: Add BCn encoders/decoders with RDO support KhronosGroup/KTX-Software#1167)=> add BCn support in a separate PR whenever libktx 5.0.0 is officially released and my BCn PR is merged.Basis Universal schemes (encoder + decoder):
UASTCETC1SSupercompression schemes (decompressor + compressor):
ZLIBZSTDDependencies (only one - libktx)
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 (ETC is not used in PR).This PR should only be merged after the release of libktx 5.0.0.
Resources