Some slight cleanup for KTX image loader

This commit is contained in:
Georg Hagen
2024-12-25 17:42:06 +01:00
parent ca184f3ca9
commit c24c3ca86f
5 changed files with 97 additions and 68 deletions

View File

@@ -34,14 +34,17 @@ add_subdirectory(libjpeg-turbo)
add_subdirectory(msdf) add_subdirectory(msdf)
add_subdirectory(eastl) add_subdirectory(eastl)
add_subdirectory(moodycamel_concurrentqueue) add_subdirectory(moodycamel_concurrentqueue)
add_subdirectory(tinyusdz)
add_subdirectory(dds_image)
if (NOT IOS AND ENABLE_CURL) if (NOT IOS AND ENABLE_CURL)
add_subdirectory(curl) add_subdirectory(curl)
endif() endif()
if(ENABLE_TEST) if (ENABLE_TEST)
add_subdirectory(catch2) add_subdirectory(catch2)
endif() endif ()
add_subdirectory(tinyusdz) if (ENABLE_KTX)
add_subdirectory(ktx-software) add_subdirectory(ktx-software)
add_subdirectory(dds_image) endif ()

View File

@@ -42,6 +42,7 @@ SetOptimisationSettings()
option(TRACY_ENABLE "Enable Tracy Profiler" OFF) option(TRACY_ENABLE "Enable Tracy Profiler" OFF)
option(ENABLE_ASSIMP "If assimp should be used" ON) option(ENABLE_ASSIMP "If assimp should be used" ON)
option(ENABLE_CURL "If curl 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_TEST "Enable testing" ON)
option(ENABLE_EXAMPLE "Enable examples" ON) option(ENABLE_EXAMPLE "Enable examples" ON)
option(ENABLE_MSDF "Enable msdf library" ON) option(ENABLE_MSDF "Enable msdf library" ON)

View File

@@ -67,7 +67,10 @@ if (NOT ANDROID AND NOT IOS)
endif() endif()
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) LinkAssimp(openVulkanoCpp)
LinkLibArchive(openVulkanoCpp) LinkLibArchive(openVulkanoCpp)
LinkLibJpegTurbo(openVulkanoCpp) LinkLibJpegTurbo(openVulkanoCpp)

View File

@@ -5,7 +5,9 @@
*/ */
#include "ImageLoaderKtx.hpp" #include "ImageLoaderKtx.hpp"
#include "Base/Logger.hpp"
#if __has_include("ktx.h")
#include <ktx.h> #include <ktx.h>
#include <memory> #include <memory>
@@ -13,66 +15,19 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
namespace
{
void KtxDestroy(ktxTexture* tex)
{
ktxTexture_Destroy(tex);
}
}
namespace OpenVulkano::Image namespace OpenVulkano::Image
{ {
std::unique_ptr<Image> ImageLoaderKtx::loadFromFile(const std::string& filePath) namespace
{ {
ktxTexture* tmp = nullptr; struct KTXTextureDeleter {
KTX_error_code error_code = void operator()(ktxTexture* texture) const {
ktxTexture_CreateFromNamedFile(filePath.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp); if (texture) {
std::unique_ptr<ktxTexture, decltype(&KtxDestroy)> texture(tmp, &KtxDestroy); ktxTexture_Destroy(texture);
if (error_code != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture: " + std::string(ktxErrorString(error_code)));
} }
auto result = ExtractImage(texture.get());
return result;
} }
};
std::unique_ptr<Image> ImageLoaderKtx::loadFromMemory(const std::vector<uint8_t>& buffer) std::unique_ptr<Image> ExtractImage(ktxTexture* texture)
{
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<ktxTexture, decltype(&KtxDestroy)> texture(tmp, &KtxDestroy);
if (error_code != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture from memory: " + std::string(ktxErrorString(error_code)));
}
auto result = ExtractImage(texture.get());
return result;
}
Math::Vector2i ImageLoaderKtx::GetImageDimensions(const std::string& filename)
{
ktxTexture* tmp = nullptr;
KTX_error_code result =
ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp);
std::unique_ptr<ktxTexture, decltype(&KtxDestroy)> texture(tmp, &KtxDestroy);
if (result != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture for dimensions: "
+ std::string(ktxErrorString(result)));
}
Math::Vector2i dimensions(texture->baseWidth, texture->baseHeight);
return dimensions;
}
std::unique_ptr<Image> ImageLoaderKtx::ExtractImage(ktxTexture* texture)
{ {
if (!texture->pData) if (!texture->pData)
{ {
@@ -118,4 +73,76 @@ namespace OpenVulkano::Image
return image; return image;
} }
}
std::unique_ptr<Image> 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<ktxTexture, KTXTextureDeleter> texture(tmp);
if (error_code != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture: " + std::string(ktxErrorString(error_code)));
}
auto result = ExtractImage(texture.get());
return result;
}
std::unique_ptr<Image> ImageLoaderKtx::loadFromMemory(const std::vector<uint8_t>& buffer)
{
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<ktxTexture, KTXTextureDeleter> texture(tmp);
if (error_code != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture from memory: " + std::string(ktxErrorString(error_code)));
}
auto result = ExtractImage(texture.get());
return result;
}
Math::Vector2i ImageLoaderKtx::GetImageDimensions(const std::string& filename)
{
ktxTexture* tmp = nullptr;
KTX_error_code result =
ktxTexture_CreateFromNamedFile(filename.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tmp);
std::unique_ptr<ktxTexture, KTXTextureDeleter> texture(tmp);
if (result != KTX_SUCCESS)
{
throw std::runtime_error("Failed to load KTX texture for dimensions: "
+ std::string(ktxErrorString(result)));
}
Math::Vector2i dimensions(texture->baseWidth, texture->baseHeight);
return dimensions;
}
} }
#else
namespace OpenVulkano::Image
{
std::unique_ptr<Image> ImageLoaderKtx::loadFromFile(const std::string& filePath)
{
Logger::DATA->error("KTX loading not available.");
return nullptr;
}
std::unique_ptr<Image> ImageLoaderKtx::loadFromMemory(const std::vector<uint8_t>& 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

View File

@@ -8,8 +8,6 @@
#include "ImageLoader.hpp" #include "ImageLoader.hpp"
struct ktxTexture;
namespace OpenVulkano::Image namespace OpenVulkano::Image
{ {
class ImageLoaderKtx : public IImageLoader class ImageLoaderKtx : public IImageLoader
@@ -18,8 +16,5 @@ namespace OpenVulkano::Image
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override; std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override; std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
Math::Vector2i GetImageDimensions(const std::string& filename) override; Math::Vector2i GetImageDimensions(const std::string& filename) override;
protected:
std::unique_ptr<Image> ExtractImage(ktxTexture* texture);
}; };
} }