Add BCn encoders/decoders with RDO support - #1167
Conversation
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Add encode/decode tests (that use both CompressBCn/DecodeBCn) *Add BCn ktx2 test files (transcoded from tests/resources/ktx2/color_grid_uastc_zstd_5.ktx2) *Cleanup BCn test fixtures *Remove `std::cout` statement Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
There are also a lot of compiler warnings from bc7enc_rdo dependency. These should be straightforward to address directly in copied files from bc7enc_rdo. |
*Add BC1, BC3, BC4, BC5, and BC7 encoding support to "ktx encode" command. *Cleanup ktxBCnParams and add BC1/BC3 quality and mode params. *Add docstrings/documentation to newly added enums/structs in ktx.h. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
@walcht, Thank you for this. Can you view the build logs? One issue I notice immediately is that there are no changes to Regarding RDO and multi-threading, the UASTC encoder also has separate multi-threading options for the encoder and for the RDO step. You can follow the same model. If we need to fix warnings in the encoder I suggest forking the encoder then incorporating the fork here by way of Re. SIMD and ISPC, be careful how you support this. We need to support building and running on arm64 processors. Also compile flags to enable SSE or other SIMD options are not compatible with straightforward use of universal build tool chains, which is why this project does not do universal builds. Better is use of compiler pre-defined macros and run-time queries to discover what the software is being compiled for and running on. However since we aren't doing universal builds there is no need to obsess over this last detail. |
|
@MarkCallow - Concerning the Concerning the build/CI logs: they are mostly failing because of bc7enc_rdo compiler warnings which should be suppressed. They will also fail because I haven't re-generated the golden files yet for ktx CTS (e.g.,
Please do so (as far as I understood, this will be forked under the KhronosGroup and any updates here will be pushed there via the Concerning SIMD and ISPC: I think it makes sense to leave this for another PR, do you agree? (reasoning is this: I have to get the basics working properly, add proper testsuite that covers all supported BCn formats, etc. Once that is done, I can open another PR for SIMD performance improvements). Or I will leave this to very end (last in TODO list above). |
*Suppress bc7enc_rdo warnings like unused-variables, memset'ing a non-trivial class (in this case the class is obviously trivial hence a void* cast is used to suppress this warning). Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
*Some CIs report further unused variables/functions that are not reported when building locally. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Updates about CI jobs:
|
|
You will need to rebase to or merge current main to get the fix for the NSIS issue on Windows. I have created a fork of https://github.com/richgel999/bc7enc_rdo. To incorporate it in your add-BCn-decoder branch do the following in the repo root directory: You can make changes in this subdirectory, as you are now, and when everything is working I can push the changes to the fork.
I agree. I wanted to make you aware that arm64 is a build target. Re. reuse, you have to add an entry to REUSE.toml to get external/bc7enc_rdo ignored. It is better to do that than add SPDX comments to all the files. The entry in REUSE.toml will have to mention a license. Use the MIT license option. Here are a few high level points.
|
|
Re the macOS build failure, because the output from the Xcode build is so voluminous it is run through a script, xcpretty, to prettify it. On a past CI service, without this, the logs exceeded the maximum allowed. Recently, for reasons I have yet to investigate, it has started swallowing compile errors. You can turn it off by editing scripts/install_macos.sh and commenting out the line that installs |
*Add BCn encoder support for `ktx create` command. *Add missing tests for `ktx encode`, `ktx extract`, and `ktx create`. *Expose BC1/BC3 approximation mode option to ktx CLIs *Misc cleanups (still early-stage PR) Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Agree. I wasn't initially aware of this. Apparently bc7f is significantly faster that the bc7 encoder used here (also the added benefit of being continuously maintained). I will integrate this right now since this seems to be straightforward (bc7enc_rdo also seems a bit not-longer-maintained so l think it's better to just integrate it now rather than waiting for it to be integrated into bc7enc_rdo repo). Concerning the decoding API: I added BCn decoders for VkUpload/GLUpload as a TODO (will follow same API as in etcunpack). Might also open a PR to add it for ASTC since I have already spent some time getting familiar with this code base.
Will add it in this PR. Will add it at the very end though since I have to finalize current formats.
I will try to address CI issues now (It is fine if I incrementally commit here to see if certain jobs pass?). |
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
…Group/bc7enc_rdo.git external/bc7enc_rdo subrepo: subdir: "external/bc7enc_rdo" merged: "dbe416d2" upstream: origin: "https://github.com/KhronosGroup/bc7enc_rdo.git" branch: "changes_for_ktx" commit: "dbe416d2" git-subrepo: version: "0.4.9" origin: "https://github.com/ingydotnet/git-subrepo" commit: "5e0f401"
*Before this commit, bc7enc_rdo dependency was manually copied to external/bc7enc_rdo directory (only needed files were copied). This was not ideal for a lot of reasons (mainly that we are introducing changes that may be streamed back to the original repo and having a subrepo/submodule is better suited for that than manually copying dependency files). *Add bc7enc_rdo to REUSE.toml with MIT license. *Git ignore compile_commands.json file (used by clangd LSP) Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
The integrated basisu_transcoder is a bit outdated (actually, significantly) and doesn't contain |
|
I was not aware that bc7f is included in basisu_transcoder. By pure coincidence I have just completed integration of Basis Universal release 2.1.0. See the Neither the KTX-Software code nor our golden files required any updates for our extensive test suite to pass with BU 2.1.0. I am therefore amenable to merging it now but will have to discuss within the Khronos WG and can't make any promises. The single image decoders used by GLUpload/VkUpload should be exposed in the library API but must be independent of the ktxTexture* classes. Software reading a KTX file incrementally will find them useful. Regarding the current ETC decoder, please note that it does not have a recognized open source license so might not be suitable for your OIIO work. |
*Add initial RDO post processing step for BC1, BC3, and BC7 but without ultrasmooth blocks support see: https://richg42.blogspot.com/2021/02/updated-bc7encrdo-with-improved-smooth.html *Fix encoder in case input texture does not have multiple-of-4 dimensions. This was reading beyond std::vector size before this commit. *Add initial RDO params to BCnParams with verbose explanation/description comments. *Misc refactoring/adjustments. Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
@MarkCallow - please don't approve the CI workflow yet (it will fail because I haven't updated the golden test files yet).
Will add bc7f once I finish RDO post processing. |
|
I have just merged PR #1170 so you will now find bc7f in One other thing re. |
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Update on this:
I spent some hours understanding the RDO code and apparently I was initially wrong about this being fixed and I think there is just no way to make multi-threaded RDO deterministic (i.e., running ST vs. MT yields same results). Why? Because running RDO on the whole image (single-threaded mode) means that each block N depends on previously RDO'ed M blocks (with M influenced by the window/dict size). In MT mode, image is divided into regions (one region per thread) and RDO is ran on these separately. I'm currently updating my guide, the no-multithreading parameter description, and a bunch of other things to reflect this. I also see that a lot of improvements can be done on the RDO function but I will postpone these to after when this PR gets merged. If you want, I can open a PR to add a comment and documentation about this with UASTC RDO (or any other RDO that is used). |
|
Thank you for your study.
The problem we originally observed was that the MT results were non-deterministic between runs. It wasn't investigated; there is still an open issue on the basis_universal repo. Do you think with the changes you have made MT will at least be deterministic between runs? We can certainly live with ST and MT giving different results provided we explain it in the docs. An obvious question is which produces the result with the least entropy (i.e. which will deflate best with zstd): ST or MT? Is the dictionary shared between the threads and would that affect the result.
Sounds good. When you have time, please give me a summary of the possible improvements.
That will be terrific. I'd like to get this in for v5.0.0. It would have to cover BasisLZ, UASTC LDR 4x4 and UASTC HDR 6x6 Intermediate. |
I just couldn't figure out how to make Doxygen generate a reference inside a C-code comment that is isn't supposed to consume... |
This is a markdown page being processed by Doxygen. According to https://www.doxygen.nl/manual/markdown.html to link a documented entity you can use |
|
Regarding |
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>
I changed it to this: I determine whether the decompressed format is SRGB vs. UNORM depending on the set BCn VkFormat (I don't look at the DFD since I assume if BCn is set to SRGB then so is the DFD's TF => otherwise an invalid KTX2 file, right?).
I think it makes sense to do this in a separate PR after this gets merged, do you agree?
I used cxxopts in ktxdiff. It's just better that having to manually parse the CLI arguments/options. Still a couple of comments that I haven't addressed yet.
|
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
The two files that causes ktxdiff to fail are: And this is the output I get: The problem is obvious when looking at this code sample in const auto diff = std::abs(lhs[i] - rhs[i]);
const auto absMin = std::min(std::abs(lhs[1]), std::abs(rhs[1]));
if (diff > tolerance * absMin)
return CompareResult{false, diff, i, i * element_size};If There is also a mistake here at const auto absMin = std::min(std::abs(lhs[1]), std::abs(rhs[1]));Should I fix it here or in another issue + PR? |
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>
|
All review comments are addressed (and some more docs issues are fixed) The only remaining thing:
So, if I understood correctly and since BC5 encoder (and all other BCn encoders) don't have a normal-map-specific parameters (such as angular error), this will only disallow RDO (i.e., will generate warning when supplied with --bcn-rdo), right? |
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
Starting to review the latest changes now. Sorry for the delay. I've been distracted with other work. As you may have seen I made a v5.0.0-rc2 with all your wonderful fixes.
|
MarkCallow
left a comment
There was a problem hiding this comment.
I still have to re-review the stuff in tools/ktx and texturetests.cc. I decided to post my comments so far from the re-review, as I've accumulated a reasonable number, mostly straightforward. This way, you can get started addressing them now, if you have the time.
| TEST_F(ktxTexture2_BCnEncodeDecodeTestRGB8_SRGB, encode_rgb8_srgb_to_bc1_then_decode) { runTest(KTX_BCN_COMPRESSION_BC1, false); } | ||
| TEST_F(ktxTexture2_BCnEncodeDecodeTestRGB8_SRGB, encode_rgb8_srgb_to_bc1_rdo_then_decode) { runTest(KTX_BCN_COMPRESSION_BC1, true); } | ||
|
|
||
| // TODO: add encode-then-decode test for BC2 once BC2 encoder is implemented |
There was a problem hiding this comment.
What is the plan for BC2?
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
|
First set of addressed review comments:
I want to implement BC2 at the very end when this PR is finished. The BC2 encoder is trivial. I just have to call BC1 on RGB and encode alpha sharply (i.e., into a fixed set of bits).
The range depends on whether the target is BC1/BC3 or BC7. If BC1/BC3 then the range is (if I remember correctly) [0, 19]. If BC7 then there is no range and the presets map to a set of OR'ed flags. That is the reason why I kept them separately initially. I can revert this or I can document the range + set of OR'ed flags that can be set (this is what I have done in latest commit).
I can add a PR that adds CI clang-format (per subdirectory) so that at least
There is probably a problem with nan/inf values. I need to look into the details but adding those nan/inf checks definitely made some tests pass (that previously didn't).
There are no |
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
I had not realized BC7 was a set of OR'ed flags. The original version you created defined the enumerand values as numbers like you are now doing in the documentation. Leave the code as is except force Is it really useful to have such fine control over the quality or are the six settings we have sufficient?
Let's wait on that until after this PR is done and merged.
I see you reverted back to imageio's half_to_float. Is there no need to fix anything in nan/inf related in that code?
For BC7 in the basis_universal repo |
Signed-off-by: Walid Chtioui <walid.chtioui.main@gmail.com>
They are not exposed to libktx users (i.e.,, public header). I removed any mention of these.
From my limited experimentation, the six settings we have are sufficient.
I think imageio's version is fine. I thought it had problems with returning nan/inf values but apparently there is no issue with it (all CIs pass).
These BC7 functions are not used by this PR (and are probably something else entirely). The bc7f encoder we are using is implemented in the file: Windows CI will pass if re-run (probably). |
There was a problem hiding this comment.
Recent changes all look good.
This looks ready except for the BC2 encoder and resolving the handling of perceptual in bc7f. Regarding the former,
do you want to wait until this is merged before adding the BC2 encoder? I am still waiting on the blocker for the v5.0.0 release before which I won't merge this. Regarding the latter, I want to review the code in basisu_transcoder and also look at basis_universal 2.5 which I think has bc7g.
I reran the Windows builds. There is still a test failure: texturetest.ktxTexture2_BCnEncodeDecodeTestRGB16_SFLOAT.encode_rgb16_sfloat_to_bc6hu_then_decode.
As discussed in #1159:
ktxTexture2_CompressBCnandktxTexture2_DecodeBCnare introduced in this PR to allow libktx users/consumers to encode/decode BCn textures from/to raw decompressed formats.https://github.com/richgel999/bc7enc_rdo does not support BC6HU/BC6HS encoding/decoding and also no BC2 (this format is essentially dead since BC3 replaces it).
Please feel free to give feedback, edit, and nitpick as much as possible.
Some context: I am adding KTX2 support to OIIO (PR: AcademySoftwareFoundation/OpenImageIO#5185) and having libktx encode/decode BCn formats significantly simplifies things (also ETC encoding/decoding which I can also open a PR for - if approved).
I haven't updated the KTX-Software-CTS with the added BCn test files.
Once this is finalized, this will fix #587.
Current TODOs:
[x] BC2 mode RDOBC2 is rarely used (I don't see any reason why to use it instead of BC3). Postponed or not planned to be implemented at all.BC6H mode RDO (HDR). I don't know if this is possible. If not, then I will not adjust the ert::reduce_entropy function for the moment.postponed (too much work/overload to include in this PR).ktx encodecommandktx extractcommandktx createcommandktxBCnParamsstruct (apparently there are many and I am not qualified to know which subset to expose or to expose them all). For the moment I will just expose them all.We agreed on using same parameters as UASTC RDO and adding additional ones if they make sense (I haven't benchmarked skip 0 MSE error option so I might remove it if it useless).
enable SIMD acceleration for BC7 encoder (using ISPC - see https://github.com/ispc/ispc) (this should be straightforward and should be enabled by default via a CMake flag; e.g., BC7_SIMD).=> since we are planning to use bc7f we will be planning to use bc7g once it is released (this is the SIMD equivalent of bc7f).LIBKTX_FEATURE_BCN_DECODERCMake flag optioncopied from MIT Licensed https://github.com/iOrange/bcdecfrom basisu)[x] add BC2 encoderpostponed/aborted (rarely used).--bcn-rdoparam.Note1: no LLMs/AI coding tools were used in any capacity whatsoever in writing or aiding in the writing of this PR.
Note2: I am an individual contributor (main reason I am contributing here is to add support for KTX2 in Blender).
Edit: TODO list edits