From bcfe03c8e1eb3646458faa02b6cceafaa40c1a94 Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Thu, 29 Nov 2018 12:19:20 -0800 Subject: [PATCH] KtxUtility improvements. This makes it easier for C++ clients to use KTX files and simplifies the JavaScript bindings. --- libs/image/include/image/KtxUtility.h | 29 ++++++++++++++++++++------- web/filament-js/jsbindings.cpp | 17 +++++----------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/libs/image/include/image/KtxUtility.h b/libs/image/include/image/KtxUtility.h index e9927e4f5a..eb55666557 100644 --- a/libs/image/include/image/KtxUtility.h +++ b/libs/image/include/image/KtxUtility.h @@ -28,6 +28,7 @@ namespace KtxUtility { using TextureFormat = filament::driver::TextureFormat; using CompressedPixelDataType = filament::driver::CompressedPixelDataType; using PixelDataType = filament::driver::PixelDataType; + using PixelDataFormat = filament::driver::PixelDataFormat; template T toCompressedFilamentEnum(uint32_t format) { @@ -78,12 +79,12 @@ namespace KtxUtility { return (T) 0xffff; } - CompressedPixelDataType toCompressedPixelDataType(uint32_t format) { - return toCompressedFilamentEnum(format); + CompressedPixelDataType toCompressedPixelDataType(const KtxInfo& info) { + return toCompressedFilamentEnum(info.glInternalFormat); } - PixelDataType toPixelDataType(uint32_t dtype) { - switch (dtype) { + PixelDataType toPixelDataType(const KtxInfo& info) { + switch (info.glType) { case KtxBundle::UNSIGNED_BYTE: return PixelDataType::UBYTE; case KtxBundle::UNSIGNED_SHORT: return PixelDataType::USHORT; case KtxBundle::HALF_FLOAT: return PixelDataType::HALF; @@ -92,8 +93,22 @@ namespace KtxUtility { return (PixelDataType) 0xff; } - TextureFormat toTextureFormat(uint32_t format) { - switch (format) { + PixelDataFormat toPixelDataFormat(const KtxInfo& info, bool rgbm = false) { + switch (info.glTypeSize) { + case 1: return PixelDataFormat::R; + case 2: return PixelDataFormat::RG; + case 3: return PixelDataFormat::RGB; + case 4: return rgbm ? PixelDataFormat::RGBM : PixelDataFormat::RGBA; + } + return (PixelDataFormat) 0xff; + } + + bool isCompressed(const KtxInfo& info) { + return info.glFormat == 0; + } + + TextureFormat toTextureFormat(const KtxInfo& info) { + switch (info.glInternalFormat) { case KtxBundle::RED: return TextureFormat::R8; case KtxBundle::RG: return TextureFormat::RG8; case KtxBundle::RGB: return TextureFormat::RGB8; @@ -153,7 +168,7 @@ namespace KtxUtility { case KtxBundle::RGBA32UI: return TextureFormat::RGBA32UI; case KtxBundle::RGBA32I: return TextureFormat::RGBA32I; } - return toCompressedFilamentEnum(format); + return toCompressedFilamentEnum(info.glInternalFormat); } } // namespace KtxUtility diff --git a/web/filament-js/jsbindings.cpp b/web/filament-js/jsbindings.cpp index 91b7dc0613..79947b1ce5 100644 --- a/web/filament-js/jsbindings.cpp +++ b/web/filament-js/jsbindings.cpp @@ -787,7 +787,7 @@ class_("KtxBundle") /// Returns "undefined" if no valid Filament enumerant exists. .function("getInternalFormat", EMBIND_LAMBDA(Texture::InternalFormat, (KtxBundle* self, bool srgb), { - auto result = KtxUtility::toTextureFormat(self->info().glInternalFormat); + auto result = KtxUtility::toTextureFormat(self->info()); if (srgb) { if (result == Texture::InternalFormat::RGB8) { result = Texture::InternalFormat::SRGB8; @@ -805,14 +805,7 @@ class_("KtxBundle") /// Returns "undefined" if no valid Filament enumerant exists. .function("getPixelDataFormat", EMBIND_LAMBDA(driver::PixelDataFormat, (KtxBundle* self, bool rgbm), { - switch (self->info().glTypeSize) { - case 1: return driver::PixelDataFormat::R; - case 2: return driver::PixelDataFormat::RG; - case 3: return driver::PixelDataFormat::RGB; - case 4: return rgbm ? driver::PixelDataFormat::RGBA : driver::PixelDataFormat::RGBM; - } - assert(false && "Unknown pixel data format."); - return (driver::PixelDataFormat) 0xff; + return KtxUtility::toPixelDataFormat(self->getInfo()); }), allow_raw_pointers()) /// getPixelDataType ::method:: @@ -820,7 +813,7 @@ class_("KtxBundle") /// Returns "undefined" if no valid Filament enumerant exists. .function("getPixelDataType", EMBIND_LAMBDA(driver::PixelDataType, (KtxBundle* self), { - return KtxUtility::toPixelDataType(self->info().glType); + return KtxUtility::toPixelDataType(self->getInfo()); }), allow_raw_pointers()) /// getCompressedPixelDataType ::method:: @@ -828,14 +821,14 @@ class_("KtxBundle") /// Returns "undefined" if no valid Filament enumerant exists. .function("getCompressedPixelDataType", EMBIND_LAMBDA(driver::CompressedPixelDataType, (KtxBundle* self), { - return KtxUtility::toCompressedPixelDataType(self->info().glInternalFormat); + return KtxUtility::toCompressedPixelDataType(self->getInfo()); }), allow_raw_pointers()) /// isCompressed ::method:: /// Per spec, compressed textures in KTX always have their glFormat field set to 0. /// ::retval:: boolean .function("isCompressed", EMBIND_LAMBDA(bool, (KtxBundle* self), { - return self->info().glFormat == 0; + return KtxUtility::isCompressed(self->getInfo()); }), allow_raw_pointers()) .function("isCubemap", &KtxBundle::isCubemap)