Skip to content

Commit

Permalink
kram - decode ETC2 on M1 if using 3d textures
Browse files Browse the repository at this point in the history
M1 supports all texture formats, but apparently not on 3d texture types - only ASTC/BC there I guess.
  • Loading branch information
alecazam committed Jul 16, 2022
1 parent 69c5f44 commit e831242
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions kramv/KramLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,34 @@ - (instancetype)init
originalFormat:originalFormat];
}

// for macOS/win Intel need to decode astc/etc
// on macOS/arm, the M1 supports all 3 encode formats
#define DO_DECODE USE_SSE

#if DO_DECODE

// this means format isnt supported on platform, but can be decoded to rgba to
// display
bool isDecodeImageNeeded(MyMTLPixelFormat pixelFormat)
bool isDecodeImageNeeded(MyMTLPixelFormat pixelFormat, MyMTLTextureType type)
{
bool needsDecode = false;

#if USE_SSE
if (isETCFormat(pixelFormat)) {
needsDecode = true;
}
else if (isASTCFormat(pixelFormat)) {
needsDecode = true;
}

#else
if (isETCFormat(pixelFormat) && type == MyMTLTextureType3D) {
needsDecode = true;
}
#endif
return needsDecode;
}

bool decodeImage(const KTXImage &image, KTXImage &imageDecoded)
{
KramDecoderParams decoderParams;
KramDecoder decoder;

#if USE_SSE
if (isETCFormat(image.pixelFormat)) {
if (!decoder.decode(image, imageDecoded, decoderParams)) {
return NO;
Expand All @@ -109,6 +110,13 @@ bool decodeImage(const KTXImage &image, KTXImage &imageDecoded)
return NO;
}
}
#else
if (isETCFormat(image.pixelFormat) && image.textureType == MyMTLTextureType3D) {
if (!decoder.decode(image, imageDecoded, decoderParams)) {
return NO;
}
}
#endif
else {
assert(false); // don't call this routine if decode not needed
}
Expand All @@ -119,8 +127,6 @@ bool decodeImage(const KTXImage &image, KTXImage &imageDecoded)
return YES;
}

#endif

#if SUPPORT_RGB

// TODO: move these into libkram
Expand Down Expand Up @@ -260,8 +266,7 @@ inline MyMTLPixelFormat remapInternalRGBFormat(MyMTLPixelFormat format)
*originalFormat = (MTLPixelFormat)image.pixelFormat;
}

#if DO_DECODE
if (isDecodeImageNeeded(image.pixelFormat)) {
if (isDecodeImageNeeded(image.pixelFormat, image.textureType)) {
KTXImage imageDecoded;
if (!decodeImage(image, imageDecoded)) {
return nil;
Expand All @@ -270,7 +275,6 @@ inline MyMTLPixelFormat remapInternalRGBFormat(MyMTLPixelFormat format)
return [self blitTextureFromImage:imageDecoded name:name];
}
else
#endif
{
// fast load path directly from mmap'ed data, decompress direct to staging
return [self blitTextureFromImage:image name:name];
Expand Down

0 comments on commit e831242

Please sign in to comment.