Skip to content

Commit

Permalink
kram/v - add SUPPORT_RGB flag, fix RGBA32F format
Browse files Browse the repository at this point in the history
RGB isn't supported by Metal, but we try our best to convert one mip level so that kramv can display the image.  kram converts it to RGBA, but info will still report the RGB format.  Row Alignment isn't always conducive to opening these, but will fix that later.  Also not handling srgb properly.  This has to first go to KramImage to load as RGBA and then back to KTXImage.

Fix RGBA32F format in the table.
  • Loading branch information
alecazam committed May 9, 2021
1 parent 38d0725 commit 8a6ec89
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
28 changes: 26 additions & 2 deletions libkram/kram/KTXImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ enum GLFormat {
GL_RG32F = 0x8230,
GL_RGBA32F = 0x8814,

#if SUPPORT_RGB
GL_RGB8 = 0x8051,
GL_SRGB8 = 0x8C41,
GL_RGB16F = 0x881B,
GL_RGB32F = 0x8815
#endif

/* These are all of the variants of ASTC, ugh. Only way to identify them is to
walk blocks and it's unclear how to convert from 3D to 2D blocks or whether hw
supports sliced 3D.
Expand Down Expand Up @@ -299,6 +306,15 @@ enum VKFormat {
// VK_FORMAT_ASTC_12x10_SRGB_BLOCK = 182,
// VK_FORMAT_ASTC_12x12_UNORM_BLOCK = 183,
// VK_FORMAT_ASTC_12x12_SRGB_BLOCK = 184,

#if SUPPORT_RGB
// import only
VK_FORMAT_R8G8B8_UNORM = 23,
VK_FORMAT_R8G8B8_SRGB = 29,
VK_FORMAT_R16G16B16_SFLOAT = 90,
VK_FORMAT_R32G32B32_SFLOAT = 106,

#endif
};

// DONE: setup a format table, so can switch on it
Expand Down Expand Up @@ -489,8 +505,16 @@ static bool initFormatsIfNeeded()

KTX_FORMAT(EXPr32f, MyMTLPixelFormatR32Float, VK_FORMAT_R32_SFLOAT, GL_R32F, GL_RED, 1, 1, 4, 1, FLAG_32F)
KTX_FORMAT(EXPrg32f, MyMTLPixelFormatRG32Float, VK_FORMAT_R32G32_SFLOAT, GL_RG32F, GL_RG, 1, 1, 8, 2, FLAG_32F)
KTX_FORMAT(EXPrg32f, MyMTLPixelFormatRGBA32Float, VK_FORMAT_R32G32B32A32_SFLOAT, GL_RGBA32F, GL_RGBA, 1, 1, 16, 4, FLAG_32F)

KTX_FORMAT(EXPrgba32f, MyMTLPixelFormatRGBA32Float, VK_FORMAT_R32G32B32A32_SFLOAT, GL_RGBA32F, GL_RGBA, 1, 1, 16, 4, FLAG_32F)

#if SUPPORT_RGB
// these are import only formats
KTX_FORMAT(EXPrgb8, MyMTLPixelFormatRGB8Unorm_internal, VK_FORMAT_R8G8B8_UNORM, GL_RGB8, GL_RGB, 1, 1, 3, 3, 0)
KTX_FORMAT(EXPsrgb8, MyMTLPixelFormatRGB8Unorm_sRGB_internal, VK_FORMAT_R8G8B8_SRGB, GL_SRGB8, GL_SRGB, 1, 1, 3, 3, FLAG_SRGB)
KTX_FORMAT(EXPrgb16f, MyMTLPixelFormatRGB16Float_internal, VK_FORMAT_R16G16B16_SFLOAT, GL_RGB16F, GL_RGB, 1, 1, 6, 3, FLAG_16F)
KTX_FORMAT(EXPrgb32f, MyMTLPixelFormatRGB32Float_internal, VK_FORMAT_R32G32B32_SFLOAT, GL_RGB32F, GL_RGB, 1, 1, 12, 3, FLAG_32F)
#endif

return true;
}

Expand Down
14 changes: 12 additions & 2 deletions libkram/kram/KTXImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@ enum MyMTLPixelFormat {

// TODO: also need rgb9e5 for fallback if ASTC HDR/6H not supported
// That is Unity's fallback if alpha not needed, otherwise RGBA16F.

#if SUPPORT_RGB
// Can import files from KTX/KTX2 with RGB data, but convert right away to RGBA.
// These are not export formats. Watch alignment on these too. These
// have no MTLPixelFormat.
MyMTLPixelFormatRGB8Unorm_internal = 200,
MyMTLPixelFormatRGB8Unorm_sRGB_internal = 201,
MyMTLPixelFormatRGB16Float_internal = 202,
MyMTLPixelFormatRGB32Float_internal = 203,
#endif
};

enum MyMTLTextureType {
// MyMTLTextureType1D = 0,
MyMTLTextureType1DArray = 1,
// MyMTLTextureType1D = 0, // not twiddled or compressed, more like a buffer but with texture limits
MyMTLTextureType1DArray = 1, // not twiddled or compressed, more like a buffer but with texture limits
MyMTLTextureType2D = 2,
MyMTLTextureType2DArray = 3,
// MyMTLTextureType2DMultisample = 4,
Expand Down
5 changes: 5 additions & 0 deletions libkram/kram/KramConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
#define COMPILE_ASTCENC 0
#endif

// rgb8/16f/32f formats only supported for import, Metal doesn't expose these formats
#ifndef SUPPORT_RGB
#define SUPPORT_RGB 1
#endif

// includes that are usable across all files
#include "KramLog.h"

Expand Down
16 changes: 14 additions & 2 deletions libkram/kram/KramImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ bool Image::loadImageFromKTX(const KTXImage& image)
switch (image.pixelFormat) {
case MyMTLPixelFormatR8Unorm:
case MyMTLPixelFormatRG8Unorm:
case MyMTLPixelFormatRGBA8Unorm: {
#if SUPPORT_RGB
case MyMTLPixelFormatRGB8Unorm_sRGB_internal: // TODO: not handling srgba yet
case MyMTLPixelFormatRGB8Unorm_internal:
#endif
case MyMTLPixelFormatRGBA8Unorm_sRGB: // TODO: not handling srgba yet
case MyMTLPixelFormatRGBA8Unorm:
{
const uint8_t* srcPixels =
image.fileData + image.mipLevels[0].offset;

Expand Down Expand Up @@ -171,6 +177,9 @@ bool Image::loadImageFromKTX(const KTXImage& image)

case MyMTLPixelFormatR16Float:
case MyMTLPixelFormatRG16Float:
#if SUPPORT_RGB
case MyMTLPixelFormatRGB16Float_internal:
#endif
case MyMTLPixelFormatRGBA16Float: {
int32_t numSrcChannels = blockSize / 2; // 2 = sizeof(_float16)
int32_t numDstChannels = 4;
Expand Down Expand Up @@ -220,7 +229,10 @@ bool Image::loadImageFromKTX(const KTXImage& image)

case MyMTLPixelFormatR32Float:
case MyMTLPixelFormatRG32Float:
case MyMTLPixelFormatRGBA32Float: {
#if SUPPORT_RGB
case MyMTLPixelFormatRGB32Float_internal:
#endif
case MyMTLPixelFormatRGBA32Float: {
const float* srcPixels =
(const float*)(image.fileData + image.mipLevels[0].offset);

Expand Down

0 comments on commit 8a6ec89

Please sign in to comment.