From a9ad5195b7660b701cc6f87d531c73bbe82160fc Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Wed, 24 Sep 2025 22:32:23 +0200 Subject: [PATCH] Cache transcoded textures --- CMakeLists.txt | 5 +- client/CMakeLists.txt | 2 +- client/render/image_loader.cpp | 23 +- client/render/image_loader.h | 5 +- client/render/scene_loader.cpp | 221 ++++++++++++++++-- client/render/scene_loader.h | 30 ++- client/scene.cpp | 15 +- client/scene.h | 5 +- client/utils/cache.h | 6 + client/utils/files.h | 25 +- client/utils/json_string.cpp | 70 ++++++ client/utils/json_string.h | 24 ++ ...b3b6988a9bc13448b720f17ed3543da7600e.patch | 49 ++++ 13 files changed, 421 insertions(+), 59 deletions(-) create mode 100644 client/utils/json_string.cpp create mode 100644 client/utils/json_string.h create mode 100644 patches/libktx/f3f6b3b6988a9bc13448b720f17ed3543da7600e.patch diff --git a/CMakeLists.txt b/CMakeLists.txt index 51ca3eab..b866bb81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,8 +238,11 @@ FetchContent_Declare(spdlog EXCLUDE_FROM_ALL SYSTEM URL https://github.co URL_HASH SHA256=15a04e69c222eb6c01094b5c7ff8a249b36bb22788d72519646fb85feb267e67) FetchContent_Declare(stb EXCLUDE_FROM_ALL SYSTEM URL https://github.com/nothings/stb/archive/013ac3beddff3dbffafd5177e7972067cd2b5083.zip URL_HASH SHA256=b7f476902bbef1b30f8ecc2d9d95c459c32302c8b559d09b589b5955463b7af8) +file(GLOB LIBKTX_PATCHES CONFIGURE_DEPENDS patches/libktx/*) FetchContent_Declare(libktx EXCLUDE_FROM_ALL SYSTEM URL https://github.com/KhronosGroup/KTX-Software/archive/refs/tags/v4.4.0.tar.gz - URL_HASH SHA256=3585d76edcdcbe3a671479686f8c81c1c10339f419e4b02a9a6f19cc6e4e0612) + URL_HASH SHA256=3585d76edcdcbe3a671479686f8c81c1c10339f419e4b02a9a6f19cc6e4e0612 + PATCH_COMMAND ${CMAKE_CURRENT_LIST_DIR}/patches/apply.sh ${LIBKTX_PATCHES} + ) FetchContent_Declare(implot EXCLUDE_FROM_ALL SYSTEM URL https://github.com/epezent/implot/archive/3da8bd34299965d3b0ab124df743fe3e076fa222.zip URL_HASH SHA256=e9b439a94aa6f09fcf12ed9c2cd784efe5e99e96242122aa683e690b29f7a59f) file(GLOB IMGUI_PATCHES CONFIGURE_DEPENDS patches/imgui/*) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index ce94f0cd..be600bd8 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -105,7 +105,7 @@ target_link_libraries(wivrn FreetypeHarfbuzz glm::glm imspinner - ktx_read + ktx simdjson spdlog::spdlog spirv-reflect-static diff --git a/client/render/image_loader.cpp b/client/render/image_loader.cpp index d666a74c..b572c065 100644 --- a/client/render/image_loader.cpp +++ b/client/render/image_loader.cpp @@ -597,7 +597,7 @@ loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, }; } -loaded_image image_loader::do_load_ktx(std::span bytes, bool srgb, const std::string & name) +loaded_image image_loader::do_load_ktx(std::span bytes, bool srgb, const std::string & name, const std::filesystem::path & output_file) { KTXTexture2 texture{}; // KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT checks that the file is compatible with the KHR_texture_basisu glTF extension @@ -615,14 +615,25 @@ loaded_image image_loader::do_load_ktx(std::span bytes, bool sr { auto format = srgb ? supported_srgb_formats.front() : supported_linear_formats.front(); - auto t0 = std::chrono::steady_clock::now(); if (ktxTexture2_TranscodeBasis(texture, format.second, 0) != KTX_SUCCESS) { spdlog::warn("ktxTexture2_TranscodeBasis: error {}", ktxErrorString(err)); throw std::runtime_error("ktxTexture2_TranscodeBasis"); } - auto dt = std::chrono::steady_clock::now() - t0; - spdlog::debug("{}: transcoded to {} in {}", name, vk::to_string(format.first), std::chrono::duration_cast(dt)); + + if (output_file != "") + { + spdlog::debug("Saving transcoded texture to {}", output_file.native()); + std::string writer = "WiVRn"; + ktxHashList_DeleteKVPair(&texture->kvDataHead, KTX_WRITER_KEY); + ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_KEY, (ktx_uint32_t)writer.length() + 1, writer.c_str()); + + err = ktxTexture2_WriteToNamedFile(texture, output_file.c_str()); + if (err != KTX_SUCCESS) + { + spdlog::warn("ktxTexture2_WriteToNamedFile: error {}", ktxErrorString(err)); + } + } } ktxVulkanTexture vk_texture; @@ -711,13 +722,13 @@ loaded_image image_loader::do_load_image(std::span bytes, bool } // Load a PNG/JPEG/KTX2 file -loaded_image image_loader::load(std::span bytes, bool srgb, const std::string & name, bool premultiply) +loaded_image image_loader::load(std::span bytes, bool srgb, const std::string & name, bool premultiply, const std::filesystem::path & output_file) { const uint8_t ktx1_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; const uint8_t ktx2_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; if (starts_with(bytes, ktx1_magic) || starts_with(bytes, ktx2_magic)) - return do_load_ktx(bytes, srgb, name); + return do_load_ktx(bytes, srgb, name, output_file); else return do_load_image(bytes, srgb, name, premultiply); } diff --git a/client/render/image_loader.h b/client/render/image_loader.h index 6431aebe..cc80daec 100644 --- a/client/render/image_loader.h +++ b/client/render/image_loader.h @@ -23,6 +23,7 @@ #include "vk/allocation.h" #include "vk/fwd.h" #include +#include #include #include #include @@ -52,7 +53,7 @@ struct image_loader ~image_loader(); // Load a PNG/JPEG/KTX2 file - loaded_image load(std::span bytes, bool srgb, const std::string & name = "", bool premultiply = false); + loaded_image load(std::span bytes, bool srgb, const std::string & name = "", bool premultiply = false, const std::filesystem::path & output_file = ""); // Load raw pixel data loaded_image load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format, const std::string & name = "", bool premultiply = false); @@ -94,5 +95,5 @@ private: loaded_image do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format, const std::string & name, bool premultiply); loaded_image do_load_image(std::span bytes, bool srgb, const std::string & name, bool premultiply); - loaded_image do_load_ktx(std::span bytes, bool srgb, const std::string & name); + loaded_image do_load_ktx(std::span bytes, bool srgb, const std::string & name, const std::filesystem::path & output_file = ""); }; diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index 83c5fe62..8de30842 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -22,6 +22,8 @@ #include "image_loader.h" #include "render/scene_components.h" #include "render/vertex_layout.h" +#include "utils/files.h" +#include "utils/json_string.h" #include "utils/mapped_file.h" #include "utils/ranges.h" #include "vk/shader.h" @@ -36,10 +38,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include @@ -224,7 +229,7 @@ std::vector read_vertex_attribute_aux(const fastgltf::Asset & asset, case 2: return read_vertex_attribute_aux2>(asset, accessor); case 3: return read_vertex_attribute_aux2>(asset, accessor); case 4: return read_vertex_attribute_aux2>(asset, accessor); - //clang-format on + // clang-format on } return {}; } @@ -233,7 +238,7 @@ std::vector read_vertex_attribute(const fastgltf::Asset & asset, con { switch (accessor.componentType) { - // clang-format off + // clang-format off case fastgltf::ComponentType::Byte: return read_vertex_attribute_aux(asset, accessor); case fastgltf::ComponentType::UnsignedByte: return read_vertex_attribute_aux(asset, accessor); case fastgltf::ComponentType::Short: return read_vertex_attribute_aux(asset, accessor); @@ -241,7 +246,7 @@ std::vector read_vertex_attribute(const fastgltf::Asset & asset, con case fastgltf::ComponentType::Int: return read_vertex_attribute_aux(asset, accessor); case fastgltf::ComponentType::UnsignedInt: return read_vertex_attribute_aux(asset, accessor); case fastgltf::ComponentType::Float: return read_vertex_attribute_aux(asset, accessor); - //clang-format on + // clang-format on case fastgltf::ComponentType::Double: // TODO abort(); break; @@ -402,7 +407,8 @@ loaded_image do_load_image( vk::raii::CommandPool & cb_pool, std::span image_data, bool srgb, - const std::pmr::string & name) + const std::pmr::string & name, + const std::filesystem::path & output_path) { switch (guess_mime_type(image_data)) { @@ -411,7 +417,7 @@ loaded_image do_load_image( case fastgltf::MimeType::KTX2: { // TODO reuse image loader image_loader loader(physical_device, device, queue, cb_pool); - auto image = loader.load(image_data, srgb, name.c_str()); + auto image = loader.load(image_data, srgb, name.c_str(), false /*premultiply*/, output_path); spdlog::debug("{}: {}x{}, format {}, {} mipmaps, {} bytes", name, image.extent.width, image.extent.height, vk::to_string(image.format), image.num_mipmaps, image.image.size()); return image; @@ -572,7 +578,7 @@ public: } std::unordered_map> images; - std::shared_ptr load_image(int index, bool srgb) + std::shared_ptr load_image(int index, const std::filesystem::path & texture_cache, bool srgb) { auto it = images.find(index); if (it != images.end()) @@ -581,20 +587,42 @@ public: if (index > gltf.images.size()) throw std::runtime_error(std::format("Image index {} out of range", index)); - auto [image_data, mime_type] = visit_source(gltf.images[index].data); - std::pmr::string image_name; if (gltf.images[index].name == "") image_name = std::format("{}(image {})", name, index); else image_name = std::format("{}({})", name, gltf.images[index].name); - auto image_ptr = std::make_shared(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name)); + std::filesystem::path cached_texture; + + if (texture_cache != "") + { + cached_texture = texture_cache / (std::to_string(index) + ".ktx"); + + if (std::filesystem::exists(cached_texture)) + { + try + { + utils::mapped_file image_data{cached_texture}; + auto image_ptr = std::make_shared(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name, "")); + return std::shared_ptr(image_ptr, &image_ptr->image_view); + } + catch (std::exception & e) + { + spdlog::warn("Cannot load cached image {}: {}, deleting it", cached_texture.native(), e.what()); + std::error_code ec; + std::filesystem::remove(cached_texture); + } + } + } + + auto [image_data, mime_type] = visit_source(gltf.images[index].data); + auto image_ptr = std::make_shared(do_load_image(physical_device, device, queue, cb_pool, image_data, srgb, image_name, cached_texture)); return std::shared_ptr(image_ptr, &image_ptr->image_view); } - std::vector> load_all_textures(std::function progress_cb) + std::vector> load_all_textures(const std::filesystem::path & texture_cache, std::function progress_cb) { // Determine which texture is sRGB std::vector srgb_array; @@ -626,7 +654,7 @@ public: { try { - texture_ref.image_view = load_image(*gltf_texture.basisuImageIndex, srgb); + texture_ref.image_view = load_image(*gltf_texture.basisuImageIndex, texture_cache, srgb); loaded_textures++; if (progress_cb) progress_cb((float)loaded_textures / total_textures); @@ -652,7 +680,7 @@ public: { try { - texture_ref.image_view = load_image(*gltf_texture.imageIndex, srgb); + texture_ref.image_view = load_image(*gltf_texture.imageIndex, texture_cache, srgb); loaded_textures++; if (progress_cb) progress_cb((float)loaded_textures / total_textures); @@ -1048,7 +1076,23 @@ public: } // namespace -std::shared_ptr scene_loader::operator()(std::span data, const std::string & name, const std::filesystem::path & parent_path, std::function progress_cb) +scene_loader::scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe & queue, uint32_t queue_family_index, std::shared_ptr default_material, std::filesystem::path texture_cache_) : + device(device), + physical_device(physical_device), + queue(queue), + queue_family_index(queue_family_index), + default_material(default_material), + texture_cache(std::move(texture_cache_)) +{ + clear_texture_cache(); +} + +std::shared_ptr scene_loader::operator()( + std::span data, + const std::string & name, + const std::filesystem::path & parent_path, + const std::filesystem::path & gltf_texture_cache, + std::function progress_cb) { vk::PhysicalDeviceProperties physical_device_properties = physical_device.getProperties(); vk::raii::CommandPool cb_pool{device, vk::CommandPoolCreateInfo{ @@ -1074,7 +1118,7 @@ std::shared_ptr scene_loader::operator()(std::span scene_loader::operator()(std::span scene_loader::operator()(const std::filesystem::path & gltf_path, std::function progress_cb) +std::shared_ptr scene_loader::operator()( + const std::filesystem::path & gltf_path, + std::function progress_cb) { - spdlog::debug("Loading {}", gltf_path.native()); + utils::mapped_file gltf_file(gltf_path); + std::filesystem::path gltf_texture_cache; - utils::mapped_file asset_file(gltf_path); + if (texture_cache != "") + { + // FNV1a hash + static const uint64_t fnv_prime = 0x100000001b3; + static const uint64_t fnv_offset_basis = 0xcbf29ce484222325; + uint64_t hash = fnv_offset_basis; + for (char c: gltf_path.native()) + hash = (hash ^ c) * fnv_prime; - auto data_buffer = fastgltf::GltfDataBuffer::FromBytes(asset_file.data(), asset_file.size()); - if (auto error = data_buffer.error(); error != fastgltf::Error::None) - throw std::system_error((int)error, fastgltf_error_category); + gltf_texture_cache = texture_cache / fmt::format("{:016x}", hash); - return (*this)(asset_file, gltf_path.filename(), gltf_path.parent_path(), progress_cb); + bool create_cache = false; + try + { + if (std::filesystem::exists(gltf_texture_cache)) + { + if (not std::filesystem::is_directory(gltf_texture_cache)) + { + std::filesystem::remove(gltf_texture_cache); + std::filesystem::create_directories(gltf_texture_cache); + spdlog::debug("\"{}\" is not a directory: creating cache", gltf_texture_cache.native()); + create_cache = true; + } + } + else + { + std::filesystem::create_directories(gltf_texture_cache); + spdlog::debug("\"{}\" does not exist: creating cache", gltf_texture_cache.native()); + create_cache = true; + } + } + catch (std::exception & e) + { + spdlog::warn("Cannot create texture cache directory {}: {}", gltf_texture_cache.native(), e.what()); + gltf_texture_cache = ""; + create_cache = false; + } + + if (create_cache) + { + std::ofstream json{gltf_texture_cache / "index.json"}; + json << "{\"filename\":" << json_string(gltf_path.native()); + json << ",\"size\":" << gltf_file.size(); + + // TODO get a timestamp for assets + if (not gltf_path.native().starts_with("assets://")) + { + auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(gltf_path)); + auto timestamp = std::chrono::duration_cast(mtime.time_since_epoch()).count(); + auto size = gltf_file.size(); + + json << ",\"timestamp\":" << timestamp; + } + json << "}"; + } + } + + return (*this)(gltf_file, gltf_path.filename(), gltf_path.parent_path(), gltf_texture_cache, progress_cb); +} + +void scene_loader::clear_texture_cache() +{ + try + { + for (const std::filesystem::directory_entry & entry: std::filesystem::directory_iterator{texture_cache}) + { + if (not entry.is_directory()) + continue; + + std::string saved_filename; + int64_t saved_timestamp = 0; + int64_t saved_size = 0; + + try + { + std::string json = utils::read_whole_file(entry.path() / "index.json"); + simdjson::dom::parser parser; + simdjson::dom::element root = parser.parse(json); + + if (auto val = root["filename"]; val.is_string()) + saved_filename = val.get_string().value(); + + if (auto val = root["size"]; val.is_int64()) + saved_size = val.get_int64(); + + if (auto val = root["timestamp"]; val.is_int64()) + saved_timestamp = val.get_int64(); + } + catch (std::exception & e) + { + // If the index file is not found, remove the cache + spdlog::info("{}: cannot open index.json ({}), removing cache directory", entry.path().native(), e.what()); + std::error_code ec; + std::filesystem::remove_all(entry.path(), ec); + continue; + } + + int64_t real_size = 0; + try + { + utils::mapped_file file{saved_filename}; + real_size = file.size(); + } + catch (std::exception & e) + { + spdlog::info("{}: cannot open {} ({}), removing cache directory", entry.path().native(), saved_filename, e.what()); + std::error_code ec; + std::filesystem::remove_all(entry.path(), ec); + continue; + } + + if (real_size != saved_size) + { + spdlog::info("{}: wrong size for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_size, real_size); + std::error_code ec; + std::filesystem::remove_all(entry.path(), ec); + continue; + } + + // TODO get a timestamp for assets + if (not saved_filename.starts_with("assets://")) + { + auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(saved_filename)); + auto timestamp = std::chrono::duration_cast(mtime.time_since_epoch()).count(); + + if (timestamp != saved_timestamp) + { + spdlog::info("{}: wrong timestamp for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_timestamp, timestamp); + std::error_code ec; + std::filesystem::remove_all(entry.path(), ec); + continue; + } + } + } + } + catch (...) + { + // Ignore errors if the texture cache directory does not exist + } } diff --git a/client/render/scene_loader.h b/client/render/scene_loader.h index f80b7bc8..407e1bd4 100644 --- a/client/render/scene_loader.h +++ b/client/render/scene_loader.h @@ -33,17 +33,29 @@ class scene_loader thread_safe & queue; uint32_t queue_family_index; std::shared_ptr default_material; + std::filesystem::path texture_cache; + + std::shared_ptr operator()( + std::span data, + const std::string & name = "", + const std::filesystem::path & parent_path = "", + const std::filesystem::path & gltf_texture_cache = "", + std::function progress_cb = {}); public: - scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe & queue, uint32_t queue_family_index, std::shared_ptr default_material) : - device(device), - physical_device(physical_device), - queue(queue), - queue_family_index(queue_family_index), - default_material(default_material) - {} + scene_loader( + vk::raii::Device & device, + vk::raii::PhysicalDevice physical_device, + thread_safe & queue, + uint32_t queue_family_index, + std::shared_ptr default_material, + std::filesystem::path texture_cache); + scene_loader(const scene_loader &) = default; - std::shared_ptr operator()(const std::filesystem::path & gltf_path, std::function progress_cb = {}); - std::shared_ptr operator()(std::span data, const std::string & name = "", const std::filesystem::path & parent_path = "", std::function progress_cb = {}); + std::shared_ptr operator()( + const std::filesystem::path & gltf_path, + std::function progress_cb = {}); + + void clear_texture_cache(); }; diff --git a/client/scene.cpp b/client/scene.cpp index fc6a09d5..7382ac4a 100644 --- a/client/scene.cpp +++ b/client/scene.cpp @@ -98,7 +98,7 @@ scene::scene(key, const meta & current_meta, std::span support { // TODO: make argument order more consistent renderer = std::make_shared(device, physical_device, queue, commandpool); - gltf_cache = std::make_shared(device, physical_device, queue, queue_family_index, renderer->get_default_material()); + gltf_cache = std::make_shared(device, physical_device, queue, queue_family_index, renderer->get_default_material(), application::get_cache_path() / "textures"); image_cache = std::make_shared(physical_device, device, queue, commandpool); } } @@ -563,14 +563,14 @@ std::pair scene::add_gltf(std::shared_ptr scene::load_gltf(const std::filesystem::path & path) +std::shared_ptr scene::load_gltf(const std::filesystem::path & path, std::function progress_cb) { - return gltf_cache->load(path.native(), path); + return gltf_cache->load(path, path, progress_cb); } -std::shared_ptr scene::load_gltf(std::span data) +void scene::unload_gltf(const std::filesystem::path & path) { - return gltf_cache->load_uncached(data); + gltf_cache->remove(path); } std::pair scene::add_gltf(const std::filesystem::path & path, uint32_t layer_mask) @@ -578,10 +578,6 @@ std::pair scene::add_gltf(const std::filesyste return add_gltf(load_gltf(path), layer_mask); } -std::pair scene::add_gltf(std::span data, uint32_t layer_mask) -{ - return add_gltf(load_gltf(data), layer_mask); -} void scene::remove(entt::entity entity) { @@ -591,7 +587,6 @@ void scene::remove(entt::entity entity) { entt::entity parent = to_be_removed.back(); to_be_removed.pop_back(); - spdlog::info("Destroying entity {}", (int)parent); world.destroy(parent); for (auto [e, node]: world.view().each()) diff --git a/client/scene.h b/client/scene.h index e631f4eb..a610fe0b 100644 --- a/client/scene.h +++ b/client/scene.h @@ -216,11 +216,10 @@ public: entt::registry world; - std::shared_ptr load_gltf(const std::filesystem::path & path); - std::shared_ptr load_gltf(std::span data); + std::shared_ptr load_gltf(const std::filesystem::path & path, std::function progress_cb = {}); + void unload_gltf(const std::filesystem::path & path); std::pair add_gltf(std::shared_ptr gltf, uint32_t layer_mask = -1); std::pair add_gltf(const std::filesystem::path & path, uint32_t layer_mask = -1); - std::pair add_gltf(std::span data, uint32_t layer_mask = -1); void remove(entt::entity entity); }; diff --git a/client/utils/cache.h b/client/utils/cache.h index 016841a9..8fa7c312 100644 --- a/client/utils/cache.h +++ b/client/utils/cache.h @@ -62,5 +62,11 @@ public: auto _ = entries.lock(); _->clear(); } + + void remove(const Key & key) + { + auto _ = entries.lock(); + _->erase(key); + } }; } // namespace utils diff --git a/client/utils/files.h b/client/utils/files.h index 22a7427c..e8f7e111 100644 --- a/client/utils/files.h +++ b/client/utils/files.h @@ -21,12 +21,13 @@ #include #include #include +#include #include namespace utils { template -static std::vector read_whole_file(std::filesystem::path filename) +static std::conditional_t, std::string, std::vector> read_whole_file(std::filesystem::path filename) { std::ifstream file(filename, std::ios::binary | std::ios::ate); file.exceptions(std::ios_base::badbit | std::ios_base::failbit); @@ -34,14 +35,26 @@ static std::vector read_whole_file(std::filesystem::path filename) size_t size = file.tellg(); file.seekg(0); - if constexpr (sizeof(T) > 1) - size = size - size % sizeof(T); + if constexpr (std::is_same_v) + { + std::string string; + string.resize(size); - std::vector bytes(size / sizeof(T)); + file.read(reinterpret_cast(string.data()), size); - file.read(reinterpret_cast(bytes.data()), size); + return string; + } + else + { + if constexpr (sizeof(T) > 1) + size = size - size % sizeof(T); - return bytes; + std::vector bytes(size / sizeof(T)); + + file.read(reinterpret_cast(bytes.data()), size); + + return bytes; + } } template diff --git a/client/utils/json_string.cpp b/client/utils/json_string.cpp new file mode 100644 index 00000000..68626e18 --- /dev/null +++ b/client/utils/json_string.cpp @@ -0,0 +1,70 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "json_string.h" + +std::string json_string(const std::string & in) +{ + std::string out; + out.reserve(in.size() + 2); + + out += '"'; + + for (char c: in) + { + switch (c) + { + case '\b': + out += "\\b"; + break; + + case '\f': + out += "\\f"; + break; + + case '\n': + out += "\\n"; + break; + + case '\r': + out += "\\r"; + break; + + case '\t': + out += "\\t"; + break; + + case '"': + out += "\\\""; + break; + + case '\\': + out += "\\\\"; + break; + + default: + out += c; + break; + } + } + + out += '"'; + + return out; +} diff --git a/client/utils/json_string.h b/client/utils/json_string.h new file mode 100644 index 00000000..9d3e4f46 --- /dev/null +++ b/client/utils/json_string.h @@ -0,0 +1,24 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +std::string json_string(const std::string & in); diff --git a/patches/libktx/f3f6b3b6988a9bc13448b720f17ed3543da7600e.patch b/patches/libktx/f3f6b3b6988a9bc13448b720f17ed3543da7600e.patch new file mode 100644 index 00000000..5987d18a --- /dev/null +++ b/patches/libktx/f3f6b3b6988a9bc13448b720f17ed3543da7600e.patch @@ -0,0 +1,49 @@ +From f3f6b3b6988a9bc13448b720f17ed3543da7600e Mon Sep 17 00:00:00 2001 +From: alex +Date: Wed, 16 Apr 2025 06:36:45 +0100 +Subject: [PATCH] GCC14/C++23 compatibility fix (#1014) +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Fixes the following error, when compiling with GCC 14, using +`-std=c++23`: + +``` +<>/basisu/encoder/basisu_kernels_sse.cpp:41: +<>/basisu/encoder/cppspmd_sse.h: In member function ‘cppspmd_sse41::spmd_kernel::vfloat& cppspmd_sse41::spmd_kernel::store(vfloat&&, const vfloat&)’: +<>/basisu/encoder/cppspmd_sse.h:496:24: error: cannot bind non-const lvalue reference of type ‘cppspmd_sse41::spmd_kernel::vfloat&’ to an rvalue of type ‘cppspmd_sse41::spmd_kernel::vfloat’ + 496 | return dst; + | ^~~ +<>/basisu/encoder/cppspmd_sse.h: In member function ‘cppspmd_sse41::spmd_kernel::vfloat& cppspmd_sse41::spmd_kernel::store_all(vfloat&&, const vfloat&)’: +<>/basisu/encoder/cppspmd_sse.h:508:24: error: cannot bind non-const lvalue reference of type ‘cppspmd_sse41::spmd_kernel::vfloat&’ to an rvalue of type ‘cppspmd_sse41::spmd_kernel::vfloat’ + 508 | return dst; + | ^~~ + +``` +--- + external/basisu/encoder/cppspmd_sse.h | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/external/basisu/encoder/cppspmd_sse.h b/external/basisu/encoder/cppspmd_sse.h +index 030824b7d0..0c77a2d8c7 100644 +--- a/external/basisu/encoder/cppspmd_sse.h ++++ b/external/basisu/encoder/cppspmd_sse.h +@@ -493,7 +493,7 @@ struct spmd_kernel + CPPSPMD_FORCE_INLINE vfloat& store(vfloat&& dst, const vfloat& src) + { + dst.m_value = blendv_mask_ps(dst.m_value, src.m_value, _mm_castsi128_ps(m_exec.m_mask)); +- return dst; ++ return { dst }; + } + + CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat& dst, const vfloat& src) +@@ -505,7 +505,7 @@ struct spmd_kernel + CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat&& dst, const vfloat& src) + { + dst.m_value = src.m_value; +- return dst; ++ return { dst }; + } + + // Linear ref to floats