Two bugs in the C# layer, both reproducible against the current main (f8b5db5).#102
Conversation
- KtxTexture.LoadTexture2D: copy m_Ktx.orientation to result.orientation in the direct-format branch (was only set in the transcoding branch), fixing IsYFlipped() returning true for files explicitly authored Y-up. - KtxNativeInstance.GetGraphicsFormat: add cases for ASTC_4x4, 5x5, 6x6, 10x10, 12x12 (sRGB and UNorm) and remove them from the unsupported fall-through. Previously only ASTC_8x8 loaded; other square ASTC files hit 'untested/unsupported format'.
|
@atteneder is this repo no longer maintained? It looks to be a long way behind the official Unity package. It would be great if this and https://github.com/atteneder/KTX-Software-Unity followed a similar pattern to your GLTFast repo so we can report issues and contribute. Is there a reason Unity hasn't chosen to fork this and take it internal instead? |
|
@Moayad-Rahhal Thanks for the PR. I try to look into it soon. @WikkidEdd as you found out correctly, I still maintain this package (just this week I started updating to libktx 5.0.0), but the public repository is outdated due to organisational and legal hurdles. I'll start an attempt to enable collaboration. Meanwhile feel free to raise issues here, work on a copy of KTX for Unity and send patches along my way. |
|
Thanks for the response @atteneder. I was thinking about looking at this #67 to get a better quality output for UASTC encoded textures, but didn't want to tackle it if it resulted in us diverging from the main Unity package. |
Bug 1 — Orientation metadata dropped for direct-format paths
KtxTexture.LoadTexture2D copies m_Ktx.orientation to result.orientation only inside the m_Ktx.needsTranscoding branch. The direct-format else branch never sets it, so KTX2 files that don't need transcoding (any direct GPU format, e.g. ASTC_8x8 with KTXorientation: ru) silently lose their orientation. Consumers calling result.orientation.IsYFlipped() get true for files explicitly authored Y-up.
Fix: copy the orientation in the direct-format branch as well.
Bug 2 — GetGraphicsFormat() only maps ASTC_8x8
The switch in KtxNativeInstance.GetGraphicsFormat maps only Astc8X8SrgbBlock/Astc8X8UNormBlock. The other square ASTC variants (Astc4X4, Astc5X5, Astc6X6, Astc10X10, Astc12X12) are listed in the unsupported fall-through, even though Unity's GraphicsFormat enum has all of them. Loading a KTX2 with any of those formats hits the "untested/unsupported format" error.
Fix: add the missing square ASTC cases (sRGB + UNorm) and remove them from the unsupported fall-through.
Reproduction
ktx create --format ASTC_6x6_SRGB_BLOCK --astc-quality medium
--convert-texcoord-origin bottom-left --assign-tf srgb in.png out.ktx2
ktx info out.ktx2 shows KTXorientation: ru.
Load with KtxTexture.LoadFromBytes(...) against current main → LogError "untested/unsupported format" (Bug 2).
With this PR → file loads, and result.orientation.IsYFlipped() correctly returns false (Bug 1).
Tested on Unity 2022.3 in Editor (macOS) and on Android device. Verified end-to-end against ASTC_6x6 and ASTC_8x8 files. Diff is minimal — no behavioral change for previously-working formats.