diff --git a/3rdParty/CMakeLists.txt b/3rdParty/CMakeLists.txt index 59f0d8e..1a8e201 100644 --- a/3rdParty/CMakeLists.txt +++ b/3rdParty/CMakeLists.txt @@ -34,14 +34,17 @@ add_subdirectory(libjpeg-turbo) add_subdirectory(msdf) add_subdirectory(eastl) add_subdirectory(moodycamel_concurrentqueue) +add_subdirectory(tinyusdz) +add_subdirectory(dds_image) + if (NOT IOS AND ENABLE_CURL) add_subdirectory(curl) endif() -if(ENABLE_TEST) +if (ENABLE_TEST) add_subdirectory(catch2) -endif() +endif () -add_subdirectory(tinyusdz) -add_subdirectory(ktx-software) -add_subdirectory(dds_image) \ No newline at end of file +if (ENABLE_KTX) + add_subdirectory(ktx-software) +endif () \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ef70745..9e2299a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ SetOptimisationSettings() option(TRACY_ENABLE "Enable Tracy Profiler" OFF) option(ENABLE_ASSIMP "If assimp should be used" ON) option(ENABLE_CURL "If curl should be used" ON) +option(ENABLE_KTX "If ktx images should be supported" ON) option(ENABLE_TEST "Enable testing" ON) option(ENABLE_EXAMPLE "Enable examples" ON) option(ENABLE_MSDF "Enable msdf library" ON) diff --git a/openVulkanoCpp/CMakeLists.txt b/openVulkanoCpp/CMakeLists.txt index 97462fa..8965373 100644 --- a/openVulkanoCpp/CMakeLists.txt +++ b/openVulkanoCpp/CMakeLists.txt @@ -67,7 +67,10 @@ if (NOT ANDROID AND NOT IOS) endif() endif() -target_link_libraries(openVulkanoCpp PUBLIC magic_enum yaml-cpp fmt spdlog glm pugixml stb eigen utf8cpp imgui_internal TracyClient stud-uuid ryml unordered_dense concurrentqueue units ktx dds_image) +target_link_libraries(openVulkanoCpp PUBLIC magic_enum yaml-cpp fmt spdlog glm pugixml stb eigen utf8cpp imgui_internal TracyClient stud-uuid ryml unordered_dense concurrentqueue units dds_image) +if (ENABLE_KTX) + target_link_libraries(openVulkanoCpp PUBLIC ktx) +endif () LinkAssimp(openVulkanoCpp) LinkLibArchive(openVulkanoCpp) LinkLibJpegTurbo(openVulkanoCpp) diff --git a/openVulkanoCpp/Image/ImageLoaderKtx.cpp b/openVulkanoCpp/Image/ImageLoaderKtx.cpp index afef434..5caa0f4 100644 --- a/openVulkanoCpp/Image/ImageLoaderKtx.cpp +++ b/openVulkanoCpp/Image/ImageLoaderKtx.cpp @@ -5,7 +5,9 @@ */ #include "ImageLoaderKtx.hpp" +#include "Base/Logger.hpp" +#if __has_include("ktx.h") #include #include @@ -13,22 +15,72 @@ #include #include -namespace -{ - void KtxDestroy(ktxTexture* tex) - { - ktxTexture_Destroy(tex); - } -} - namespace OpenVulkano::Image { + namespace + { + struct KTXTextureDeleter { + void operator()(ktxTexture* texture) const { + if (texture) { + ktxTexture_Destroy(texture); + } + } + }; + + std::unique_ptr ExtractImage(ktxTexture* texture) + { + if (!texture->pData) + { + throw std::runtime_error("No image data found in KTX texture."); + } + + auto width = static_cast(texture->baseWidth); + auto height = static_cast(texture->baseHeight); + + auto image = std::make_unique(); + image->resolution.x = width; + image->resolution.y = height; + image->resolution.z = 1; + + if (texture->classId == ktxTexture1_c) + { + ktxTexture1* tx = reinterpret_cast(texture); + DataFormat format = DataFormat::Format::UNDEFINED; + switch (tx->glInternalformat) + { + case 0x8051: /* GL_RGB8_EXT */ format = DataFormat::Format::R8G8B8_UNORM; break; + case 0x8054: /* GL_RGB16_EXT */ format = DataFormat::Format::R16G16B16_UNORM; break; + case 0x8057: /* GL_RGB5_A1_EXT */ format = DataFormat::Format::R5G5B5A1_UNORM_PACK16; break; + case 0x8058: /* GL_RGBA8_EXT */ format = DataFormat::Format::R8G8B8A8_UNORM; break; + case 0x8059: /* GL_RGB10_A2_EXT */ format = DataFormat::Format::A2R10G10B10_UNORM_PACK32; break; + case 0x805B: /* GL_RGBA16_EXT */ format = DataFormat::Format::R16G16B16A16_UNORM; break; + default: throw std::runtime_error("Unhandled texture1 format: " + tx->glInternalformat); + } + image->dataFormat = format; + } + else if (texture->classId == ktxTexture2_c) + { + ktxTexture2* tx = reinterpret_cast(texture); + image->dataFormat = reinterpret_cast(tx->vkFormat); + } + else [[unlikely]] + { + throw std::runtime_error("ktxTexture has unhandled classId!"); + } + + image->data = Array(texture->dataSize); + memcpy(image->data.Data(), texture->pData, image->data.Size()); + + return image; + } + } + std::unique_ptr ImageLoaderKtx::loadFromFile(const std::string& filePath) { ktxTexture* tmp = nullptr; KTX_error_code error_code = ktxTexture_CreateFromNamedFile(filePath.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp); - std::unique_ptr texture(tmp, &KtxDestroy); + std::unique_ptr texture(tmp); if (error_code != KTX_SUCCESS) { @@ -44,7 +96,7 @@ namespace OpenVulkano::Image ktxTexture* tmp = nullptr; KTX_error_code error_code = ktxTexture_CreateFromMemory(buffer.data(), buffer.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp); - std::unique_ptr texture(tmp, &KtxDestroy); + std::unique_ptr texture(tmp); if (error_code != KTX_SUCCESS) { @@ -60,7 +112,7 @@ namespace OpenVulkano::Image ktxTexture* tmp = nullptr; KTX_error_code result = ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp); - std::unique_ptr texture(tmp, &KtxDestroy); + std::unique_ptr texture(tmp); if (result != KTX_SUCCESS) { @@ -71,51 +123,26 @@ namespace OpenVulkano::Image Math::Vector2i dimensions(texture->baseWidth, texture->baseHeight); return dimensions; } - - std::unique_ptr ImageLoaderKtx::ExtractImage(ktxTexture* texture) +} +#else +namespace OpenVulkano::Image +{ + std::unique_ptr ImageLoaderKtx::loadFromFile(const std::string& filePath) { - if (!texture->pData) - { - throw std::runtime_error("No image data found in KTX texture."); - } - - auto width = static_cast(texture->baseWidth); - auto height = static_cast(texture->baseHeight); - - auto image = std::make_unique(); - image->resolution.x = width; - image->resolution.y = height; - image->resolution.z = 1; - - if (texture->classId == ktxTexture1_c) - { - ktxTexture1* tx = reinterpret_cast(texture); - DataFormat format = DataFormat::Format::UNDEFINED; - switch (tx->glInternalformat) - { - case 0x8051: /* GL_RGB8_EXT */ format = DataFormat::Format::R8G8B8_UNORM; break; - case 0x8054: /* GL_RGB16_EXT */ format = DataFormat::Format::R16G16B16_UNORM; break; - case 0x8057: /* GL_RGB5_A1_EXT */ format = DataFormat::Format::R5G5B5A1_UNORM_PACK16; break; - case 0x8058: /* GL_RGBA8_EXT */ format = DataFormat::Format::R8G8B8A8_UNORM; break; - case 0x8059: /* GL_RGB10_A2_EXT */ format = DataFormat::Format::A2R10G10B10_UNORM_PACK32; break; - case 0x805B: /* GL_RGBA16_EXT */ format = DataFormat::Format::R16G16B16A16_UNORM; break; - default: throw std::runtime_error("Unhandled texture1 format: " + tx->glInternalformat); - } - image->dataFormat = format; - } - else if (texture->classId == ktxTexture2_c) - { - ktxTexture2* tx = reinterpret_cast(texture); - image->dataFormat = reinterpret_cast(tx->vkFormat); - } - else [[unlikely]] - { - throw std::runtime_error("ktxTexture has unhandled classId!"); - } - - image->data = Array(texture->dataSize); - memcpy(image->data.Data(), texture->pData, image->data.Size()); - - return image; + Logger::DATA->error("KTX loading not available."); + return nullptr; } -} \ No newline at end of file + + std::unique_ptr ImageLoaderKtx::loadFromMemory(const std::vector& buffer) + { + Logger::DATA->error("KTX loading not available."); + return nullptr; + } + + Math::Vector2i ImageLoaderKtx::GetImageDimensions(const std::string& filename) + { + Logger::DATA->error("KTX loading not available."); + return {0,0}; + } +} +#endif \ No newline at end of file diff --git a/openVulkanoCpp/Image/ImageLoaderKtx.hpp b/openVulkanoCpp/Image/ImageLoaderKtx.hpp index c9f9db9..dd86078 100644 --- a/openVulkanoCpp/Image/ImageLoaderKtx.hpp +++ b/openVulkanoCpp/Image/ImageLoaderKtx.hpp @@ -8,8 +8,6 @@ #include "ImageLoader.hpp" -struct ktxTexture; - namespace OpenVulkano::Image { class ImageLoaderKtx : public IImageLoader @@ -18,8 +16,5 @@ namespace OpenVulkano::Image std::unique_ptr loadFromFile(const std::string& filePath) override; std::unique_ptr loadFromMemory(const std::vector& buffer) override; Math::Vector2i GetImageDimensions(const std::string& filename) override; - - protected: - std::unique_ptr ExtractImage(ktxTexture* texture); }; }