KhronosGroup / KTX-Software

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

Error on --format R8G8B8A8_SRGB (v4.3.0-alpha3) #751

Closed donmccurdy closed 1 year ago

donmccurdy commented 1 year ago

I'm creating some PNG textures, and want to encode them as RGBA32. Commonly authoring tools will omit the alpha channel in the PNG, but I need that channel to be present in the encoded KTX2 data, so we get RGBA32 and not RGB32. Here's what I tried:

# (A)
ktx create --format R8G8B8A8_SRGB \
  --zstd 18 --assign-oetf srgb --assign-primaries bt709 --generate-mipmap \
  rgb8.png rgba8.ktx2

# (B)
ktx create --format R8G8B8A8_SRGB --input-swizzle rgb1 \
  --zstd 18 --assign-oetf srgb --assign-primaries bt709 --generate-mipmap \
  rgb8.png rgba8.ktx2

For both, I see this error:

ktx create fatal: rgb8.png: Input file channel count 3 is less than the required 4 for VK_FORMAT_R8G8B8A8_SRGB.

My workaround for now is to use imagemagick to put an alpha channel into the source file, but ideally this could be done in the ktx CLI directly. I'm not sure whether the --input-swizzle flag should be required here or not.

Imagemagick:

convert rgb8.png png32:rgba8.png

Source image:

2d_rgb8.png.zip

MarkCallow commented 1 year ago

IINM --input-swizzle rgb1 should add the alpha channel. @aqnuep please confirm.

aqnuep commented 1 year ago

IINM --input-swizzle rgb1 should add the alpha channel. @aqnuep please confirm.

No, that won't work. The current version of the KTX Tools has been designed to require the input content to match the output format in terms of component count and bit-depth (8bpp or 16bpp normalized data can come from PNG, while 16/32-bit float or integer data can come from EXR), thus unfortunately this is not currently supported.

If support for having different component counts in inputs vs outputs is necessary (e.g. for the create and extract command) then that could be added as a feature request, as it would need a somewhat wider set of image transformations. Just adding such swizzling wouldn't be too difficult though.

@lexaknyazev, FYI.

MarkCallow commented 1 year ago

Given that Metal (and therefore MoltenVK) does not support RGB textures being able to add an opaque alpha channel without needing to use an external tool seems an important feature to me.

aqnuep commented 1 year ago

I don't disagree with that, in fact it's not specific to Metal, many desktop GPUs do not support RGB, only RGBA. I'm just stating that such functionality was not part of the original set of requirements as there was a 1-to-1 mapping from input image formats to output texture formats.

aqnuep commented 1 year ago

In any case, I'll put together a PR for supporting this (with and without an explicit input swizzle parameter).

aqnuep commented 1 year ago

I've created PR #755 to add support for this. Also added about 100 test cases covering the added new functionality (see CTS PR https://github.com/KhronosGroup/KTX-Software-CTS/pull/3).